blob: 78a0df7514876d649f7b7578464ef69cfa6da8e5 [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 Tarreauf6efda12010-08-03 20:34:06 +020046 * Kill an stksess (only if its ref_cnt is zero).
47 */
48void stksess_kill(struct stktable *t, struct stksess *ts)
49{
50 if (ts->ref_cnt)
51 return;
52
53 eb32_delete(&ts->exp);
54 ebmb_delete(&ts->key);
55 stksess_free(t, ts);
56}
57
58/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020059 * Initialize or update the key in the sticky session <ts> present in table <t>
60 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010061 */
Willy Tarreau393379c2010-06-06 12:11:37 +020062void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010063{
64 if (t->type != STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +020065 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010066 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020067 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
68 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010069 }
70}
71
72
73/*
Willy Tarreau393379c2010-06-06 12:11:37 +020074 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
75 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010076 */
Willy Tarreau393379c2010-06-06 12:11:37 +020077static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010078{
Willy Tarreau393379c2010-06-06 12:11:37 +020079 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020080 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020081 ts->key.node.leaf_p = NULL;
82 ts->exp.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010083 return ts;
84}
85
86/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020087 * Trash oldest <to_batch> sticky sessions from table <t>
88 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010089 */
90static int stktable_trash_oldest(struct stktable *t, int to_batch)
91{
92 struct stksess *ts;
93 struct eb32_node *eb;
94 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020095 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010096
97 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
98
99 while (batched < to_batch) {
100
101 if (unlikely(!eb)) {
102 /* we might have reached the end of the tree, typically because
103 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200104 * half. Let's loop back to the beginning of the tree now if we
105 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100106 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200107 if (looped)
108 break;
109 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100110 eb = eb32_first(&t->exps);
111 if (likely(!eb))
112 break;
113 }
114
115 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200116 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100117 eb = eb32_next(eb);
118
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200119 /* don't delete an entry which is currently referenced */
120 if (ts->ref_cnt)
121 continue;
122
Willy Tarreau86257dc2010-06-06 12:57:10 +0200123 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100124
Willy Tarreau86257dc2010-06-06 12:57:10 +0200125 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100126 if (!tick_isset(ts->expire))
127 continue;
128
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 ts->exp.key = ts->expire;
130 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100131
Willy Tarreau86257dc2010-06-06 12:57:10 +0200132 if (!eb || eb->key > ts->exp.key)
133 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100134
135 continue;
136 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreauaea940e2010-06-06 11:56:36 +0200138 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200139 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140 stksess_free(t, ts);
141 batched++;
142 }
143
144 return batched;
145}
146
147/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200148 * Allocate and initialise a new sticky session.
149 * The new sticky session is returned or NULL in case of lack of memory.
150 * Sticky sessions should only be allocated this way, and must be freed using
151 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100152 */
153struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
154{
155 struct stksess *ts;
156
157 if (unlikely(t->current == t->size)) {
158 if ( t->nopurge )
159 return NULL;
160
161 if (!stktable_trash_oldest(t, t->size >> 8))
162 return NULL;
163 }
164
Willy Tarreau393379c2010-06-06 12:11:37 +0200165 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100166 if (ts) {
167 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200168 stksess_init(t, ts);
169 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 }
171
172 return ts;
173}
174
175/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200176 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200177 * 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_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180{
181 struct ebmb_node *eb;
182
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183 if (t->type == STKTABLE_TYPE_STRING)
184 eb = ebst_lookup_len(&t->keys, key->key, key->key_len);
185 else
186 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
187
188 if (unlikely(!eb)) {
189 /* no session found */
190 return NULL;
191 }
192
Willy Tarreau86257dc2010-06-06 12:57:10 +0200193 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194}
195
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200196/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
197 * This is mainly used for situations where we want to refresh a key's usage so
198 * that it does not expire, and we want to have it created if it was not there.
199 * The stksess is returned, or NULL if it could not be created.
200 */
201struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
202{
203 struct stksess *ts;
204
205 ts = stktable_lookup_key(table, key);
206 if (likely(ts))
207 return stktable_touch(table, ts);
208
209 /* entry does not exist, initialize a new one */
210 ts = stksess_new(table, key);
211 if (likely(ts))
212 stktable_store(table, ts);
213 return ts;
214}
215
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200216/*
217 * Looks in table <t> for a sticky session with same key as <ts>.
218 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100219 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200220struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100221{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100222 struct ebmb_node *eb;
223
224 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200225 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100226 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200227 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200229 if (unlikely(!eb))
230 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200232 return ebmb_entry(eb, struct stksess, key);
233}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100234
Willy Tarreaucb183642010-06-06 17:58:34 +0200235/* Update the expiration timer for <ts> but do not touch its expiration node.
236 * The table's expiration timer is updated if set.
237 */
238struct stksess *stktable_touch(struct stktable *t, struct stksess *ts)
239{
240 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
241 if (t->expire) {
242 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
243 task_queue(t->exp_task);
244 }
245 return ts;
246}
247
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200248/* Insert new sticky session <ts> in the table. It is assumed that it does not
249 * yet exist (the caller must check this). The table's timeout is updated if it
250 * is set. <ts> is returned.
251 */
252struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
253{
254 ebmb_insert(&t->keys, &ts->key, t->key_size);
Willy Tarreaucb183642010-06-06 17:58:34 +0200255 stktable_touch(t, ts);
256 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200257 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200258 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100259}
260
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200261/* Returns a valid or initialized stksess for the specified stktable_key in the
262 * specified table, or NULL if the key was NULL, or if no entry was found nor
263 * could be created. The entry's expiration is updated.
264 */
265struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
266{
267 struct stksess *ts;
268
269 if (!key)
270 return NULL;
271
272 ts = stktable_lookup_key(table, key);
273 if (ts == NULL) {
274 /* entry does not exist, initialize a new one */
275 ts = stksess_new(table, key);
276 if (!ts)
277 return NULL;
278 stktable_store(table, ts);
279 }
280 else
281 stktable_touch(table, ts);
282 return ts;
283}
284
Emeric Brun3bd697e2010-01-04 15:23:48 +0100285/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200286 * Trash expired sticky sessions from table <t>. The next expiration date is
287 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100288 */
289static int stktable_trash_expired(struct stktable *t)
290{
291 struct stksess *ts;
292 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200293 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100294
295 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
296
297 while (1) {
298 if (unlikely(!eb)) {
299 /* we might have reached the end of the tree, typically because
300 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200301 * half. Let's loop back to the beginning of the tree now if we
302 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100303 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200304 if (looped)
305 break;
306 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100307 eb = eb32_first(&t->exps);
308 if (likely(!eb))
309 break;
310 }
311
312 if (likely(tick_is_lt(now_ms, eb->key))) {
313 /* timer not expired yet, revisit it later */
314 t->exp_next = eb->key;
315 return t->exp_next;
316 }
317
318 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200319 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100320 eb = eb32_next(eb);
321
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200322 /* don't delete an entry which is currently referenced */
323 if (ts->ref_cnt)
324 continue;
325
Willy Tarreau86257dc2010-06-06 12:57:10 +0200326 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100327
328 if (!tick_is_expired(ts->expire, now_ms)) {
329 if (!tick_isset(ts->expire))
330 continue;
331
Willy Tarreau86257dc2010-06-06 12:57:10 +0200332 ts->exp.key = ts->expire;
333 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100334
Willy Tarreau86257dc2010-06-06 12:57:10 +0200335 if (!eb || eb->key > ts->exp.key)
336 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100337 continue;
338 }
339
340 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200341 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100342 stksess_free(t, ts);
343 }
344
345 /* We have found no task to expire in any tree */
346 t->exp_next = TICK_ETERNITY;
347 return t->exp_next;
348}
349
350/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200351 * Task processing function to trash expired sticky sessions. A pointer to the
352 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100353 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200354static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100355{
356 struct stktable *t = (struct stktable *)task->context;
357
358 task->expire = stktable_trash_expired(t);
359 return task;
360}
361
Willy Tarreauaea940e2010-06-06 11:56:36 +0200362/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363int stktable_init(struct stktable *t)
364{
365 if (t->size) {
366 memset(&t->keys, 0, sizeof(t->keys));
367 memset(&t->exps, 0, sizeof(t->exps));
368
Willy Tarreau393379c2010-06-06 12:11:37 +0200369 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100370
371 t->exp_next = TICK_ETERNITY;
372 if ( t->expire ) {
373 t->exp_task = task_new();
374 t->exp_task->process = process_table_expire;
375 t->exp_task->expire = TICK_ETERNITY;
376 t->exp_task->context = (void *)t;
377 }
378 return t->pool != NULL;
379 }
380 return 1;
381}
382
383/*
384 * Configuration keywords of known table types
385 */
386struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
387 { "integer", 0, 4 },
Willy Tarreauaea940e2010-06-06 11:56:36 +0200388 { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100389
390
391/*
392 * Parse table type configuration.
393 * Returns 0 on successful parsing, else 1.
394 * <myidx> is set at next configuration <args> index.
395 */
396int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
397{
398 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
399 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
400 continue;
401
402 *key_size = stktable_types[*type].default_size;
403 (*myidx)++;
404
Willy Tarreauaea940e2010-06-06 11:56:36 +0200405 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100406 if (strcmp("len", args[*myidx]) == 0) {
407 (*myidx)++;
408 *key_size = atol(args[*myidx]);
409 if ( !*key_size )
410 break;
411 /* null terminated string needs +1 for '\0'. */
412 (*key_size)++;
413 (*myidx)++;
414 }
415 }
416 return 0;
417 }
418 return 1;
419}
420
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200421/*****************************************************************/
422/* typed pattern to typed table key functions */
423/*****************************************************************/
424
425static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
426{
427 return (void *)&pdata->integer;
428}
429
430static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
431{
432 return (void *)&pdata->ip.s_addr;
433}
434
435static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
436{
437 kdata->integer = ntohl(pdata->ip.s_addr);
438 return (void *)&kdata->integer;
439}
440
441static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
442{
443 kdata->ip.s_addr = htonl(pdata->integer);
444 return (void *)&kdata->ip.s_addr;
445}
446
447static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
448{
449 *len = pdata->str.len;
450 return (void *)pdata->str.str;
451}
452
453static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
454{
455 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
456 return NULL;
457
458 *len = strlen((const char *)kdata->buf);
459 return (void *)kdata->buf;
460}
461
462static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
463{
464 void *key;
465
466 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
467 if (!key)
468 return NULL;
469
470 *len = strlen((const char *)key);
471 return key;
472}
473
474static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
475{
476 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
477 return NULL;
478
479 return (void *)&kdata->ip.s_addr;
480}
481
482
483static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
484{
485 int i;
486
487 kdata->integer = 0;
488 for (i = 0; i < pdata->str.len; i++) {
489 uint32_t val = pdata->str.str[i] - '0';
490
491 if (val > 9)
492 break;
493
494 kdata->integer = kdata->integer * 10 + val;
495 }
496 return (void *)&kdata->integer;
497}
498
499/*****************************************************************/
500/* typed pattern to typed table key matrix: */
501/* pattern_to_key[from pattern type][to table key type] */
502/* NULL pointer used for impossible pattern casts */
503/*****************************************************************/
504
505typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
506static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
507 { k_ip2ip, k_ip2int, k_ip2str },
508 { k_int2ip, k_int2int, k_int2str },
509 { k_str2ip, k_str2int, k_str2str },
510};
511
512
513/*
514 * Process a fetch + format conversion as defined by the pattern expression <expr>
515 * on request or response considering the <dir> parameter. Returns either NULL if
516 * no key could be extracted, or a pointer to the converted result stored in
517 * static_table_key in format <table_type>.
518 */
519struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
520 struct pattern_expr *expr, unsigned long table_type)
521{
522 struct pattern *ptrn;
523
524 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
525 if (!ptrn)
526 return NULL;
527
528 static_table_key.key_len = (size_t)-1;
529 static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
530
531 if (!static_table_key.key)
532 return NULL;
533
534 return &static_table_key;
535}
536
537/*
538 * Returns 1 if pattern expression <expr> result can be converted to table key of
539 * type <table_type>, otherwise zero. Used in configuration check.
540 */
541int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
542{
543 if (table_type >= STKTABLE_TYPES)
544 return 0;
545
546 if (LIST_ISEMPTY(&expr->conv_exprs)) {
547 if (!pattern_to_key[expr->fetch->out_type][table_type])
548 return 0;
549 } else {
550 struct pattern_conv_expr *conv_expr;
551 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
552
553 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
554 return 0;
555 }
556 return 1;
557}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100558
Willy Tarreau08d5f982010-06-06 13:34:54 +0200559/* Extra data types processing */
560struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200561 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
562 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
563 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
564 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
565 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
566 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
567 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
568 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
569 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
570 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
571 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
572 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
573 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
574 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
575 [STKTABLE_DT_BYTES_OUT_RATE]= { .name = "bytes_out_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200576};
577
578/*
579 * Returns the data type number for the stktable_data_type whose name is <name>,
580 * or <0 if not found.
581 */
582int stktable_get_data_type(char *name)
583{
584 int type;
585
586 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
587 if (strcmp(name, stktable_data_types[type].name) == 0)
588 return type;
589 }
590 return -1;
591}
592
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200593/* Returns pointer to proxy containing table <name> or NULL if not found */
594struct proxy *find_stktable(const char *name)
595{
596 struct proxy *px;
597
598 for (px = proxy; px; px = px->next) {
599 if (px->table.size && strcmp(px->id, name) == 0)
600 return px;
601 }
602 return NULL;
603}