blob: 5e9aa35718d18445699122c076cff0304e197d12 [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>
Emeric Brun32da3c42010-09-23 18:39:19 +020030#include <proto/peers.h>
31#include <types/global.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010032
Willy Tarreau41883e22010-06-06 17:39:30 +020033/* structure used to return a table key built from a pattern */
34struct stktable_key static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020035
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 Tarreauf6efda12010-08-03 20:34:06 +020047 * Kill an stksess (only if its ref_cnt is zero).
48 */
49void stksess_kill(struct stktable *t, struct stksess *ts)
50{
51 if (ts->ref_cnt)
52 return;
53
54 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020055 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020056 ebmb_delete(&ts->key);
57 stksess_free(t, ts);
58}
59
60/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020061 * Initialize or update the key in the sticky session <ts> present in table <t>
62 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010063 */
Willy Tarreau393379c2010-06-06 12:11:37 +020064void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010065{
66 if (t->type != STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +020067 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010068 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020069 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
70 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010071 }
72}
73
74
75/*
Willy Tarreau393379c2010-06-06 12:11:37 +020076 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
77 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010078 */
Willy Tarreau393379c2010-06-06 12:11:37 +020079static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010080{
Willy Tarreau393379c2010-06-06 12:11:37 +020081 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020082 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020083 ts->key.node.leaf_p = NULL;
84 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020085 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010086 return ts;
87}
88
89/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020090 * Trash oldest <to_batch> sticky sessions from table <t>
91 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010092 */
93static int stktable_trash_oldest(struct stktable *t, int to_batch)
94{
95 struct stksess *ts;
96 struct eb32_node *eb;
97 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020098 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010099
100 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
101
102 while (batched < to_batch) {
103
104 if (unlikely(!eb)) {
105 /* we might have reached the end of the tree, typically because
106 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200107 * half. Let's loop back to the beginning of the tree now if we
108 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100109 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200110 if (looped)
111 break;
112 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100113 eb = eb32_first(&t->exps);
114 if (likely(!eb))
115 break;
116 }
117
118 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200119 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100120 eb = eb32_next(eb);
121
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200122 /* don't delete an entry which is currently referenced */
123 if (ts->ref_cnt)
124 continue;
125
Willy Tarreau86257dc2010-06-06 12:57:10 +0200126 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100127
Willy Tarreau86257dc2010-06-06 12:57:10 +0200128 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100129 if (!tick_isset(ts->expire))
130 continue;
131
Willy Tarreau86257dc2010-06-06 12:57:10 +0200132 ts->exp.key = ts->expire;
133 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100134
Willy Tarreau86257dc2010-06-06 12:57:10 +0200135 if (!eb || eb->key > ts->exp.key)
136 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
138 continue;
139 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140
Willy Tarreauaea940e2010-06-06 11:56:36 +0200141 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200142 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200143 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100144 stksess_free(t, ts);
145 batched++;
146 }
147
148 return batched;
149}
150
151/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200152 * Allocate and initialise a new sticky session.
153 * The new sticky session is returned or NULL in case of lack of memory.
154 * Sticky sessions should only be allocated this way, and must be freed using
155 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100156 */
157struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
158{
159 struct stksess *ts;
160
161 if (unlikely(t->current == t->size)) {
162 if ( t->nopurge )
163 return NULL;
164
Emeric Brunfbce6d02010-09-23 18:10:00 +0200165 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100166 return NULL;
167 }
168
Willy Tarreau393379c2010-06-06 12:11:37 +0200169 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 if (ts) {
171 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200172 stksess_init(t, ts);
173 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100174 }
175
176 return ts;
177}
178
179/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200180 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200181 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100182 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200183struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100184{
185 struct ebmb_node *eb;
186
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 if (t->type == STKTABLE_TYPE_STRING)
Emeric Brun485479d2010-09-23 18:02:19 +0200188 eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189 else
190 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
191
192 if (unlikely(!eb)) {
193 /* no session found */
194 return NULL;
195 }
196
Willy Tarreau86257dc2010-06-06 12:57:10 +0200197 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100198}
199
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200200/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
201 * This is mainly used for situations where we want to refresh a key's usage so
202 * that it does not expire, and we want to have it created if it was not there.
203 * The stksess is returned, or NULL if it could not be created.
204 */
205struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
206{
207 struct stksess *ts;
208
209 ts = stktable_lookup_key(table, key);
210 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200211 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200212
213 /* entry does not exist, initialize a new one */
214 ts = stksess_new(table, key);
215 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200216 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200217 return ts;
218}
219
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200220/*
221 * Looks in table <t> for a sticky session with same key as <ts>.
222 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100223 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200224struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100225{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100226 struct ebmb_node *eb;
227
228 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200229 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100230 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200231 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100232
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200233 if (unlikely(!eb))
234 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200236 return ebmb_entry(eb, struct stksess, key);
237}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238
Willy Tarreaucb183642010-06-06 17:58:34 +0200239/* Update the expiration timer for <ts> but do not touch its expiration node.
240 * The table's expiration timer is updated if set.
241 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200242struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200243{
Emeric Brun85e77c72010-09-23 18:16:52 +0200244 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200245 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
246 if (t->expire) {
247 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
248 task_queue(t->exp_task);
249 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200250
251 if (t->sync_task && local) {
252 ts->upd.key = ++t->update;
253 t->localupdate = t->update;
254 eb32_delete(&ts->upd);
255 eb = eb32_insert(&t->updates, &ts->upd);
256 if (eb != &ts->upd) {
257 eb32_delete(eb);
258 eb32_insert(&t->updates, &ts->upd);
259 }
260 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
261 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200262 return ts;
263}
264
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200265/* Insert new sticky session <ts> in the table. It is assumed that it does not
266 * yet exist (the caller must check this). The table's timeout is updated if it
267 * is set. <ts> is returned.
268 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200269struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200270{
271 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200272 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200273 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200274 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200275 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100276}
277
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200278/* Returns a valid or initialized stksess for the specified stktable_key in the
279 * specified table, or NULL if the key was NULL, or if no entry was found nor
280 * could be created. The entry's expiration is updated.
281 */
282struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
283{
284 struct stksess *ts;
285
286 if (!key)
287 return NULL;
288
289 ts = stktable_lookup_key(table, key);
290 if (ts == NULL) {
291 /* entry does not exist, initialize a new one */
292 ts = stksess_new(table, key);
293 if (!ts)
294 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200295 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200296 }
297 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200298 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200299 return ts;
300}
301
Emeric Brun3bd697e2010-01-04 15:23:48 +0100302/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200303 * Trash expired sticky sessions from table <t>. The next expiration date is
304 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100305 */
306static int stktable_trash_expired(struct stktable *t)
307{
308 struct stksess *ts;
309 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200310 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100311
312 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
313
314 while (1) {
315 if (unlikely(!eb)) {
316 /* we might have reached the end of the tree, typically because
317 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200318 * half. Let's loop back to the beginning of the tree now if we
319 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100320 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200321 if (looped)
322 break;
323 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100324 eb = eb32_first(&t->exps);
325 if (likely(!eb))
326 break;
327 }
328
329 if (likely(tick_is_lt(now_ms, eb->key))) {
330 /* timer not expired yet, revisit it later */
331 t->exp_next = eb->key;
332 return t->exp_next;
333 }
334
335 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200336 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100337 eb = eb32_next(eb);
338
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200339 /* don't delete an entry which is currently referenced */
340 if (ts->ref_cnt)
341 continue;
342
Willy Tarreau86257dc2010-06-06 12:57:10 +0200343 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100344
345 if (!tick_is_expired(ts->expire, now_ms)) {
346 if (!tick_isset(ts->expire))
347 continue;
348
Willy Tarreau86257dc2010-06-06 12:57:10 +0200349 ts->exp.key = ts->expire;
350 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100351
Willy Tarreau86257dc2010-06-06 12:57:10 +0200352 if (!eb || eb->key > ts->exp.key)
353 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100354 continue;
355 }
356
357 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200358 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200359 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100360 stksess_free(t, ts);
361 }
362
363 /* We have found no task to expire in any tree */
364 t->exp_next = TICK_ETERNITY;
365 return t->exp_next;
366}
367
368/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200369 * Task processing function to trash expired sticky sessions. A pointer to the
370 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100371 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200372static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100373{
374 struct stktable *t = (struct stktable *)task->context;
375
376 task->expire = stktable_trash_expired(t);
377 return task;
378}
379
Willy Tarreauaea940e2010-06-06 11:56:36 +0200380/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100381int stktable_init(struct stktable *t)
382{
383 if (t->size) {
384 memset(&t->keys, 0, sizeof(t->keys));
385 memset(&t->exps, 0, sizeof(t->exps));
386
Willy Tarreau393379c2010-06-06 12:11:37 +0200387 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100388
389 t->exp_next = TICK_ETERNITY;
390 if ( t->expire ) {
391 t->exp_task = task_new();
392 t->exp_task->process = process_table_expire;
393 t->exp_task->expire = TICK_ETERNITY;
394 t->exp_task->context = (void *)t;
395 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200396 if (t->peers.p && t->peers.p->peers_fe) {
397 peers_register_table(t->peers.p, t);
398 }
399
Emeric Brun3bd697e2010-01-04 15:23:48 +0100400 return t->pool != NULL;
401 }
402 return 1;
403}
404
405/*
406 * Configuration keywords of known table types
407 */
Emeric Brun485479d2010-09-23 18:02:19 +0200408struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
David du Colombier4f92d322011-03-24 11:09:31 +0100409 { "ipv6", 0, 16 },
Emeric Brun3bd697e2010-01-04 15:23:48 +0100410 { "integer", 0, 4 },
Emeric Brun485479d2010-09-23 18:02:19 +0200411 { "string", STK_F_CUSTOM_KEYSIZE, 32 },
David du Colombier4f92d322011-03-24 11:09:31 +0100412 { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100413
414
415/*
416 * Parse table type configuration.
417 * Returns 0 on successful parsing, else 1.
418 * <myidx> is set at next configuration <args> index.
419 */
420int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
421{
422 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
423 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
424 continue;
425
426 *key_size = stktable_types[*type].default_size;
427 (*myidx)++;
428
Willy Tarreauaea940e2010-06-06 11:56:36 +0200429 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100430 if (strcmp("len", args[*myidx]) == 0) {
431 (*myidx)++;
432 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200433 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100434 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200435 if (*type == STKTABLE_TYPE_STRING) {
436 /* null terminated string needs +1 for '\0'. */
437 (*key_size)++;
438 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100439 (*myidx)++;
440 }
441 }
442 return 0;
443 }
444 return 1;
445}
446
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200447/*****************************************************************/
448/* typed pattern to typed table key functions */
449/*****************************************************************/
450
451static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
452{
453 return (void *)&pdata->integer;
454}
455
456static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
457{
458 return (void *)&pdata->ip.s_addr;
459}
460
David du Colombier4f92d322011-03-24 11:09:31 +0100461static void *k_ip2ipv6(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
462{
463 v4tov6(&pdata->ipv6, &pdata->ip);
464 return (void *)&pdata->ipv6.s6_addr;
465}
466
467static void *k_ipv62ipv6(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
468{
469 return (void *)&pdata->ipv6.s6_addr;
470}
471
472/*
473static void *k_ipv62ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
474{
475 v6tov4(&pdata->ip, &pdata->ipv6);
476 return (void *)&pdata->ip.s_addr;
477}
478*/
479
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200480static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
481{
482 kdata->integer = ntohl(pdata->ip.s_addr);
483 return (void *)&kdata->integer;
484}
485
486static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
487{
488 kdata->ip.s_addr = htonl(pdata->integer);
489 return (void *)&kdata->ip.s_addr;
490}
491
492static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
493{
494 *len = pdata->str.len;
495 return (void *)pdata->str.str;
496}
497
498static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
499{
500 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
501 return NULL;
502
503 *len = strlen((const char *)kdata->buf);
504 return (void *)kdata->buf;
505}
506
David du Colombier4f92d322011-03-24 11:09:31 +0100507static void *k_ipv62str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
508{
509 if (!inet_ntop(AF_INET6, &pdata->ipv6, kdata->buf, sizeof(kdata->buf)))
510 return NULL;
511
512 *len = strlen((const char *)kdata->buf);
513 return (void *)kdata->buf;
514}
515
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200516static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
517{
518 void *key;
519
520 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
521 if (!key)
522 return NULL;
523
524 *len = strlen((const char *)key);
525 return key;
526}
527
528static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
529{
530 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
531 return NULL;
532
533 return (void *)&kdata->ip.s_addr;
534}
535
David du Colombier4f92d322011-03-24 11:09:31 +0100536static void *k_str2ipv6(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
537{
538 if (!inet_pton(AF_INET6, pdata->str.str, &kdata->ipv6))
539 return NULL;
540
541 return (void *)&kdata->ipv6.s6_addr;
542}
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200543
544static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
545{
546 int i;
547
548 kdata->integer = 0;
549 for (i = 0; i < pdata->str.len; i++) {
550 uint32_t val = pdata->str.str[i] - '0';
551
552 if (val > 9)
553 break;
554
555 kdata->integer = kdata->integer * 10 + val;
556 }
557 return (void *)&kdata->integer;
558}
559
560/*****************************************************************/
561/* typed pattern to typed table key matrix: */
562/* pattern_to_key[from pattern type][to table key type] */
563/* NULL pointer used for impossible pattern casts */
564/*****************************************************************/
565
David du Colombier4f92d322011-03-24 11:09:31 +0100566/*
567 * Conversions from IPv6 to IPv4 are available, but we haven't
568 * added them to the table since they doesn't seem sufficely
569 * relevant and could cause confusion in configuration.
570 */
571
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200572typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
573static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
David du Colombier4f92d322011-03-24 11:09:31 +0100574/* table type: IP IPV6 INTEGER STRING BINARY */
575/* pattern type: IP */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
576/* IPV6 */ { NULL, k_ipv62ipv6, NULL, k_ipv62str, NULL },
577/* INTEGER */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
578/* STRING */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
579/* DATA */ { NULL, NULL, NULL, NULL, k_str2str },
580/* CONSTSTRING */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
581
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200582};
583
584
585/*
586 * Process a fetch + format conversion as defined by the pattern expression <expr>
587 * on request or response considering the <dir> parameter. Returns either NULL if
588 * no key could be extracted, or a pointer to the converted result stored in
589 * static_table_key in format <table_type>.
590 */
Emeric Brun485479d2010-09-23 18:02:19 +0200591struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7, int dir,
592 struct pattern_expr *expr)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200593{
594 struct pattern *ptrn;
595
596 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
597 if (!ptrn)
598 return NULL;
599
Emeric Brun485479d2010-09-23 18:02:19 +0200600 static_table_key.key_len = t->key_size;
601 static_table_key.key = pattern_to_key[ptrn->type][t->type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200602
603 if (!static_table_key.key)
604 return NULL;
605
Willy Tarreaua0e58612011-01-04 14:50:49 +0100606 if ((static_table_key.key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200607 /* need padding with null */
608
609 /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf)
610 cause t->key_size is necessary less than sizeof(static_table_key.data) */
611
612 if ((char *)static_table_key.key > (char *)&static_table_key.data &&
613 (char *)static_table_key.key < (char *)&static_table_key.data + sizeof(static_table_key.data)) {
614 /* key buffer is part of the static_table_key private data buffer, but is not aligned */
615
616 if (sizeof(static_table_key.data) - ((char *)static_table_key.key - (char *)&static_table_key.data) < t->key_size) {
617 /* if not remain enougth place for padding , process a realign */
618 memmove(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
619 static_table_key.key = static_table_key.data.buf;
620 }
621 }
622 else if (static_table_key.key != static_table_key.data.buf) {
623 /* key definitly not part of the static_table_key private data buffer */
624
625 memcpy(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
626 static_table_key.key = static_table_key.data.buf;
627 }
628
629 memset(static_table_key.key + static_table_key.key_len, 0, t->key_size - static_table_key.key_len);
630 }
631
632
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200633 return &static_table_key;
634}
635
636/*
637 * Returns 1 if pattern expression <expr> result can be converted to table key of
638 * type <table_type>, otherwise zero. Used in configuration check.
639 */
640int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
641{
642 if (table_type >= STKTABLE_TYPES)
643 return 0;
644
645 if (LIST_ISEMPTY(&expr->conv_exprs)) {
646 if (!pattern_to_key[expr->fetch->out_type][table_type])
647 return 0;
648 } else {
649 struct pattern_conv_expr *conv_expr;
650 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
651
652 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
653 return 0;
654 }
655 return 1;
656}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100657
Willy Tarreau08d5f982010-06-06 13:34:54 +0200658/* Extra data types processing */
659struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200660 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
661 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
662 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
663 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
664 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
665 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
666 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
667 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
668 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
669 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
670 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
671 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
672 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
673 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
674 [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 +0200675};
676
677/*
678 * Returns the data type number for the stktable_data_type whose name is <name>,
679 * or <0 if not found.
680 */
681int stktable_get_data_type(char *name)
682{
683 int type;
684
685 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
686 if (strcmp(name, stktable_data_types[type].name) == 0)
687 return type;
688 }
689 return -1;
690}
691
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200692/* Returns pointer to proxy containing table <name> or NULL if not found */
693struct proxy *find_stktable(const char *name)
694{
695 struct proxy *px;
696
697 for (px = proxy; px; px = px->next) {
698 if (px->table.size && strcmp(px->id, name) == 0)
699 return px;
700 }
701 return NULL;
702}