blob: 994fbebef83d1db247195b7201a25ad56cdd7bc2 [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 Tarreauf0b38bf2010-06-06 13:22:23 +020032/* static structure used to return a table key built from a pattern */
33static struct stktable_key static_table_key;
34
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 Tarreauaea940e2010-06-06 11:56:36 +020046 * Initialize or update the key in the sticky session <ts> present in table <t>
47 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010048 */
Willy Tarreau393379c2010-06-06 12:11:37 +020049void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010050{
51 if (t->type != STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +020052 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010053 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020054 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
55 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010056 }
57}
58
59
60/*
Willy Tarreau393379c2010-06-06 12:11:37 +020061 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
62 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010063 */
Willy Tarreau393379c2010-06-06 12:11:37 +020064static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010065{
Willy Tarreau393379c2010-06-06 12:11:37 +020066 memset((void *)ts - t->data_size, 0, t->data_size);
67 ts->sid = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020068 ts->key.node.leaf_p = NULL;
69 ts->exp.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010070 return ts;
71}
72
73/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020074 * Trash oldest <to_batch> sticky sessions from table <t>
75 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010076 */
77static int stktable_trash_oldest(struct stktable *t, int to_batch)
78{
79 struct stksess *ts;
80 struct eb32_node *eb;
81 int batched = 0;
82
83 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
84
85 while (batched < to_batch) {
86
87 if (unlikely(!eb)) {
88 /* we might have reached the end of the tree, typically because
89 * <now_ms> is in the first half and we're first scanning the last
90 * half. Let's loop back to the beginning of the tree now.
91 */
92 eb = eb32_first(&t->exps);
93 if (likely(!eb))
94 break;
95 }
96
97 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +020098 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +010099 eb = eb32_next(eb);
100
Willy Tarreau86257dc2010-06-06 12:57:10 +0200101 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102
Willy Tarreau86257dc2010-06-06 12:57:10 +0200103 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100104 if (!tick_isset(ts->expire))
105 continue;
106
Willy Tarreau86257dc2010-06-06 12:57:10 +0200107 ts->exp.key = ts->expire;
108 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100109
Willy Tarreau86257dc2010-06-06 12:57:10 +0200110 if (!eb || eb->key > ts->exp.key)
111 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100112
113 continue;
114 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100115
Willy Tarreauaea940e2010-06-06 11:56:36 +0200116 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200117 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100118 stksess_free(t, ts);
119 batched++;
120 }
121
122 return batched;
123}
124
125/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200126 * Allocate and initialise a new sticky session.
127 * The new sticky session is returned or NULL in case of lack of memory.
128 * Sticky sessions should only be allocated this way, and must be freed using
129 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130 */
131struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
132{
133 struct stksess *ts;
134
135 if (unlikely(t->current == t->size)) {
136 if ( t->nopurge )
137 return NULL;
138
139 if (!stktable_trash_oldest(t, t->size >> 8))
140 return NULL;
141 }
142
Willy Tarreau393379c2010-06-06 12:11:37 +0200143 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100144 if (ts) {
145 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200146 stksess_init(t, ts);
147 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100148 }
149
150 return ts;
151}
152
153/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200154 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200155 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100156 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200157struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100158{
159 struct ebmb_node *eb;
160
Emeric Brun3bd697e2010-01-04 15:23:48 +0100161 if (t->type == STKTABLE_TYPE_STRING)
162 eb = ebst_lookup_len(&t->keys, key->key, key->key_len);
163 else
164 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
165
166 if (unlikely(!eb)) {
167 /* no session found */
168 return NULL;
169 }
170
Willy Tarreau86257dc2010-06-06 12:57:10 +0200171 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100172}
173
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200174/*
175 * Looks in table <t> for a sticky session with same key as <ts>.
176 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100177 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200178struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180 struct ebmb_node *eb;
181
182 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200183 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100184 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200185 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100186
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200187 if (unlikely(!eb))
188 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200190 return ebmb_entry(eb, struct stksess, key);
191}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100192
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200193/* Insert new sticky session <ts> in the table. It is assumed that it does not
194 * yet exist (the caller must check this). The table's timeout is updated if it
195 * is set. <ts> is returned.
196 */
197struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
198{
199 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100200
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200201 ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
202 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100203
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200204 if (t->expire) {
205 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
206 task_queue(t->exp_task);
207 }
208 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100209}
210
211/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200212 * Trash expired sticky sessions from table <t>. The next expiration date is
213 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100214 */
215static int stktable_trash_expired(struct stktable *t)
216{
217 struct stksess *ts;
218 struct eb32_node *eb;
219
220 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
221
222 while (1) {
223 if (unlikely(!eb)) {
224 /* we might have reached the end of the tree, typically because
225 * <now_ms> is in the first half and we're first scanning the last
226 * half. Let's loop back to the beginning of the tree now.
227 */
228 eb = eb32_first(&t->exps);
229 if (likely(!eb))
230 break;
231 }
232
233 if (likely(tick_is_lt(now_ms, eb->key))) {
234 /* timer not expired yet, revisit it later */
235 t->exp_next = eb->key;
236 return t->exp_next;
237 }
238
239 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200240 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100241 eb = eb32_next(eb);
242
Willy Tarreau86257dc2010-06-06 12:57:10 +0200243 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100244
245 if (!tick_is_expired(ts->expire, now_ms)) {
246 if (!tick_isset(ts->expire))
247 continue;
248
Willy Tarreau86257dc2010-06-06 12:57:10 +0200249 ts->exp.key = ts->expire;
250 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100251
Willy Tarreau86257dc2010-06-06 12:57:10 +0200252 if (!eb || eb->key > ts->exp.key)
253 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100254 continue;
255 }
256
257 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200258 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100259 stksess_free(t, ts);
260 }
261
262 /* We have found no task to expire in any tree */
263 t->exp_next = TICK_ETERNITY;
264 return t->exp_next;
265}
266
267/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200268 * Task processing function to trash expired sticky sessions. A pointer to the
269 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100270 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200271static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100272{
273 struct stktable *t = (struct stktable *)task->context;
274
275 task->expire = stktable_trash_expired(t);
276 return task;
277}
278
Willy Tarreauaea940e2010-06-06 11:56:36 +0200279/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100280int stktable_init(struct stktable *t)
281{
282 if (t->size) {
283 memset(&t->keys, 0, sizeof(t->keys));
284 memset(&t->exps, 0, sizeof(t->exps));
285
Willy Tarreau393379c2010-06-06 12:11:37 +0200286 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100287
288 t->exp_next = TICK_ETERNITY;
289 if ( t->expire ) {
290 t->exp_task = task_new();
291 t->exp_task->process = process_table_expire;
292 t->exp_task->expire = TICK_ETERNITY;
293 t->exp_task->context = (void *)t;
294 }
295 return t->pool != NULL;
296 }
297 return 1;
298}
299
300/*
301 * Configuration keywords of known table types
302 */
303struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
304 { "integer", 0, 4 },
Willy Tarreauaea940e2010-06-06 11:56:36 +0200305 { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100306
307
308/*
309 * Parse table type configuration.
310 * Returns 0 on successful parsing, else 1.
311 * <myidx> is set at next configuration <args> index.
312 */
313int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
314{
315 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
316 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
317 continue;
318
319 *key_size = stktable_types[*type].default_size;
320 (*myidx)++;
321
Willy Tarreauaea940e2010-06-06 11:56:36 +0200322 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323 if (strcmp("len", args[*myidx]) == 0) {
324 (*myidx)++;
325 *key_size = atol(args[*myidx]);
326 if ( !*key_size )
327 break;
328 /* null terminated string needs +1 for '\0'. */
329 (*key_size)++;
330 (*myidx)++;
331 }
332 }
333 return 0;
334 }
335 return 1;
336}
337
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200338/*****************************************************************/
339/* typed pattern to typed table key functions */
340/*****************************************************************/
341
342static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
343{
344 return (void *)&pdata->integer;
345}
346
347static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
348{
349 return (void *)&pdata->ip.s_addr;
350}
351
352static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
353{
354 kdata->integer = ntohl(pdata->ip.s_addr);
355 return (void *)&kdata->integer;
356}
357
358static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
359{
360 kdata->ip.s_addr = htonl(pdata->integer);
361 return (void *)&kdata->ip.s_addr;
362}
363
364static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
365{
366 *len = pdata->str.len;
367 return (void *)pdata->str.str;
368}
369
370static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
371{
372 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
373 return NULL;
374
375 *len = strlen((const char *)kdata->buf);
376 return (void *)kdata->buf;
377}
378
379static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
380{
381 void *key;
382
383 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
384 if (!key)
385 return NULL;
386
387 *len = strlen((const char *)key);
388 return key;
389}
390
391static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
392{
393 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
394 return NULL;
395
396 return (void *)&kdata->ip.s_addr;
397}
398
399
400static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
401{
402 int i;
403
404 kdata->integer = 0;
405 for (i = 0; i < pdata->str.len; i++) {
406 uint32_t val = pdata->str.str[i] - '0';
407
408 if (val > 9)
409 break;
410
411 kdata->integer = kdata->integer * 10 + val;
412 }
413 return (void *)&kdata->integer;
414}
415
416/*****************************************************************/
417/* typed pattern to typed table key matrix: */
418/* pattern_to_key[from pattern type][to table key type] */
419/* NULL pointer used for impossible pattern casts */
420/*****************************************************************/
421
422typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
423static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
424 { k_ip2ip, k_ip2int, k_ip2str },
425 { k_int2ip, k_int2int, k_int2str },
426 { k_str2ip, k_str2int, k_str2str },
427};
428
429
430/*
431 * Process a fetch + format conversion as defined by the pattern expression <expr>
432 * on request or response considering the <dir> parameter. Returns either NULL if
433 * no key could be extracted, or a pointer to the converted result stored in
434 * static_table_key in format <table_type>.
435 */
436struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
437 struct pattern_expr *expr, unsigned long table_type)
438{
439 struct pattern *ptrn;
440
441 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
442 if (!ptrn)
443 return NULL;
444
445 static_table_key.key_len = (size_t)-1;
446 static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
447
448 if (!static_table_key.key)
449 return NULL;
450
451 return &static_table_key;
452}
453
454/*
455 * Returns 1 if pattern expression <expr> result can be converted to table key of
456 * type <table_type>, otherwise zero. Used in configuration check.
457 */
458int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
459{
460 if (table_type >= STKTABLE_TYPES)
461 return 0;
462
463 if (LIST_ISEMPTY(&expr->conv_exprs)) {
464 if (!pattern_to_key[expr->fetch->out_type][table_type])
465 return 0;
466 } else {
467 struct pattern_conv_expr *conv_expr;
468 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
469
470 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
471 return 0;
472 }
473 return 1;
474}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100475
Willy Tarreau08d5f982010-06-06 13:34:54 +0200476/* Extra data types processing */
477struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau69b870f2010-06-06 14:30:13 +0200478 [STKTABLE_DT_CONN_CUM] = { .name = "conn_cum", .data_length = stktable_data_size(conn_cum) },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200479};
480
481/*
482 * Returns the data type number for the stktable_data_type whose name is <name>,
483 * or <0 if not found.
484 */
485int stktable_get_data_type(char *name)
486{
487 int type;
488
489 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
490 if (strcmp(name, stktable_data_types[type].name) == 0)
491 return type;
492 }
493 return -1;
494}
495