blob: fa469255529a4a59a8c35dd2e11cbaf4c9934c13 [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>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <string.h>
14
15#include <common/config.h>
16#include <common/memory.h>
17#include <common/mini-clist.h>
18#include <common/standard.h>
19#include <common/time.h>
20
21#include <ebmbtree.h>
22#include <ebsttree.h>
23
24#include <types/stick_table.h>
25
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020026#include <proto/pattern.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010027#include <proto/proxy.h>
28#include <proto/session.h>
29#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 Tarreauaea940e2010-06-06 11:56:36 +0200154 * Looks in table <t> for a sticky session matching <key>.
155 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100156 */
157struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key)
158{
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 Tarreauaea940e2010-06-06 11:56:36 +0200174/* Try to store sticky session <ts> in the table. If another entry already
175 * exists with the same key, its server ID is updated with <sid> and a non
176 * zero value is returned so that the caller knows it can release its stksess.
177 * If no similar entry was present, <ts> is inserted into the tree and assigned
178 * server ID <sid>. Zero is returned in this case, and the caller must not
179 * release the stksess.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200181int stktable_store(struct stktable *t, struct stksess *ts, int sid)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100182{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183 struct ebmb_node *eb;
184
185 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200186 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200188 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189
190 if (unlikely(!eb)) {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200191 /* no existing session, insert ours */
192 ts->sid = sid;
Willy Tarreau86257dc2010-06-06 12:57:10 +0200193 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194
Willy Tarreau86257dc2010-06-06 12:57:10 +0200195 ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
196 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100197
198 if (t->expire) {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200199 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100200 task_queue(t->exp_task);
201 }
202 return 0;
203 }
204
Willy Tarreau86257dc2010-06-06 12:57:10 +0200205 ts = ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100206
207 if ( ts->sid != sid )
208 ts->sid = sid;
209 return 1;
210}
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