blob: 7a2fcc21af8bb33643d985ab65877f2710c90813 [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>
15
16#include <common/config.h>
17#include <common/memory.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
20#include <common/time.h>
21
22#include <ebmbtree.h>
23#include <ebsttree.h>
24
Willy Tarreaud9f316a2014-07-10 14:03:38 +020025#include <proto/arg.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020026#include <proto/proto_http.h>
27#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010028#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020029#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020030#include <proto/stream.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020031#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010032#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020033#include <proto/peers.h>
34#include <types/global.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010035
Willy Tarreau12785782012-04-27 21:37:17 +020036/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010037struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020038
Emeric Brun3bd697e2010-01-04 15:23:48 +010039/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020040 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
41 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010042 */
43void stksess_free(struct stktable *t, struct stksess *ts)
44{
45 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020046 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010047}
48
49/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020050 * Kill an stksess (only if its ref_cnt is zero).
51 */
52void stksess_kill(struct stktable *t, struct stksess *ts)
53{
54 if (ts->ref_cnt)
55 return;
56
57 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020058 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020059 ebmb_delete(&ts->key);
60 stksess_free(t, ts);
61}
62
63/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020064 * Initialize or update the key in the sticky session <ts> present in table <t>
65 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010066 */
Willy Tarreau393379c2010-06-06 12:11:37 +020067void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010068{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020069 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020070 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010071 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020072 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
73 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010074 }
75}
76
77
78/*
Willy Tarreau393379c2010-06-06 12:11:37 +020079 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
80 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010081 */
Willy Tarreau393379c2010-06-06 12:11:37 +020082static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010083{
Willy Tarreau393379c2010-06-06 12:11:37 +020084 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020085 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020086 ts->key.node.leaf_p = NULL;
87 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020088 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010089 return ts;
90}
91
92/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020093 * Trash oldest <to_batch> sticky sessions from table <t>
94 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010095 */
Willy Tarreau3a925c12013-09-04 17:54:01 +020096int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +010097{
98 struct stksess *ts;
99 struct eb32_node *eb;
100 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200101 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102
103 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
104
105 while (batched < to_batch) {
106
107 if (unlikely(!eb)) {
108 /* we might have reached the end of the tree, typically because
109 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200110 * half. Let's loop back to the beginning of the tree now if we
111 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100112 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200113 if (looped)
114 break;
115 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100116 eb = eb32_first(&t->exps);
117 if (likely(!eb))
118 break;
119 }
120
121 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200122 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100123 eb = eb32_next(eb);
124
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200125 /* don't delete an entry which is currently referenced */
126 if (ts->ref_cnt)
127 continue;
128
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130
Willy Tarreau86257dc2010-06-06 12:57:10 +0200131 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100132 if (!tick_isset(ts->expire))
133 continue;
134
Willy Tarreau86257dc2010-06-06 12:57:10 +0200135 ts->exp.key = ts->expire;
136 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreau86257dc2010-06-06 12:57:10 +0200138 if (!eb || eb->key > ts->exp.key)
139 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140
141 continue;
142 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143
Willy Tarreauaea940e2010-06-06 11:56:36 +0200144 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200145 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200146 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147 stksess_free(t, ts);
148 batched++;
149 }
150
151 return batched;
152}
153
154/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200155 * Allocate and initialise a new sticky session.
156 * The new sticky session is returned or NULL in case of lack of memory.
157 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200158 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
159 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100160 */
161struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
162{
163 struct stksess *ts;
164
165 if (unlikely(t->current == t->size)) {
166 if ( t->nopurge )
167 return NULL;
168
Emeric Brunfbce6d02010-09-23 18:10:00 +0200169 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 return NULL;
171 }
172
Willy Tarreau393379c2010-06-06 12:11:37 +0200173 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100174 if (ts) {
175 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200176 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200177 if (key)
178 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179 }
180
181 return ts;
182}
183
184/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200185 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200186 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200188struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189{
190 struct ebmb_node *eb;
191
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200192 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200193 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 +0100194 else
195 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
196
197 if (unlikely(!eb)) {
198 /* no session found */
199 return NULL;
200 }
201
Willy Tarreau86257dc2010-06-06 12:57:10 +0200202 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100203}
204
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200205/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
206 * This is mainly used for situations where we want to refresh a key's usage so
207 * that it does not expire, and we want to have it created if it was not there.
208 * The stksess is returned, or NULL if it could not be created.
209 */
210struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
211{
212 struct stksess *ts;
213
214 ts = stktable_lookup_key(table, key);
215 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200216 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200217
218 /* entry does not exist, initialize a new one */
219 ts = stksess_new(table, key);
220 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200221 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200222 return ts;
223}
224
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200225/*
226 * Looks in table <t> for a sticky session with same key as <ts>.
227 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200229struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100230{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231 struct ebmb_node *eb;
232
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200233 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200234 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200236 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200238 if (unlikely(!eb))
239 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100240
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200241 return ebmb_entry(eb, struct stksess, key);
242}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243
Willy Tarreaucb183642010-06-06 17:58:34 +0200244/* Update the expiration timer for <ts> but do not touch its expiration node.
245 * The table's expiration timer is updated if set.
246 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200247struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts,
248 int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200249{
Emeric Brun85e77c72010-09-23 18:16:52 +0200250 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200251 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200252 if (t->expire) {
253 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
254 task_queue(t->exp_task);
255 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200256
Emeric Brunaaf58602015-06-15 17:23:30 +0200257 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200258 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200259 /* If this entry is not in the tree
260 or not scheduled for at least one peer */
261 if (!ts->upd.node.leaf_p
262 || (int)(t->commitupdate - ts->upd.key) >= 0
263 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200264 ts->upd.key = ++t->update;
265 t->localupdate = t->update;
266 eb32_delete(&ts->upd);
267 eb = eb32_insert(&t->updates, &ts->upd);
268 if (eb != &ts->upd) {
269 eb32_delete(eb);
270 eb32_insert(&t->updates, &ts->upd);
271 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200272 }
273 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
274 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200275 return ts;
276}
277
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200278/* Update the expiration timer for <ts> but do not touch its expiration node.
279 * The table's expiration timer is updated if set. The date of expiration coming from
280 * <t> stick-table configuration.
281 */
282struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
283{
284 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
285
286 return stktable_touch_with_exp(t, ts, local, expire);
287}
288
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200289/* Insert new sticky session <ts> in the table. It is assumed that it does not
290 * yet exist (the caller must check this). The table's timeout is updated if it
291 * is set. <ts> is returned.
292 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200293struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200294{
295 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200296 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200297 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200298 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200299 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100300}
301
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200302/* Same function as stktable_store(), but with <expire> as supplementary argument
303 * to set the date of expiration of <ts> new sticky session thanks to
304 * stktable_touch_with_exp().
305 */
306struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts,
307 int local, int expire)
308{
309 ebmb_insert(&t->keys, &ts->key, t->key_size);
310 stktable_touch_with_exp(t, ts, local, expire);
311 ts->exp.key = ts->expire;
312 eb32_insert(&t->exps, &ts->exp);
313 return ts;
314}
315
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200316/* Returns a valid or initialized stksess for the specified stktable_key in the
317 * specified table, or NULL if the key was NULL, or if no entry was found nor
318 * could be created. The entry's expiration is updated.
319 */
320struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
321{
322 struct stksess *ts;
323
324 if (!key)
325 return NULL;
326
327 ts = stktable_lookup_key(table, key);
328 if (ts == NULL) {
329 /* entry does not exist, initialize a new one */
330 ts = stksess_new(table, key);
331 if (!ts)
332 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200333 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200334 }
335 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200336 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200337 return ts;
338}
339
Emeric Brun3bd697e2010-01-04 15:23:48 +0100340/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200341 * Trash expired sticky sessions from table <t>. The next expiration date is
342 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100343 */
344static int stktable_trash_expired(struct stktable *t)
345{
346 struct stksess *ts;
347 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200348 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100349
350 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
351
352 while (1) {
353 if (unlikely(!eb)) {
354 /* we might have reached the end of the tree, typically because
355 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200356 * half. Let's loop back to the beginning of the tree now if we
357 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100358 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200359 if (looped)
360 break;
361 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100362 eb = eb32_first(&t->exps);
363 if (likely(!eb))
364 break;
365 }
366
367 if (likely(tick_is_lt(now_ms, eb->key))) {
368 /* timer not expired yet, revisit it later */
369 t->exp_next = eb->key;
370 return t->exp_next;
371 }
372
373 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200374 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100375 eb = eb32_next(eb);
376
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200377 /* don't delete an entry which is currently referenced */
378 if (ts->ref_cnt)
379 continue;
380
Willy Tarreau86257dc2010-06-06 12:57:10 +0200381 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100382
383 if (!tick_is_expired(ts->expire, now_ms)) {
384 if (!tick_isset(ts->expire))
385 continue;
386
Willy Tarreau86257dc2010-06-06 12:57:10 +0200387 ts->exp.key = ts->expire;
388 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100389
Willy Tarreau86257dc2010-06-06 12:57:10 +0200390 if (!eb || eb->key > ts->exp.key)
391 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100392 continue;
393 }
394
395 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200396 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200397 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100398 stksess_free(t, ts);
399 }
400
401 /* We have found no task to expire in any tree */
402 t->exp_next = TICK_ETERNITY;
403 return t->exp_next;
404}
405
406/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200407 * Task processing function to trash expired sticky sessions. A pointer to the
408 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100409 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200410static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100411{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200412 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100413
414 task->expire = stktable_trash_expired(t);
415 return task;
416}
417
Willy Tarreauaea940e2010-06-06 11:56:36 +0200418/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100419int stktable_init(struct stktable *t)
420{
421 if (t->size) {
422 memset(&t->keys, 0, sizeof(t->keys));
423 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100424 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100425
Willy Tarreau393379c2010-06-06 12:11:37 +0200426 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100427
428 t->exp_next = TICK_ETERNITY;
429 if ( t->expire ) {
430 t->exp_task = task_new();
431 t->exp_task->process = process_table_expire;
432 t->exp_task->expire = TICK_ETERNITY;
433 t->exp_task->context = (void *)t;
434 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200435 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200436 peers_register_table(t->peers.p, t);
437 }
438
Emeric Brun3bd697e2010-01-04 15:23:48 +0100439 return t->pool != NULL;
440 }
441 return 1;
442}
443
444/*
445 * Configuration keywords of known table types
446 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200447struct stktable_type stktable_types[SMP_TYPES] = {
448 [SMP_T_SINT] = { "integer", 0, 4 },
449 [SMP_T_IPV4] = { "ip", 0, 4 },
450 [SMP_T_IPV6] = { "ipv6", 0, 16 },
451 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
452 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
453};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100454
455/*
456 * Parse table type configuration.
457 * Returns 0 on successful parsing, else 1.
458 * <myidx> is set at next configuration <args> index.
459 */
460int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
461{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200462 for (*type = 0; *type < SMP_TYPES; (*type)++) {
463 if (!stktable_types[*type].kw)
464 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100465 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
466 continue;
467
468 *key_size = stktable_types[*type].default_size;
469 (*myidx)++;
470
Willy Tarreauaea940e2010-06-06 11:56:36 +0200471 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100472 if (strcmp("len", args[*myidx]) == 0) {
473 (*myidx)++;
474 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200475 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100476 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200477 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200478 /* null terminated string needs +1 for '\0'. */
479 (*key_size)++;
480 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100481 (*myidx)++;
482 }
483 }
484 return 0;
485 }
486 return 1;
487}
488
Willy Tarreau8fed9032014-07-03 17:02:46 +0200489/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200490 * Note that the sample *is* modified and that the returned key may point
491 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200492 * Returns NULL if the sample could not be converted (eg: no matching type),
493 * otherwise a pointer to the static stktable_key filled with what is needed
494 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200495 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200496struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200497{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200498 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200499 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200500 return NULL;
501
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200502 /* Fill static_table_key. */
503 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200504
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200505 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200506 static_table_key->key = &smp->data.u.ipv4;
507 static_table_key->key_len = 4;
508 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200509
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200510 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200511 static_table_key->key = &smp->data.u.ipv6;
512 static_table_key->key_len = 16;
513 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200514
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200515 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200516 /* The stick table require a 32bit unsigned int, "sint" is a
517 * signed 64 it, so we can convert it inplace.
518 */
519 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
520 static_table_key->key = &smp->data.u.sint;
521 static_table_key->key_len = 4;
522 break;
523
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200524 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200525 if (!smp_make_safe(smp))
526 return NULL;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200527 static_table_key->key = smp->data.u.str.str;
528 static_table_key->key_len = smp->data.u.str.len;
529 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200530
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200531 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200532 if (smp->data.u.str.len < t->key_size) {
533 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +0200534 if (!smp_make_rw(smp))
535 return NULL;
536
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200537 if (smp->data.u.str.size < t->key_size)
538 if (!smp_dup(smp))
539 return NULL;
540 if (smp->data.u.str.size < t->key_size)
541 return NULL;
542 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
543 t->key_size - smp->data.u.str.len);
544 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200545 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200546 static_table_key->key = smp->data.u.str.str;
547 static_table_key->key_len = smp->data.u.str.len;
548 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200549
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200550 default: /* impossible case. */
551 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200552 }
553
Willy Tarreau07115412012-10-29 21:56:59 +0100554 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200555}
556
557/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200558 * Process a fetch + format conversion as defined by the sample expression <expr>
559 * on request or response considering the <opt> parameter. Returns either NULL if
560 * no key could be extracted, or a pointer to the converted result stored in
561 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
562 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200563 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
564 * without SMP_OPT_FINAL). The output will be usable like this :
565 *
566 * return MAY_CHANGE FINAL Meaning for the sample
567 * NULL 0 * Not present and will never be (eg: header)
568 * NULL 1 0 Not present or unstable, could change (eg: req_len)
569 * NULL 1 1 Not present, will not change anymore
570 * smp 0 * Present and will not change (eg: header)
571 * smp 1 0 not possible
572 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200573 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200574struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200575 unsigned int opt, struct sample_expr *expr, struct sample *smp)
576{
577 if (smp)
578 memset(smp, 0, sizeof(*smp));
579
Willy Tarreau192252e2015-04-04 01:47:55 +0200580 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200581 if (!smp)
582 return NULL;
583
584 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
585 return NULL; /* we can only use stable samples */
586
587 return smp_to_stkey(smp, t);
588}
589
590/*
Willy Tarreau12785782012-04-27 21:37:17 +0200591 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200592 * type <table_type>, otherwise zero. Used in configuration check.
593 */
Willy Tarreau12785782012-04-27 21:37:17 +0200594int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200595{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100596 int out_type;
597
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200598 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200599 return 0;
600
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100601 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200602
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200603 /* Convert sample. */
604 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100605 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200606
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200607 return 1;
608}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100609
Willy Tarreauedee1d62014-07-15 16:44:27 +0200610/* Extra data types processing : after the last one, some room may remain
611 * before STKTABLE_DATA_TYPES that may be used to register extra data types
612 * at run time.
613 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200614struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200615 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200616 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200617 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200618 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200619 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
620 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
621 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
622 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
623 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
624 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
625 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
626 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
627 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
628 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
629 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
630 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
631 [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 +0200632};
633
Willy Tarreauedee1d62014-07-15 16:44:27 +0200634/* Registers stick-table extra data type with index <idx>, name <name>, type
635 * <std_type> and arg type <arg_type>. If the index is negative, the next free
636 * index is automatically allocated. The allocated index is returned, or -1 if
637 * no free index was found or <name> was already registered. The <name> is used
638 * directly as a pointer, so if it's not stable, the caller must allocate it.
639 */
640int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
641{
642 if (idx < 0) {
643 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
644 if (!stktable_data_types[idx].name)
645 break;
646
647 if (strcmp(stktable_data_types[idx].name, name) == 0)
648 return -1;
649 }
650 }
651
652 if (idx >= STKTABLE_DATA_TYPES)
653 return -1;
654
655 if (stktable_data_types[idx].name != NULL)
656 return -1;
657
658 stktable_data_types[idx].name = name;
659 stktable_data_types[idx].std_type = std_type;
660 stktable_data_types[idx].arg_type = arg_type;
661 return idx;
662}
663
Willy Tarreau08d5f982010-06-06 13:34:54 +0200664/*
665 * Returns the data type number for the stktable_data_type whose name is <name>,
666 * or <0 if not found.
667 */
668int stktable_get_data_type(char *name)
669{
670 int type;
671
672 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200673 if (!stktable_data_types[type].name)
674 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200675 if (strcmp(name, stktable_data_types[type].name) == 0)
676 return type;
677 }
678 return -1;
679}
680
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200681/* Casts sample <smp> to the type of the table specified in arg(0), and looks
682 * it up into this table. Returns true if found, false otherwise. The input
683 * type is STR so that input samples are converted to string (since all types
684 * can be converted to strings), then the function casts the string again into
685 * the table's type. This is a double conversion, but in the future we might
686 * support automatic input types to perform the cast on the fly.
687 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200688static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200689{
690 struct stktable *t;
691 struct stktable_key *key;
692 struct stksess *ts;
693
694 t = &arg_p[0].data.prx->table;
695
696 key = smp_to_stkey(smp, t);
697 if (!key)
698 return 0;
699
700 ts = stktable_lookup_key(t, key);
701
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200702 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200703 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200704 smp->flags = SMP_F_VOL_TEST;
705 return 1;
706}
707
708/* Casts sample <smp> to the type of the table specified in arg(0), and looks
709 * it up into this table. Returns the data rate received from clients in bytes/s
710 * if the key is present in the table, otherwise zero, so that comparisons can
711 * be easily performed. If the inspected parameter is not stored in the table,
712 * <not found> is returned.
713 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200714static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200715{
716 struct stktable *t;
717 struct stktable_key *key;
718 struct stksess *ts;
719 void *ptr;
720
721 t = &arg_p[0].data.prx->table;
722
723 key = smp_to_stkey(smp, t);
724 if (!key)
725 return 0;
726
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200727 ts = stktable_lookup_key(t, key);
728
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200729 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200730 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200731 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200732
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200733 if (!ts) /* key not present */
734 return 1;
735
736 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
737 if (!ptr)
738 return 0; /* parameter not stored */
739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200741 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
742 return 1;
743}
744
745/* Casts sample <smp> to the type of the table specified in arg(0), and looks
746 * it up into this table. Returns the cumulated number of connections for the key
747 * if the key is present in the table, otherwise zero, so that comparisons can
748 * be easily performed. If the inspected parameter is not stored in the table,
749 * <not found> is returned.
750 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200751static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200752{
753 struct stktable *t;
754 struct stktable_key *key;
755 struct stksess *ts;
756 void *ptr;
757
758 t = &arg_p[0].data.prx->table;
759
760 key = smp_to_stkey(smp, t);
761 if (!key)
762 return 0;
763
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200764 ts = stktable_lookup_key(t, key);
765
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200766 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200767 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200768 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200769
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200770 if (!ts) /* key not present */
771 return 1;
772
773 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
774 if (!ptr)
775 return 0; /* parameter not stored */
776
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200777 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200778 return 1;
779}
780
781/* Casts sample <smp> to the type of the table specified in arg(0), and looks
782 * it up into this table. Returns the number of concurrent connections for the
783 * key if the key is present in the table, otherwise zero, so that comparisons
784 * can be easily performed. If the inspected parameter is not stored in the
785 * table, <not found> is returned.
786 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200787static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200788{
789 struct stktable *t;
790 struct stktable_key *key;
791 struct stksess *ts;
792 void *ptr;
793
794 t = &arg_p[0].data.prx->table;
795
796 key = smp_to_stkey(smp, t);
797 if (!key)
798 return 0;
799
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200800 ts = stktable_lookup_key(t, key);
801
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200802 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200803 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200804 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200805
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200806 if (!ts) /* key not present */
807 return 1;
808
809 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
810 if (!ptr)
811 return 0; /* parameter not stored */
812
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200813 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200814 return 1;
815}
816
817/* Casts sample <smp> to the type of the table specified in arg(0), and looks
818 * it up into this table. Returns the rate of incoming connections from the key
819 * if the key is present in the table, otherwise zero, so that comparisons can
820 * be easily performed. If the inspected parameter is not stored in the table,
821 * <not found> is returned.
822 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200823static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200824{
825 struct stktable *t;
826 struct stktable_key *key;
827 struct stksess *ts;
828 void *ptr;
829
830 t = &arg_p[0].data.prx->table;
831
832 key = smp_to_stkey(smp, t);
833 if (!key)
834 return 0;
835
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200836 ts = stktable_lookup_key(t, key);
837
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200838 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200839 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200840 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200841
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200842 if (!ts) /* key not present */
843 return 1;
844
845 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
846 if (!ptr)
847 return 0; /* parameter not stored */
848
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200849 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200850 t->data_arg[STKTABLE_DT_CONN_RATE].u);
851 return 1;
852}
853
854/* Casts sample <smp> to the type of the table specified in arg(0), and looks
855 * it up into this table. Returns the data rate sent to clients in bytes/s
856 * if the key is present in the table, otherwise zero, so that comparisons can
857 * be easily performed. If the inspected parameter is not stored in the table,
858 * <not found> is returned.
859 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200860static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200861{
862 struct stktable *t;
863 struct stktable_key *key;
864 struct stksess *ts;
865 void *ptr;
866
867 t = &arg_p[0].data.prx->table;
868
869 key = smp_to_stkey(smp, t);
870 if (!key)
871 return 0;
872
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200873 ts = stktable_lookup_key(t, key);
874
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200875 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200876 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200877 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200878
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200879 if (!ts) /* key not present */
880 return 1;
881
882 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
883 if (!ptr)
884 return 0; /* parameter not stored */
885
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200886 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200887 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
888 return 1;
889}
890
891/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200892 * it up into this table. Returns the value of the GPT0 tag for the key
893 * if the key is present in the table, otherwise false, so that comparisons can
894 * be easily performed. If the inspected parameter is not stored in the table,
895 * <not found> is returned.
896 */
897static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
898{
899 struct stktable *t;
900 struct stktable_key *key;
901 struct stksess *ts;
902 void *ptr;
903
904 t = &arg_p[0].data.prx->table;
905
906 key = smp_to_stkey(smp, t);
907 if (!key)
908 return 0;
909
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200910 ts = stktable_lookup_key(t, key);
911
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200912 smp->flags = SMP_F_VOL_TEST;
913 smp->data.type = SMP_T_SINT;
914 smp->data.u.sint = 0;
915
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200916 if (!ts) /* key not present */
917 return 1;
918
919 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
920 if (!ptr)
921 return 0; /* parameter not stored */
922
923 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
924 return 1;
925}
926
927/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200928 * it up into this table. Returns the value of the GPC0 counter for the key
929 * if the key is present in the table, otherwise zero, so that comparisons can
930 * be easily performed. If the inspected parameter is not stored in the table,
931 * <not found> is returned.
932 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200933static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200934{
935 struct stktable *t;
936 struct stktable_key *key;
937 struct stksess *ts;
938 void *ptr;
939
940 t = &arg_p[0].data.prx->table;
941
942 key = smp_to_stkey(smp, t);
943 if (!key)
944 return 0;
945
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200946 ts = stktable_lookup_key(t, key);
947
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200948 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200949 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200950 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200951
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200952 if (!ts) /* key not present */
953 return 1;
954
955 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
956 if (!ptr)
957 return 0; /* parameter not stored */
958
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200959 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200960 return 1;
961}
962
963/* Casts sample <smp> to the type of the table specified in arg(0), and looks
964 * it up into this table. Returns the event rate of the GPC0 counter for the key
965 * if the key is present in the table, otherwise zero, so that comparisons can
966 * be easily performed. If the inspected parameter is not stored in the table,
967 * <not found> is returned.
968 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200969static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200970{
971 struct stktable *t;
972 struct stktable_key *key;
973 struct stksess *ts;
974 void *ptr;
975
976 t = &arg_p[0].data.prx->table;
977
978 key = smp_to_stkey(smp, t);
979 if (!key)
980 return 0;
981
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200982 ts = stktable_lookup_key(t, key);
983
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200984 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200985 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200986 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200987
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200988 if (!ts) /* key not present */
989 return 1;
990
991 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
992 if (!ptr)
993 return 0; /* parameter not stored */
994
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200995 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200996 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
997 return 1;
998}
999
1000/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1001 * it up into this table. Returns the cumulated number of HTTP request errors
1002 * for the key if the key is present in the table, otherwise zero, so that
1003 * comparisons can be easily performed. If the inspected parameter is not stored
1004 * in the table, <not found> is returned.
1005 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001006static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001007{
1008 struct stktable *t;
1009 struct stktable_key *key;
1010 struct stksess *ts;
1011 void *ptr;
1012
1013 t = &arg_p[0].data.prx->table;
1014
1015 key = smp_to_stkey(smp, t);
1016 if (!key)
1017 return 0;
1018
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001019 ts = stktable_lookup_key(t, key);
1020
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001021 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001022 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001023 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001024
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001025 if (!ts) /* key not present */
1026 return 1;
1027
1028 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1029 if (!ptr)
1030 return 0; /* parameter not stored */
1031
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001032 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001033 return 1;
1034}
1035
1036/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1037 * it up into this table. Returns the HTTP request error rate the key
1038 * if the key is present in the table, otherwise zero, so that comparisons can
1039 * be easily performed. If the inspected parameter is not stored in the table,
1040 * <not found> is returned.
1041 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001042static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001043{
1044 struct stktable *t;
1045 struct stktable_key *key;
1046 struct stksess *ts;
1047 void *ptr;
1048
1049 t = &arg_p[0].data.prx->table;
1050
1051 key = smp_to_stkey(smp, t);
1052 if (!key)
1053 return 0;
1054
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001055 ts = stktable_lookup_key(t, key);
1056
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001057 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001058 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001059 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001060
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001061 if (!ts) /* key not present */
1062 return 1;
1063
1064 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1065 if (!ptr)
1066 return 0; /* parameter not stored */
1067
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001068 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001069 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1070 return 1;
1071}
1072
1073/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1074 * it up into this table. Returns the cumulated number of HTTP request for the
1075 * key if the key is present in the table, otherwise zero, so that comparisons
1076 * can be easily performed. If the inspected parameter is not stored in the
1077 * table, <not found> is returned.
1078 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001079static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001080{
1081 struct stktable *t;
1082 struct stktable_key *key;
1083 struct stksess *ts;
1084 void *ptr;
1085
1086 t = &arg_p[0].data.prx->table;
1087
1088 key = smp_to_stkey(smp, t);
1089 if (!key)
1090 return 0;
1091
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001092 ts = stktable_lookup_key(t, key);
1093
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001094 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001095 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001096 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001097
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001098 if (!ts) /* key not present */
1099 return 1;
1100
1101 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1102 if (!ptr)
1103 return 0; /* parameter not stored */
1104
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001105 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001106 return 1;
1107}
1108
1109/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1110 * it up into this table. Returns the HTTP request rate the key if the key is
1111 * present in the table, otherwise zero, so that comparisons can be easily
1112 * performed. If the inspected parameter is not stored in the table, <not found>
1113 * is returned.
1114 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001115static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001116{
1117 struct stktable *t;
1118 struct stktable_key *key;
1119 struct stksess *ts;
1120 void *ptr;
1121
1122 t = &arg_p[0].data.prx->table;
1123
1124 key = smp_to_stkey(smp, t);
1125 if (!key)
1126 return 0;
1127
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001128 ts = stktable_lookup_key(t, key);
1129
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001130 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001131 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001132 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001133
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001134 if (!ts) /* key not present */
1135 return 1;
1136
1137 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1138 if (!ptr)
1139 return 0; /* parameter not stored */
1140
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001141 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001142 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1143 return 1;
1144}
1145
1146/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1147 * it up into this table. Returns the volume of datareceived from clients in kbytes
1148 * if the key is present in the table, otherwise zero, so that comparisons can
1149 * be easily performed. If the inspected parameter is not stored in the table,
1150 * <not found> is returned.
1151 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001152static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001153{
1154 struct stktable *t;
1155 struct stktable_key *key;
1156 struct stksess *ts;
1157 void *ptr;
1158
1159 t = &arg_p[0].data.prx->table;
1160
1161 key = smp_to_stkey(smp, t);
1162 if (!key)
1163 return 0;
1164
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001165 ts = stktable_lookup_key(t, key);
1166
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001167 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001168 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001169 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001170
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001171 if (!ts) /* key not present */
1172 return 1;
1173
1174 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1175 if (!ptr)
1176 return 0; /* parameter not stored */
1177
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001178 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001179 return 1;
1180}
1181
1182/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1183 * it up into this table. Returns the volume of data sent to clients in kbytes
1184 * if the key is present in the table, otherwise zero, so that comparisons can
1185 * be easily performed. If the inspected parameter is not stored in the table,
1186 * <not found> is returned.
1187 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001188static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001189{
1190 struct stktable *t;
1191 struct stktable_key *key;
1192 struct stksess *ts;
1193 void *ptr;
1194
1195 t = &arg_p[0].data.prx->table;
1196
1197 key = smp_to_stkey(smp, t);
1198 if (!key)
1199 return 0;
1200
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001201 ts = stktable_lookup_key(t, key);
1202
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001203 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001204 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001205 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001206
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001207 if (!ts) /* key not present */
1208 return 1;
1209
1210 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1211 if (!ptr)
1212 return 0; /* parameter not stored */
1213
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001214 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001215 return 1;
1216}
1217
1218/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1219 * it up into this table. Returns the server ID associated with the key if the
1220 * key is present in the table, otherwise zero, so that comparisons can be
1221 * easily performed. If the inspected parameter is not stored in the table,
1222 * <not found> is returned.
1223 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001224static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001225{
1226 struct stktable *t;
1227 struct stktable_key *key;
1228 struct stksess *ts;
1229 void *ptr;
1230
1231 t = &arg_p[0].data.prx->table;
1232
1233 key = smp_to_stkey(smp, t);
1234 if (!key)
1235 return 0;
1236
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001237 ts = stktable_lookup_key(t, key);
1238
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001239 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001240 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001241 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001242
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001243 if (!ts) /* key not present */
1244 return 1;
1245
1246 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1247 if (!ptr)
1248 return 0; /* parameter not stored */
1249
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001250 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001251 return 1;
1252}
1253
1254/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1255 * it up into this table. Returns the cumulated number of sessions for the
1256 * key if the key is present in the table, otherwise zero, so that comparisons
1257 * can be easily performed. If the inspected parameter is not stored in the
1258 * table, <not found> is returned.
1259 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001260static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001261{
1262 struct stktable *t;
1263 struct stktable_key *key;
1264 struct stksess *ts;
1265 void *ptr;
1266
1267 t = &arg_p[0].data.prx->table;
1268
1269 key = smp_to_stkey(smp, t);
1270 if (!key)
1271 return 0;
1272
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001273 ts = stktable_lookup_key(t, key);
1274
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001275 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001276 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001277 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001278
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001279 if (!ts) /* key not present */
1280 return 1;
1281
1282 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1283 if (!ptr)
1284 return 0; /* parameter not stored */
1285
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001286 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001287 return 1;
1288}
1289
1290/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1291 * it up into this table. Returns the session rate the key if the key is
1292 * present in the table, otherwise zero, so that comparisons can be easily
1293 * performed. If the inspected parameter is not stored in the table, <not found>
1294 * is returned.
1295 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001296static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001297{
1298 struct stktable *t;
1299 struct stktable_key *key;
1300 struct stksess *ts;
1301 void *ptr;
1302
1303 t = &arg_p[0].data.prx->table;
1304
1305 key = smp_to_stkey(smp, t);
1306 if (!key)
1307 return 0;
1308
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001309 ts = stktable_lookup_key(t, key);
1310
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001311 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001312 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001313 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001314
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001315 if (!ts) /* key not present */
1316 return 1;
1317
1318 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1319 if (!ptr)
1320 return 0; /* parameter not stored */
1321
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001322 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001323 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1324 return 1;
1325}
1326
1327/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1328 * it up into this table. Returns the amount of concurrent connections tracking
1329 * the same key if the key is present in the table, otherwise zero, so that
1330 * comparisons can be easily performed. If the inspected parameter is not
1331 * stored in the table, <not found> is returned.
1332 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001333static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001334{
1335 struct stktable *t;
1336 struct stktable_key *key;
1337 struct stksess *ts;
1338
1339 t = &arg_p[0].data.prx->table;
1340
1341 key = smp_to_stkey(smp, t);
1342 if (!key)
1343 return 0;
1344
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001345 ts = stktable_lookup_key(t, key);
1346
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001347 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001348 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001349 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001350
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001351 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001352 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001353
1354 return 1;
1355}
1356
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001357/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001358static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001359 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001360{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001361 struct stksess *ts;
1362 struct stkctr *stkctr;
1363
1364 /* Extract the stksess, return OK if no stksess available. */
1365 if (s)
1366 stkctr = &s->stkctr[rule->arg.gpc.sc];
1367 else
1368 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001369
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001370 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001371 if (ts) {
1372 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001373
Willy Tarreau79c1e912016-01-25 14:54:45 +01001374 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
1375 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
1376 if (ptr1)
1377 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1378 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001379
Willy Tarreau79c1e912016-01-25 14:54:45 +01001380 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1381 if (ptr2)
1382 stktable_data_cast(ptr2, gpc0)++;
1383
1384 /* If data was modified, we need to touch to re-schedule sync */
1385 if (ptr1 || ptr2)
1386 stktable_touch(stkctr->table, ts, 1);
1387 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001388 return ACT_RET_CONT;
1389}
1390
1391/* This function is a common parser for using variables. It understands
1392 * the formats:
1393 *
1394 * sc-inc-gpc0(<stick-table ID>)
1395 *
1396 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1397 * it returns 1 and the variable <expr> is filled with the pointer to the
1398 * expression to execute.
1399 */
1400static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1401 struct act_rule *rule, char **err)
1402{
1403 const char *cmd_name = args[*arg-1];
1404 char *error;
1405
1406 cmd_name += strlen("sc-inc-gpc0");
1407 if (*cmd_name == '\0') {
1408 /* default stick table id. */
1409 rule->arg.gpc.sc = 0;
1410 } else {
1411 /* parse the stick table id. */
1412 if (*cmd_name != '(') {
1413 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1414 return ACT_RET_PRS_ERR;
1415 }
1416 cmd_name++; /* jump the '(' */
1417 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1418 if (*error != ')') {
1419 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1420 return ACT_RET_PRS_ERR;
1421 }
1422
1423 if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) {
1424 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
1425 ACT_ACTION_TRK_SCMAX-1);
1426 return ACT_RET_PRS_ERR;
1427 }
1428 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001429 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001430 rule->action_ptr = action_inc_gpc0;
1431 return ACT_RET_PRS_OK;
1432}
1433
1434/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001435static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001436 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001437{
1438 void *ptr;
1439 struct stksess *ts;
1440 struct stkctr *stkctr;
1441
1442 /* Extract the stksess, return OK if no stksess available. */
1443 if (s)
1444 stkctr = &s->stkctr[rule->arg.gpt.sc];
1445 else
1446 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001447
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001448 ts = stkctr_entry(stkctr);
1449 if (!ts)
1450 return ACT_RET_CONT;
1451
1452 /* Store the sample in the required sc, and ignore errors. */
1453 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001454 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001455 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001456 stktable_touch(stkctr->table, ts, 1);
1457 }
1458
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001459 return ACT_RET_CONT;
1460}
1461
1462/* This function is a common parser for using variables. It understands
1463 * the format:
1464 *
1465 * set-gpt0(<stick-table ID>) <expression>
1466 *
1467 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1468 * it returns 1 and the variable <expr> is filled with the pointer to the
1469 * expression to execute.
1470 */
1471static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
1472 struct act_rule *rule, char **err)
1473
1474
1475{
1476 const char *cmd_name = args[*arg-1];
1477 char *error;
1478
1479 cmd_name += strlen("sc-set-gpt0");
1480 if (*cmd_name == '\0') {
1481 /* default stick table id. */
1482 rule->arg.gpt.sc = 0;
1483 } else {
1484 /* parse the stick table id. */
1485 if (*cmd_name != '(') {
1486 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1487 return ACT_RET_PRS_ERR;
1488 }
1489 cmd_name++; /* jump the '(' */
1490 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1491 if (*error != ')') {
1492 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1493 return ACT_RET_PRS_ERR;
1494 }
1495
1496 if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) {
1497 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
1498 args[*arg-1], ACT_ACTION_TRK_SCMAX-1);
1499 return ACT_RET_PRS_ERR;
1500 }
1501 }
1502
1503 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
1504 if (*error != '\0') {
1505 memprintf(err, "invalid integer value '%s'", args[*arg]);
1506 return ACT_RET_PRS_ERR;
1507 }
1508 (*arg)++;
1509
Thierry FOURNIER42148732015-09-02 17:17:33 +02001510 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001511 rule->action_ptr = action_set_gpt0;
1512
1513 return ACT_RET_PRS_OK;
1514}
1515
1516static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001517 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001518 { "sc-set-gpt0", parse_set_gpt0, 1 },
1519 { /* END */ }
1520}};
1521
Willy Tarreau620408f2016-10-21 16:37:51 +02001522static struct action_kw_list tcp_sess_kws = { { }, {
1523 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
1524 { "sc-set-gpt0", parse_set_gpt0, 1 },
1525 { /* END */ }
1526}};
1527
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001528static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001529 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001530 { "sc-set-gpt0", parse_set_gpt0, 1 },
1531 { /* END */ }
1532}};
1533
1534static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001535 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001536 { "sc-set-gpt0", parse_set_gpt0, 1 },
1537 { /* END */ }
1538}};
1539
1540static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001541 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001542 { "sc-set-gpt0", parse_set_gpt0, 1 },
1543 { /* END */ }
1544}};
1545
1546static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001547 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001548 { "sc-set-gpt0", parse_set_gpt0, 1 },
1549 { /* END */ }
1550}};
1551
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001552/* Note: must not be declared <const> as its list will be overwritten */
1553static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02001554 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
1555 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1556 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1557 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1558 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1559 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1560 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1561 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1562 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1563 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1564 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1565 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1566 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1567 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1568 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1569 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1570 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1571 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1572 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001573 { /* END */ },
1574}};
1575
1576__attribute__((constructor))
1577static void __stick_table_init(void)
1578{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001579 /* register som action keywords. */
1580 tcp_req_conn_keywords_register(&tcp_conn_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +02001581 tcp_req_sess_keywords_register(&tcp_sess_kws);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001582 tcp_req_cont_keywords_register(&tcp_req_kws);
1583 tcp_res_cont_keywords_register(&tcp_res_kws);
1584 http_req_keywords_register(&http_req_kws);
1585 http_res_keywords_register(&http_res_kws);
1586
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001587 /* register sample fetch and format conversion keywords */
1588 sample_register_convs(&sample_conv_kws);
1589}