blob: bf8df0f44704a8b3a96f091010969afda3ac9af6 [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 Tarreauf0b38bf2010-06-06 13:22:23 +020025#include <proto/pattern.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010026#include <proto/proxy.h>
27#include <proto/session.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020028#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010029#include <proto/task.h>
30
31
Willy Tarreau41883e22010-06-06 17:39:30 +020032/* structure used to return a table key built from a pattern */
33struct stktable_key static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020034
Emeric Brun3bd697e2010-01-04 15:23:48 +010035/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020036 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
37 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010038 */
39void stksess_free(struct stktable *t, struct stksess *ts)
40{
41 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020042 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010043}
44
45/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020046 * Kill an stksess (only if its ref_cnt is zero).
47 */
48void stksess_kill(struct stktable *t, struct stksess *ts)
49{
50 if (ts->ref_cnt)
51 return;
52
53 eb32_delete(&ts->exp);
54 ebmb_delete(&ts->key);
55 stksess_free(t, ts);
56}
57
58/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020059 * Initialize or update the key in the sticky session <ts> present in table <t>
60 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010061 */
Willy Tarreau393379c2010-06-06 12:11:37 +020062void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010063{
64 if (t->type != STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +020065 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010066 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020067 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
68 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010069 }
70}
71
72
73/*
Willy Tarreau393379c2010-06-06 12:11:37 +020074 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
75 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010076 */
Willy Tarreau393379c2010-06-06 12:11:37 +020077static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010078{
Willy Tarreau393379c2010-06-06 12:11:37 +020079 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020080 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020081 ts->key.node.leaf_p = NULL;
82 ts->exp.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010083 return ts;
84}
85
86/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020087 * Trash oldest <to_batch> sticky sessions from table <t>
88 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010089 */
90static int stktable_trash_oldest(struct stktable *t, int to_batch)
91{
92 struct stksess *ts;
93 struct eb32_node *eb;
94 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020095 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010096
97 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
98
99 while (batched < to_batch) {
100
101 if (unlikely(!eb)) {
102 /* we might have reached the end of the tree, typically because
103 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200104 * half. Let's loop back to the beginning of the tree now if we
105 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100106 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200107 if (looped)
108 break;
109 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100110 eb = eb32_first(&t->exps);
111 if (likely(!eb))
112 break;
113 }
114
115 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200116 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100117 eb = eb32_next(eb);
118
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200119 /* don't delete an entry which is currently referenced */
120 if (ts->ref_cnt)
121 continue;
122
Willy Tarreau86257dc2010-06-06 12:57:10 +0200123 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100124
Willy Tarreau86257dc2010-06-06 12:57:10 +0200125 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100126 if (!tick_isset(ts->expire))
127 continue;
128
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 ts->exp.key = ts->expire;
130 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100131
Willy Tarreau86257dc2010-06-06 12:57:10 +0200132 if (!eb || eb->key > ts->exp.key)
133 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100134
135 continue;
136 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreauaea940e2010-06-06 11:56:36 +0200138 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200139 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140 stksess_free(t, ts);
141 batched++;
142 }
143
144 return batched;
145}
146
147/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200148 * Allocate and initialise a new sticky session.
149 * The new sticky session is returned or NULL in case of lack of memory.
150 * Sticky sessions should only be allocated this way, and must be freed using
151 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100152 */
153struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
154{
155 struct stksess *ts;
156
157 if (unlikely(t->current == t->size)) {
158 if ( t->nopurge )
159 return NULL;
160
Emeric Brunfbce6d02010-09-23 18:10:00 +0200161 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100162 return NULL;
163 }
164
Willy Tarreau393379c2010-06-06 12:11:37 +0200165 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100166 if (ts) {
167 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200168 stksess_init(t, ts);
169 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 }
171
172 return ts;
173}
174
175/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200176 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200177 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100178 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200179struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180{
181 struct ebmb_node *eb;
182
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183 if (t->type == STKTABLE_TYPE_STRING)
Emeric Brun485479d2010-09-23 18:02:19 +0200184 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 +0100185 else
186 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
187
188 if (unlikely(!eb)) {
189 /* no session found */
190 return NULL;
191 }
192
Willy Tarreau86257dc2010-06-06 12:57:10 +0200193 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194}
195
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200196/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
197 * This is mainly used for situations where we want to refresh a key's usage so
198 * that it does not expire, and we want to have it created if it was not there.
199 * The stksess is returned, or NULL if it could not be created.
200 */
201struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
202{
203 struct stksess *ts;
204
205 ts = stktable_lookup_key(table, key);
206 if (likely(ts))
207 return stktable_touch(table, ts);
208
209 /* entry does not exist, initialize a new one */
210 ts = stksess_new(table, key);
211 if (likely(ts))
212 stktable_store(table, ts);
213 return ts;
214}
215
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200216/*
217 * Looks in table <t> for a sticky session with same key as <ts>.
218 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100219 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200220struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100221{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100222 struct ebmb_node *eb;
223
224 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200225 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100226 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200227 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200229 if (unlikely(!eb))
230 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200232 return ebmb_entry(eb, struct stksess, key);
233}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100234
Willy Tarreaucb183642010-06-06 17:58:34 +0200235/* Update the expiration timer for <ts> but do not touch its expiration node.
236 * The table's expiration timer is updated if set.
237 */
238struct stksess *stktable_touch(struct stktable *t, struct stksess *ts)
239{
240 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
241 if (t->expire) {
242 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
243 task_queue(t->exp_task);
244 }
245 return ts;
246}
247
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200248/* Insert new sticky session <ts> in the table. It is assumed that it does not
249 * yet exist (the caller must check this). The table's timeout is updated if it
250 * is set. <ts> is returned.
251 */
252struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
253{
254 ebmb_insert(&t->keys, &ts->key, t->key_size);
Willy Tarreaucb183642010-06-06 17:58:34 +0200255 stktable_touch(t, ts);
256 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200257 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200258 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100259}
260
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200261/* Returns a valid or initialized stksess for the specified stktable_key in the
262 * specified table, or NULL if the key was NULL, or if no entry was found nor
263 * could be created. The entry's expiration is updated.
264 */
265struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
266{
267 struct stksess *ts;
268
269 if (!key)
270 return NULL;
271
272 ts = stktable_lookup_key(table, key);
273 if (ts == NULL) {
274 /* entry does not exist, initialize a new one */
275 ts = stksess_new(table, key);
276 if (!ts)
277 return NULL;
278 stktable_store(table, ts);
279 }
280 else
281 stktable_touch(table, ts);
282 return ts;
283}
284
Emeric Brun3bd697e2010-01-04 15:23:48 +0100285/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200286 * Trash expired sticky sessions from table <t>. The next expiration date is
287 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100288 */
289static int stktable_trash_expired(struct stktable *t)
290{
291 struct stksess *ts;
292 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200293 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100294
295 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
296
297 while (1) {
298 if (unlikely(!eb)) {
299 /* we might have reached the end of the tree, typically because
300 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200301 * half. Let's loop back to the beginning of the tree now if we
302 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100303 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200304 if (looped)
305 break;
306 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100307 eb = eb32_first(&t->exps);
308 if (likely(!eb))
309 break;
310 }
311
312 if (likely(tick_is_lt(now_ms, eb->key))) {
313 /* timer not expired yet, revisit it later */
314 t->exp_next = eb->key;
315 return t->exp_next;
316 }
317
318 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200319 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100320 eb = eb32_next(eb);
321
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200322 /* don't delete an entry which is currently referenced */
323 if (ts->ref_cnt)
324 continue;
325
Willy Tarreau86257dc2010-06-06 12:57:10 +0200326 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100327
328 if (!tick_is_expired(ts->expire, now_ms)) {
329 if (!tick_isset(ts->expire))
330 continue;
331
Willy Tarreau86257dc2010-06-06 12:57:10 +0200332 ts->exp.key = ts->expire;
333 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100334
Willy Tarreau86257dc2010-06-06 12:57:10 +0200335 if (!eb || eb->key > ts->exp.key)
336 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100337 continue;
338 }
339
340 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200341 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100342 stksess_free(t, ts);
343 }
344
345 /* We have found no task to expire in any tree */
346 t->exp_next = TICK_ETERNITY;
347 return t->exp_next;
348}
349
350/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200351 * Task processing function to trash expired sticky sessions. A pointer to the
352 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100353 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200354static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100355{
356 struct stktable *t = (struct stktable *)task->context;
357
358 task->expire = stktable_trash_expired(t);
359 return task;
360}
361
Willy Tarreauaea940e2010-06-06 11:56:36 +0200362/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363int stktable_init(struct stktable *t)
364{
365 if (t->size) {
366 memset(&t->keys, 0, sizeof(t->keys));
367 memset(&t->exps, 0, sizeof(t->exps));
368
Willy Tarreau393379c2010-06-06 12:11:37 +0200369 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100370
371 t->exp_next = TICK_ETERNITY;
372 if ( t->expire ) {
373 t->exp_task = task_new();
374 t->exp_task->process = process_table_expire;
375 t->exp_task->expire = TICK_ETERNITY;
376 t->exp_task->context = (void *)t;
377 }
378 return t->pool != NULL;
379 }
380 return 1;
381}
382
383/*
384 * Configuration keywords of known table types
385 */
Emeric Brun485479d2010-09-23 18:02:19 +0200386struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
Emeric Brun3bd697e2010-01-04 15:23:48 +0100387 { "integer", 0, 4 },
Emeric Brun485479d2010-09-23 18:02:19 +0200388 { "string", STK_F_CUSTOM_KEYSIZE, 32 },
389 { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
391
392/*
393 * Parse table type configuration.
394 * Returns 0 on successful parsing, else 1.
395 * <myidx> is set at next configuration <args> index.
396 */
397int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
398{
399 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
400 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
401 continue;
402
403 *key_size = stktable_types[*type].default_size;
404 (*myidx)++;
405
Willy Tarreauaea940e2010-06-06 11:56:36 +0200406 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100407 if (strcmp("len", args[*myidx]) == 0) {
408 (*myidx)++;
409 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200410 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100411 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200412 if (*type == STKTABLE_TYPE_STRING) {
413 /* null terminated string needs +1 for '\0'. */
414 (*key_size)++;
415 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100416 (*myidx)++;
417 }
418 }
419 return 0;
420 }
421 return 1;
422}
423
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200424/*****************************************************************/
425/* typed pattern to typed table key functions */
426/*****************************************************************/
427
428static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
429{
430 return (void *)&pdata->integer;
431}
432
433static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
434{
435 return (void *)&pdata->ip.s_addr;
436}
437
438static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
439{
440 kdata->integer = ntohl(pdata->ip.s_addr);
441 return (void *)&kdata->integer;
442}
443
444static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
445{
446 kdata->ip.s_addr = htonl(pdata->integer);
447 return (void *)&kdata->ip.s_addr;
448}
449
450static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
451{
452 *len = pdata->str.len;
453 return (void *)pdata->str.str;
454}
455
456static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
457{
458 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
459 return NULL;
460
461 *len = strlen((const char *)kdata->buf);
462 return (void *)kdata->buf;
463}
464
465static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
466{
467 void *key;
468
469 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
470 if (!key)
471 return NULL;
472
473 *len = strlen((const char *)key);
474 return key;
475}
476
477static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
478{
479 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
480 return NULL;
481
482 return (void *)&kdata->ip.s_addr;
483}
484
485
486static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
487{
488 int i;
489
490 kdata->integer = 0;
491 for (i = 0; i < pdata->str.len; i++) {
492 uint32_t val = pdata->str.str[i] - '0';
493
494 if (val > 9)
495 break;
496
497 kdata->integer = kdata->integer * 10 + val;
498 }
499 return (void *)&kdata->integer;
500}
501
502/*****************************************************************/
503/* typed pattern to typed table key matrix: */
504/* pattern_to_key[from pattern type][to table key type] */
505/* NULL pointer used for impossible pattern casts */
506/*****************************************************************/
507
508typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
509static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
Emeric Brun485479d2010-09-23 18:02:19 +0200510/* table type: IP INTEGER STRING BINARY */
511/* pattern type: IP */ { k_ip2ip, k_ip2int, k_ip2str, NULL },
512/* INTEGER */ { k_int2ip, k_int2int, k_int2str, NULL },
513/* STRING */ { k_str2ip, k_str2int, k_str2str, k_str2str },
514/* DATA */ { NULL, NULL, NULL, k_str2str },
515/* CONSTSTRING */ { k_str2ip, k_str2int, k_str2str, k_str2str },
516/* CONSTDATA */ { NULL, NULL, NULL, k_str2str },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200517};
518
519
520/*
521 * Process a fetch + format conversion as defined by the pattern expression <expr>
522 * on request or response considering the <dir> parameter. Returns either NULL if
523 * no key could be extracted, or a pointer to the converted result stored in
524 * static_table_key in format <table_type>.
525 */
Emeric Brun485479d2010-09-23 18:02:19 +0200526struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7, int dir,
527 struct pattern_expr *expr)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200528{
529 struct pattern *ptrn;
530
531 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
532 if (!ptrn)
533 return NULL;
534
Emeric Brun485479d2010-09-23 18:02:19 +0200535 static_table_key.key_len = t->key_size;
536 static_table_key.key = pattern_to_key[ptrn->type][t->type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200537
538 if (!static_table_key.key)
539 return NULL;
540
Emeric Brun485479d2010-09-23 18:02:19 +0200541 if ((static_table_key.key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) {
542 /* need padding with null */
543
544 /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf)
545 cause t->key_size is necessary less than sizeof(static_table_key.data) */
546
547 if ((char *)static_table_key.key > (char *)&static_table_key.data &&
548 (char *)static_table_key.key < (char *)&static_table_key.data + sizeof(static_table_key.data)) {
549 /* key buffer is part of the static_table_key private data buffer, but is not aligned */
550
551 if (sizeof(static_table_key.data) - ((char *)static_table_key.key - (char *)&static_table_key.data) < t->key_size) {
552 /* if not remain enougth place for padding , process a realign */
553 memmove(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
554 static_table_key.key = static_table_key.data.buf;
555 }
556 }
557 else if (static_table_key.key != static_table_key.data.buf) {
558 /* key definitly not part of the static_table_key private data buffer */
559
560 memcpy(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
561 static_table_key.key = static_table_key.data.buf;
562 }
563
564 memset(static_table_key.key + static_table_key.key_len, 0, t->key_size - static_table_key.key_len);
565 }
566
567
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200568 return &static_table_key;
569}
570
571/*
572 * Returns 1 if pattern expression <expr> result can be converted to table key of
573 * type <table_type>, otherwise zero. Used in configuration check.
574 */
575int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
576{
577 if (table_type >= STKTABLE_TYPES)
578 return 0;
579
580 if (LIST_ISEMPTY(&expr->conv_exprs)) {
581 if (!pattern_to_key[expr->fetch->out_type][table_type])
582 return 0;
583 } else {
584 struct pattern_conv_expr *conv_expr;
585 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
586
587 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
588 return 0;
589 }
590 return 1;
591}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100592
Willy Tarreau08d5f982010-06-06 13:34:54 +0200593/* Extra data types processing */
594struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200595 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
596 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
597 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
598 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
599 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
600 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
601 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
602 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
603 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
604 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
605 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
606 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
607 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
608 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
609 [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 +0200610};
611
612/*
613 * Returns the data type number for the stktable_data_type whose name is <name>,
614 * or <0 if not found.
615 */
616int stktable_get_data_type(char *name)
617{
618 int type;
619
620 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
621 if (strcmp(name, stktable_data_types[type].name) == 0)
622 return type;
623 }
624 return -1;
625}
626
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200627/* Returns pointer to proxy containing table <name> or NULL if not found */
628struct proxy *find_stktable(const char *name)
629{
630 struct proxy *px;
631
632 for (px = proxy; px; px = px->next) {
633 if (px->table.size && strcmp(px->id, name) == 0)
634 return px;
635 }
636 return NULL;
637}