blob: 12265916c53dbeb54d4ea285740f4cc5f97b4b41 [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>
Emeric Brun3bd697e2010-01-04 15:23:48 +010028#include <proto/session.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{
67 if (t->type != STKTABLE_TYPE_STRING)
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
Emeric Brun3bd697e2010-01-04 15:23:48 +0100190 if (t->type == STKTABLE_TYPE_STRING)
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
231 if (t->type == STKTABLE_TYPE_STRING)
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
254 if (t->sync_task && local) {
255 ts->upd.key = ++t->update;
256 t->localupdate = t->update;
257 eb32_delete(&ts->upd);
258 eb = eb32_insert(&t->updates, &ts->upd);
259 if (eb != &ts->upd) {
260 eb32_delete(eb);
261 eb32_insert(&t->updates, &ts->upd);
262 }
263 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
264 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200265 return ts;
266}
267
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200268/* Insert new sticky session <ts> in the table. It is assumed that it does not
269 * yet exist (the caller must check this). The table's timeout is updated if it
270 * is set. <ts> is returned.
271 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200272struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200273{
274 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200275 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200276 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200277 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200278 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100279}
280
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200281/* Returns a valid or initialized stksess for the specified stktable_key in the
282 * specified table, or NULL if the key was NULL, or if no entry was found nor
283 * could be created. The entry's expiration is updated.
284 */
285struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
286{
287 struct stksess *ts;
288
289 if (!key)
290 return NULL;
291
292 ts = stktable_lookup_key(table, key);
293 if (ts == NULL) {
294 /* entry does not exist, initialize a new one */
295 ts = stksess_new(table, key);
296 if (!ts)
297 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200298 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200299 }
300 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200301 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200302 return ts;
303}
304
Emeric Brun3bd697e2010-01-04 15:23:48 +0100305/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200306 * Trash expired sticky sessions from table <t>. The next expiration date is
307 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100308 */
309static int stktable_trash_expired(struct stktable *t)
310{
311 struct stksess *ts;
312 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200313 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314
315 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
316
317 while (1) {
318 if (unlikely(!eb)) {
319 /* we might have reached the end of the tree, typically because
320 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200321 * half. Let's loop back to the beginning of the tree now if we
322 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200324 if (looped)
325 break;
326 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100327 eb = eb32_first(&t->exps);
328 if (likely(!eb))
329 break;
330 }
331
332 if (likely(tick_is_lt(now_ms, eb->key))) {
333 /* timer not expired yet, revisit it later */
334 t->exp_next = eb->key;
335 return t->exp_next;
336 }
337
338 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200339 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100340 eb = eb32_next(eb);
341
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200342 /* don't delete an entry which is currently referenced */
343 if (ts->ref_cnt)
344 continue;
345
Willy Tarreau86257dc2010-06-06 12:57:10 +0200346 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100347
348 if (!tick_is_expired(ts->expire, now_ms)) {
349 if (!tick_isset(ts->expire))
350 continue;
351
Willy Tarreau86257dc2010-06-06 12:57:10 +0200352 ts->exp.key = ts->expire;
353 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100354
Willy Tarreau86257dc2010-06-06 12:57:10 +0200355 if (!eb || eb->key > ts->exp.key)
356 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357 continue;
358 }
359
360 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200361 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200362 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363 stksess_free(t, ts);
364 }
365
366 /* We have found no task to expire in any tree */
367 t->exp_next = TICK_ETERNITY;
368 return t->exp_next;
369}
370
371/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200372 * Task processing function to trash expired sticky sessions. A pointer to the
373 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100374 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200375static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100376{
377 struct stktable *t = (struct stktable *)task->context;
378
379 task->expire = stktable_trash_expired(t);
380 return task;
381}
382
Willy Tarreauaea940e2010-06-06 11:56:36 +0200383/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100384int stktable_init(struct stktable *t)
385{
386 if (t->size) {
387 memset(&t->keys, 0, sizeof(t->keys));
388 memset(&t->exps, 0, sizeof(t->exps));
389
Willy Tarreau393379c2010-06-06 12:11:37 +0200390 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100391
392 t->exp_next = TICK_ETERNITY;
393 if ( t->expire ) {
394 t->exp_task = task_new();
395 t->exp_task->process = process_table_expire;
396 t->exp_task->expire = TICK_ETERNITY;
397 t->exp_task->context = (void *)t;
398 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200399 if (t->peers.p && t->peers.p->peers_fe) {
400 peers_register_table(t->peers.p, t);
401 }
402
Emeric Brun3bd697e2010-01-04 15:23:48 +0100403 return t->pool != NULL;
404 }
405 return 1;
406}
407
408/*
409 * Configuration keywords of known table types
410 */
Emeric Brun485479d2010-09-23 18:02:19 +0200411struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
David du Colombier4f92d322011-03-24 11:09:31 +0100412 { "ipv6", 0, 16 },
Emeric Brun3bd697e2010-01-04 15:23:48 +0100413 { "integer", 0, 4 },
Emeric Brun485479d2010-09-23 18:02:19 +0200414 { "string", STK_F_CUSTOM_KEYSIZE, 32 },
David du Colombier4f92d322011-03-24 11:09:31 +0100415 { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100416
417
418/*
419 * Parse table type configuration.
420 * Returns 0 on successful parsing, else 1.
421 * <myidx> is set at next configuration <args> index.
422 */
423int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
424{
425 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
426 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
427 continue;
428
429 *key_size = stktable_types[*type].default_size;
430 (*myidx)++;
431
Willy Tarreauaea940e2010-06-06 11:56:36 +0200432 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100433 if (strcmp("len", args[*myidx]) == 0) {
434 (*myidx)++;
435 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200436 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100437 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200438 if (*type == STKTABLE_TYPE_STRING) {
439 /* null terminated string needs +1 for '\0'. */
440 (*key_size)++;
441 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100442 (*myidx)++;
443 }
444 }
445 return 0;
446 }
447 return 1;
448}
449
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200450/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200451/* typed sample to typed table key functions */
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200452/*****************************************************************/
453
Willy Tarreau342acb42012-04-23 22:03:39 +0200454static void *k_int2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200455{
Willy Tarreau342acb42012-04-23 22:03:39 +0200456 return (void *)&smp->data.uint;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200457}
458
Willy Tarreau342acb42012-04-23 22:03:39 +0200459static void *k_ip2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200460{
Willy Tarreau803685f2013-12-02 23:17:27 +0100461 if (smp->type == SMP_T_IPV6) {
462 v6tov4(&kdata->ip, &smp->data.ipv6);
463 return (void *)&kdata->ip.s_addr;
464 }
465 else {
466 return (void *)&smp->data.ipv4.s_addr;
467 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200468}
469
Willy Tarreau342acb42012-04-23 22:03:39 +0200470static void *k_ip2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len)
David du Colombier4f92d322011-03-24 11:09:31 +0100471{
Willy Tarreau803685f2013-12-02 23:17:27 +0100472 if (smp->type == SMP_T_IPV6) {
473 return (void *)&smp->data.ipv6.s6_addr;
474 }
475 else {
476 v4tov6(&kdata->ipv6, &smp->data.ipv4);
477 return (void *)&kdata->ipv6.s6_addr;
478 }
David du Colombier4f92d322011-03-24 11:09:31 +0100479}
David du Colombier4f92d322011-03-24 11:09:31 +0100480
Willy Tarreau342acb42012-04-23 22:03:39 +0200481static void *k_ip2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200482{
Willy Tarreau803685f2013-12-02 23:17:27 +0100483 if (smp->type == SMP_T_IPV6) {
484 if (!v6tov4(&kdata->ip, &smp->data.ipv6))
485 return NULL;
486 kdata->integer = ntohl(kdata->ip.s_addr);
487 }
488 else {
489 kdata->integer = ntohl(smp->data.ipv4.s_addr);
490 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200491 return (void *)&kdata->integer;
492}
493
Willy Tarreau342acb42012-04-23 22:03:39 +0200494static void *k_int2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200495{
Willy Tarreau342acb42012-04-23 22:03:39 +0200496 kdata->ip.s_addr = htonl(smp->data.uint);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200497 return (void *)&kdata->ip.s_addr;
498}
499
Willy Tarreau342acb42012-04-23 22:03:39 +0200500static void *k_str2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200501{
Willy Tarreau342acb42012-04-23 22:03:39 +0200502 *len = smp->data.str.len;
503 return (void *)smp->data.str.str;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200504}
505
Willy Tarreau342acb42012-04-23 22:03:39 +0200506static void *k_ip2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200507{
Willy Tarreau803685f2013-12-02 23:17:27 +0100508 if (smp->type == SMP_T_IPV6) {
509 if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, *len))
510 return NULL;
511 }
512 else {
513 if (!inet_ntop(AF_INET, &smp->data.ipv4, kdata->buf, *len))
514 return NULL;
515 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200516
517 *len = strlen((const char *)kdata->buf);
518 return (void *)kdata->buf;
519}
520
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200521static void *k_ip2bin(struct sample *smp, union stktable_key_data *kdata, size_t *len)
522{
523 if (smp->type == SMP_T_IPV4) {
524 if (*len > 4)
525 *len = 4;
526 memcpy(kdata->buf, &smp->data.ipv4, *len);
527 }
528 else if (smp->type == SMP_T_IPV6) {
529 if (*len > 16)
530 *len = 16;
531 memcpy(kdata->buf, &smp->data.ipv6, *len);
532 }
533 else
534 *len = 0;
535 return (void *)kdata->buf;
536}
537
Emeric Brun8ac33d92012-10-17 13:36:06 +0200538static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
539{
540 unsigned char c;
541 int ptr = 0;
Willy Tarreauf22180f2012-12-09 11:08:14 +0100542 int max = *len;
543 int size = 0;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200544
Willy Tarreauf22180f2012-12-09 11:08:14 +0100545 while (ptr < smp->data.str.len && size <= max - 2) {
Emeric Brun8ac33d92012-10-17 13:36:06 +0200546 c = smp->data.str.str[ptr++];
Willy Tarreauf22180f2012-12-09 11:08:14 +0100547 kdata->buf[size++] = hextab[(c >> 4) & 0xF];
548 kdata->buf[size++] = hextab[c & 0xF];
Emeric Brun8ac33d92012-10-17 13:36:06 +0200549 }
Willy Tarreauf22180f2012-12-09 11:08:14 +0100550 *len = size;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200551 return (void *)kdata->buf;
552}
553
Willy Tarreau342acb42012-04-23 22:03:39 +0200554static void *k_int2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200555{
556 void *key;
557
Willy Tarreauf22180f2012-12-09 11:08:14 +0100558 key = (void *)ultoa_r(smp->data.uint, kdata->buf, *len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200559 if (!key)
560 return NULL;
561
562 *len = strlen((const char *)key);
563 return key;
564}
565
Willy Tarreau342acb42012-04-23 22:03:39 +0200566static void *k_str2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200567{
Willy Tarreau342acb42012-04-23 22:03:39 +0200568 if (!buf2ip(smp->data.str.str, smp->data.str.len, &kdata->ip))
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200569 return NULL;
570
571 return (void *)&kdata->ip.s_addr;
572}
573
Willy Tarreau342acb42012-04-23 22:03:39 +0200574static void *k_str2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len)
David du Colombier4f92d322011-03-24 11:09:31 +0100575{
Willy Tarreau342acb42012-04-23 22:03:39 +0200576 if (!inet_pton(AF_INET6, smp->data.str.str, &kdata->ipv6))
David du Colombier4f92d322011-03-24 11:09:31 +0100577 return NULL;
578
579 return (void *)&kdata->ipv6.s6_addr;
580}
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200581
Willy Tarreau342acb42012-04-23 22:03:39 +0200582static void *k_str2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200583{
584 int i;
585
586 kdata->integer = 0;
Willy Tarreau342acb42012-04-23 22:03:39 +0200587 for (i = 0; i < smp->data.str.len; i++) {
588 uint32_t val = smp->data.str.str[i] - '0';
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200589
590 if (val > 9)
591 break;
592
593 kdata->integer = kdata->integer * 10 + val;
594 }
595 return (void *)&kdata->integer;
596}
597
598/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200599/* typed sample to typed table key matrix: */
600/* sample_to_key[from sample type][to table key type] */
601/* NULL pointer used for impossible sample casts */
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200602/*****************************************************************/
603
Willy Tarreau12785782012-04-27 21:37:17 +0200604typedef void *(*sample_to_key_fct)(struct sample *smp, union stktable_key_data *kdata, size_t *len);
605static sample_to_key_fct sample_to_key[SMP_TYPES][STKTABLE_TYPES] = {
David du Colombier4f92d322011-03-24 11:09:31 +0100606/* table type: IP IPV6 INTEGER STRING BINARY */
Willy Tarreau422aa072012-04-20 20:49:27 +0200607/* patt. type: BOOL */ { NULL, NULL, k_int2int, k_int2str, NULL },
608/* UINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
609/* SINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100610/* ADDR */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200611/* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, k_ip2bin },
612/* IPV6 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, k_ip2bin },
Willy Tarreau422aa072012-04-20 20:49:27 +0200613/* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
Emeric Brun8ac33d92012-10-17 13:36:06 +0200614/* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200615};
616
617
Willy Tarreau8fed9032014-07-03 17:02:46 +0200618/* Prepares a stktable_key from a sample <smp> to search into table <t>.
619 * Returns NULL if the sample could not be converted (eg: no matching type),
620 * otherwise a pointer to the static stktable_key filled with what is needed
621 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200622 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200623struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200624{
Willy Tarreau12785782012-04-27 21:37:17 +0200625 if (!sample_to_key[smp->type][t->type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200626 return NULL;
627
Willy Tarreau07115412012-10-29 21:56:59 +0100628 static_table_key->key_len = t->key_size;
629 static_table_key->key = sample_to_key[smp->type][t->type](smp, &static_table_key->data, &static_table_key->key_len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200630
Willy Tarreau07115412012-10-29 21:56:59 +0100631 if (!static_table_key->key)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200632 return NULL;
633
Willy Tarreau07115412012-10-29 21:56:59 +0100634 if (static_table_key->key_len == 0)
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200635 return NULL;
636
Willy Tarreau07115412012-10-29 21:56:59 +0100637 if ((static_table_key->key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200638 /* need padding with null */
639
640 /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf)
641 cause t->key_size is necessary less than sizeof(static_table_key.data) */
642
Willy Tarreau07115412012-10-29 21:56:59 +0100643 if ((char *)static_table_key->key > (char *)&static_table_key->data &&
644 (char *)static_table_key->key < (char *)&static_table_key->data + global.tune.bufsize) {
Emeric Brun485479d2010-09-23 18:02:19 +0200645 /* key buffer is part of the static_table_key private data buffer, but is not aligned */
646
Willy Tarreau07115412012-10-29 21:56:59 +0100647 if (global.tune.bufsize - ((char *)static_table_key->key - (char *)&static_table_key->data) < t->key_size) {
648 /* if not remain enough place for padding , process a realign */
649 memmove(static_table_key->data.buf, static_table_key->key, static_table_key->key_len);
650 static_table_key->key = static_table_key->data.buf;
Emeric Brun485479d2010-09-23 18:02:19 +0200651 }
652 }
Willy Tarreau07115412012-10-29 21:56:59 +0100653 else if (static_table_key->key != static_table_key->data.buf) {
Emeric Brun485479d2010-09-23 18:02:19 +0200654 /* key definitly not part of the static_table_key private data buffer */
655
Willy Tarreau07115412012-10-29 21:56:59 +0100656 memcpy(static_table_key->data.buf, static_table_key->key, static_table_key->key_len);
657 static_table_key->key = static_table_key->data.buf;
Emeric Brun485479d2010-09-23 18:02:19 +0200658 }
659
Willy Tarreau07115412012-10-29 21:56:59 +0100660 memset(static_table_key->key + static_table_key->key_len, 0, t->key_size - static_table_key->key_len);
Emeric Brun485479d2010-09-23 18:02:19 +0200661 }
662
Willy Tarreau07115412012-10-29 21:56:59 +0100663 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200664}
665
666/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200667 * Process a fetch + format conversion as defined by the sample expression <expr>
668 * on request or response considering the <opt> parameter. Returns either NULL if
669 * no key could be extracted, or a pointer to the converted result stored in
670 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
671 * and its flags will be initialized so that the caller gets a copy of the input
672 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present).
673 */
674struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7,
675 unsigned int opt, struct sample_expr *expr, struct sample *smp)
676{
677 if (smp)
678 memset(smp, 0, sizeof(*smp));
679
680 smp = sample_process(px, l4, l7, opt, expr, smp);
681 if (!smp)
682 return NULL;
683
684 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
685 return NULL; /* we can only use stable samples */
686
687 return smp_to_stkey(smp, t);
688}
689
690/*
Willy Tarreau12785782012-04-27 21:37:17 +0200691 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200692 * type <table_type>, otherwise zero. Used in configuration check.
693 */
Willy Tarreau12785782012-04-27 21:37:17 +0200694int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200695{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100696 int out_type;
697
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200698 if (table_type >= STKTABLE_TYPES)
699 return 0;
700
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100701 out_type = smp_expr_output_type(expr);
702 if (!sample_to_key[out_type][table_type])
703 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200704
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200705 return 1;
706}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100707
Willy Tarreauedee1d62014-07-15 16:44:27 +0200708/* Extra data types processing : after the last one, some room may remain
709 * before STKTABLE_DATA_TYPES that may be used to register extra data types
710 * at run time.
711 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200712struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200713 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
714 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200715 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200716 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
717 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
718 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
719 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
720 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
721 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
722 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
723 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
724 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
725 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
726 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
727 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
728 [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 +0200729};
730
Willy Tarreauedee1d62014-07-15 16:44:27 +0200731/* Registers stick-table extra data type with index <idx>, name <name>, type
732 * <std_type> and arg type <arg_type>. If the index is negative, the next free
733 * index is automatically allocated. The allocated index is returned, or -1 if
734 * no free index was found or <name> was already registered. The <name> is used
735 * directly as a pointer, so if it's not stable, the caller must allocate it.
736 */
737int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
738{
739 if (idx < 0) {
740 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
741 if (!stktable_data_types[idx].name)
742 break;
743
744 if (strcmp(stktable_data_types[idx].name, name) == 0)
745 return -1;
746 }
747 }
748
749 if (idx >= STKTABLE_DATA_TYPES)
750 return -1;
751
752 if (stktable_data_types[idx].name != NULL)
753 return -1;
754
755 stktable_data_types[idx].name = name;
756 stktable_data_types[idx].std_type = std_type;
757 stktable_data_types[idx].arg_type = arg_type;
758 return idx;
759}
760
Willy Tarreau08d5f982010-06-06 13:34:54 +0200761/*
762 * Returns the data type number for the stktable_data_type whose name is <name>,
763 * or <0 if not found.
764 */
765int stktable_get_data_type(char *name)
766{
767 int type;
768
769 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200770 if (!stktable_data_types[type].name)
771 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200772 if (strcmp(name, stktable_data_types[type].name) == 0)
773 return type;
774 }
775 return -1;
776}
777
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200778/* Returns pointer to proxy containing table <name> or NULL if not found */
779struct proxy *find_stktable(const char *name)
780{
781 struct proxy *px;
Willy Tarreau991610d2014-03-15 08:03:57 +0100782 struct ebpt_node *node;
783
784 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
785 px = container_of(node, struct proxy, conf.by_name);
786
787 if (strcmp(px->id, name) != 0)
788 break;
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200789
Willy Tarreau991610d2014-03-15 08:03:57 +0100790 if (px->table.size)
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200791 return px;
792 }
793 return NULL;
794}
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200795
796/* Casts sample <smp> to the type of the table specified in arg(0), and looks
797 * it up into this table. Returns true if found, false otherwise. The input
798 * type is STR so that input samples are converted to string (since all types
799 * can be converted to strings), then the function casts the string again into
800 * the table's type. This is a double conversion, but in the future we might
801 * support automatic input types to perform the cast on the fly.
802 */
803static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp)
804{
805 struct stktable *t;
806 struct stktable_key *key;
807 struct stksess *ts;
808
809 t = &arg_p[0].data.prx->table;
810
811 key = smp_to_stkey(smp, t);
812 if (!key)
813 return 0;
814
815 ts = stktable_lookup_key(t, key);
816
817 smp->type = SMP_T_BOOL;
818 smp->data.uint = !!ts;
819 smp->flags = SMP_F_VOL_TEST;
820 return 1;
821}
822
823/* Casts sample <smp> to the type of the table specified in arg(0), and looks
824 * it up into this table. Returns the data rate received from clients in bytes/s
825 * if the key is present in the table, otherwise zero, so that comparisons can
826 * be easily performed. If the inspected parameter is not stored in the table,
827 * <not found> is returned.
828 */
829static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp)
830{
831 struct stktable *t;
832 struct stktable_key *key;
833 struct stksess *ts;
834 void *ptr;
835
836 t = &arg_p[0].data.prx->table;
837
838 key = smp_to_stkey(smp, t);
839 if (!key)
840 return 0;
841
842 smp->flags = SMP_F_VOL_TEST;
843 smp->type = SMP_T_UINT;
844 smp->data.uint = 0;
845
846 ts = stktable_lookup_key(t, key);
847 if (!ts) /* key not present */
848 return 1;
849
850 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
851 if (!ptr)
852 return 0; /* parameter not stored */
853
854 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
855 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
856 return 1;
857}
858
859/* Casts sample <smp> to the type of the table specified in arg(0), and looks
860 * it up into this table. Returns the cumulated number of connections for the key
861 * if the key is present in the table, otherwise zero, so that comparisons can
862 * be easily performed. If the inspected parameter is not stored in the table,
863 * <not found> is returned.
864 */
865static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp)
866{
867 struct stktable *t;
868 struct stktable_key *key;
869 struct stksess *ts;
870 void *ptr;
871
872 t = &arg_p[0].data.prx->table;
873
874 key = smp_to_stkey(smp, t);
875 if (!key)
876 return 0;
877
878 smp->flags = SMP_F_VOL_TEST;
879 smp->type = SMP_T_UINT;
880 smp->data.uint = 0;
881
882 ts = stktable_lookup_key(t, key);
883 if (!ts) /* key not present */
884 return 1;
885
886 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
887 if (!ptr)
888 return 0; /* parameter not stored */
889
890 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
891 return 1;
892}
893
894/* Casts sample <smp> to the type of the table specified in arg(0), and looks
895 * it up into this table. Returns the number of concurrent connections for the
896 * key if the key is present in the table, otherwise zero, so that comparisons
897 * can be easily performed. If the inspected parameter is not stored in the
898 * table, <not found> is returned.
899 */
900static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp)
901{
902 struct stktable *t;
903 struct stktable_key *key;
904 struct stksess *ts;
905 void *ptr;
906
907 t = &arg_p[0].data.prx->table;
908
909 key = smp_to_stkey(smp, t);
910 if (!key)
911 return 0;
912
913 smp->flags = SMP_F_VOL_TEST;
914 smp->type = SMP_T_UINT;
915 smp->data.uint = 0;
916
917 ts = stktable_lookup_key(t, key);
918 if (!ts) /* key not present */
919 return 1;
920
921 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
922 if (!ptr)
923 return 0; /* parameter not stored */
924
925 smp->data.uint = stktable_data_cast(ptr, conn_cur);
926 return 1;
927}
928
929/* Casts sample <smp> to the type of the table specified in arg(0), and looks
930 * it up into this table. Returns the rate of incoming connections from the key
931 * if the key is present in the table, otherwise zero, so that comparisons can
932 * be easily performed. If the inspected parameter is not stored in the table,
933 * <not found> is returned.
934 */
935static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp)
936{
937 struct stktable *t;
938 struct stktable_key *key;
939 struct stksess *ts;
940 void *ptr;
941
942 t = &arg_p[0].data.prx->table;
943
944 key = smp_to_stkey(smp, t);
945 if (!key)
946 return 0;
947
948 smp->flags = SMP_F_VOL_TEST;
949 smp->type = SMP_T_UINT;
950 smp->data.uint = 0;
951
952 ts = stktable_lookup_key(t, key);
953 if (!ts) /* key not present */
954 return 1;
955
956 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
957 if (!ptr)
958 return 0; /* parameter not stored */
959
960 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
961 t->data_arg[STKTABLE_DT_CONN_RATE].u);
962 return 1;
963}
964
965/* Casts sample <smp> to the type of the table specified in arg(0), and looks
966 * it up into this table. Returns the data rate sent to clients in bytes/s
967 * if the key is present in the table, otherwise zero, so that comparisons can
968 * be easily performed. If the inspected parameter is not stored in the table,
969 * <not found> is returned.
970 */
971static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp)
972{
973 struct stktable *t;
974 struct stktable_key *key;
975 struct stksess *ts;
976 void *ptr;
977
978 t = &arg_p[0].data.prx->table;
979
980 key = smp_to_stkey(smp, t);
981 if (!key)
982 return 0;
983
984 smp->flags = SMP_F_VOL_TEST;
985 smp->type = SMP_T_UINT;
986 smp->data.uint = 0;
987
988 ts = stktable_lookup_key(t, key);
989 if (!ts) /* key not present */
990 return 1;
991
992 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
993 if (!ptr)
994 return 0; /* parameter not stored */
995
996 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
997 t->data_arg[STKTABLE_DT_BYTES_OUT_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 value of the GPC0 counter for the key
1003 * if the key is present in the table, otherwise zero, so that comparisons can
1004 * be easily performed. If the inspected parameter is not stored in the table,
1005 * <not found> is returned.
1006 */
1007static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp)
1008{
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
1020 smp->flags = SMP_F_VOL_TEST;
1021 smp->type = SMP_T_UINT;
1022 smp->data.uint = 0;
1023
1024 ts = stktable_lookup_key(t, key);
1025 if (!ts) /* key not present */
1026 return 1;
1027
1028 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
1029 if (!ptr)
1030 return 0; /* parameter not stored */
1031
1032 smp->data.uint = stktable_data_cast(ptr, gpc0);
1033 return 1;
1034}
1035
1036/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1037 * it up into this table. Returns the event rate of the GPC0 counter for the key
1038 * if the key is present in the table, otherwise zero, so that comparisons can
1039 * be easily performed. If the inspected parameter is not stored in the table,
1040 * <not found> is returned.
1041 */
1042static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp)
1043{
1044 struct stktable *t;
1045 struct stktable_key *key;
1046 struct stksess *ts;
1047 void *ptr;
1048
1049 t = &arg_p[0].data.prx->table;
1050
1051 key = smp_to_stkey(smp, t);
1052 if (!key)
1053 return 0;
1054
1055 smp->flags = SMP_F_VOL_TEST;
1056 smp->type = SMP_T_UINT;
1057 smp->data.uint = 0;
1058
1059 ts = stktable_lookup_key(t, key);
1060 if (!ts) /* key not present */
1061 return 1;
1062
1063 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
1064 if (!ptr)
1065 return 0; /* parameter not stored */
1066
1067 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
1068 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
1069 return 1;
1070}
1071
1072/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1073 * it up into this table. Returns the cumulated number of HTTP request errors
1074 * for the key if the key is present in the table, otherwise zero, so that
1075 * comparisons can be easily performed. If the inspected parameter is not stored
1076 * in the table, <not found> is returned.
1077 */
1078static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp)
1079{
1080 struct stktable *t;
1081 struct stktable_key *key;
1082 struct stksess *ts;
1083 void *ptr;
1084
1085 t = &arg_p[0].data.prx->table;
1086
1087 key = smp_to_stkey(smp, t);
1088 if (!key)
1089 return 0;
1090
1091 smp->flags = SMP_F_VOL_TEST;
1092 smp->type = SMP_T_UINT;
1093 smp->data.uint = 0;
1094
1095 ts = stktable_lookup_key(t, key);
1096 if (!ts) /* key not present */
1097 return 1;
1098
1099 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1100 if (!ptr)
1101 return 0; /* parameter not stored */
1102
1103 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
1104 return 1;
1105}
1106
1107/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1108 * it up into this table. Returns the HTTP request error rate the key
1109 * if the key is present in the table, otherwise zero, so that comparisons can
1110 * be easily performed. If the inspected parameter is not stored in the table,
1111 * <not found> is returned.
1112 */
1113static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp)
1114{
1115 struct stktable *t;
1116 struct stktable_key *key;
1117 struct stksess *ts;
1118 void *ptr;
1119
1120 t = &arg_p[0].data.prx->table;
1121
1122 key = smp_to_stkey(smp, t);
1123 if (!key)
1124 return 0;
1125
1126 smp->flags = SMP_F_VOL_TEST;
1127 smp->type = SMP_T_UINT;
1128 smp->data.uint = 0;
1129
1130 ts = stktable_lookup_key(t, key);
1131 if (!ts) /* key not present */
1132 return 1;
1133
1134 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1135 if (!ptr)
1136 return 0; /* parameter not stored */
1137
1138 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
1139 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1140 return 1;
1141}
1142
1143/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1144 * it up into this table. Returns the cumulated number of HTTP request for the
1145 * key if the key is present in the table, otherwise zero, so that comparisons
1146 * can be easily performed. If the inspected parameter is not stored in the
1147 * table, <not found> is returned.
1148 */
1149static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp)
1150{
1151 struct stktable *t;
1152 struct stktable_key *key;
1153 struct stksess *ts;
1154 void *ptr;
1155
1156 t = &arg_p[0].data.prx->table;
1157
1158 key = smp_to_stkey(smp, t);
1159 if (!key)
1160 return 0;
1161
1162 smp->flags = SMP_F_VOL_TEST;
1163 smp->type = SMP_T_UINT;
1164 smp->data.uint = 0;
1165
1166 ts = stktable_lookup_key(t, key);
1167 if (!ts) /* key not present */
1168 return 1;
1169
1170 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1171 if (!ptr)
1172 return 0; /* parameter not stored */
1173
1174 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
1175 return 1;
1176}
1177
1178/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1179 * it up into this table. Returns the HTTP request rate the key if the key is
1180 * present in the table, otherwise zero, so that comparisons can be easily
1181 * performed. If the inspected parameter is not stored in the table, <not found>
1182 * is returned.
1183 */
1184static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp)
1185{
1186 struct stktable *t;
1187 struct stktable_key *key;
1188 struct stksess *ts;
1189 void *ptr;
1190
1191 t = &arg_p[0].data.prx->table;
1192
1193 key = smp_to_stkey(smp, t);
1194 if (!key)
1195 return 0;
1196
1197 smp->flags = SMP_F_VOL_TEST;
1198 smp->type = SMP_T_UINT;
1199 smp->data.uint = 0;
1200
1201 ts = stktable_lookup_key(t, key);
1202 if (!ts) /* key not present */
1203 return 1;
1204
1205 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1206 if (!ptr)
1207 return 0; /* parameter not stored */
1208
1209 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
1210 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1211 return 1;
1212}
1213
1214/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1215 * it up into this table. Returns the volume of datareceived from clients in kbytes
1216 * if the key is present in the table, otherwise zero, so that comparisons can
1217 * be easily performed. If the inspected parameter is not stored in the table,
1218 * <not found> is returned.
1219 */
1220static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp)
1221{
1222 struct stktable *t;
1223 struct stktable_key *key;
1224 struct stksess *ts;
1225 void *ptr;
1226
1227 t = &arg_p[0].data.prx->table;
1228
1229 key = smp_to_stkey(smp, t);
1230 if (!key)
1231 return 0;
1232
1233 smp->flags = SMP_F_VOL_TEST;
1234 smp->type = SMP_T_UINT;
1235 smp->data.uint = 0;
1236
1237 ts = stktable_lookup_key(t, key);
1238 if (!ts) /* key not present */
1239 return 1;
1240
1241 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1242 if (!ptr)
1243 return 0; /* parameter not stored */
1244
1245 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
1246 return 1;
1247}
1248
1249/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1250 * it up into this table. Returns the volume of data sent to clients in kbytes
1251 * if the key is present in the table, otherwise zero, so that comparisons can
1252 * be easily performed. If the inspected parameter is not stored in the table,
1253 * <not found> is returned.
1254 */
1255static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp)
1256{
1257 struct stktable *t;
1258 struct stktable_key *key;
1259 struct stksess *ts;
1260 void *ptr;
1261
1262 t = &arg_p[0].data.prx->table;
1263
1264 key = smp_to_stkey(smp, t);
1265 if (!key)
1266 return 0;
1267
1268 smp->flags = SMP_F_VOL_TEST;
1269 smp->type = SMP_T_UINT;
1270 smp->data.uint = 0;
1271
1272 ts = stktable_lookup_key(t, key);
1273 if (!ts) /* key not present */
1274 return 1;
1275
1276 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1277 if (!ptr)
1278 return 0; /* parameter not stored */
1279
1280 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
1281 return 1;
1282}
1283
1284/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1285 * it up into this table. Returns the server ID associated with the key if the
1286 * key is present in the table, otherwise zero, so that comparisons can be
1287 * easily performed. If the inspected parameter is not stored in the table,
1288 * <not found> is returned.
1289 */
1290static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp)
1291{
1292 struct stktable *t;
1293 struct stktable_key *key;
1294 struct stksess *ts;
1295 void *ptr;
1296
1297 t = &arg_p[0].data.prx->table;
1298
1299 key = smp_to_stkey(smp, t);
1300 if (!key)
1301 return 0;
1302
1303 smp->flags = SMP_F_VOL_TEST;
1304 smp->type = SMP_T_UINT;
1305 smp->data.uint = 0;
1306
1307 ts = stktable_lookup_key(t, key);
1308 if (!ts) /* key not present */
1309 return 1;
1310
1311 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1312 if (!ptr)
1313 return 0; /* parameter not stored */
1314
1315 smp->data.uint = stktable_data_cast(ptr, server_id);
1316 return 1;
1317}
1318
1319/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1320 * it up into this table. Returns the cumulated number of sessions for the
1321 * key if the key is present in the table, otherwise zero, so that comparisons
1322 * can be easily performed. If the inspected parameter is not stored in the
1323 * table, <not found> is returned.
1324 */
1325static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp)
1326{
1327 struct stktable *t;
1328 struct stktable_key *key;
1329 struct stksess *ts;
1330 void *ptr;
1331
1332 t = &arg_p[0].data.prx->table;
1333
1334 key = smp_to_stkey(smp, t);
1335 if (!key)
1336 return 0;
1337
1338 smp->flags = SMP_F_VOL_TEST;
1339 smp->type = SMP_T_UINT;
1340 smp->data.uint = 0;
1341
1342 ts = stktable_lookup_key(t, key);
1343 if (!ts) /* key not present */
1344 return 1;
1345
1346 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1347 if (!ptr)
1348 return 0; /* parameter not stored */
1349
1350 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
1351 return 1;
1352}
1353
1354/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1355 * it up into this table. Returns the session rate the key if the key is
1356 * present in the table, otherwise zero, so that comparisons can be easily
1357 * performed. If the inspected parameter is not stored in the table, <not found>
1358 * is returned.
1359 */
1360static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp)
1361{
1362 struct stktable *t;
1363 struct stktable_key *key;
1364 struct stksess *ts;
1365 void *ptr;
1366
1367 t = &arg_p[0].data.prx->table;
1368
1369 key = smp_to_stkey(smp, t);
1370 if (!key)
1371 return 0;
1372
1373 smp->flags = SMP_F_VOL_TEST;
1374 smp->type = SMP_T_UINT;
1375 smp->data.uint = 0;
1376
1377 ts = stktable_lookup_key(t, key);
1378 if (!ts) /* key not present */
1379 return 1;
1380
1381 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1382 if (!ptr)
1383 return 0; /* parameter not stored */
1384
1385 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
1386 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1387 return 1;
1388}
1389
1390/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1391 * it up into this table. Returns the amount of concurrent connections tracking
1392 * the same key if the key is present in the table, otherwise zero, so that
1393 * comparisons can be easily performed. If the inspected parameter is not
1394 * stored in the table, <not found> is returned.
1395 */
1396static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp)
1397{
1398 struct stktable *t;
1399 struct stktable_key *key;
1400 struct stksess *ts;
1401
1402 t = &arg_p[0].data.prx->table;
1403
1404 key = smp_to_stkey(smp, t);
1405 if (!key)
1406 return 0;
1407
1408 smp->flags = SMP_F_VOL_TEST;
1409 smp->type = SMP_T_UINT;
1410 smp->data.uint = 0;
1411
1412 ts = stktable_lookup_key(t, key);
1413 if (ts)
1414 smp->data.uint = ts->ref_cnt;
1415
1416 return 1;
1417}
1418
1419
1420/* Note: must not be declared <const> as its list will be overwritten */
1421static struct sample_conv_kw_list sample_conv_kws = {ILH, {
1422 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_BOOL },
1423 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1424 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1425 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1426 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1427 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1428 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1429 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1430 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1431 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1432 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1433 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1434 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1435 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1436 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1437 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1438 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1439 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT },
1440 { /* END */ },
1441}};
1442
1443__attribute__((constructor))
1444static void __stick_table_init(void)
1445{
1446 /* register sample fetch and format conversion keywords */
1447 sample_register_convs(&sample_conv_kws);
1448}