blob: 5723dffd2841c4c6a25bbbc3939ee9d937170faf [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 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);
Willy Tarreau86257dc2010-06-06 12:57:10 +020067 ts->key.node.leaf_p = NULL;
68 ts->exp.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010069 return ts;
70}
71
72/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020073 * Trash oldest <to_batch> sticky sessions from table <t>
74 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010075 */
76static int stktable_trash_oldest(struct stktable *t, int to_batch)
77{
78 struct stksess *ts;
79 struct eb32_node *eb;
80 int batched = 0;
81
82 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
83
84 while (batched < to_batch) {
85
86 if (unlikely(!eb)) {
87 /* we might have reached the end of the tree, typically because
88 * <now_ms> is in the first half and we're first scanning the last
89 * half. Let's loop back to the beginning of the tree now.
90 */
91 eb = eb32_first(&t->exps);
92 if (likely(!eb))
93 break;
94 }
95
96 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +020097 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +010098 eb = eb32_next(eb);
99
Willy Tarreau86257dc2010-06-06 12:57:10 +0200100 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100101
Willy Tarreau86257dc2010-06-06 12:57:10 +0200102 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100103 if (!tick_isset(ts->expire))
104 continue;
105
Willy Tarreau86257dc2010-06-06 12:57:10 +0200106 ts->exp.key = ts->expire;
107 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100108
Willy Tarreau86257dc2010-06-06 12:57:10 +0200109 if (!eb || eb->key > ts->exp.key)
110 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100111
112 continue;
113 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100114
Willy Tarreauaea940e2010-06-06 11:56:36 +0200115 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200116 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100117 stksess_free(t, ts);
118 batched++;
119 }
120
121 return batched;
122}
123
124/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200125 * Allocate and initialise a new sticky session.
126 * The new sticky session is returned or NULL in case of lack of memory.
127 * Sticky sessions should only be allocated this way, and must be freed using
128 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100129 */
130struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
131{
132 struct stksess *ts;
133
134 if (unlikely(t->current == t->size)) {
135 if ( t->nopurge )
136 return NULL;
137
138 if (!stktable_trash_oldest(t, t->size >> 8))
139 return NULL;
140 }
141
Willy Tarreau393379c2010-06-06 12:11:37 +0200142 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143 if (ts) {
144 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200145 stksess_init(t, ts);
146 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147 }
148
149 return ts;
150}
151
152/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200153 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200154 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100155 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200156struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100157{
158 struct ebmb_node *eb;
159
Emeric Brun3bd697e2010-01-04 15:23:48 +0100160 if (t->type == STKTABLE_TYPE_STRING)
161 eb = ebst_lookup_len(&t->keys, key->key, key->key_len);
162 else
163 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
164
165 if (unlikely(!eb)) {
166 /* no session found */
167 return NULL;
168 }
169
Willy Tarreau86257dc2010-06-06 12:57:10 +0200170 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100171}
172
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200173/*
174 * Looks in table <t> for a sticky session with same key as <ts>.
175 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100176 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200177struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100178{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179 struct ebmb_node *eb;
180
181 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200182 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200184 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100185
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200186 if (unlikely(!eb))
187 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100188
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200189 return ebmb_entry(eb, struct stksess, key);
190}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100191
Willy Tarreaucb183642010-06-06 17:58:34 +0200192/* Update the expiration timer for <ts> but do not touch its expiration node.
193 * The table's expiration timer is updated if set.
194 */
195struct stksess *stktable_touch(struct stktable *t, struct stksess *ts)
196{
197 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
198 if (t->expire) {
199 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
200 task_queue(t->exp_task);
201 }
202 return ts;
203}
204
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200205/* Insert new sticky session <ts> in the table. It is assumed that it does not
206 * yet exist (the caller must check this). The table's timeout is updated if it
207 * is set. <ts> is returned.
208 */
209struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
210{
211 ebmb_insert(&t->keys, &ts->key, t->key_size);
Willy Tarreaucb183642010-06-06 17:58:34 +0200212 stktable_touch(t, ts);
213 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200214 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200215 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100216}
217
218/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200219 * Trash expired sticky sessions from table <t>. The next expiration date is
220 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100221 */
222static int stktable_trash_expired(struct stktable *t)
223{
224 struct stksess *ts;
225 struct eb32_node *eb;
226
227 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
228
229 while (1) {
230 if (unlikely(!eb)) {
231 /* we might have reached the end of the tree, typically because
232 * <now_ms> is in the first half and we're first scanning the last
233 * half. Let's loop back to the beginning of the tree now.
234 */
235 eb = eb32_first(&t->exps);
236 if (likely(!eb))
237 break;
238 }
239
240 if (likely(tick_is_lt(now_ms, eb->key))) {
241 /* timer not expired yet, revisit it later */
242 t->exp_next = eb->key;
243 return t->exp_next;
244 }
245
246 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200247 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100248 eb = eb32_next(eb);
249
Willy Tarreau86257dc2010-06-06 12:57:10 +0200250 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100251
252 if (!tick_is_expired(ts->expire, now_ms)) {
253 if (!tick_isset(ts->expire))
254 continue;
255
Willy Tarreau86257dc2010-06-06 12:57:10 +0200256 ts->exp.key = ts->expire;
257 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100258
Willy Tarreau86257dc2010-06-06 12:57:10 +0200259 if (!eb || eb->key > ts->exp.key)
260 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100261 continue;
262 }
263
264 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200265 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100266 stksess_free(t, ts);
267 }
268
269 /* We have found no task to expire in any tree */
270 t->exp_next = TICK_ETERNITY;
271 return t->exp_next;
272}
273
274/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200275 * Task processing function to trash expired sticky sessions. A pointer to the
276 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100277 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200278static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100279{
280 struct stktable *t = (struct stktable *)task->context;
281
282 task->expire = stktable_trash_expired(t);
283 return task;
284}
285
Willy Tarreauaea940e2010-06-06 11:56:36 +0200286/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100287int stktable_init(struct stktable *t)
288{
289 if (t->size) {
290 memset(&t->keys, 0, sizeof(t->keys));
291 memset(&t->exps, 0, sizeof(t->exps));
292
Willy Tarreau393379c2010-06-06 12:11:37 +0200293 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100294
295 t->exp_next = TICK_ETERNITY;
296 if ( t->expire ) {
297 t->exp_task = task_new();
298 t->exp_task->process = process_table_expire;
299 t->exp_task->expire = TICK_ETERNITY;
300 t->exp_task->context = (void *)t;
301 }
302 return t->pool != NULL;
303 }
304 return 1;
305}
306
307/*
308 * Configuration keywords of known table types
309 */
310struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
311 { "integer", 0, 4 },
Willy Tarreauaea940e2010-06-06 11:56:36 +0200312 { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100313
314
315/*
316 * Parse table type configuration.
317 * Returns 0 on successful parsing, else 1.
318 * <myidx> is set at next configuration <args> index.
319 */
320int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
321{
322 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
323 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
324 continue;
325
326 *key_size = stktable_types[*type].default_size;
327 (*myidx)++;
328
Willy Tarreauaea940e2010-06-06 11:56:36 +0200329 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100330 if (strcmp("len", args[*myidx]) == 0) {
331 (*myidx)++;
332 *key_size = atol(args[*myidx]);
333 if ( !*key_size )
334 break;
335 /* null terminated string needs +1 for '\0'. */
336 (*key_size)++;
337 (*myidx)++;
338 }
339 }
340 return 0;
341 }
342 return 1;
343}
344
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200345/*****************************************************************/
346/* typed pattern to typed table key functions */
347/*****************************************************************/
348
349static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
350{
351 return (void *)&pdata->integer;
352}
353
354static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
355{
356 return (void *)&pdata->ip.s_addr;
357}
358
359static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
360{
361 kdata->integer = ntohl(pdata->ip.s_addr);
362 return (void *)&kdata->integer;
363}
364
365static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
366{
367 kdata->ip.s_addr = htonl(pdata->integer);
368 return (void *)&kdata->ip.s_addr;
369}
370
371static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
372{
373 *len = pdata->str.len;
374 return (void *)pdata->str.str;
375}
376
377static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
378{
379 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
380 return NULL;
381
382 *len = strlen((const char *)kdata->buf);
383 return (void *)kdata->buf;
384}
385
386static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
387{
388 void *key;
389
390 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
391 if (!key)
392 return NULL;
393
394 *len = strlen((const char *)key);
395 return key;
396}
397
398static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
399{
400 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
401 return NULL;
402
403 return (void *)&kdata->ip.s_addr;
404}
405
406
407static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
408{
409 int i;
410
411 kdata->integer = 0;
412 for (i = 0; i < pdata->str.len; i++) {
413 uint32_t val = pdata->str.str[i] - '0';
414
415 if (val > 9)
416 break;
417
418 kdata->integer = kdata->integer * 10 + val;
419 }
420 return (void *)&kdata->integer;
421}
422
423/*****************************************************************/
424/* typed pattern to typed table key matrix: */
425/* pattern_to_key[from pattern type][to table key type] */
426/* NULL pointer used for impossible pattern casts */
427/*****************************************************************/
428
429typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
430static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
431 { k_ip2ip, k_ip2int, k_ip2str },
432 { k_int2ip, k_int2int, k_int2str },
433 { k_str2ip, k_str2int, k_str2str },
434};
435
436
437/*
438 * Process a fetch + format conversion as defined by the pattern expression <expr>
439 * on request or response considering the <dir> parameter. Returns either NULL if
440 * no key could be extracted, or a pointer to the converted result stored in
441 * static_table_key in format <table_type>.
442 */
443struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
444 struct pattern_expr *expr, unsigned long table_type)
445{
446 struct pattern *ptrn;
447
448 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
449 if (!ptrn)
450 return NULL;
451
452 static_table_key.key_len = (size_t)-1;
453 static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
454
455 if (!static_table_key.key)
456 return NULL;
457
458 return &static_table_key;
459}
460
461/*
462 * Returns 1 if pattern expression <expr> result can be converted to table key of
463 * type <table_type>, otherwise zero. Used in configuration check.
464 */
465int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
466{
467 if (table_type >= STKTABLE_TYPES)
468 return 0;
469
470 if (LIST_ISEMPTY(&expr->conv_exprs)) {
471 if (!pattern_to_key[expr->fetch->out_type][table_type])
472 return 0;
473 } else {
474 struct pattern_conv_expr *conv_expr;
475 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
476
477 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
478 return 0;
479 }
480 return 1;
481}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100482
Willy Tarreau08d5f982010-06-06 13:34:54 +0200483/* Extra data types processing */
484struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau69b870f2010-06-06 14:30:13 +0200485 [STKTABLE_DT_CONN_CUM] = { .name = "conn_cum", .data_length = stktable_data_size(conn_cum) },
Willy Tarreau13c29de2010-06-06 16:40:39 +0200486 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200487};
488
489/*
490 * Returns the data type number for the stktable_data_type whose name is <name>,
491 * or <0 if not found.
492 */
493int stktable_get_data_type(char *name)
494{
495 int type;
496
497 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
498 if (strcmp(name, stktable_data_types[type].name) == 0)
499 return type;
500 }
501 return -1;
502}
503