blob: a708d3c53386423d000f5dd51c0e217153401b59 [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
Emeric Brun3bd697e2010-01-04 15:23:48 +010025#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026#include <proto/sample.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010027#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 Tarreau12785782012-04-27 21:37:17 +020033/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010034struct 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 */
Willy Tarreau3a925c12013-09-04 17:54:01 +020093int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +010094{
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
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200155 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
156 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100157 */
158struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
159{
160 struct stksess *ts;
161
162 if (unlikely(t->current == t->size)) {
163 if ( t->nopurge )
164 return NULL;
165
Emeric Brunfbce6d02010-09-23 18:10:00 +0200166 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100167 return NULL;
168 }
169
Willy Tarreau393379c2010-06-06 12:11:37 +0200170 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100171 if (ts) {
172 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200173 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200174 if (key)
175 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100176 }
177
178 return ts;
179}
180
181/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200182 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200183 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100184 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200185struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100186{
187 struct ebmb_node *eb;
188
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189 if (t->type == STKTABLE_TYPE_STRING)
Emeric Brun485479d2010-09-23 18:02:19 +0200190 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 +0100191 else
192 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
193
194 if (unlikely(!eb)) {
195 /* no session found */
196 return NULL;
197 }
198
Willy Tarreau86257dc2010-06-06 12:57:10 +0200199 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100200}
201
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200202/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
203 * This is mainly used for situations where we want to refresh a key's usage so
204 * that it does not expire, and we want to have it created if it was not there.
205 * The stksess is returned, or NULL if it could not be created.
206 */
207struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
208{
209 struct stksess *ts;
210
211 ts = stktable_lookup_key(table, key);
212 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200213 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200214
215 /* entry does not exist, initialize a new one */
216 ts = stksess_new(table, key);
217 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200218 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200219 return ts;
220}
221
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200222/*
223 * Looks in table <t> for a sticky session with same key as <ts>.
224 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100225 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200226struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100227{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228 struct ebmb_node *eb;
229
230 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200231 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100232 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200233 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100234
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200235 if (unlikely(!eb))
236 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200238 return ebmb_entry(eb, struct stksess, key);
239}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100240
Willy Tarreaucb183642010-06-06 17:58:34 +0200241/* Update the expiration timer for <ts> but do not touch its expiration node.
242 * The table's expiration timer is updated if set.
243 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200244struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200245{
Emeric Brun85e77c72010-09-23 18:16:52 +0200246 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200247 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
248 if (t->expire) {
249 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
250 task_queue(t->exp_task);
251 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200252
253 if (t->sync_task && local) {
254 ts->upd.key = ++t->update;
255 t->localupdate = t->update;
256 eb32_delete(&ts->upd);
257 eb = eb32_insert(&t->updates, &ts->upd);
258 if (eb != &ts->upd) {
259 eb32_delete(eb);
260 eb32_insert(&t->updates, &ts->upd);
261 }
262 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
263 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200264 return ts;
265}
266
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200267/* Insert new sticky session <ts> in the table. It is assumed that it does not
268 * yet exist (the caller must check this). The table's timeout is updated if it
269 * is set. <ts> is returned.
270 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200271struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200272{
273 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200274 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200275 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200276 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200277 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100278}
279
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200280/* Returns a valid or initialized stksess for the specified stktable_key in the
281 * specified table, or NULL if the key was NULL, or if no entry was found nor
282 * could be created. The entry's expiration is updated.
283 */
284struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
285{
286 struct stksess *ts;
287
288 if (!key)
289 return NULL;
290
291 ts = stktable_lookup_key(table, key);
292 if (ts == NULL) {
293 /* entry does not exist, initialize a new one */
294 ts = stksess_new(table, key);
295 if (!ts)
296 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200297 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200298 }
299 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200300 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200301 return ts;
302}
303
Emeric Brun3bd697e2010-01-04 15:23:48 +0100304/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200305 * Trash expired sticky sessions from table <t>. The next expiration date is
306 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100307 */
308static int stktable_trash_expired(struct stktable *t)
309{
310 struct stksess *ts;
311 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200312 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100313
314 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
315
316 while (1) {
317 if (unlikely(!eb)) {
318 /* we might have reached the end of the tree, typically because
319 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200320 * half. Let's loop back to the beginning of the tree now if we
321 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100322 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200323 if (looped)
324 break;
325 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100326 eb = eb32_first(&t->exps);
327 if (likely(!eb))
328 break;
329 }
330
331 if (likely(tick_is_lt(now_ms, eb->key))) {
332 /* timer not expired yet, revisit it later */
333 t->exp_next = eb->key;
334 return t->exp_next;
335 }
336
337 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200338 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100339 eb = eb32_next(eb);
340
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200341 /* don't delete an entry which is currently referenced */
342 if (ts->ref_cnt)
343 continue;
344
Willy Tarreau86257dc2010-06-06 12:57:10 +0200345 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100346
347 if (!tick_is_expired(ts->expire, now_ms)) {
348 if (!tick_isset(ts->expire))
349 continue;
350
Willy Tarreau86257dc2010-06-06 12:57:10 +0200351 ts->exp.key = ts->expire;
352 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100353
Willy Tarreau86257dc2010-06-06 12:57:10 +0200354 if (!eb || eb->key > ts->exp.key)
355 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100356 continue;
357 }
358
359 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200360 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200361 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100362 stksess_free(t, ts);
363 }
364
365 /* We have found no task to expire in any tree */
366 t->exp_next = TICK_ETERNITY;
367 return t->exp_next;
368}
369
370/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200371 * Task processing function to trash expired sticky sessions. A pointer to the
372 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100373 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200374static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100375{
376 struct stktable *t = (struct stktable *)task->context;
377
378 task->expire = stktable_trash_expired(t);
379 return task;
380}
381
Willy Tarreauaea940e2010-06-06 11:56:36 +0200382/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383int stktable_init(struct stktable *t)
384{
385 if (t->size) {
386 memset(&t->keys, 0, sizeof(t->keys));
387 memset(&t->exps, 0, sizeof(t->exps));
388
Willy Tarreau393379c2010-06-06 12:11:37 +0200389 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
391 t->exp_next = TICK_ETERNITY;
392 if ( t->expire ) {
393 t->exp_task = task_new();
394 t->exp_task->process = process_table_expire;
395 t->exp_task->expire = TICK_ETERNITY;
396 t->exp_task->context = (void *)t;
397 }
Emeric Brun32da3c42010-09-23 18:39:19 +0200398 if (t->peers.p && t->peers.p->peers_fe) {
399 peers_register_table(t->peers.p, t);
400 }
401
Emeric Brun3bd697e2010-01-04 15:23:48 +0100402 return t->pool != NULL;
403 }
404 return 1;
405}
406
407/*
408 * Configuration keywords of known table types
409 */
Emeric Brun485479d2010-09-23 18:02:19 +0200410struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
David du Colombier4f92d322011-03-24 11:09:31 +0100411 { "ipv6", 0, 16 },
Emeric Brun3bd697e2010-01-04 15:23:48 +0100412 { "integer", 0, 4 },
Emeric Brun485479d2010-09-23 18:02:19 +0200413 { "string", STK_F_CUSTOM_KEYSIZE, 32 },
David du Colombier4f92d322011-03-24 11:09:31 +0100414 { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100415
416
417/*
418 * Parse table type configuration.
419 * Returns 0 on successful parsing, else 1.
420 * <myidx> is set at next configuration <args> index.
421 */
422int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
423{
424 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
425 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
426 continue;
427
428 *key_size = stktable_types[*type].default_size;
429 (*myidx)++;
430
Willy Tarreauaea940e2010-06-06 11:56:36 +0200431 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100432 if (strcmp("len", args[*myidx]) == 0) {
433 (*myidx)++;
434 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200435 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100436 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200437 if (*type == STKTABLE_TYPE_STRING) {
438 /* null terminated string needs +1 for '\0'. */
439 (*key_size)++;
440 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100441 (*myidx)++;
442 }
443 }
444 return 0;
445 }
446 return 1;
447}
448
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200449/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200450/* typed sample to typed table key functions */
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200451/*****************************************************************/
452
Willy Tarreau342acb42012-04-23 22:03:39 +0200453static void *k_int2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200454{
Willy Tarreau342acb42012-04-23 22:03:39 +0200455 return (void *)&smp->data.uint;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200456}
457
Willy Tarreau342acb42012-04-23 22:03:39 +0200458static void *k_ip2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200459{
Willy Tarreau803685f2013-12-02 23:17:27 +0100460 if (smp->type == SMP_T_IPV6) {
461 v6tov4(&kdata->ip, &smp->data.ipv6);
462 return (void *)&kdata->ip.s_addr;
463 }
464 else {
465 return (void *)&smp->data.ipv4.s_addr;
466 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200467}
468
Willy Tarreau342acb42012-04-23 22:03:39 +0200469static void *k_ip2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len)
David du Colombier4f92d322011-03-24 11:09:31 +0100470{
Willy Tarreau803685f2013-12-02 23:17:27 +0100471 if (smp->type == SMP_T_IPV6) {
472 return (void *)&smp->data.ipv6.s6_addr;
473 }
474 else {
475 v4tov6(&kdata->ipv6, &smp->data.ipv4);
476 return (void *)&kdata->ipv6.s6_addr;
477 }
David du Colombier4f92d322011-03-24 11:09:31 +0100478}
David du Colombier4f92d322011-03-24 11:09:31 +0100479
Willy Tarreau342acb42012-04-23 22:03:39 +0200480static void *k_ip2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200481{
Willy Tarreau803685f2013-12-02 23:17:27 +0100482 if (smp->type == SMP_T_IPV6) {
483 if (!v6tov4(&kdata->ip, &smp->data.ipv6))
484 return NULL;
485 kdata->integer = ntohl(kdata->ip.s_addr);
486 }
487 else {
488 kdata->integer = ntohl(smp->data.ipv4.s_addr);
489 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200490 return (void *)&kdata->integer;
491}
492
Willy Tarreau342acb42012-04-23 22:03:39 +0200493static void *k_int2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200494{
Willy Tarreau342acb42012-04-23 22:03:39 +0200495 kdata->ip.s_addr = htonl(smp->data.uint);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200496 return (void *)&kdata->ip.s_addr;
497}
498
Willy Tarreau342acb42012-04-23 22:03:39 +0200499static void *k_str2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200500{
Willy Tarreau342acb42012-04-23 22:03:39 +0200501 *len = smp->data.str.len;
502 return (void *)smp->data.str.str;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200503}
504
Willy Tarreau342acb42012-04-23 22:03:39 +0200505static void *k_ip2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200506{
Willy Tarreau803685f2013-12-02 23:17:27 +0100507 if (smp->type == SMP_T_IPV6) {
508 if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, *len))
509 return NULL;
510 }
511 else {
512 if (!inet_ntop(AF_INET, &smp->data.ipv4, kdata->buf, *len))
513 return NULL;
514 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200515
516 *len = strlen((const char *)kdata->buf);
517 return (void *)kdata->buf;
518}
519
Emeric Brun8ac33d92012-10-17 13:36:06 +0200520static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
521{
522 unsigned char c;
523 int ptr = 0;
Willy Tarreauf22180f2012-12-09 11:08:14 +0100524 int max = *len;
525 int size = 0;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200526
Willy Tarreauf22180f2012-12-09 11:08:14 +0100527 while (ptr < smp->data.str.len && size <= max - 2) {
Emeric Brun8ac33d92012-10-17 13:36:06 +0200528 c = smp->data.str.str[ptr++];
Willy Tarreauf22180f2012-12-09 11:08:14 +0100529 kdata->buf[size++] = hextab[(c >> 4) & 0xF];
530 kdata->buf[size++] = hextab[c & 0xF];
Emeric Brun8ac33d92012-10-17 13:36:06 +0200531 }
Willy Tarreauf22180f2012-12-09 11:08:14 +0100532 *len = size;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200533 return (void *)kdata->buf;
534}
535
Willy Tarreau342acb42012-04-23 22:03:39 +0200536static void *k_int2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200537{
538 void *key;
539
Willy Tarreauf22180f2012-12-09 11:08:14 +0100540 key = (void *)ultoa_r(smp->data.uint, kdata->buf, *len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200541 if (!key)
542 return NULL;
543
544 *len = strlen((const char *)key);
545 return key;
546}
547
Willy Tarreau342acb42012-04-23 22:03:39 +0200548static void *k_str2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200549{
Willy Tarreau342acb42012-04-23 22:03:39 +0200550 if (!buf2ip(smp->data.str.str, smp->data.str.len, &kdata->ip))
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200551 return NULL;
552
553 return (void *)&kdata->ip.s_addr;
554}
555
Willy Tarreau342acb42012-04-23 22:03:39 +0200556static void *k_str2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len)
David du Colombier4f92d322011-03-24 11:09:31 +0100557{
Willy Tarreau342acb42012-04-23 22:03:39 +0200558 if (!inet_pton(AF_INET6, smp->data.str.str, &kdata->ipv6))
David du Colombier4f92d322011-03-24 11:09:31 +0100559 return NULL;
560
561 return (void *)&kdata->ipv6.s6_addr;
562}
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200563
Willy Tarreau342acb42012-04-23 22:03:39 +0200564static void *k_str2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200565{
566 int i;
567
568 kdata->integer = 0;
Willy Tarreau342acb42012-04-23 22:03:39 +0200569 for (i = 0; i < smp->data.str.len; i++) {
570 uint32_t val = smp->data.str.str[i] - '0';
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200571
572 if (val > 9)
573 break;
574
575 kdata->integer = kdata->integer * 10 + val;
576 }
577 return (void *)&kdata->integer;
578}
579
580/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200581/* typed sample to typed table key matrix: */
582/* sample_to_key[from sample type][to table key type] */
583/* NULL pointer used for impossible sample casts */
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200584/*****************************************************************/
585
Willy Tarreau12785782012-04-27 21:37:17 +0200586typedef void *(*sample_to_key_fct)(struct sample *smp, union stktable_key_data *kdata, size_t *len);
587static sample_to_key_fct sample_to_key[SMP_TYPES][STKTABLE_TYPES] = {
David du Colombier4f92d322011-03-24 11:09:31 +0100588/* table type: IP IPV6 INTEGER STRING BINARY */
Willy Tarreau422aa072012-04-20 20:49:27 +0200589/* patt. type: BOOL */ { NULL, NULL, k_int2int, k_int2str, NULL },
590/* UINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
591/* SINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100592/* ADDR */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200593/* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau803685f2013-12-02 23:17:27 +0100594/* IPV6 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200595/* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
Emeric Brun8ac33d92012-10-17 13:36:06 +0200596/* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200597};
598
599
600/*
Willy Tarreau12785782012-04-27 21:37:17 +0200601 * Process a fetch + format conversion as defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200602 * on request or response considering the <opt> parameter. Returns either NULL if
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200603 * no key could be extracted, or a pointer to the converted result stored in
Willy Tarreaud0083942014-06-25 16:20:53 +0200604 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
605 * and its flags will be initialized so that the caller gets a copy of the input
606 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present).
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200607 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200608struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7,
Willy Tarreaud0083942014-06-25 16:20:53 +0200609 unsigned int opt, struct sample_expr *expr, struct sample *smp)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200610{
Willy Tarreaud0083942014-06-25 16:20:53 +0200611 if (smp)
612 memset(smp, 0, sizeof(*smp));
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200613
Willy Tarreaud0083942014-06-25 16:20:53 +0200614 smp = sample_process(px, l4, l7, opt, expr, smp);
Willy Tarreau342acb42012-04-23 22:03:39 +0200615 if (!smp)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200616 return NULL;
617
Willy Tarreau67ff7e02013-12-05 02:19:58 +0100618 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
619 return NULL; /* we can only use stable samples */
620
Willy Tarreau12785782012-04-27 21:37:17 +0200621 if (!sample_to_key[smp->type][t->type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200622 return NULL;
623
Willy Tarreau07115412012-10-29 21:56:59 +0100624 static_table_key->key_len = t->key_size;
625 static_table_key->key = sample_to_key[smp->type][t->type](smp, &static_table_key->data, &static_table_key->key_len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200626
Willy Tarreau07115412012-10-29 21:56:59 +0100627 if (!static_table_key->key)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200628 return NULL;
629
Willy Tarreau07115412012-10-29 21:56:59 +0100630 if (static_table_key->key_len == 0)
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200631 return NULL;
632
Willy Tarreau07115412012-10-29 21:56:59 +0100633 if ((static_table_key->key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200634 /* need padding with null */
635
636 /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf)
637 cause t->key_size is necessary less than sizeof(static_table_key.data) */
638
Willy Tarreau07115412012-10-29 21:56:59 +0100639 if ((char *)static_table_key->key > (char *)&static_table_key->data &&
640 (char *)static_table_key->key < (char *)&static_table_key->data + global.tune.bufsize) {
Emeric Brun485479d2010-09-23 18:02:19 +0200641 /* key buffer is part of the static_table_key private data buffer, but is not aligned */
642
Willy Tarreau07115412012-10-29 21:56:59 +0100643 if (global.tune.bufsize - ((char *)static_table_key->key - (char *)&static_table_key->data) < t->key_size) {
644 /* if not remain enough place for padding , process a realign */
645 memmove(static_table_key->data.buf, static_table_key->key, static_table_key->key_len);
646 static_table_key->key = static_table_key->data.buf;
Emeric Brun485479d2010-09-23 18:02:19 +0200647 }
648 }
Willy Tarreau07115412012-10-29 21:56:59 +0100649 else if (static_table_key->key != static_table_key->data.buf) {
Emeric Brun485479d2010-09-23 18:02:19 +0200650 /* key definitly not part of the static_table_key private data buffer */
651
Willy Tarreau07115412012-10-29 21:56:59 +0100652 memcpy(static_table_key->data.buf, static_table_key->key, static_table_key->key_len);
653 static_table_key->key = static_table_key->data.buf;
Emeric Brun485479d2010-09-23 18:02:19 +0200654 }
655
Willy Tarreau07115412012-10-29 21:56:59 +0100656 memset(static_table_key->key + static_table_key->key_len, 0, t->key_size - static_table_key->key_len);
Emeric Brun485479d2010-09-23 18:02:19 +0200657 }
658
Willy Tarreau07115412012-10-29 21:56:59 +0100659 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200660}
661
662/*
Willy Tarreau12785782012-04-27 21:37:17 +0200663 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200664 * type <table_type>, otherwise zero. Used in configuration check.
665 */
Willy Tarreau12785782012-04-27 21:37:17 +0200666int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200667{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100668 int out_type;
669
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200670 if (table_type >= STKTABLE_TYPES)
671 return 0;
672
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100673 out_type = smp_expr_output_type(expr);
674 if (!sample_to_key[out_type][table_type])
675 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200676
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200677 return 1;
678}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100679
Willy Tarreau08d5f982010-06-06 13:34:54 +0200680/* Extra data types processing */
681struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200682 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
683 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200684 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200685 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
686 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
687 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
688 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
689 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
690 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
691 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
692 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
693 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
694 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
695 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
696 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
697 [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 +0200698};
699
700/*
701 * Returns the data type number for the stktable_data_type whose name is <name>,
702 * or <0 if not found.
703 */
704int stktable_get_data_type(char *name)
705{
706 int type;
707
708 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
709 if (strcmp(name, stktable_data_types[type].name) == 0)
710 return type;
711 }
712 return -1;
713}
714
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200715/* Returns pointer to proxy containing table <name> or NULL if not found */
716struct proxy *find_stktable(const char *name)
717{
718 struct proxy *px;
Willy Tarreau991610d2014-03-15 08:03:57 +0100719 struct ebpt_node *node;
720
721 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
722 px = container_of(node, struct proxy, conf.by_name);
723
724 if (strcmp(px->id, name) != 0)
725 break;
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200726
Willy Tarreau991610d2014-03-15 08:03:57 +0100727 if (px->table.size)
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200728 return px;
729 }
730 return NULL;
731}