blob: 5e7a2c48f0e90dd6e601817df8615cec983423cd [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>
Emeric Brun3bd697e2010-01-04 15:23:48 +010026#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020027#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020028#include <proto/stream.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020029#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010030#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020031#include <proto/peers.h>
32#include <types/global.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010033
Willy Tarreau12785782012-04-27 21:37:17 +020034/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010035struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020036
Emeric Brun3bd697e2010-01-04 15:23:48 +010037/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020038 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
39 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010040 */
41void stksess_free(struct stktable *t, struct stksess *ts)
42{
43 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020044 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010045}
46
47/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020048 * Kill an stksess (only if its ref_cnt is zero).
49 */
50void stksess_kill(struct stktable *t, struct stksess *ts)
51{
52 if (ts->ref_cnt)
53 return;
54
55 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020056 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020057 ebmb_delete(&ts->key);
58 stksess_free(t, ts);
59}
60
61/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020062 * Initialize or update the key in the sticky session <ts> present in table <t>
63 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010064 */
Willy Tarreau393379c2010-06-06 12:11:37 +020065void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010066{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020067 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020068 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010069 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020070 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
71 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010072 }
73}
74
75
76/*
Willy Tarreau393379c2010-06-06 12:11:37 +020077 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
78 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010079 */
Willy Tarreau393379c2010-06-06 12:11:37 +020080static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010081{
Willy Tarreau393379c2010-06-06 12:11:37 +020082 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020083 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020084 ts->key.node.leaf_p = NULL;
85 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020086 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010087 return ts;
88}
89
90/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020091 * Trash oldest <to_batch> sticky sessions from table <t>
92 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010093 */
Willy Tarreau3a925c12013-09-04 17:54:01 +020094int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +010095{
96 struct stksess *ts;
97 struct eb32_node *eb;
98 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020099 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100100
101 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
102
103 while (batched < to_batch) {
104
105 if (unlikely(!eb)) {
106 /* we might have reached the end of the tree, typically because
107 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200108 * half. Let's loop back to the beginning of the tree now if we
109 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100110 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200111 if (looped)
112 break;
113 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100114 eb = eb32_first(&t->exps);
115 if (likely(!eb))
116 break;
117 }
118
119 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200120 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100121 eb = eb32_next(eb);
122
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200123 /* don't delete an entry which is currently referenced */
124 if (ts->ref_cnt)
125 continue;
126
Willy Tarreau86257dc2010-06-06 12:57:10 +0200127 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100128
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130 if (!tick_isset(ts->expire))
131 continue;
132
Willy Tarreau86257dc2010-06-06 12:57:10 +0200133 ts->exp.key = ts->expire;
134 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100135
Willy Tarreau86257dc2010-06-06 12:57:10 +0200136 if (!eb || eb->key > ts->exp.key)
137 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100138
139 continue;
140 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100141
Willy Tarreauaea940e2010-06-06 11:56:36 +0200142 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200143 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200144 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100145 stksess_free(t, ts);
146 batched++;
147 }
148
149 return batched;
150}
151
152/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200153 * Allocate and initialise a new sticky session.
154 * The new sticky session is returned or NULL in case of lack of memory.
155 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200156 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
157 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100158 */
159struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
160{
161 struct stksess *ts;
162
163 if (unlikely(t->current == t->size)) {
164 if ( t->nopurge )
165 return NULL;
166
Emeric Brunfbce6d02010-09-23 18:10:00 +0200167 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100168 return NULL;
169 }
170
Willy Tarreau393379c2010-06-06 12:11:37 +0200171 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100172 if (ts) {
173 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200174 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200175 if (key)
176 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100177 }
178
179 return ts;
180}
181
182/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200183 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200184 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100185 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200186struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187{
188 struct ebmb_node *eb;
189
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200190 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200191 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 +0100192 else
193 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
194
195 if (unlikely(!eb)) {
196 /* no session found */
197 return NULL;
198 }
199
Willy Tarreau86257dc2010-06-06 12:57:10 +0200200 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100201}
202
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200203/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
204 * This is mainly used for situations where we want to refresh a key's usage so
205 * that it does not expire, and we want to have it created if it was not there.
206 * The stksess is returned, or NULL if it could not be created.
207 */
208struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
209{
210 struct stksess *ts;
211
212 ts = stktable_lookup_key(table, key);
213 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200214 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200215
216 /* entry does not exist, initialize a new one */
217 ts = stksess_new(table, key);
218 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200219 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200220 return ts;
221}
222
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200223/*
224 * Looks in table <t> for a sticky session with same key as <ts>.
225 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100226 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200227struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100229 struct ebmb_node *eb;
230
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200231 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200232 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100233 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200234 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200236 if (unlikely(!eb))
237 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200239 return ebmb_entry(eb, struct stksess, key);
240}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100241
Willy Tarreaucb183642010-06-06 17:58:34 +0200242/* Update the expiration timer for <ts> but do not touch its expiration node.
243 * The table's expiration timer is updated if set.
244 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200245struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200246{
Emeric Brun85e77c72010-09-23 18:16:52 +0200247 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200248 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
249 if (t->expire) {
250 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
251 task_queue(t->exp_task);
252 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200253
Emeric Brunaaf58602015-06-15 17:23:30 +0200254 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200255 if (t->sync_task && local) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200256 /* If this entry was already pushed to a peer
257 We want to push it again */
258 if ((int)(ts->upd.key - t->commitupdate) <= 0) {
259 ts->upd.key = ++t->update;
260 t->localupdate = t->update;
261 eb32_delete(&ts->upd);
262 eb = eb32_insert(&t->updates, &ts->upd);
263 if (eb != &ts->upd) {
264 eb32_delete(eb);
265 eb32_insert(&t->updates, &ts->upd);
266 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200267 }
268 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
269 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200270 return ts;
271}
272
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200273/* Insert new sticky session <ts> in the table. It is assumed that it does not
274 * yet exist (the caller must check this). The table's timeout is updated if it
275 * is set. <ts> is returned.
276 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200277struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200278{
279 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200280 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200281 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200282 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200283 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100284}
285
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200286/* Returns a valid or initialized stksess for the specified stktable_key in the
287 * specified table, or NULL if the key was NULL, or if no entry was found nor
288 * could be created. The entry's expiration is updated.
289 */
290struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
291{
292 struct stksess *ts;
293
294 if (!key)
295 return NULL;
296
297 ts = stktable_lookup_key(table, key);
298 if (ts == NULL) {
299 /* entry does not exist, initialize a new one */
300 ts = stksess_new(table, key);
301 if (!ts)
302 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200303 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200304 }
305 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200306 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200307 return ts;
308}
309
Emeric Brun3bd697e2010-01-04 15:23:48 +0100310/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200311 * Trash expired sticky sessions from table <t>. The next expiration date is
312 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100313 */
314static int stktable_trash_expired(struct stktable *t)
315{
316 struct stksess *ts;
317 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200318 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100319
320 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
321
322 while (1) {
323 if (unlikely(!eb)) {
324 /* we might have reached the end of the tree, typically because
325 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200326 * half. Let's loop back to the beginning of the tree now if we
327 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100328 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200329 if (looped)
330 break;
331 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100332 eb = eb32_first(&t->exps);
333 if (likely(!eb))
334 break;
335 }
336
337 if (likely(tick_is_lt(now_ms, eb->key))) {
338 /* timer not expired yet, revisit it later */
339 t->exp_next = eb->key;
340 return t->exp_next;
341 }
342
343 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200344 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100345 eb = eb32_next(eb);
346
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200347 /* don't delete an entry which is currently referenced */
348 if (ts->ref_cnt)
349 continue;
350
Willy Tarreau86257dc2010-06-06 12:57:10 +0200351 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100352
353 if (!tick_is_expired(ts->expire, now_ms)) {
354 if (!tick_isset(ts->expire))
355 continue;
356
Willy Tarreau86257dc2010-06-06 12:57:10 +0200357 ts->exp.key = ts->expire;
358 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100359
Willy Tarreau86257dc2010-06-06 12:57:10 +0200360 if (!eb || eb->key > ts->exp.key)
361 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100362 continue;
363 }
364
365 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200366 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200367 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100368 stksess_free(t, ts);
369 }
370
371 /* We have found no task to expire in any tree */
372 t->exp_next = TICK_ETERNITY;
373 return t->exp_next;
374}
375
376/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200377 * Task processing function to trash expired sticky sessions. A pointer to the
378 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100379 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200380static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100381{
382 struct stktable *t = (struct stktable *)task->context;
383
384 task->expire = stktable_trash_expired(t);
385 return task;
386}
387
Willy Tarreauaea940e2010-06-06 11:56:36 +0200388/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100389int stktable_init(struct stktable *t)
390{
391 if (t->size) {
392 memset(&t->keys, 0, sizeof(t->keys));
393 memset(&t->exps, 0, sizeof(t->exps));
394
Willy Tarreau393379c2010-06-06 12:11:37 +0200395 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100396
397 t->exp_next = TICK_ETERNITY;
398 if ( t->expire ) {
399 t->exp_task = task_new();
400 t->exp_task->process = process_table_expire;
401 t->exp_task->expire = TICK_ETERNITY;
402 t->exp_task->context = (void *)t;
403 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200404 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200405 peers_register_table(t->peers.p, t);
406 }
407
Emeric Brun3bd697e2010-01-04 15:23:48 +0100408 return t->pool != NULL;
409 }
410 return 1;
411}
412
413/*
414 * Configuration keywords of known table types
415 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200416struct stktable_type stktable_types[SMP_TYPES] = {
417 [SMP_T_SINT] = { "integer", 0, 4 },
418 [SMP_T_IPV4] = { "ip", 0, 4 },
419 [SMP_T_IPV6] = { "ipv6", 0, 16 },
420 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
421 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
422};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100423
424/*
425 * Parse table type configuration.
426 * Returns 0 on successful parsing, else 1.
427 * <myidx> is set at next configuration <args> index.
428 */
429int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
430{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200431 for (*type = 0; *type < SMP_TYPES; (*type)++) {
432 if (!stktable_types[*type].kw)
433 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100434 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
435 continue;
436
437 *key_size = stktable_types[*type].default_size;
438 (*myidx)++;
439
Willy Tarreauaea940e2010-06-06 11:56:36 +0200440 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100441 if (strcmp("len", args[*myidx]) == 0) {
442 (*myidx)++;
443 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200444 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100445 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200446 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200447 /* null terminated string needs +1 for '\0'. */
448 (*key_size)++;
449 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100450 (*myidx)++;
451 }
452 }
453 return 0;
454 }
455 return 1;
456}
457
Willy Tarreau8fed9032014-07-03 17:02:46 +0200458/* Prepares a stktable_key from a sample <smp> to search into table <t>.
459 * Returns NULL if the sample could not be converted (eg: no matching type),
460 * otherwise a pointer to the static stktable_key filled with what is needed
461 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200462 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200463struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200464{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200465 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200466 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200467 return NULL;
468
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200469 /* Fill static_table_key. */
470 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200471
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200472 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200473 static_table_key->key = &smp->data.u.ipv4;
474 static_table_key->key_len = 4;
475 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200476
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200477 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200478 static_table_key->key = &smp->data.u.ipv6;
479 static_table_key->key_len = 16;
480 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200481
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200482 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200483 /* The stick table require a 32bit unsigned int, "sint" is a
484 * signed 64 it, so we can convert it inplace.
485 */
486 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
487 static_table_key->key = &smp->data.u.sint;
488 static_table_key->key_len = 4;
489 break;
490
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200491 case SMP_T_STR:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200492 /* Must be NULL terminated. */
493 if (smp->data.u.str.len >= smp->data.u.str.size ||
494 smp->data.u.str.str[smp->data.u.str.len] != '\0') {
495 if (!smp_dup(smp))
496 return NULL;
497 if (smp->data.u.str.len >= smp->data.u.str.size)
498 return NULL;
499 smp->data.u.str.str[smp->data.u.str.len] = '\0';
Emeric Brun485479d2010-09-23 18:02:19 +0200500 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200501 static_table_key->key = smp->data.u.str.str;
502 static_table_key->key_len = smp->data.u.str.len;
503 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200504
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200505 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200506 if (smp->data.u.str.len < t->key_size) {
507 /* This type needs padding with 0. */
508 if (smp->data.u.str.size < t->key_size)
509 if (!smp_dup(smp))
510 return NULL;
511 if (smp->data.u.str.size < t->key_size)
512 return NULL;
513 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
514 t->key_size - smp->data.u.str.len);
515 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200516 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200517 static_table_key->key = smp->data.u.str.str;
518 static_table_key->key_len = smp->data.u.str.len;
519 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200520
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200521 default: /* impossible case. */
522 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200523 }
524
Willy Tarreau07115412012-10-29 21:56:59 +0100525 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200526}
527
528/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200529 * Process a fetch + format conversion as defined by the sample expression <expr>
530 * on request or response considering the <opt> parameter. Returns either NULL if
531 * no key could be extracted, or a pointer to the converted result stored in
532 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
533 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200534 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
535 * without SMP_OPT_FINAL). The output will be usable like this :
536 *
537 * return MAY_CHANGE FINAL Meaning for the sample
538 * NULL 0 * Not present and will never be (eg: header)
539 * NULL 1 0 Not present or unstable, could change (eg: req_len)
540 * NULL 1 1 Not present, will not change anymore
541 * smp 0 * Present and will not change (eg: header)
542 * smp 1 0 not possible
543 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200544 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200545struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200546 unsigned int opt, struct sample_expr *expr, struct sample *smp)
547{
548 if (smp)
549 memset(smp, 0, sizeof(*smp));
550
Willy Tarreau192252e2015-04-04 01:47:55 +0200551 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200552 if (!smp)
553 return NULL;
554
555 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
556 return NULL; /* we can only use stable samples */
557
558 return smp_to_stkey(smp, t);
559}
560
561/*
Willy Tarreau12785782012-04-27 21:37:17 +0200562 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200563 * type <table_type>, otherwise zero. Used in configuration check.
564 */
Willy Tarreau12785782012-04-27 21:37:17 +0200565int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200566{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100567 int out_type;
568
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200569 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200570 return 0;
571
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100572 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200573
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200574 /* Convert sample. */
575 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100576 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200577
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200578 return 1;
579}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100580
Willy Tarreauedee1d62014-07-15 16:44:27 +0200581/* Extra data types processing : after the last one, some room may remain
582 * before STKTABLE_DATA_TYPES that may be used to register extra data types
583 * at run time.
584 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200585struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200586 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200587 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200588 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200589 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200590 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
591 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
592 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
593 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
594 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
595 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
596 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
597 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
598 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
599 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
600 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
601 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
602 [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 +0200603};
604
Willy Tarreauedee1d62014-07-15 16:44:27 +0200605/* Registers stick-table extra data type with index <idx>, name <name>, type
606 * <std_type> and arg type <arg_type>. If the index is negative, the next free
607 * index is automatically allocated. The allocated index is returned, or -1 if
608 * no free index was found or <name> was already registered. The <name> is used
609 * directly as a pointer, so if it's not stable, the caller must allocate it.
610 */
611int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
612{
613 if (idx < 0) {
614 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
615 if (!stktable_data_types[idx].name)
616 break;
617
618 if (strcmp(stktable_data_types[idx].name, name) == 0)
619 return -1;
620 }
621 }
622
623 if (idx >= STKTABLE_DATA_TYPES)
624 return -1;
625
626 if (stktable_data_types[idx].name != NULL)
627 return -1;
628
629 stktable_data_types[idx].name = name;
630 stktable_data_types[idx].std_type = std_type;
631 stktable_data_types[idx].arg_type = arg_type;
632 return idx;
633}
634
Willy Tarreau08d5f982010-06-06 13:34:54 +0200635/*
636 * Returns the data type number for the stktable_data_type whose name is <name>,
637 * or <0 if not found.
638 */
639int stktable_get_data_type(char *name)
640{
641 int type;
642
643 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200644 if (!stktable_data_types[type].name)
645 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200646 if (strcmp(name, stktable_data_types[type].name) == 0)
647 return type;
648 }
649 return -1;
650}
651
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200652/* Casts sample <smp> to the type of the table specified in arg(0), and looks
653 * it up into this table. Returns true if found, false otherwise. The input
654 * type is STR so that input samples are converted to string (since all types
655 * can be converted to strings), then the function casts the string again into
656 * the table's type. This is a double conversion, but in the future we might
657 * support automatic input types to perform the cast on the fly.
658 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200659static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200660{
661 struct stktable *t;
662 struct stktable_key *key;
663 struct stksess *ts;
664
665 t = &arg_p[0].data.prx->table;
666
667 key = smp_to_stkey(smp, t);
668 if (!key)
669 return 0;
670
671 ts = stktable_lookup_key(t, key);
672
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200673 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200674 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200675 smp->flags = SMP_F_VOL_TEST;
676 return 1;
677}
678
679/* Casts sample <smp> to the type of the table specified in arg(0), and looks
680 * it up into this table. Returns the data rate received from clients in bytes/s
681 * if the key is present in the table, otherwise zero, so that comparisons can
682 * be easily performed. If the inspected parameter is not stored in the table,
683 * <not found> is returned.
684 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200685static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200686{
687 struct stktable *t;
688 struct stktable_key *key;
689 struct stksess *ts;
690 void *ptr;
691
692 t = &arg_p[0].data.prx->table;
693
694 key = smp_to_stkey(smp, t);
695 if (!key)
696 return 0;
697
698 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200699 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200700 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200701
702 ts = stktable_lookup_key(t, key);
703 if (!ts) /* key not present */
704 return 1;
705
706 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
707 if (!ptr)
708 return 0; /* parameter not stored */
709
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200710 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200711 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
712 return 1;
713}
714
715/* Casts sample <smp> to the type of the table specified in arg(0), and looks
716 * it up into this table. Returns the cumulated number of connections for the key
717 * if the key is present in the table, otherwise zero, so that comparisons can
718 * be easily performed. If the inspected parameter is not stored in the table,
719 * <not found> is returned.
720 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200721static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200722{
723 struct stktable *t;
724 struct stktable_key *key;
725 struct stksess *ts;
726 void *ptr;
727
728 t = &arg_p[0].data.prx->table;
729
730 key = smp_to_stkey(smp, t);
731 if (!key)
732 return 0;
733
734 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200735 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200736 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200737
738 ts = stktable_lookup_key(t, key);
739 if (!ts) /* key not present */
740 return 1;
741
742 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
743 if (!ptr)
744 return 0; /* parameter not stored */
745
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200746 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200747 return 1;
748}
749
750/* Casts sample <smp> to the type of the table specified in arg(0), and looks
751 * it up into this table. Returns the number of concurrent connections for the
752 * key if the key is present in the table, otherwise zero, so that comparisons
753 * can be easily performed. If the inspected parameter is not stored in the
754 * table, <not found> is returned.
755 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200756static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200757{
758 struct stktable *t;
759 struct stktable_key *key;
760 struct stksess *ts;
761 void *ptr;
762
763 t = &arg_p[0].data.prx->table;
764
765 key = smp_to_stkey(smp, t);
766 if (!key)
767 return 0;
768
769 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200770 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200771 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200772
773 ts = stktable_lookup_key(t, key);
774 if (!ts) /* key not present */
775 return 1;
776
777 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
778 if (!ptr)
779 return 0; /* parameter not stored */
780
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200781 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200782 return 1;
783}
784
785/* Casts sample <smp> to the type of the table specified in arg(0), and looks
786 * it up into this table. Returns the rate of incoming connections from the key
787 * if the key is present in the table, otherwise zero, so that comparisons can
788 * be easily performed. If the inspected parameter is not stored in the table,
789 * <not found> is returned.
790 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200791static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200792{
793 struct stktable *t;
794 struct stktable_key *key;
795 struct stksess *ts;
796 void *ptr;
797
798 t = &arg_p[0].data.prx->table;
799
800 key = smp_to_stkey(smp, t);
801 if (!key)
802 return 0;
803
804 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200805 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200806 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200807
808 ts = stktable_lookup_key(t, key);
809 if (!ts) /* key not present */
810 return 1;
811
812 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
813 if (!ptr)
814 return 0; /* parameter not stored */
815
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200816 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200817 t->data_arg[STKTABLE_DT_CONN_RATE].u);
818 return 1;
819}
820
821/* Casts sample <smp> to the type of the table specified in arg(0), and looks
822 * it up into this table. Returns the data rate sent to clients in bytes/s
823 * if the key is present in the table, otherwise zero, so that comparisons can
824 * be easily performed. If the inspected parameter is not stored in the table,
825 * <not found> is returned.
826 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200827static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200828{
829 struct stktable *t;
830 struct stktable_key *key;
831 struct stksess *ts;
832 void *ptr;
833
834 t = &arg_p[0].data.prx->table;
835
836 key = smp_to_stkey(smp, t);
837 if (!key)
838 return 0;
839
840 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200841 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200842 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200843
844 ts = stktable_lookup_key(t, key);
845 if (!ts) /* key not present */
846 return 1;
847
848 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
849 if (!ptr)
850 return 0; /* parameter not stored */
851
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200852 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200853 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
854 return 1;
855}
856
857/* Casts sample <smp> to the type of the table specified in arg(0), and looks
858 * it up into this table. Returns the value of the GPC0 counter for the key
859 * if the key is present in the table, otherwise zero, so that comparisons can
860 * be easily performed. If the inspected parameter is not stored in the table,
861 * <not found> is returned.
862 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200863static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200864{
865 struct stktable *t;
866 struct stktable_key *key;
867 struct stksess *ts;
868 void *ptr;
869
870 t = &arg_p[0].data.prx->table;
871
872 key = smp_to_stkey(smp, t);
873 if (!key)
874 return 0;
875
876 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
880 ts = stktable_lookup_key(t, key);
881 if (!ts) /* key not present */
882 return 1;
883
884 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
885 if (!ptr)
886 return 0; /* parameter not stored */
887
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200888 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200889 return 1;
890}
891
892/* Casts sample <smp> to the type of the table specified in arg(0), and looks
893 * it up into this table. Returns the event rate of the GPC0 counter for the key
894 * if the key is present in the table, otherwise zero, so that comparisons can
895 * be easily performed. If the inspected parameter is not stored in the table,
896 * <not found> is returned.
897 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200898static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200899{
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
911 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200912 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200913 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200914
915 ts = stktable_lookup_key(t, key);
916 if (!ts) /* key not present */
917 return 1;
918
919 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
920 if (!ptr)
921 return 0; /* parameter not stored */
922
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200923 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200924 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
925 return 1;
926}
927
928/* Casts sample <smp> to the type of the table specified in arg(0), and looks
929 * it up into this table. Returns the cumulated number of HTTP request errors
930 * for the key if the key is present in the table, otherwise zero, so that
931 * comparisons can be easily performed. If the inspected parameter is not stored
932 * in the table, <not found> is returned.
933 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200934static int sample_conv_table_http_err_cnt(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
947 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200948 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200949 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200950
951 ts = stktable_lookup_key(t, key);
952 if (!ts) /* key not present */
953 return 1;
954
955 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
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, http_err_cnt);
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 HTTP request error rate 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_http_err_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
982 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200983 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200984 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200985
986 ts = stktable_lookup_key(t, key);
987 if (!ts) /* key not present */
988 return 1;
989
990 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
991 if (!ptr)
992 return 0; /* parameter not stored */
993
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200994 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200995 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
996 return 1;
997}
998
999/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1000 * it up into this table. Returns the cumulated number of HTTP request for the
1001 * key if the key is present in the table, otherwise zero, so that comparisons
1002 * can be easily performed. If the inspected parameter is not stored in the
1003 * table, <not found> is returned.
1004 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001005static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001006{
1007 struct stktable *t;
1008 struct stktable_key *key;
1009 struct stksess *ts;
1010 void *ptr;
1011
1012 t = &arg_p[0].data.prx->table;
1013
1014 key = smp_to_stkey(smp, t);
1015 if (!key)
1016 return 0;
1017
1018 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001019 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001020 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001021
1022 ts = stktable_lookup_key(t, key);
1023 if (!ts) /* key not present */
1024 return 1;
1025
1026 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1027 if (!ptr)
1028 return 0; /* parameter not stored */
1029
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001030 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001031 return 1;
1032}
1033
1034/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1035 * it up into this table. Returns the HTTP request rate the key if the key is
1036 * present in the table, otherwise zero, so that comparisons can be easily
1037 * performed. If the inspected parameter is not stored in the table, <not found>
1038 * is returned.
1039 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001040static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001041{
1042 struct stktable *t;
1043 struct stktable_key *key;
1044 struct stksess *ts;
1045 void *ptr;
1046
1047 t = &arg_p[0].data.prx->table;
1048
1049 key = smp_to_stkey(smp, t);
1050 if (!key)
1051 return 0;
1052
1053 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001054 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001055 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001056
1057 ts = stktable_lookup_key(t, key);
1058 if (!ts) /* key not present */
1059 return 1;
1060
1061 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1062 if (!ptr)
1063 return 0; /* parameter not stored */
1064
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001065 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001066 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1067 return 1;
1068}
1069
1070/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1071 * it up into this table. Returns the volume of datareceived from clients in kbytes
1072 * if the key is present in the table, otherwise zero, so that comparisons can
1073 * be easily performed. If the inspected parameter is not stored in the table,
1074 * <not found> is returned.
1075 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001076static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001077{
1078 struct stktable *t;
1079 struct stktable_key *key;
1080 struct stksess *ts;
1081 void *ptr;
1082
1083 t = &arg_p[0].data.prx->table;
1084
1085 key = smp_to_stkey(smp, t);
1086 if (!key)
1087 return 0;
1088
1089 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001090 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001091 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001092
1093 ts = stktable_lookup_key(t, key);
1094 if (!ts) /* key not present */
1095 return 1;
1096
1097 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1098 if (!ptr)
1099 return 0; /* parameter not stored */
1100
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001101 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001102 return 1;
1103}
1104
1105/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1106 * it up into this table. Returns the volume of data sent to clients in kbytes
1107 * if the key is present in the table, otherwise zero, so that comparisons can
1108 * be easily performed. If the inspected parameter is not stored in the table,
1109 * <not found> is returned.
1110 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001111static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001112{
1113 struct stktable *t;
1114 struct stktable_key *key;
1115 struct stksess *ts;
1116 void *ptr;
1117
1118 t = &arg_p[0].data.prx->table;
1119
1120 key = smp_to_stkey(smp, t);
1121 if (!key)
1122 return 0;
1123
1124 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001125 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001126 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001127
1128 ts = stktable_lookup_key(t, key);
1129 if (!ts) /* key not present */
1130 return 1;
1131
1132 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1133 if (!ptr)
1134 return 0; /* parameter not stored */
1135
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001136 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001137 return 1;
1138}
1139
1140/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1141 * it up into this table. Returns the server ID associated with the key if the
1142 * key is present in the table, otherwise zero, so that comparisons can be
1143 * easily performed. If the inspected parameter is not stored in the table,
1144 * <not found> is returned.
1145 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001146static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001147{
1148 struct stktable *t;
1149 struct stktable_key *key;
1150 struct stksess *ts;
1151 void *ptr;
1152
1153 t = &arg_p[0].data.prx->table;
1154
1155 key = smp_to_stkey(smp, t);
1156 if (!key)
1157 return 0;
1158
1159 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001160 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001161 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001162
1163 ts = stktable_lookup_key(t, key);
1164 if (!ts) /* key not present */
1165 return 1;
1166
1167 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1168 if (!ptr)
1169 return 0; /* parameter not stored */
1170
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001171 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001172 return 1;
1173}
1174
1175/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1176 * it up into this table. Returns the cumulated number of sessions for the
1177 * key if the key is present in the table, otherwise zero, so that comparisons
1178 * can be easily performed. If the inspected parameter is not stored in the
1179 * table, <not found> is returned.
1180 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001181static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001182{
1183 struct stktable *t;
1184 struct stktable_key *key;
1185 struct stksess *ts;
1186 void *ptr;
1187
1188 t = &arg_p[0].data.prx->table;
1189
1190 key = smp_to_stkey(smp, t);
1191 if (!key)
1192 return 0;
1193
1194 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001195 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001196 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001197
1198 ts = stktable_lookup_key(t, key);
1199 if (!ts) /* key not present */
1200 return 1;
1201
1202 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1203 if (!ptr)
1204 return 0; /* parameter not stored */
1205
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001206 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001207 return 1;
1208}
1209
1210/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1211 * it up into this table. Returns the session rate the key if the key is
1212 * present in the table, otherwise zero, so that comparisons can be easily
1213 * performed. If the inspected parameter is not stored in the table, <not found>
1214 * is returned.
1215 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001216static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001217{
1218 struct stktable *t;
1219 struct stktable_key *key;
1220 struct stksess *ts;
1221 void *ptr;
1222
1223 t = &arg_p[0].data.prx->table;
1224
1225 key = smp_to_stkey(smp, t);
1226 if (!key)
1227 return 0;
1228
1229 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001230 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001231 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001232
1233 ts = stktable_lookup_key(t, key);
1234 if (!ts) /* key not present */
1235 return 1;
1236
1237 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1238 if (!ptr)
1239 return 0; /* parameter not stored */
1240
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001241 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001242 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1243 return 1;
1244}
1245
1246/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1247 * it up into this table. Returns the amount of concurrent connections tracking
1248 * the same key if the key is present in the table, otherwise zero, so that
1249 * comparisons can be easily performed. If the inspected parameter is not
1250 * stored in the table, <not found> is returned.
1251 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001252static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001253{
1254 struct stktable *t;
1255 struct stktable_key *key;
1256 struct stksess *ts;
1257
1258 t = &arg_p[0].data.prx->table;
1259
1260 key = smp_to_stkey(smp, t);
1261 if (!key)
1262 return 0;
1263
1264 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001265 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001266 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001267
1268 ts = stktable_lookup_key(t, key);
1269 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001270 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001271
1272 return 1;
1273}
1274
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001275/* Note: must not be declared <const> as its list will be overwritten */
1276static struct sample_conv_kw_list sample_conv_kws = {ILH, {
1277 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_BOOL },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001278 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1279 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1280 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1281 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1282 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1283 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1284 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1285 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1286 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1287 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1288 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1289 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1290 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1291 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1292 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1293 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1294 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001295 { /* END */ },
1296}};
1297
1298__attribute__((constructor))
1299static void __stick_table_init(void)
1300{
1301 /* register sample fetch and format conversion keywords */
1302 sample_register_convs(&sample_conv_kws);
1303}