blob: fb75d3f5d0969f7e5433d1a1e925fd4434997c2e [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);
Emeric Brun85e77c72010-09-23 18:16:52 +020054 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020055 ebmb_delete(&ts->key);
56 stksess_free(t, ts);
57}
58
59/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020060 * Initialize or update the key in the sticky session <ts> present in table <t>
61 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010062 */
Willy Tarreau393379c2010-06-06 12:11:37 +020063void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010064{
65 if (t->type != STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +020066 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010067 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020068 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
69 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010070 }
71}
72
73
74/*
Willy Tarreau393379c2010-06-06 12:11:37 +020075 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
76 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010077 */
Willy Tarreau393379c2010-06-06 12:11:37 +020078static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010079{
Willy Tarreau393379c2010-06-06 12:11:37 +020080 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020081 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020082 ts->key.node.leaf_p = NULL;
83 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020084 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010085 return ts;
86}
87
88/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020089 * Trash oldest <to_batch> sticky sessions from table <t>
90 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010091 */
92static int stktable_trash_oldest(struct stktable *t, int to_batch)
93{
94 struct stksess *ts;
95 struct eb32_node *eb;
96 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020097 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010098
99 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
100
101 while (batched < to_batch) {
102
103 if (unlikely(!eb)) {
104 /* we might have reached the end of the tree, typically because
105 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200106 * half. Let's loop back to the beginning of the tree now if we
107 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100108 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200109 if (looped)
110 break;
111 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100112 eb = eb32_first(&t->exps);
113 if (likely(!eb))
114 break;
115 }
116
117 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200118 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100119 eb = eb32_next(eb);
120
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200121 /* don't delete an entry which is currently referenced */
122 if (ts->ref_cnt)
123 continue;
124
Willy Tarreau86257dc2010-06-06 12:57:10 +0200125 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100126
Willy Tarreau86257dc2010-06-06 12:57:10 +0200127 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100128 if (!tick_isset(ts->expire))
129 continue;
130
Willy Tarreau86257dc2010-06-06 12:57:10 +0200131 ts->exp.key = ts->expire;
132 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100133
Willy Tarreau86257dc2010-06-06 12:57:10 +0200134 if (!eb || eb->key > ts->exp.key)
135 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100136
137 continue;
138 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100139
Willy Tarreauaea940e2010-06-06 11:56:36 +0200140 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200141 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200142 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143 stksess_free(t, ts);
144 batched++;
145 }
146
147 return batched;
148}
149
150/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200151 * Allocate and initialise a new sticky session.
152 * The new sticky session is returned or NULL in case of lack of memory.
153 * Sticky sessions should only be allocated this way, and must be freed using
154 * stksess_free(). Increase table <t> sticky session counter.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100155 */
156struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
157{
158 struct stksess *ts;
159
160 if (unlikely(t->current == t->size)) {
161 if ( t->nopurge )
162 return NULL;
163
Emeric Brunfbce6d02010-09-23 18:10:00 +0200164 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100165 return NULL;
166 }
167
Willy Tarreau393379c2010-06-06 12:11:37 +0200168 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100169 if (ts) {
170 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200171 stksess_init(t, ts);
172 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100173 }
174
175 return ts;
176}
177
178/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200179 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200180 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100181 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200182struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183{
184 struct ebmb_node *eb;
185
Emeric Brun3bd697e2010-01-04 15:23:48 +0100186 if (t->type == STKTABLE_TYPE_STRING)
Emeric Brun485479d2010-09-23 18:02:19 +0200187 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 +0100188 else
189 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
190
191 if (unlikely(!eb)) {
192 /* no session found */
193 return NULL;
194 }
195
Willy Tarreau86257dc2010-06-06 12:57:10 +0200196 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100197}
198
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200199/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
200 * This is mainly used for situations where we want to refresh a key's usage so
201 * that it does not expire, and we want to have it created if it was not there.
202 * The stksess is returned, or NULL if it could not be created.
203 */
204struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
205{
206 struct stksess *ts;
207
208 ts = stktable_lookup_key(table, key);
209 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200210 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200211
212 /* entry does not exist, initialize a new one */
213 ts = stksess_new(table, key);
214 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200215 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200216 return ts;
217}
218
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200219/*
220 * Looks in table <t> for a sticky session with same key as <ts>.
221 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100222 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200223struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100224{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100225 struct ebmb_node *eb;
226
227 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200228 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100229 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200230 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200232 if (unlikely(!eb))
233 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100234
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200235 return ebmb_entry(eb, struct stksess, key);
236}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237
Willy Tarreaucb183642010-06-06 17:58:34 +0200238/* Update the expiration timer for <ts> but do not touch its expiration node.
239 * The table's expiration timer is updated if set.
240 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200241struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200242{
Emeric Brun85e77c72010-09-23 18:16:52 +0200243 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200244 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
245 if (t->expire) {
246 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
247 task_queue(t->exp_task);
248 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200249
250 if (t->sync_task && local) {
251 ts->upd.key = ++t->update;
252 t->localupdate = t->update;
253 eb32_delete(&ts->upd);
254 eb = eb32_insert(&t->updates, &ts->upd);
255 if (eb != &ts->upd) {
256 eb32_delete(eb);
257 eb32_insert(&t->updates, &ts->upd);
258 }
259 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
260 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200261 return ts;
262}
263
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200264/* Insert new sticky session <ts> in the table. It is assumed that it does not
265 * yet exist (the caller must check this). The table's timeout is updated if it
266 * is set. <ts> is returned.
267 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200268struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200269{
270 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200271 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200272 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200273 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200274 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100275}
276
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200277/* Returns a valid or initialized stksess for the specified stktable_key in the
278 * specified table, or NULL if the key was NULL, or if no entry was found nor
279 * could be created. The entry's expiration is updated.
280 */
281struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
282{
283 struct stksess *ts;
284
285 if (!key)
286 return NULL;
287
288 ts = stktable_lookup_key(table, key);
289 if (ts == NULL) {
290 /* entry does not exist, initialize a new one */
291 ts = stksess_new(table, key);
292 if (!ts)
293 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200294 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200295 }
296 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200297 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200298 return ts;
299}
300
Emeric Brun3bd697e2010-01-04 15:23:48 +0100301/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200302 * Trash expired sticky sessions from table <t>. The next expiration date is
303 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100304 */
305static int stktable_trash_expired(struct stktable *t)
306{
307 struct stksess *ts;
308 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200309 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100310
311 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
312
313 while (1) {
314 if (unlikely(!eb)) {
315 /* we might have reached the end of the tree, typically because
316 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200317 * half. Let's loop back to the beginning of the tree now if we
318 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100319 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200320 if (looped)
321 break;
322 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323 eb = eb32_first(&t->exps);
324 if (likely(!eb))
325 break;
326 }
327
328 if (likely(tick_is_lt(now_ms, eb->key))) {
329 /* timer not expired yet, revisit it later */
330 t->exp_next = eb->key;
331 return t->exp_next;
332 }
333
334 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200335 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100336 eb = eb32_next(eb);
337
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200338 /* don't delete an entry which is currently referenced */
339 if (ts->ref_cnt)
340 continue;
341
Willy Tarreau86257dc2010-06-06 12:57:10 +0200342 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100343
344 if (!tick_is_expired(ts->expire, now_ms)) {
345 if (!tick_isset(ts->expire))
346 continue;
347
Willy Tarreau86257dc2010-06-06 12:57:10 +0200348 ts->exp.key = ts->expire;
349 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100350
Willy Tarreau86257dc2010-06-06 12:57:10 +0200351 if (!eb || eb->key > ts->exp.key)
352 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100353 continue;
354 }
355
356 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200357 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200358 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100359 stksess_free(t, ts);
360 }
361
362 /* We have found no task to expire in any tree */
363 t->exp_next = TICK_ETERNITY;
364 return t->exp_next;
365}
366
367/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200368 * Task processing function to trash expired sticky sessions. A pointer to the
369 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100370 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200371static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100372{
373 struct stktable *t = (struct stktable *)task->context;
374
375 task->expire = stktable_trash_expired(t);
376 return task;
377}
378
Willy Tarreauaea940e2010-06-06 11:56:36 +0200379/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100380int stktable_init(struct stktable *t)
381{
382 if (t->size) {
383 memset(&t->keys, 0, sizeof(t->keys));
384 memset(&t->exps, 0, sizeof(t->exps));
385
Willy Tarreau393379c2010-06-06 12:11:37 +0200386 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100387
388 t->exp_next = TICK_ETERNITY;
389 if ( t->expire ) {
390 t->exp_task = task_new();
391 t->exp_task->process = process_table_expire;
392 t->exp_task->expire = TICK_ETERNITY;
393 t->exp_task->context = (void *)t;
394 }
395 return t->pool != NULL;
396 }
397 return 1;
398}
399
400/*
401 * Configuration keywords of known table types
402 */
Emeric Brun485479d2010-09-23 18:02:19 +0200403struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
Emeric Brun3bd697e2010-01-04 15:23:48 +0100404 { "integer", 0, 4 },
Emeric Brun485479d2010-09-23 18:02:19 +0200405 { "string", STK_F_CUSTOM_KEYSIZE, 32 },
406 { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100407
408
409/*
410 * Parse table type configuration.
411 * Returns 0 on successful parsing, else 1.
412 * <myidx> is set at next configuration <args> index.
413 */
414int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
415{
416 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
417 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
418 continue;
419
420 *key_size = stktable_types[*type].default_size;
421 (*myidx)++;
422
Willy Tarreauaea940e2010-06-06 11:56:36 +0200423 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100424 if (strcmp("len", args[*myidx]) == 0) {
425 (*myidx)++;
426 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200427 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100428 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200429 if (*type == STKTABLE_TYPE_STRING) {
430 /* null terminated string needs +1 for '\0'. */
431 (*key_size)++;
432 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100433 (*myidx)++;
434 }
435 }
436 return 0;
437 }
438 return 1;
439}
440
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200441/*****************************************************************/
442/* typed pattern to typed table key functions */
443/*****************************************************************/
444
445static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
446{
447 return (void *)&pdata->integer;
448}
449
450static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
451{
452 return (void *)&pdata->ip.s_addr;
453}
454
455static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
456{
457 kdata->integer = ntohl(pdata->ip.s_addr);
458 return (void *)&kdata->integer;
459}
460
461static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
462{
463 kdata->ip.s_addr = htonl(pdata->integer);
464 return (void *)&kdata->ip.s_addr;
465}
466
467static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
468{
469 *len = pdata->str.len;
470 return (void *)pdata->str.str;
471}
472
473static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
474{
475 if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
476 return NULL;
477
478 *len = strlen((const char *)kdata->buf);
479 return (void *)kdata->buf;
480}
481
482static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
483{
484 void *key;
485
486 key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
487 if (!key)
488 return NULL;
489
490 *len = strlen((const char *)key);
491 return key;
492}
493
494static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
495{
496 if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
497 return NULL;
498
499 return (void *)&kdata->ip.s_addr;
500}
501
502
503static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
504{
505 int i;
506
507 kdata->integer = 0;
508 for (i = 0; i < pdata->str.len; i++) {
509 uint32_t val = pdata->str.str[i] - '0';
510
511 if (val > 9)
512 break;
513
514 kdata->integer = kdata->integer * 10 + val;
515 }
516 return (void *)&kdata->integer;
517}
518
519/*****************************************************************/
520/* typed pattern to typed table key matrix: */
521/* pattern_to_key[from pattern type][to table key type] */
522/* NULL pointer used for impossible pattern casts */
523/*****************************************************************/
524
525typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
526static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
Emeric Brun485479d2010-09-23 18:02:19 +0200527/* table type: IP INTEGER STRING BINARY */
528/* pattern type: IP */ { k_ip2ip, k_ip2int, k_ip2str, NULL },
529/* INTEGER */ { k_int2ip, k_int2int, k_int2str, NULL },
530/* STRING */ { k_str2ip, k_str2int, k_str2str, k_str2str },
531/* DATA */ { NULL, NULL, NULL, k_str2str },
532/* CONSTSTRING */ { k_str2ip, k_str2int, k_str2str, k_str2str },
533/* CONSTDATA */ { NULL, NULL, NULL, k_str2str },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200534};
535
536
537/*
538 * Process a fetch + format conversion as defined by the pattern expression <expr>
539 * on request or response considering the <dir> parameter. Returns either NULL if
540 * no key could be extracted, or a pointer to the converted result stored in
541 * static_table_key in format <table_type>.
542 */
Emeric Brun485479d2010-09-23 18:02:19 +0200543struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7, int dir,
544 struct pattern_expr *expr)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200545{
546 struct pattern *ptrn;
547
548 ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
549 if (!ptrn)
550 return NULL;
551
Emeric Brun485479d2010-09-23 18:02:19 +0200552 static_table_key.key_len = t->key_size;
553 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 +0200554
555 if (!static_table_key.key)
556 return NULL;
557
Emeric Brun485479d2010-09-23 18:02:19 +0200558 if ((static_table_key.key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) {
559 /* need padding with null */
560
561 /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf)
562 cause t->key_size is necessary less than sizeof(static_table_key.data) */
563
564 if ((char *)static_table_key.key > (char *)&static_table_key.data &&
565 (char *)static_table_key.key < (char *)&static_table_key.data + sizeof(static_table_key.data)) {
566 /* key buffer is part of the static_table_key private data buffer, but is not aligned */
567
568 if (sizeof(static_table_key.data) - ((char *)static_table_key.key - (char *)&static_table_key.data) < t->key_size) {
569 /* if not remain enougth place for padding , process a realign */
570 memmove(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
571 static_table_key.key = static_table_key.data.buf;
572 }
573 }
574 else if (static_table_key.key != static_table_key.data.buf) {
575 /* key definitly not part of the static_table_key private data buffer */
576
577 memcpy(static_table_key.data.buf, static_table_key.key, static_table_key.key_len);
578 static_table_key.key = static_table_key.data.buf;
579 }
580
581 memset(static_table_key.key + static_table_key.key_len, 0, t->key_size - static_table_key.key_len);
582 }
583
584
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200585 return &static_table_key;
586}
587
588/*
589 * Returns 1 if pattern expression <expr> result can be converted to table key of
590 * type <table_type>, otherwise zero. Used in configuration check.
591 */
592int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
593{
594 if (table_type >= STKTABLE_TYPES)
595 return 0;
596
597 if (LIST_ISEMPTY(&expr->conv_exprs)) {
598 if (!pattern_to_key[expr->fetch->out_type][table_type])
599 return 0;
600 } else {
601 struct pattern_conv_expr *conv_expr;
602 conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
603
604 if (!pattern_to_key[conv_expr->conv->out_type][table_type])
605 return 0;
606 }
607 return 1;
608}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100609
Willy Tarreau08d5f982010-06-06 13:34:54 +0200610/* Extra data types processing */
611struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200612 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
613 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
614 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
615 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
616 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
617 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
618 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
619 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
620 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
621 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
622 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
623 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
624 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
625 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
626 [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 +0200627};
628
629/*
630 * Returns the data type number for the stktable_data_type whose name is <name>,
631 * or <0 if not found.
632 */
633int stktable_get_data_type(char *name)
634{
635 int type;
636
637 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
638 if (strcmp(name, stktable_data_types[type].name) == 0)
639 return type;
640 }
641 return -1;
642}
643
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200644/* Returns pointer to proxy containing table <name> or NULL if not found */
645struct proxy *find_stktable(const char *name)
646{
647 struct proxy *px;
648
649 for (px = proxy; px; px = px->next) {
650 if (px->table.size && strcmp(px->id, name) == 0)
651 return px;
652 }
653 return NULL;
654}