blob: a86f48f41a587f63f614b024094888c447ee9a3d [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
25#include <types/stick_table.h>
26
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020027#include <proto/pattern.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010028#include <proto/proxy.h>
29#include <proto/session.h>
30#include <proto/task.h>
31
32
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020033/* static structure used to return a table key built from a pattern */
34static struct stktable_key static_table_key;
35
Emeric Brun3bd697e2010-01-04 15:23:48 +010036/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020037 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
38 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010039 */
40void stksess_free(struct stktable *t, struct stksess *ts)
41{
42 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020043 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010044}
45
46/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020047 * Initialize or update the key in the sticky session <ts> present in table <t>
48 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010049 */
Willy Tarreau393379c2010-06-06 12:11:37 +020050void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010051{
52 if (t->type != STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +020053 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010054 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020055 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
56 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010057 }
58}
59
60
61/*
Willy Tarreau393379c2010-06-06 12:11:37 +020062 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
63 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010064 */
Willy Tarreau393379c2010-06-06 12:11:37 +020065static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010066{
Willy Tarreau393379c2010-06-06 12:11:37 +020067 memset((void *)ts - t->data_size, 0, t->data_size);
68 ts->sid = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020069 ts->key.node.leaf_p = NULL;
70 ts->exp.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010071 return ts;
72}
73
74/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020075 * Trash oldest <to_batch> sticky sessions from table <t>
76 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010077 */
78static int stktable_trash_oldest(struct stktable *t, int to_batch)
79{
80 struct stksess *ts;
81 struct eb32_node *eb;
82 int batched = 0;
83
84 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
85
86 while (batched < to_batch) {
87
88 if (unlikely(!eb)) {
89 /* we might have reached the end of the tree, typically because
90 * <now_ms> is in the first half and we're first scanning the last
91 * half. Let's loop back to the beginning of the tree now.
92 */
93 eb = eb32_first(&t->exps);
94 if (likely(!eb))
95 break;
96 }
97
98 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +020099 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100100 eb = eb32_next(eb);
101
Willy Tarreau86257dc2010-06-06 12:57:10 +0200102 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100103
Willy Tarreau86257dc2010-06-06 12:57:10 +0200104 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100105 if (!tick_isset(ts->expire))
106 continue;
107
Willy Tarreau86257dc2010-06-06 12:57:10 +0200108 ts->exp.key = ts->expire;
109 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100110
Willy Tarreau86257dc2010-06-06 12:57:10 +0200111 if (!eb || eb->key > ts->exp.key)
112 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100113
114 continue;
115 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100116
Willy Tarreauaea940e2010-06-06 11:56:36 +0200117 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200118 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100119 stksess_free(t, ts);
120 batched++;
121 }
122
123 return batched;
124}
125
126/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200127 * Allocate and initialise a new sticky session.
128 * The new sticky session is returned or NULL in case of lack of memory.
129 * Sticky sessions should only be allocated this way, and must be freed using
130 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100131 */
132struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
133{
134 struct stksess *ts;
135
136 if (unlikely(t->current == t->size)) {
137 if ( t->nopurge )
138 return NULL;
139
140 if (!stktable_trash_oldest(t, t->size >> 8))
141 return NULL;
142 }
143
Willy Tarreau393379c2010-06-06 12:11:37 +0200144 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100145 if (ts) {
146 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200147 stksess_init(t, ts);
148 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100149 }
150
151 return ts;
152}
153
154/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200155 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200156 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100157 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200158struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100159{
160 struct ebmb_node *eb;
161
Emeric Brun3bd697e2010-01-04 15:23:48 +0100162 if (t->type == STKTABLE_TYPE_STRING)
163 eb = ebst_lookup_len(&t->keys, key->key, key->key_len);
164 else
165 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
166
167 if (unlikely(!eb)) {
168 /* no session found */
169 return NULL;
170 }
171
Willy Tarreau86257dc2010-06-06 12:57:10 +0200172 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100173}
174
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200175/*
176 * Looks in table <t> for a sticky session with same key as <ts>.
177 * 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(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100181 struct ebmb_node *eb;
182
183 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200184 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100185 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200186 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200188 if (unlikely(!eb))
189 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100190
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200191 return ebmb_entry(eb, struct stksess, key);
192}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100193
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200194/* Insert new sticky session <ts> in the table. It is assumed that it does not
195 * yet exist (the caller must check this). The table's timeout is updated if it
196 * is set. <ts> is returned.
197 */
198struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
199{
200 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100201
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200202 ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
203 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100204
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200205 if (t->expire) {
206 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
207 task_queue(t->exp_task);
208 }
209 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100210}
211
212/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200213 * Trash expired sticky sessions from table <t>. The next expiration date is
214 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100215 */
216static int stktable_trash_expired(struct stktable *t)
217{
218 struct stksess *ts;
219 struct eb32_node *eb;
220
221 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
222
223 while (1) {
224 if (unlikely(!eb)) {
225 /* we might have reached the end of the tree, typically because
226 * <now_ms> is in the first half and we're first scanning the last
227 * half. Let's loop back to the beginning of the tree now.
228 */
229 eb = eb32_first(&t->exps);
230 if (likely(!eb))
231 break;
232 }
233
234 if (likely(tick_is_lt(now_ms, eb->key))) {
235 /* timer not expired yet, revisit it later */
236 t->exp_next = eb->key;
237 return t->exp_next;
238 }
239
240 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200241 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100242 eb = eb32_next(eb);
243
Willy Tarreau86257dc2010-06-06 12:57:10 +0200244 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100245
246 if (!tick_is_expired(ts->expire, now_ms)) {
247 if (!tick_isset(ts->expire))
248 continue;
249
Willy Tarreau86257dc2010-06-06 12:57:10 +0200250 ts->exp.key = ts->expire;
251 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100252
Willy Tarreau86257dc2010-06-06 12:57:10 +0200253 if (!eb || eb->key > ts->exp.key)
254 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100255 continue;
256 }
257
258 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200259 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100260 stksess_free(t, ts);
261 }
262
263 /* We have found no task to expire in any tree */
264 t->exp_next = TICK_ETERNITY;
265 return t->exp_next;
266}
267
268/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200269 * Task processing function to trash expired sticky sessions. A pointer to the
270 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100271 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200272static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100273{
274 struct stktable *t = (struct stktable *)task->context;
275
276 task->expire = stktable_trash_expired(t);
277 return task;
278}
279
Willy Tarreauaea940e2010-06-06 11:56:36 +0200280/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100281int stktable_init(struct stktable *t)
282{
283 if (t->size) {
284 memset(&t->keys, 0, sizeof(t->keys));
285 memset(&t->exps, 0, sizeof(t->exps));
286
Willy Tarreau393379c2010-06-06 12:11:37 +0200287 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100288
289 t->exp_next = TICK_ETERNITY;
290 if ( t->expire ) {
291 t->exp_task = task_new();
292 t->exp_task->process = process_table_expire;
293 t->exp_task->expire = TICK_ETERNITY;
294 t->exp_task->context = (void *)t;
295 }
296 return t->pool != NULL;
297 }
298 return 1;
299}
300
301/*
302 * Configuration keywords of known table types
303 */
304struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
305 { "integer", 0, 4 },
Willy Tarreauaea940e2010-06-06 11:56:36 +0200306 { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100307
308
309/*
310 * Parse table type configuration.
311 * Returns 0 on successful parsing, else 1.
312 * <myidx> is set at next configuration <args> index.
313 */
314int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
315{
316 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
317 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
318 continue;
319
320 *key_size = stktable_types[*type].default_size;
321 (*myidx)++;
322
Willy Tarreauaea940e2010-06-06 11:56:36 +0200323 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100324 if (strcmp("len", args[*myidx]) == 0) {
325 (*myidx)++;
326 *key_size = atol(args[*myidx]);
327 if ( !*key_size )
328 break;
329 /* null terminated string needs +1 for '\0'. */
330 (*key_size)++;
331 (*myidx)++;
332 }
333 }
334 return 0;
335 }
336 return 1;
337}
338
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200339/*****************************************************************/
340/* typed pattern to typed table key functions */
341/*****************************************************************/
342
343static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
344{
345 return (void *)&pdata->integer;
346}
347
348static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
349{
350 return (void *)&pdata->ip.s_addr;
351}
352
353static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
354{
355 kdata->integer = ntohl(pdata->ip.s_addr);
356 return (void *)&kdata->integer;
357}
358
359static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
360{
361 kdata->ip.s_addr = htonl(pdata->integer);
362 return (void *)&kdata->ip.s_addr;
363}
364
365static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
366{
367 *len = pdata->str.len;
368 return (void *)pdata->str.str;
369}
370
371static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
372{
373 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
374 return NULL;
375
376 *len = strlen((const char *)kdata->buf);
377 return (void *)kdata->buf;
378}
379
380static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
381{
382 void *key;
383
384 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
385 if (!key)
386 return NULL;
387
388 *len = strlen((const char *)key);
389 return key;
390}
391
392static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
393{
394 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
395 return NULL;
396
397 return (void *)&kdata->ip.s_addr;
398}
399
400
401static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
402{
403 int i;
404
405 kdata->integer = 0;
406 for (i = 0; i < pdata->str.len; i++) {
407 uint32_t val = pdata->str.str[i] - '0';
408
409 if (val > 9)
410 break;
411
412 kdata->integer = kdata->integer * 10 + val;
413 }
414 return (void *)&kdata->integer;
415}
416
417/*****************************************************************/
418/* typed pattern to typed table key matrix: */
419/* pattern_to_key[from pattern type][to table key type] */
420/* NULL pointer used for impossible pattern casts */
421/*****************************************************************/
422
423typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
424static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
425 { k_ip2ip, k_ip2int, k_ip2str },
426 { k_int2ip, k_int2int, k_int2str },
427 { k_str2ip, k_str2int, k_str2str },
428};
429
430
431/*
432 * Process a fetch + format conversion as defined by the pattern expression <expr>
433 * on request or response considering the <dir> parameter. Returns either NULL if
434 * no key could be extracted, or a pointer to the converted result stored in
435 * static_table_key in format <table_type>.
436 */
437struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
438 struct pattern_expr *expr, unsigned long table_type)
439{
440 struct pattern *ptrn;
441
442 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
443 if (!ptrn)
444 return NULL;
445
446 static_table_key.key_len = (size_t)-1;
447 static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
448
449 if (!static_table_key.key)
450 return NULL;
451
452 return &static_table_key;
453}
454
455/*
456 * Returns 1 if pattern expression <expr> result can be converted to table key of
457 * type <table_type>, otherwise zero. Used in configuration check.
458 */
459int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
460{
461 if (table_type >= STKTABLE_TYPES)
462 return 0;
463
464 if (LIST_ISEMPTY(&expr->conv_exprs)) {
465 if (!pattern_to_key[expr->fetch->out_type][table_type])
466 return 0;
467 } else {
468 struct pattern_conv_expr *conv_expr;
469 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
470
471 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
472 return 0;
473 }
474 return 1;
475}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100476
Willy Tarreau08d5f982010-06-06 13:34:54 +0200477/* Extra data types processing */
478struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau69b870f2010-06-06 14:30:13 +0200479 [STKTABLE_DT_CONN_CUM] = { .name = "conn_cum", .data_length = stktable_data_size(conn_cum) },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200480};
481
482/*
483 * Returns the data type number for the stktable_data_type whose name is <name>,
484 * or <0 if not found.
485 */
486int stktable_get_data_type(char *name)
487{
488 int type;
489
490 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
491 if (strcmp(name, stktable_data_types[type].name) == 0)
492 return type;
493 }
494 return -1;
495}
496