blob: 4cd6c75962c16f486ec1ab791356bccbdd4662ce [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 Tarreauaea940e2010-06-06 11:56:36 +0200155 * Looks in table <t> for a sticky session matching <key>.
156 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100157 */
158struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key)
159{
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 Tarreauaea940e2010-06-06 11:56:36 +0200175/* Try to store sticky session <ts> in the table. If another entry already
176 * exists with the same key, its server ID is updated with <sid> and a non
177 * zero value is returned so that the caller knows it can release its stksess.
178 * If no similar entry was present, <ts> is inserted into the tree and assigned
179 * server ID <sid>. Zero is returned in this case, and the caller must not
180 * release the stksess.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100181 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200182int stktable_store(struct stktable *t, struct stksess *ts, int sid)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100184 struct ebmb_node *eb;
185
186 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200187 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100188 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200189 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100190
191 if (unlikely(!eb)) {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200192 /* no existing session, insert ours */
193 ts->sid = sid;
Willy Tarreau86257dc2010-06-06 12:57:10 +0200194 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100195
Willy Tarreau86257dc2010-06-06 12:57:10 +0200196 ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
197 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100198
199 if (t->expire) {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200200 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100201 task_queue(t->exp_task);
202 }
203 return 0;
204 }
205
Willy Tarreau86257dc2010-06-06 12:57:10 +0200206 ts = ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100207
208 if ( ts->sid != sid )
209 ts->sid = sid;
210 return 1;
211}
212
213/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200214 * Trash expired sticky sessions from table <t>. The next expiration date is
215 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100216 */
217static int stktable_trash_expired(struct stktable *t)
218{
219 struct stksess *ts;
220 struct eb32_node *eb;
221
222 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
223
224 while (1) {
225 if (unlikely(!eb)) {
226 /* we might have reached the end of the tree, typically because
227 * <now_ms> is in the first half and we're first scanning the last
228 * half. Let's loop back to the beginning of the tree now.
229 */
230 eb = eb32_first(&t->exps);
231 if (likely(!eb))
232 break;
233 }
234
235 if (likely(tick_is_lt(now_ms, eb->key))) {
236 /* timer not expired yet, revisit it later */
237 t->exp_next = eb->key;
238 return t->exp_next;
239 }
240
241 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200242 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243 eb = eb32_next(eb);
244
Willy Tarreau86257dc2010-06-06 12:57:10 +0200245 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100246
247 if (!tick_is_expired(ts->expire, now_ms)) {
248 if (!tick_isset(ts->expire))
249 continue;
250
Willy Tarreau86257dc2010-06-06 12:57:10 +0200251 ts->exp.key = ts->expire;
252 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100253
Willy Tarreau86257dc2010-06-06 12:57:10 +0200254 if (!eb || eb->key > ts->exp.key)
255 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100256 continue;
257 }
258
259 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200260 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100261 stksess_free(t, ts);
262 }
263
264 /* We have found no task to expire in any tree */
265 t->exp_next = TICK_ETERNITY;
266 return t->exp_next;
267}
268
269/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200270 * Task processing function to trash expired sticky sessions. A pointer to the
271 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100272 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200273static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100274{
275 struct stktable *t = (struct stktable *)task->context;
276
277 task->expire = stktable_trash_expired(t);
278 return task;
279}
280
Willy Tarreauaea940e2010-06-06 11:56:36 +0200281/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100282int stktable_init(struct stktable *t)
283{
284 if (t->size) {
285 memset(&t->keys, 0, sizeof(t->keys));
286 memset(&t->exps, 0, sizeof(t->exps));
287
Willy Tarreau393379c2010-06-06 12:11:37 +0200288 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100289
290 t->exp_next = TICK_ETERNITY;
291 if ( t->expire ) {
292 t->exp_task = task_new();
293 t->exp_task->process = process_table_expire;
294 t->exp_task->expire = TICK_ETERNITY;
295 t->exp_task->context = (void *)t;
296 }
297 return t->pool != NULL;
298 }
299 return 1;
300}
301
302/*
303 * Configuration keywords of known table types
304 */
305struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
306 { "integer", 0, 4 },
Willy Tarreauaea940e2010-06-06 11:56:36 +0200307 { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100308
309
310/*
311 * Parse table type configuration.
312 * Returns 0 on successful parsing, else 1.
313 * <myidx> is set at next configuration <args> index.
314 */
315int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
316{
317 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
318 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
319 continue;
320
321 *key_size = stktable_types[*type].default_size;
322 (*myidx)++;
323
Willy Tarreauaea940e2010-06-06 11:56:36 +0200324 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100325 if (strcmp("len", args[*myidx]) == 0) {
326 (*myidx)++;
327 *key_size = atol(args[*myidx]);
328 if ( !*key_size )
329 break;
330 /* null terminated string needs +1 for '\0'. */
331 (*key_size)++;
332 (*myidx)++;
333 }
334 }
335 return 0;
336 }
337 return 1;
338}
339
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200340/*****************************************************************/
341/* typed pattern to typed table key functions */
342/*****************************************************************/
343
344static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
345{
346 return (void *)&pdata->integer;
347}
348
349static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
350{
351 return (void *)&pdata->ip.s_addr;
352}
353
354static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
355{
356 kdata->integer = ntohl(pdata->ip.s_addr);
357 return (void *)&kdata->integer;
358}
359
360static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
361{
362 kdata->ip.s_addr = htonl(pdata->integer);
363 return (void *)&kdata->ip.s_addr;
364}
365
366static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
367{
368 *len = pdata->str.len;
369 return (void *)pdata->str.str;
370}
371
372static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
373{
374 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
375 return NULL;
376
377 *len = strlen((const char *)kdata->buf);
378 return (void *)kdata->buf;
379}
380
381static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
382{
383 void *key;
384
385 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
386 if (!key)
387 return NULL;
388
389 *len = strlen((const char *)key);
390 return key;
391}
392
393static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
394{
395 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
396 return NULL;
397
398 return (void *)&kdata->ip.s_addr;
399}
400
401
402static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
403{
404 int i;
405
406 kdata->integer = 0;
407 for (i = 0; i < pdata->str.len; i++) {
408 uint32_t val = pdata->str.str[i] - '0';
409
410 if (val > 9)
411 break;
412
413 kdata->integer = kdata->integer * 10 + val;
414 }
415 return (void *)&kdata->integer;
416}
417
418/*****************************************************************/
419/* typed pattern to typed table key matrix: */
420/* pattern_to_key[from pattern type][to table key type] */
421/* NULL pointer used for impossible pattern casts */
422/*****************************************************************/
423
424typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
425static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
426 { k_ip2ip, k_ip2int, k_ip2str },
427 { k_int2ip, k_int2int, k_int2str },
428 { k_str2ip, k_str2int, k_str2str },
429};
430
431
432/*
433 * Process a fetch + format conversion as defined by the pattern expression <expr>
434 * on request or response considering the <dir> parameter. Returns either NULL if
435 * no key could be extracted, or a pointer to the converted result stored in
436 * static_table_key in format <table_type>.
437 */
438struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
439 struct pattern_expr *expr, unsigned long table_type)
440{
441 struct pattern *ptrn;
442
443 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
444 if (!ptrn)
445 return NULL;
446
447 static_table_key.key_len = (size_t)-1;
448 static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
449
450 if (!static_table_key.key)
451 return NULL;
452
453 return &static_table_key;
454}
455
456/*
457 * Returns 1 if pattern expression <expr> result can be converted to table key of
458 * type <table_type>, otherwise zero. Used in configuration check.
459 */
460int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
461{
462 if (table_type >= STKTABLE_TYPES)
463 return 0;
464
465 if (LIST_ISEMPTY(&expr->conv_exprs)) {
466 if (!pattern_to_key[expr->fetch->out_type][table_type])
467 return 0;
468 } else {
469 struct pattern_conv_expr *conv_expr;
470 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
471
472 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
473 return 0;
474 }
475 return 1;
476}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100477
Willy Tarreau08d5f982010-06-06 13:34:54 +0200478/* Extra data types processing */
479struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau69b870f2010-06-06 14:30:13 +0200480 [STKTABLE_DT_CONN_CUM] = { .name = "conn_cum", .data_length = stktable_data_size(conn_cum) },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200481};
482
483/*
484 * Returns the data type number for the stktable_data_type whose name is <name>,
485 * or <0 if not found.
486 */
487int stktable_get_data_type(char *name)
488{
489 int type;
490
491 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
492 if (strcmp(name, stktable_data_types[type].name) == 0)
493 return type;
494 }
495 return -1;
496}
497