blob: 7026fe65659a1c03a831d7a434aae59377570b4f [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
Vincent Bernatef8f4fe2016-11-17 15:42:40 +0100173 ts = pool_alloc2(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100174 if (ts) {
175 t->current++;
Vincent Bernatef8f4fe2016-11-17 15:42:40 +0100176 ts += t->data_size;
Willy Tarreau393379c2010-06-06 12:11:37 +0200177 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200178 if (key)
179 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180 }
181
182 return ts;
183}
184
185/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200186 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200187 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100188 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200189struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100190{
191 struct ebmb_node *eb;
192
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200193 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200194 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 +0100195 else
196 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
197
198 if (unlikely(!eb)) {
199 /* no session found */
200 return NULL;
201 }
202
Willy Tarreau86257dc2010-06-06 12:57:10 +0200203 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100204}
205
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200206/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
207 * This is mainly used for situations where we want to refresh a key's usage so
208 * that it does not expire, and we want to have it created if it was not there.
209 * The stksess is returned, or NULL if it could not be created.
210 */
211struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
212{
213 struct stksess *ts;
214
215 ts = stktable_lookup_key(table, key);
216 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200217 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200218
219 /* entry does not exist, initialize a new one */
220 ts = stksess_new(table, key);
221 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200222 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200223 return ts;
224}
225
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200226/*
227 * Looks in table <t> for a sticky session with same key as <ts>.
228 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100229 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200230struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100232 struct ebmb_node *eb;
233
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200234 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200235 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100236 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200237 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200239 if (unlikely(!eb))
240 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100241
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200242 return ebmb_entry(eb, struct stksess, key);
243}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100244
Willy Tarreaucb183642010-06-06 17:58:34 +0200245/* Update the expiration timer for <ts> but do not touch its expiration node.
246 * The table's expiration timer is updated if set.
247 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200248struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts,
249 int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200250{
Emeric Brun85e77c72010-09-23 18:16:52 +0200251 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200252 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200253 if (t->expire) {
254 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
255 task_queue(t->exp_task);
256 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200257
Emeric Brunaaf58602015-06-15 17:23:30 +0200258 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200259 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200260 /* If this entry is not in the tree
261 or not scheduled for at least one peer */
262 if (!ts->upd.node.leaf_p
263 || (int)(t->commitupdate - ts->upd.key) >= 0
264 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200265 ts->upd.key = ++t->update;
266 t->localupdate = t->update;
267 eb32_delete(&ts->upd);
268 eb = eb32_insert(&t->updates, &ts->upd);
269 if (eb != &ts->upd) {
270 eb32_delete(eb);
271 eb32_insert(&t->updates, &ts->upd);
272 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200273 }
274 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
275 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200276 return ts;
277}
278
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200279/* Update the expiration timer for <ts> but do not touch its expiration node.
280 * The table's expiration timer is updated if set. The date of expiration coming from
281 * <t> stick-table configuration.
282 */
283struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
284{
285 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
286
287 return stktable_touch_with_exp(t, ts, local, expire);
288}
289
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200290/* Insert new sticky session <ts> in the table. It is assumed that it does not
291 * yet exist (the caller must check this). The table's timeout is updated if it
292 * is set. <ts> is returned.
293 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200294struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200295{
296 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200297 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200298 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200299 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200300 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100301}
302
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200303/* Same function as stktable_store(), but with <expire> as supplementary argument
304 * to set the date of expiration of <ts> new sticky session thanks to
305 * stktable_touch_with_exp().
306 */
307struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts,
308 int local, int expire)
309{
310 ebmb_insert(&t->keys, &ts->key, t->key_size);
311 stktable_touch_with_exp(t, ts, local, expire);
312 ts->exp.key = ts->expire;
313 eb32_insert(&t->exps, &ts->exp);
314 return ts;
315}
316
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200317/* Returns a valid or initialized stksess for the specified stktable_key in the
318 * specified table, or NULL if the key was NULL, or if no entry was found nor
319 * could be created. The entry's expiration is updated.
320 */
321struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
322{
323 struct stksess *ts;
324
325 if (!key)
326 return NULL;
327
328 ts = stktable_lookup_key(table, key);
329 if (ts == NULL) {
330 /* entry does not exist, initialize a new one */
331 ts = stksess_new(table, key);
332 if (!ts)
333 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200334 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200335 }
336 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200337 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200338 return ts;
339}
340
Emeric Brun3bd697e2010-01-04 15:23:48 +0100341/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200342 * Trash expired sticky sessions from table <t>. The next expiration date is
343 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100344 */
345static int stktable_trash_expired(struct stktable *t)
346{
347 struct stksess *ts;
348 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200349 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100350
351 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
352
353 while (1) {
354 if (unlikely(!eb)) {
355 /* we might have reached the end of the tree, typically because
356 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200357 * half. Let's loop back to the beginning of the tree now if we
358 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100359 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200360 if (looped)
361 break;
362 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363 eb = eb32_first(&t->exps);
364 if (likely(!eb))
365 break;
366 }
367
368 if (likely(tick_is_lt(now_ms, eb->key))) {
369 /* timer not expired yet, revisit it later */
370 t->exp_next = eb->key;
371 return t->exp_next;
372 }
373
374 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200375 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100376 eb = eb32_next(eb);
377
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200378 /* don't delete an entry which is currently referenced */
379 if (ts->ref_cnt)
380 continue;
381
Willy Tarreau86257dc2010-06-06 12:57:10 +0200382 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383
384 if (!tick_is_expired(ts->expire, now_ms)) {
385 if (!tick_isset(ts->expire))
386 continue;
387
Willy Tarreau86257dc2010-06-06 12:57:10 +0200388 ts->exp.key = ts->expire;
389 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
Willy Tarreau86257dc2010-06-06 12:57:10 +0200391 if (!eb || eb->key > ts->exp.key)
392 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100393 continue;
394 }
395
396 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200397 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200398 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100399 stksess_free(t, ts);
400 }
401
402 /* We have found no task to expire in any tree */
403 t->exp_next = TICK_ETERNITY;
404 return t->exp_next;
405}
406
407/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200408 * Task processing function to trash expired sticky sessions. A pointer to the
409 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100410 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200411static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100412{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200413 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100414
415 task->expire = stktable_trash_expired(t);
416 return task;
417}
418
Willy Tarreauaea940e2010-06-06 11:56:36 +0200419/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100420int stktable_init(struct stktable *t)
421{
422 if (t->size) {
423 memset(&t->keys, 0, sizeof(t->keys));
424 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100425 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100426
Willy Tarreau393379c2010-06-06 12:11:37 +0200427 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100428
429 t->exp_next = TICK_ETERNITY;
430 if ( t->expire ) {
431 t->exp_task = task_new();
432 t->exp_task->process = process_table_expire;
433 t->exp_task->expire = TICK_ETERNITY;
434 t->exp_task->context = (void *)t;
435 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200436 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200437 peers_register_table(t->peers.p, t);
438 }
439
Emeric Brun3bd697e2010-01-04 15:23:48 +0100440 return t->pool != NULL;
441 }
442 return 1;
443}
444
445/*
446 * Configuration keywords of known table types
447 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200448struct stktable_type stktable_types[SMP_TYPES] = {
449 [SMP_T_SINT] = { "integer", 0, 4 },
450 [SMP_T_IPV4] = { "ip", 0, 4 },
451 [SMP_T_IPV6] = { "ipv6", 0, 16 },
452 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
453 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
454};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100455
456/*
457 * Parse table type configuration.
458 * Returns 0 on successful parsing, else 1.
459 * <myidx> is set at next configuration <args> index.
460 */
461int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
462{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200463 for (*type = 0; *type < SMP_TYPES; (*type)++) {
464 if (!stktable_types[*type].kw)
465 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100466 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
467 continue;
468
469 *key_size = stktable_types[*type].default_size;
470 (*myidx)++;
471
Willy Tarreauaea940e2010-06-06 11:56:36 +0200472 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100473 if (strcmp("len", args[*myidx]) == 0) {
474 (*myidx)++;
475 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200476 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100477 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200478 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200479 /* null terminated string needs +1 for '\0'. */
480 (*key_size)++;
481 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100482 (*myidx)++;
483 }
484 }
485 return 0;
486 }
487 return 1;
488}
489
Willy Tarreau8fed9032014-07-03 17:02:46 +0200490/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200491 * Note that the sample *is* modified and that the returned key may point
492 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200493 * Returns NULL if the sample could not be converted (eg: no matching type),
494 * otherwise a pointer to the static stktable_key filled with what is needed
495 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200496 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200497struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200498{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200499 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200500 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200501 return NULL;
502
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200503 /* Fill static_table_key. */
504 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200505
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200506 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200507 static_table_key->key = &smp->data.u.ipv4;
508 static_table_key->key_len = 4;
509 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200510
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200511 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200512 static_table_key->key = &smp->data.u.ipv6;
513 static_table_key->key_len = 16;
514 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200515
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200516 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200517 /* The stick table require a 32bit unsigned int, "sint" is a
518 * signed 64 it, so we can convert it inplace.
519 */
520 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
521 static_table_key->key = &smp->data.u.sint;
522 static_table_key->key_len = 4;
523 break;
524
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200525 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200526 if (!smp_make_safe(smp))
527 return NULL;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200528 static_table_key->key = smp->data.u.str.str;
529 static_table_key->key_len = smp->data.u.str.len;
530 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200531
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200532 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200533 if (smp->data.u.str.len < t->key_size) {
534 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +0200535 if (!smp_make_rw(smp))
536 return NULL;
537
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200538 if (smp->data.u.str.size < t->key_size)
539 if (!smp_dup(smp))
540 return NULL;
541 if (smp->data.u.str.size < t->key_size)
542 return NULL;
543 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
544 t->key_size - smp->data.u.str.len);
545 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200546 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200547 static_table_key->key = smp->data.u.str.str;
548 static_table_key->key_len = smp->data.u.str.len;
549 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200550
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200551 default: /* impossible case. */
552 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200553 }
554
Willy Tarreau07115412012-10-29 21:56:59 +0100555 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200556}
557
558/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200559 * Process a fetch + format conversion as defined by the sample expression <expr>
560 * on request or response considering the <opt> parameter. Returns either NULL if
561 * no key could be extracted, or a pointer to the converted result stored in
562 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
563 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200564 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
565 * without SMP_OPT_FINAL). The output will be usable like this :
566 *
567 * return MAY_CHANGE FINAL Meaning for the sample
568 * NULL 0 * Not present and will never be (eg: header)
569 * NULL 1 0 Not present or unstable, could change (eg: req_len)
570 * NULL 1 1 Not present, will not change anymore
571 * smp 0 * Present and will not change (eg: header)
572 * smp 1 0 not possible
573 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200574 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200575struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200576 unsigned int opt, struct sample_expr *expr, struct sample *smp)
577{
578 if (smp)
579 memset(smp, 0, sizeof(*smp));
580
Willy Tarreau192252e2015-04-04 01:47:55 +0200581 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200582 if (!smp)
583 return NULL;
584
585 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
586 return NULL; /* we can only use stable samples */
587
588 return smp_to_stkey(smp, t);
589}
590
591/*
Willy Tarreau12785782012-04-27 21:37:17 +0200592 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200593 * type <table_type>, otherwise zero. Used in configuration check.
594 */
Willy Tarreau12785782012-04-27 21:37:17 +0200595int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200596{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100597 int out_type;
598
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200599 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200600 return 0;
601
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100602 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200603
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200604 /* Convert sample. */
605 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100606 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200607
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200608 return 1;
609}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100610
Willy Tarreauedee1d62014-07-15 16:44:27 +0200611/* Extra data types processing : after the last one, some room may remain
612 * before STKTABLE_DATA_TYPES that may be used to register extra data types
613 * at run time.
614 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200615struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200616 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200617 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200618 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200619 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200620 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
621 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
622 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
623 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
624 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
625 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
626 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
627 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
628 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
629 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
630 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
631 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
632 [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 +0200633};
634
Willy Tarreauedee1d62014-07-15 16:44:27 +0200635/* Registers stick-table extra data type with index <idx>, name <name>, type
636 * <std_type> and arg type <arg_type>. If the index is negative, the next free
637 * index is automatically allocated. The allocated index is returned, or -1 if
638 * no free index was found or <name> was already registered. The <name> is used
639 * directly as a pointer, so if it's not stable, the caller must allocate it.
640 */
641int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
642{
643 if (idx < 0) {
644 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
645 if (!stktable_data_types[idx].name)
646 break;
647
648 if (strcmp(stktable_data_types[idx].name, name) == 0)
649 return -1;
650 }
651 }
652
653 if (idx >= STKTABLE_DATA_TYPES)
654 return -1;
655
656 if (stktable_data_types[idx].name != NULL)
657 return -1;
658
659 stktable_data_types[idx].name = name;
660 stktable_data_types[idx].std_type = std_type;
661 stktable_data_types[idx].arg_type = arg_type;
662 return idx;
663}
664
Willy Tarreau08d5f982010-06-06 13:34:54 +0200665/*
666 * Returns the data type number for the stktable_data_type whose name is <name>,
667 * or <0 if not found.
668 */
669int stktable_get_data_type(char *name)
670{
671 int type;
672
673 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200674 if (!stktable_data_types[type].name)
675 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200676 if (strcmp(name, stktable_data_types[type].name) == 0)
677 return type;
678 }
679 return -1;
680}
681
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200682/* Casts sample <smp> to the type of the table specified in arg(0), and looks
683 * it up into this table. Returns true if found, false otherwise. The input
684 * type is STR so that input samples are converted to string (since all types
685 * can be converted to strings), then the function casts the string again into
686 * the table's type. This is a double conversion, but in the future we might
687 * support automatic input types to perform the cast on the fly.
688 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200689static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200690{
691 struct stktable *t;
692 struct stktable_key *key;
693 struct stksess *ts;
694
695 t = &arg_p[0].data.prx->table;
696
697 key = smp_to_stkey(smp, t);
698 if (!key)
699 return 0;
700
701 ts = stktable_lookup_key(t, key);
702
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200703 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200704 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200705 smp->flags = SMP_F_VOL_TEST;
706 return 1;
707}
708
709/* Casts sample <smp> to the type of the table specified in arg(0), and looks
710 * it up into this table. Returns the data rate received from clients in bytes/s
711 * if the key is present in the table, otherwise zero, so that comparisons can
712 * be easily performed. If the inspected parameter is not stored in the table,
713 * <not found> is returned.
714 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200715static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200716{
717 struct stktable *t;
718 struct stktable_key *key;
719 struct stksess *ts;
720 void *ptr;
721
722 t = &arg_p[0].data.prx->table;
723
724 key = smp_to_stkey(smp, t);
725 if (!key)
726 return 0;
727
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200728 ts = stktable_lookup_key(t, key);
729
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200730 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200731 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200732 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200733
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200734 if (!ts) /* key not present */
735 return 1;
736
737 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
738 if (!ptr)
739 return 0; /* parameter not stored */
740
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200741 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200742 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
743 return 1;
744}
745
746/* Casts sample <smp> to the type of the table specified in arg(0), and looks
747 * it up into this table. Returns the cumulated number of connections for the key
748 * if the key is present in the table, otherwise zero, so that comparisons can
749 * be easily performed. If the inspected parameter is not stored in the table,
750 * <not found> is returned.
751 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200752static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200753{
754 struct stktable *t;
755 struct stktable_key *key;
756 struct stksess *ts;
757 void *ptr;
758
759 t = &arg_p[0].data.prx->table;
760
761 key = smp_to_stkey(smp, t);
762 if (!key)
763 return 0;
764
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200765 ts = stktable_lookup_key(t, key);
766
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200767 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200768 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200769 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200770
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200771 if (!ts) /* key not present */
772 return 1;
773
774 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
775 if (!ptr)
776 return 0; /* parameter not stored */
777
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200778 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200779 return 1;
780}
781
782/* Casts sample <smp> to the type of the table specified in arg(0), and looks
783 * it up into this table. Returns the number of concurrent connections for the
784 * key if the key is present in the table, otherwise zero, so that comparisons
785 * can be easily performed. If the inspected parameter is not stored in the
786 * table, <not found> is returned.
787 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200788static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200789{
790 struct stktable *t;
791 struct stktable_key *key;
792 struct stksess *ts;
793 void *ptr;
794
795 t = &arg_p[0].data.prx->table;
796
797 key = smp_to_stkey(smp, t);
798 if (!key)
799 return 0;
800
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200801 ts = stktable_lookup_key(t, key);
802
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200803 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200804 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200805 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200806
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200807 if (!ts) /* key not present */
808 return 1;
809
810 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
811 if (!ptr)
812 return 0; /* parameter not stored */
813
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200814 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200815 return 1;
816}
817
818/* Casts sample <smp> to the type of the table specified in arg(0), and looks
819 * it up into this table. Returns the rate of incoming connections from the key
820 * if the key is present in the table, otherwise zero, so that comparisons can
821 * be easily performed. If the inspected parameter is not stored in the table,
822 * <not found> is returned.
823 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200824static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200825{
826 struct stktable *t;
827 struct stktable_key *key;
828 struct stksess *ts;
829 void *ptr;
830
831 t = &arg_p[0].data.prx->table;
832
833 key = smp_to_stkey(smp, t);
834 if (!key)
835 return 0;
836
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200837 ts = stktable_lookup_key(t, key);
838
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200839 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200840 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200841 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200842
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200843 if (!ts) /* key not present */
844 return 1;
845
846 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
847 if (!ptr)
848 return 0; /* parameter not stored */
849
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200850 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200851 t->data_arg[STKTABLE_DT_CONN_RATE].u);
852 return 1;
853}
854
855/* Casts sample <smp> to the type of the table specified in arg(0), and looks
856 * it up into this table. Returns the data rate sent to clients in bytes/s
857 * if the key is present in the table, otherwise zero, so that comparisons can
858 * be easily performed. If the inspected parameter is not stored in the table,
859 * <not found> is returned.
860 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200861static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200862{
863 struct stktable *t;
864 struct stktable_key *key;
865 struct stksess *ts;
866 void *ptr;
867
868 t = &arg_p[0].data.prx->table;
869
870 key = smp_to_stkey(smp, t);
871 if (!key)
872 return 0;
873
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200874 ts = stktable_lookup_key(t, key);
875
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200876 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200877 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200878 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200879
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200880 if (!ts) /* key not present */
881 return 1;
882
883 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
884 if (!ptr)
885 return 0; /* parameter not stored */
886
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200887 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200888 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
889 return 1;
890}
891
892/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200893 * it up into this table. Returns the value of the GPT0 tag for the key
894 * if the key is present in the table, otherwise false, so that comparisons can
895 * be easily performed. If the inspected parameter is not stored in the table,
896 * <not found> is returned.
897 */
898static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
899{
900 struct stktable *t;
901 struct stktable_key *key;
902 struct stksess *ts;
903 void *ptr;
904
905 t = &arg_p[0].data.prx->table;
906
907 key = smp_to_stkey(smp, t);
908 if (!key)
909 return 0;
910
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200911 ts = stktable_lookup_key(t, key);
912
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200913 smp->flags = SMP_F_VOL_TEST;
914 smp->data.type = SMP_T_SINT;
915 smp->data.u.sint = 0;
916
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200917 if (!ts) /* key not present */
918 return 1;
919
920 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
921 if (!ptr)
922 return 0; /* parameter not stored */
923
924 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
925 return 1;
926}
927
928/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200929 * it up into this table. Returns the value of the GPC0 counter for the key
930 * if the key is present in the table, otherwise zero, so that comparisons can
931 * be easily performed. If the inspected parameter is not stored in the table,
932 * <not found> is returned.
933 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200934static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200935{
936 struct stktable *t;
937 struct stktable_key *key;
938 struct stksess *ts;
939 void *ptr;
940
941 t = &arg_p[0].data.prx->table;
942
943 key = smp_to_stkey(smp, t);
944 if (!key)
945 return 0;
946
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200947 ts = stktable_lookup_key(t, key);
948
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200949 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200950 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200951 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200952
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200953 if (!ts) /* key not present */
954 return 1;
955
956 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
957 if (!ptr)
958 return 0; /* parameter not stored */
959
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200960 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200961 return 1;
962}
963
964/* Casts sample <smp> to the type of the table specified in arg(0), and looks
965 * it up into this table. Returns the event rate of the GPC0 counter for the key
966 * if the key is present in the table, otherwise zero, so that comparisons can
967 * be easily performed. If the inspected parameter is not stored in the table,
968 * <not found> is returned.
969 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200970static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200971{
972 struct stktable *t;
973 struct stktable_key *key;
974 struct stksess *ts;
975 void *ptr;
976
977 t = &arg_p[0].data.prx->table;
978
979 key = smp_to_stkey(smp, t);
980 if (!key)
981 return 0;
982
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200983 ts = stktable_lookup_key(t, key);
984
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200985 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200986 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200987 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200988
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200989 if (!ts) /* key not present */
990 return 1;
991
992 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
993 if (!ptr)
994 return 0; /* parameter not stored */
995
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200996 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200997 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
998 return 1;
999}
1000
1001/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1002 * it up into this table. Returns the cumulated number of HTTP request errors
1003 * for the key if the key is present in the table, otherwise zero, so that
1004 * comparisons can be easily performed. If the inspected parameter is not stored
1005 * in the table, <not found> is returned.
1006 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001007static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001008{
1009 struct stktable *t;
1010 struct stktable_key *key;
1011 struct stksess *ts;
1012 void *ptr;
1013
1014 t = &arg_p[0].data.prx->table;
1015
1016 key = smp_to_stkey(smp, t);
1017 if (!key)
1018 return 0;
1019
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001020 ts = stktable_lookup_key(t, key);
1021
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001022 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001023 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001024 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001025
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001026 if (!ts) /* key not present */
1027 return 1;
1028
1029 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1030 if (!ptr)
1031 return 0; /* parameter not stored */
1032
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001033 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001034 return 1;
1035}
1036
1037/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1038 * it up into this table. Returns the HTTP request error rate the key
1039 * if the key is present in the table, otherwise zero, so that comparisons can
1040 * be easily performed. If the inspected parameter is not stored in the table,
1041 * <not found> is returned.
1042 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001043static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001044{
1045 struct stktable *t;
1046 struct stktable_key *key;
1047 struct stksess *ts;
1048 void *ptr;
1049
1050 t = &arg_p[0].data.prx->table;
1051
1052 key = smp_to_stkey(smp, t);
1053 if (!key)
1054 return 0;
1055
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001056 ts = stktable_lookup_key(t, key);
1057
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001058 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001059 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001060 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001061
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001062 if (!ts) /* key not present */
1063 return 1;
1064
1065 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1066 if (!ptr)
1067 return 0; /* parameter not stored */
1068
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001069 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001070 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1071 return 1;
1072}
1073
1074/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1075 * it up into this table. Returns the cumulated number of HTTP request for the
1076 * key if the key is present in the table, otherwise zero, so that comparisons
1077 * can be easily performed. If the inspected parameter is not stored in the
1078 * table, <not found> is returned.
1079 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001080static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001081{
1082 struct stktable *t;
1083 struct stktable_key *key;
1084 struct stksess *ts;
1085 void *ptr;
1086
1087 t = &arg_p[0].data.prx->table;
1088
1089 key = smp_to_stkey(smp, t);
1090 if (!key)
1091 return 0;
1092
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001093 ts = stktable_lookup_key(t, key);
1094
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001095 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001096 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001097 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001098
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001099 if (!ts) /* key not present */
1100 return 1;
1101
1102 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1103 if (!ptr)
1104 return 0; /* parameter not stored */
1105
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001106 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001107 return 1;
1108}
1109
1110/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1111 * it up into this table. Returns the HTTP request rate the key if the key is
1112 * present in the table, otherwise zero, so that comparisons can be easily
1113 * performed. If the inspected parameter is not stored in the table, <not found>
1114 * is returned.
1115 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001116static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001117{
1118 struct stktable *t;
1119 struct stktable_key *key;
1120 struct stksess *ts;
1121 void *ptr;
1122
1123 t = &arg_p[0].data.prx->table;
1124
1125 key = smp_to_stkey(smp, t);
1126 if (!key)
1127 return 0;
1128
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001129 ts = stktable_lookup_key(t, key);
1130
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001131 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001132 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001133 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001134
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001135 if (!ts) /* key not present */
1136 return 1;
1137
1138 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1139 if (!ptr)
1140 return 0; /* parameter not stored */
1141
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001142 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001143 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1144 return 1;
1145}
1146
1147/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1148 * it up into this table. Returns the volume of datareceived from clients in kbytes
1149 * if the key is present in the table, otherwise zero, so that comparisons can
1150 * be easily performed. If the inspected parameter is not stored in the table,
1151 * <not found> is returned.
1152 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001153static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001154{
1155 struct stktable *t;
1156 struct stktable_key *key;
1157 struct stksess *ts;
1158 void *ptr;
1159
1160 t = &arg_p[0].data.prx->table;
1161
1162 key = smp_to_stkey(smp, t);
1163 if (!key)
1164 return 0;
1165
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001166 ts = stktable_lookup_key(t, key);
1167
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001168 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001169 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001170 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001171
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001172 if (!ts) /* key not present */
1173 return 1;
1174
1175 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1176 if (!ptr)
1177 return 0; /* parameter not stored */
1178
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001179 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001180 return 1;
1181}
1182
1183/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1184 * it up into this table. Returns the volume of data sent to clients in kbytes
1185 * if the key is present in the table, otherwise zero, so that comparisons can
1186 * be easily performed. If the inspected parameter is not stored in the table,
1187 * <not found> is returned.
1188 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001189static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001190{
1191 struct stktable *t;
1192 struct stktable_key *key;
1193 struct stksess *ts;
1194 void *ptr;
1195
1196 t = &arg_p[0].data.prx->table;
1197
1198 key = smp_to_stkey(smp, t);
1199 if (!key)
1200 return 0;
1201
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001202 ts = stktable_lookup_key(t, key);
1203
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001204 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001205 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001206 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001207
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001208 if (!ts) /* key not present */
1209 return 1;
1210
1211 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1212 if (!ptr)
1213 return 0; /* parameter not stored */
1214
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001215 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001216 return 1;
1217}
1218
1219/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1220 * it up into this table. Returns the server ID associated with the key if the
1221 * key is present in the table, otherwise zero, so that comparisons can be
1222 * easily performed. If the inspected parameter is not stored in the table,
1223 * <not found> is returned.
1224 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001225static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001226{
1227 struct stktable *t;
1228 struct stktable_key *key;
1229 struct stksess *ts;
1230 void *ptr;
1231
1232 t = &arg_p[0].data.prx->table;
1233
1234 key = smp_to_stkey(smp, t);
1235 if (!key)
1236 return 0;
1237
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001238 ts = stktable_lookup_key(t, key);
1239
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001240 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001241 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001242 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001243
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001244 if (!ts) /* key not present */
1245 return 1;
1246
1247 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1248 if (!ptr)
1249 return 0; /* parameter not stored */
1250
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001251 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001252 return 1;
1253}
1254
1255/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1256 * it up into this table. Returns the cumulated number of sessions for the
1257 * key if the key is present in the table, otherwise zero, so that comparisons
1258 * can be easily performed. If the inspected parameter is not stored in the
1259 * table, <not found> is returned.
1260 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001261static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001262{
1263 struct stktable *t;
1264 struct stktable_key *key;
1265 struct stksess *ts;
1266 void *ptr;
1267
1268 t = &arg_p[0].data.prx->table;
1269
1270 key = smp_to_stkey(smp, t);
1271 if (!key)
1272 return 0;
1273
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001274 ts = stktable_lookup_key(t, key);
1275
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001276 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001277 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001278 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001279
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001280 if (!ts) /* key not present */
1281 return 1;
1282
1283 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1284 if (!ptr)
1285 return 0; /* parameter not stored */
1286
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001287 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001288 return 1;
1289}
1290
1291/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1292 * it up into this table. Returns the session rate the key if the key is
1293 * present in the table, otherwise zero, so that comparisons can be easily
1294 * performed. If the inspected parameter is not stored in the table, <not found>
1295 * is returned.
1296 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001297static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001298{
1299 struct stktable *t;
1300 struct stktable_key *key;
1301 struct stksess *ts;
1302 void *ptr;
1303
1304 t = &arg_p[0].data.prx->table;
1305
1306 key = smp_to_stkey(smp, t);
1307 if (!key)
1308 return 0;
1309
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001310 ts = stktable_lookup_key(t, key);
1311
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001312 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001313 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001314 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001315
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001316 if (!ts) /* key not present */
1317 return 1;
1318
1319 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1320 if (!ptr)
1321 return 0; /* parameter not stored */
1322
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001323 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001324 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1325 return 1;
1326}
1327
1328/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1329 * it up into this table. Returns the amount of concurrent connections tracking
1330 * the same key if the key is present in the table, otherwise zero, so that
1331 * comparisons can be easily performed. If the inspected parameter is not
1332 * stored in the table, <not found> is returned.
1333 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001334static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001335{
1336 struct stktable *t;
1337 struct stktable_key *key;
1338 struct stksess *ts;
1339
1340 t = &arg_p[0].data.prx->table;
1341
1342 key = smp_to_stkey(smp, t);
1343 if (!key)
1344 return 0;
1345
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001346 ts = stktable_lookup_key(t, key);
1347
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001348 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001349 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001350 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001351
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001352 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001353 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001354
1355 return 1;
1356}
1357
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001358/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001359static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001360 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001361{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001362 struct stksess *ts;
1363 struct stkctr *stkctr;
1364
1365 /* Extract the stksess, return OK if no stksess available. */
1366 if (s)
1367 stkctr = &s->stkctr[rule->arg.gpc.sc];
1368 else
1369 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001370
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001371 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001372 if (ts) {
1373 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001374
Willy Tarreau79c1e912016-01-25 14:54:45 +01001375 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
1376 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
1377 if (ptr1)
1378 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1379 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001380
Willy Tarreau79c1e912016-01-25 14:54:45 +01001381 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1382 if (ptr2)
1383 stktable_data_cast(ptr2, gpc0)++;
1384
1385 /* If data was modified, we need to touch to re-schedule sync */
1386 if (ptr1 || ptr2)
1387 stktable_touch(stkctr->table, ts, 1);
1388 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001389 return ACT_RET_CONT;
1390}
1391
1392/* This function is a common parser for using variables. It understands
1393 * the formats:
1394 *
1395 * sc-inc-gpc0(<stick-table ID>)
1396 *
1397 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1398 * it returns 1 and the variable <expr> is filled with the pointer to the
1399 * expression to execute.
1400 */
1401static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1402 struct act_rule *rule, char **err)
1403{
1404 const char *cmd_name = args[*arg-1];
1405 char *error;
1406
1407 cmd_name += strlen("sc-inc-gpc0");
1408 if (*cmd_name == '\0') {
1409 /* default stick table id. */
1410 rule->arg.gpc.sc = 0;
1411 } else {
1412 /* parse the stick table id. */
1413 if (*cmd_name != '(') {
1414 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1415 return ACT_RET_PRS_ERR;
1416 }
1417 cmd_name++; /* jump the '(' */
1418 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1419 if (*error != ')') {
1420 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1421 return ACT_RET_PRS_ERR;
1422 }
1423
1424 if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) {
1425 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
1426 ACT_ACTION_TRK_SCMAX-1);
1427 return ACT_RET_PRS_ERR;
1428 }
1429 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001430 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001431 rule->action_ptr = action_inc_gpc0;
1432 return ACT_RET_PRS_OK;
1433}
1434
1435/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001436static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001437 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001438{
1439 void *ptr;
1440 struct stksess *ts;
1441 struct stkctr *stkctr;
1442
1443 /* Extract the stksess, return OK if no stksess available. */
1444 if (s)
1445 stkctr = &s->stkctr[rule->arg.gpt.sc];
1446 else
1447 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001448
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001449 ts = stkctr_entry(stkctr);
1450 if (!ts)
1451 return ACT_RET_CONT;
1452
1453 /* Store the sample in the required sc, and ignore errors. */
1454 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001455 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001456 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001457 stktable_touch(stkctr->table, ts, 1);
1458 }
1459
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001460 return ACT_RET_CONT;
1461}
1462
1463/* This function is a common parser for using variables. It understands
1464 * the format:
1465 *
1466 * set-gpt0(<stick-table ID>) <expression>
1467 *
1468 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1469 * it returns 1 and the variable <expr> is filled with the pointer to the
1470 * expression to execute.
1471 */
1472static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
1473 struct act_rule *rule, char **err)
1474
1475
1476{
1477 const char *cmd_name = args[*arg-1];
1478 char *error;
1479
1480 cmd_name += strlen("sc-set-gpt0");
1481 if (*cmd_name == '\0') {
1482 /* default stick table id. */
1483 rule->arg.gpt.sc = 0;
1484 } else {
1485 /* parse the stick table id. */
1486 if (*cmd_name != '(') {
1487 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1488 return ACT_RET_PRS_ERR;
1489 }
1490 cmd_name++; /* jump the '(' */
1491 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1492 if (*error != ')') {
1493 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1494 return ACT_RET_PRS_ERR;
1495 }
1496
1497 if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) {
1498 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
1499 args[*arg-1], ACT_ACTION_TRK_SCMAX-1);
1500 return ACT_RET_PRS_ERR;
1501 }
1502 }
1503
1504 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
1505 if (*error != '\0') {
1506 memprintf(err, "invalid integer value '%s'", args[*arg]);
1507 return ACT_RET_PRS_ERR;
1508 }
1509 (*arg)++;
1510
Thierry FOURNIER42148732015-09-02 17:17:33 +02001511 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001512 rule->action_ptr = action_set_gpt0;
1513
1514 return ACT_RET_PRS_OK;
1515}
1516
1517static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001518 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001519 { "sc-set-gpt0", parse_set_gpt0, 1 },
1520 { /* END */ }
1521}};
1522
Willy Tarreau620408f2016-10-21 16:37:51 +02001523static struct action_kw_list tcp_sess_kws = { { }, {
1524 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
1525 { "sc-set-gpt0", parse_set_gpt0, 1 },
1526 { /* END */ }
1527}};
1528
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001529static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001530 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001531 { "sc-set-gpt0", parse_set_gpt0, 1 },
1532 { /* END */ }
1533}};
1534
1535static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001536 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001537 { "sc-set-gpt0", parse_set_gpt0, 1 },
1538 { /* END */ }
1539}};
1540
1541static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001542 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001543 { "sc-set-gpt0", parse_set_gpt0, 1 },
1544 { /* END */ }
1545}};
1546
1547static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001548 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001549 { "sc-set-gpt0", parse_set_gpt0, 1 },
1550 { /* END */ }
1551}};
1552
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001553/* Note: must not be declared <const> as its list will be overwritten */
1554static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02001555 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
1556 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1557 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1558 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1559 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1560 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1561 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1562 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1563 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1564 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1565 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1566 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1567 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1568 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1569 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1570 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1571 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1572 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1573 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001574 { /* END */ },
1575}};
1576
1577__attribute__((constructor))
1578static void __stick_table_init(void)
1579{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001580 /* register som action keywords. */
1581 tcp_req_conn_keywords_register(&tcp_conn_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +02001582 tcp_req_sess_keywords_register(&tcp_sess_kws);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001583 tcp_req_cont_keywords_register(&tcp_req_kws);
1584 tcp_res_cont_keywords_register(&tcp_res_kws);
1585 http_req_keywords_register(&http_req_kws);
1586 http_res_keywords_register(&http_res_kws);
1587
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001588 /* register sample fetch and format conversion keywords */
1589 sample_register_convs(&sample_conv_kws);
1590}