blob: 471424a83c36569444ec9cdf74b46e2b2df00f14 [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 Tarreaue7f3d7a2010-06-14 14:53:07 +020067 ts->ref_cnt = 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;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020082 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010083
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
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020091 * half. Let's loop back to the beginning of the tree now if we
92 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +010093 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020094 if (looped)
95 break;
96 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +010097 eb = eb32_first(&t->exps);
98 if (likely(!eb))
99 break;
100 }
101
102 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200103 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100104 eb = eb32_next(eb);
105
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200106 /* don't delete an entry which is currently referenced */
107 if (ts->ref_cnt)
108 continue;
109
Willy Tarreau86257dc2010-06-06 12:57:10 +0200110 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100111
Willy Tarreau86257dc2010-06-06 12:57:10 +0200112 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100113 if (!tick_isset(ts->expire))
114 continue;
115
Willy Tarreau86257dc2010-06-06 12:57:10 +0200116 ts->exp.key = ts->expire;
117 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100118
Willy Tarreau86257dc2010-06-06 12:57:10 +0200119 if (!eb || eb->key > ts->exp.key)
120 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100121
122 continue;
123 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100124
Willy Tarreauaea940e2010-06-06 11:56:36 +0200125 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200126 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100127 stksess_free(t, ts);
128 batched++;
129 }
130
131 return batched;
132}
133
134/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200135 * Allocate and initialise a new sticky session.
136 * The new sticky session is returned or NULL in case of lack of memory.
137 * Sticky sessions should only be allocated this way, and must be freed using
138 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100139 */
140struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
141{
142 struct stksess *ts;
143
144 if (unlikely(t->current == t->size)) {
145 if ( t->nopurge )
146 return NULL;
147
148 if (!stktable_trash_oldest(t, t->size >> 8))
149 return NULL;
150 }
151
Willy Tarreau393379c2010-06-06 12:11:37 +0200152 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 if (ts) {
154 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200155 stksess_init(t, ts);
156 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100157 }
158
159 return ts;
160}
161
162/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200163 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200164 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100165 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200166struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100167{
168 struct ebmb_node *eb;
169
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 if (t->type == STKTABLE_TYPE_STRING)
171 eb = ebst_lookup_len(&t->keys, key->key, key->key_len);
172 else
173 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
174
175 if (unlikely(!eb)) {
176 /* no session found */
177 return NULL;
178 }
179
Willy Tarreau86257dc2010-06-06 12:57:10 +0200180 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100181}
182
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200183/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
184 * This is mainly used for situations where we want to refresh a key's usage so
185 * that it does not expire, and we want to have it created if it was not there.
186 * The stksess is returned, or NULL if it could not be created.
187 */
188struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
189{
190 struct stksess *ts;
191
192 ts = stktable_lookup_key(table, key);
193 if (likely(ts))
194 return stktable_touch(table, ts);
195
196 /* entry does not exist, initialize a new one */
197 ts = stksess_new(table, key);
198 if (likely(ts))
199 stktable_store(table, ts);
200 return ts;
201}
202
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200203/*
204 * Looks in table <t> for a sticky session with same key as <ts>.
205 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100206 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200207struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100208{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100209 struct ebmb_node *eb;
210
211 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200212 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100213 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200214 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100215
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200216 if (unlikely(!eb))
217 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100218
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200219 return ebmb_entry(eb, struct stksess, key);
220}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100221
Willy Tarreaucb183642010-06-06 17:58:34 +0200222/* Update the expiration timer for <ts> but do not touch its expiration node.
223 * The table's expiration timer is updated if set.
224 */
225struct stksess *stktable_touch(struct stktable *t, struct stksess *ts)
226{
227 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
228 if (t->expire) {
229 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
230 task_queue(t->exp_task);
231 }
232 return ts;
233}
234
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200235/* Insert new sticky session <ts> in the table. It is assumed that it does not
236 * yet exist (the caller must check this). The table's timeout is updated if it
237 * is set. <ts> is returned.
238 */
239struct stksess *stktable_store(struct stktable *t, struct stksess *ts)
240{
241 ebmb_insert(&t->keys, &ts->key, t->key_size);
Willy Tarreaucb183642010-06-06 17:58:34 +0200242 stktable_touch(t, ts);
243 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200244 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200245 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100246}
247
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200248/* Returns a valid or initialized stksess for the specified stktable_key in the
249 * specified table, or NULL if the key was NULL, or if no entry was found nor
250 * could be created. The entry's expiration is updated.
251 */
252struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
253{
254 struct stksess *ts;
255
256 if (!key)
257 return NULL;
258
259 ts = stktable_lookup_key(table, key);
260 if (ts == NULL) {
261 /* entry does not exist, initialize a new one */
262 ts = stksess_new(table, key);
263 if (!ts)
264 return NULL;
265 stktable_store(table, ts);
266 }
267 else
268 stktable_touch(table, ts);
269 return ts;
270}
271
Emeric Brun3bd697e2010-01-04 15:23:48 +0100272/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200273 * Trash expired sticky sessions from table <t>. The next expiration date is
274 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100275 */
276static int stktable_trash_expired(struct stktable *t)
277{
278 struct stksess *ts;
279 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200280 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100281
282 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
283
284 while (1) {
285 if (unlikely(!eb)) {
286 /* we might have reached the end of the tree, typically because
287 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200288 * half. Let's loop back to the beginning of the tree now if we
289 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100290 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200291 if (looped)
292 break;
293 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100294 eb = eb32_first(&t->exps);
295 if (likely(!eb))
296 break;
297 }
298
299 if (likely(tick_is_lt(now_ms, eb->key))) {
300 /* timer not expired yet, revisit it later */
301 t->exp_next = eb->key;
302 return t->exp_next;
303 }
304
305 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200306 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100307 eb = eb32_next(eb);
308
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200309 /* don't delete an entry which is currently referenced */
310 if (ts->ref_cnt)
311 continue;
312
Willy Tarreau86257dc2010-06-06 12:57:10 +0200313 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314
315 if (!tick_is_expired(ts->expire, now_ms)) {
316 if (!tick_isset(ts->expire))
317 continue;
318
Willy Tarreau86257dc2010-06-06 12:57:10 +0200319 ts->exp.key = ts->expire;
320 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100321
Willy Tarreau86257dc2010-06-06 12:57:10 +0200322 if (!eb || eb->key > ts->exp.key)
323 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100324 continue;
325 }
326
327 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200328 ebmb_delete(&ts->key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100329 stksess_free(t, ts);
330 }
331
332 /* We have found no task to expire in any tree */
333 t->exp_next = TICK_ETERNITY;
334 return t->exp_next;
335}
336
337/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200338 * Task processing function to trash expired sticky sessions. A pointer to the
339 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100340 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200341static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100342{
343 struct stktable *t = (struct stktable *)task->context;
344
345 task->expire = stktable_trash_expired(t);
346 return task;
347}
348
Willy Tarreauaea940e2010-06-06 11:56:36 +0200349/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100350int stktable_init(struct stktable *t)
351{
352 if (t->size) {
353 memset(&t->keys, 0, sizeof(t->keys));
354 memset(&t->exps, 0, sizeof(t->exps));
355
Willy Tarreau393379c2010-06-06 12:11:37 +0200356 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357
358 t->exp_next = TICK_ETERNITY;
359 if ( t->expire ) {
360 t->exp_task = task_new();
361 t->exp_task->process = process_table_expire;
362 t->exp_task->expire = TICK_ETERNITY;
363 t->exp_task->context = (void *)t;
364 }
365 return t->pool != NULL;
366 }
367 return 1;
368}
369
370/*
371 * Configuration keywords of known table types
372 */
373struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
374 { "integer", 0, 4 },
Willy Tarreauaea940e2010-06-06 11:56:36 +0200375 { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100376
377
378/*
379 * Parse table type configuration.
380 * Returns 0 on successful parsing, else 1.
381 * <myidx> is set at next configuration <args> index.
382 */
383int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
384{
385 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
386 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
387 continue;
388
389 *key_size = stktable_types[*type].default_size;
390 (*myidx)++;
391
Willy Tarreauaea940e2010-06-06 11:56:36 +0200392 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100393 if (strcmp("len", args[*myidx]) == 0) {
394 (*myidx)++;
395 *key_size = atol(args[*myidx]);
396 if ( !*key_size )
397 break;
398 /* null terminated string needs +1 for '\0'. */
399 (*key_size)++;
400 (*myidx)++;
401 }
402 }
403 return 0;
404 }
405 return 1;
406}
407
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200408/*****************************************************************/
409/* typed pattern to typed table key functions */
410/*****************************************************************/
411
412static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
413{
414 return (void *)&pdata->integer;
415}
416
417static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
418{
419 return (void *)&pdata->ip.s_addr;
420}
421
422static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
423{
424 kdata->integer = ntohl(pdata->ip.s_addr);
425 return (void *)&kdata->integer;
426}
427
428static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
429{
430 kdata->ip.s_addr = htonl(pdata->integer);
431 return (void *)&kdata->ip.s_addr;
432}
433
434static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
435{
436 *len = pdata->str.len;
437 return (void *)pdata->str.str;
438}
439
440static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
441{
442 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
443 return NULL;
444
445 *len = strlen((const char *)kdata->buf);
446 return (void *)kdata->buf;
447}
448
449static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
450{
451 void *key;
452
453 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
454 if (!key)
455 return NULL;
456
457 *len = strlen((const char *)key);
458 return key;
459}
460
461static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
462{
463 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
464 return NULL;
465
466 return (void *)&kdata->ip.s_addr;
467}
468
469
470static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
471{
472 int i;
473
474 kdata->integer = 0;
475 for (i = 0; i < pdata->str.len; i++) {
476 uint32_t val = pdata->str.str[i] - '0';
477
478 if (val > 9)
479 break;
480
481 kdata->integer = kdata->integer * 10 + val;
482 }
483 return (void *)&kdata->integer;
484}
485
486/*****************************************************************/
487/* typed pattern to typed table key matrix: */
488/* pattern_to_key[from pattern type][to table key type] */
489/* NULL pointer used for impossible pattern casts */
490/*****************************************************************/
491
492typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
493static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
494 { k_ip2ip, k_ip2int, k_ip2str },
495 { k_int2ip, k_int2int, k_int2str },
496 { k_str2ip, k_str2int, k_str2str },
497};
498
499
500/*
501 * Process a fetch + format conversion as defined by the pattern expression <expr>
502 * on request or response considering the <dir> parameter. Returns either NULL if
503 * no key could be extracted, or a pointer to the converted result stored in
504 * static_table_key in format <table_type>.
505 */
506struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
507 struct pattern_expr *expr, unsigned long table_type)
508{
509 struct pattern *ptrn;
510
511 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
512 if (!ptrn)
513 return NULL;
514
515 static_table_key.key_len = (size_t)-1;
516 static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
517
518 if (!static_table_key.key)
519 return NULL;
520
521 return &static_table_key;
522}
523
524/*
525 * Returns 1 if pattern expression <expr> result can be converted to table key of
526 * type <table_type>, otherwise zero. Used in configuration check.
527 */
528int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
529{
530 if (table_type >= STKTABLE_TYPES)
531 return 0;
532
533 if (LIST_ISEMPTY(&expr->conv_exprs)) {
534 if (!pattern_to_key[expr->fetch->out_type][table_type])
535 return 0;
536 } else {
537 struct pattern_conv_expr *conv_expr;
538 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
539
540 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
541 return 0;
542 }
543 return 1;
544}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100545
Willy Tarreau08d5f982010-06-06 13:34:54 +0200546/* Extra data types processing */
547struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau13c29de2010-06-06 16:40:39 +0200548 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) },
Willy Tarreau38285c12010-06-18 16:35:43 +0200549 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .data_length = stktable_data_size(conn_cnt) },
Willy Tarreau91c43d72010-06-20 11:19:22 +0200550 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .data_length = stktable_data_size(conn_rate), .arg_type = ARG_T_DELAY },
Willy Tarreau38285c12010-06-18 16:35:43 +0200551 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .data_length = stktable_data_size(conn_cur) },
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200552 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .data_length = stktable_data_size(sess_cnt) },
Willy Tarreau91c43d72010-06-20 11:19:22 +0200553 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .data_length = stktable_data_size(sess_rate), .arg_type = ARG_T_DELAY },
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200554 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .data_length = stktable_data_size(bytes_in_cnt) },
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200555 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .data_length = stktable_data_size(bytes_in_rate), .arg_type = ARG_T_DELAY },
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200556 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .data_length = stktable_data_size(bytes_out_cnt) },
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200557 [STKTABLE_DT_BYTES_OUT_RATE]= { .name = "bytes_out_rate",.data_length = stktable_data_size(bytes_out_rate), .arg_type = ARG_T_DELAY },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200558};
559
560/*
561 * Returns the data type number for the stktable_data_type whose name is <name>,
562 * or <0 if not found.
563 */
564int stktable_get_data_type(char *name)
565{
566 int type;
567
568 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
569 if (strcmp(name, stktable_data_types[type].name) == 0)
570 return type;
571 }
572 return -1;
573}
574
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200575/* Returns pointer to proxy containing table <name> or NULL if not found */
576struct proxy *find_stktable(const char *name)
577{
578 struct proxy *px;
579
580 for (px = proxy; px; px = px->next) {
581 if (px->table.size && strcmp(px->id, name) == 0)
582 return px;
583 }
584 return NULL;
585}