blob: c77b4029ef895476b0a8102a24a013aa5e2a0ed2 [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
Vincent Bernat496ef862016-11-17 15:42:40 +0100170 ts = pool_alloc2(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100171 if (ts) {
172 t->current++;
Vincent Bernat496ef862016-11-17 15:42:40 +0100173 ts += t->data_size;
Willy Tarreau393379c2010-06-06 12:11:37 +0200174 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200175 if (key)
176 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100177 }
178
179 return ts;
180}
181
182/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200183 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200184 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100185 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200186struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187{
188 struct ebmb_node *eb;
189
Emeric Brun3bd697e2010-01-04 15:23:48 +0100190 if (t->type == STKTABLE_TYPE_STRING)
Emeric Brun485479d2010-09-23 18:02:19 +0200191 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 +0100192 else
193 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
194
195 if (unlikely(!eb)) {
196 /* no session found */
197 return NULL;
198 }
199
Willy Tarreau86257dc2010-06-06 12:57:10 +0200200 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100201}
202
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200203/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
204 * This is mainly used for situations where we want to refresh a key's usage so
205 * that it does not expire, and we want to have it created if it was not there.
206 * The stksess is returned, or NULL if it could not be created.
207 */
208struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
209{
210 struct stksess *ts;
211
212 ts = stktable_lookup_key(table, key);
213 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200214 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200215
216 /* entry does not exist, initialize a new one */
217 ts = stksess_new(table, key);
218 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200219 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200220 return ts;
221}
222
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200223/*
224 * Looks in table <t> for a sticky session with same key as <ts>.
225 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100226 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200227struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100229 struct ebmb_node *eb;
230
231 if (t->type == STKTABLE_TYPE_STRING)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200232 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100233 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200234 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200236 if (unlikely(!eb))
237 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200239 return ebmb_entry(eb, struct stksess, key);
240}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100241
Willy Tarreaucb183642010-06-06 17:58:34 +0200242/* Update the expiration timer for <ts> but do not touch its expiration node.
243 * The table's expiration timer is updated if set.
244 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200245struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200246{
Emeric Brun85e77c72010-09-23 18:16:52 +0200247 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200248 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
249 if (t->expire) {
250 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
251 task_queue(t->exp_task);
252 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200253
254 if (t->sync_task && local) {
255 ts->upd.key = ++t->update;
256 t->localupdate = t->update;
257 eb32_delete(&ts->upd);
258 eb = eb32_insert(&t->updates, &ts->upd);
259 if (eb != &ts->upd) {
260 eb32_delete(eb);
261 eb32_insert(&t->updates, &ts->upd);
262 }
263 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
264 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200265 return ts;
266}
267
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200268/* Insert new sticky session <ts> in the table. It is assumed that it does not
269 * yet exist (the caller must check this). The table's timeout is updated if it
270 * is set. <ts> is returned.
271 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200272struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200273{
274 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200275 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200276 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200277 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200278 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100279}
280
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200281/* Returns a valid or initialized stksess for the specified stktable_key in the
282 * specified table, or NULL if the key was NULL, or if no entry was found nor
283 * could be created. The entry's expiration is updated.
284 */
285struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
286{
287 struct stksess *ts;
288
289 if (!key)
290 return NULL;
291
292 ts = stktable_lookup_key(table, key);
293 if (ts == NULL) {
294 /* entry does not exist, initialize a new one */
295 ts = stksess_new(table, key);
296 if (!ts)
297 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200298 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200299 }
300 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200301 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200302 return ts;
303}
304
Emeric Brun3bd697e2010-01-04 15:23:48 +0100305/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200306 * Trash expired sticky sessions from table <t>. The next expiration date is
307 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100308 */
309static int stktable_trash_expired(struct stktable *t)
310{
311 struct stksess *ts;
312 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200313 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314
315 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
316
317 while (1) {
318 if (unlikely(!eb)) {
319 /* we might have reached the end of the tree, typically because
320 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200321 * half. Let's loop back to the beginning of the tree now if we
322 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200324 if (looped)
325 break;
326 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100327 eb = eb32_first(&t->exps);
328 if (likely(!eb))
329 break;
330 }
331
332 if (likely(tick_is_lt(now_ms, eb->key))) {
333 /* timer not expired yet, revisit it later */
334 t->exp_next = eb->key;
335 return t->exp_next;
336 }
337
338 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200339 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100340 eb = eb32_next(eb);
341
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200342 /* don't delete an entry which is currently referenced */
343 if (ts->ref_cnt)
344 continue;
345
Willy Tarreau86257dc2010-06-06 12:57:10 +0200346 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100347
348 if (!tick_is_expired(ts->expire, now_ms)) {
349 if (!tick_isset(ts->expire))
350 continue;
351
Willy Tarreau86257dc2010-06-06 12:57:10 +0200352 ts->exp.key = ts->expire;
353 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100354
Willy Tarreau86257dc2010-06-06 12:57:10 +0200355 if (!eb || eb->key > ts->exp.key)
356 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357 continue;
358 }
359
360 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200361 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200362 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363 stksess_free(t, ts);
364 }
365
366 /* We have found no task to expire in any tree */
367 t->exp_next = TICK_ETERNITY;
368 return t->exp_next;
369}
370
371/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200372 * Task processing function to trash expired sticky sessions. A pointer to the
373 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100374 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200375static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100376{
377 struct stktable *t = (struct stktable *)task->context;
378
379 task->expire = stktable_trash_expired(t);
380 return task;
381}
382
Willy Tarreauaea940e2010-06-06 11:56:36 +0200383/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100384int stktable_init(struct stktable *t)
385{
386 if (t->size) {
387 memset(&t->keys, 0, sizeof(t->keys));
388 memset(&t->exps, 0, sizeof(t->exps));
Emeric Bruna320fd12015-12-16 15:28:12 +0100389 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
Willy Tarreau393379c2010-06-06 12:11:37 +0200391 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100392
393 t->exp_next = TICK_ETERNITY;
394 if ( t->expire ) {
395 t->exp_task = task_new();
396 t->exp_task->process = process_table_expire;
397 t->exp_task->expire = TICK_ETERNITY;
398 t->exp_task->context = (void *)t;
399 }
Willy Tarreauf8a9d2e2015-05-01 18:29:57 +0200400 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200401 peers_register_table(t->peers.p, t);
402 }
403
Emeric Brun3bd697e2010-01-04 15:23:48 +0100404 return t->pool != NULL;
405 }
406 return 1;
407}
408
409/*
410 * Configuration keywords of known table types
411 */
Emeric Brun485479d2010-09-23 18:02:19 +0200412struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
David du Colombier4f92d322011-03-24 11:09:31 +0100413 { "ipv6", 0, 16 },
Emeric Brun3bd697e2010-01-04 15:23:48 +0100414 { "integer", 0, 4 },
Emeric Brun485479d2010-09-23 18:02:19 +0200415 { "string", STK_F_CUSTOM_KEYSIZE, 32 },
David du Colombier4f92d322011-03-24 11:09:31 +0100416 { "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
Emeric Brun3bd697e2010-01-04 15:23:48 +0100417
418
419/*
420 * Parse table type configuration.
421 * Returns 0 on successful parsing, else 1.
422 * <myidx> is set at next configuration <args> index.
423 */
424int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
425{
426 for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
427 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
428 continue;
429
430 *key_size = stktable_types[*type].default_size;
431 (*myidx)++;
432
Willy Tarreauaea940e2010-06-06 11:56:36 +0200433 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100434 if (strcmp("len", args[*myidx]) == 0) {
435 (*myidx)++;
436 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200437 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100438 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200439 if (*type == STKTABLE_TYPE_STRING) {
440 /* null terminated string needs +1 for '\0'. */
441 (*key_size)++;
442 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100443 (*myidx)++;
444 }
445 }
446 return 0;
447 }
448 return 1;
449}
450
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200451/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200452/* typed sample to typed table key functions */
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200453/*****************************************************************/
454
Willy Tarreau342acb42012-04-23 22:03:39 +0200455static void *k_int2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200456{
Willy Tarreau342acb42012-04-23 22:03:39 +0200457 return (void *)&smp->data.uint;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200458}
459
Willy Tarreau342acb42012-04-23 22:03:39 +0200460static void *k_ip2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200461{
Willy Tarreau803685f2013-12-02 23:17:27 +0100462 if (smp->type == SMP_T_IPV6) {
463 v6tov4(&kdata->ip, &smp->data.ipv6);
464 return (void *)&kdata->ip.s_addr;
465 }
466 else {
467 return (void *)&smp->data.ipv4.s_addr;
468 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200469}
470
Willy Tarreau342acb42012-04-23 22:03:39 +0200471static void *k_ip2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len)
David du Colombier4f92d322011-03-24 11:09:31 +0100472{
Willy Tarreau803685f2013-12-02 23:17:27 +0100473 if (smp->type == SMP_T_IPV6) {
474 return (void *)&smp->data.ipv6.s6_addr;
475 }
476 else {
477 v4tov6(&kdata->ipv6, &smp->data.ipv4);
478 return (void *)&kdata->ipv6.s6_addr;
479 }
David du Colombier4f92d322011-03-24 11:09:31 +0100480}
David du Colombier4f92d322011-03-24 11:09:31 +0100481
Willy Tarreau342acb42012-04-23 22:03:39 +0200482static void *k_ip2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200483{
Willy Tarreau803685f2013-12-02 23:17:27 +0100484 if (smp->type == SMP_T_IPV6) {
485 if (!v6tov4(&kdata->ip, &smp->data.ipv6))
486 return NULL;
487 kdata->integer = ntohl(kdata->ip.s_addr);
488 }
489 else {
490 kdata->integer = ntohl(smp->data.ipv4.s_addr);
491 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200492 return (void *)&kdata->integer;
493}
494
Willy Tarreau342acb42012-04-23 22:03:39 +0200495static void *k_int2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200496{
Willy Tarreau342acb42012-04-23 22:03:39 +0200497 kdata->ip.s_addr = htonl(smp->data.uint);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200498 return (void *)&kdata->ip.s_addr;
499}
500
Willy Tarreau342acb42012-04-23 22:03:39 +0200501static void *k_str2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200502{
Willy Tarreau342acb42012-04-23 22:03:39 +0200503 *len = smp->data.str.len;
504 return (void *)smp->data.str.str;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200505}
506
Willy Tarreau342acb42012-04-23 22:03:39 +0200507static void *k_ip2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200508{
Willy Tarreau803685f2013-12-02 23:17:27 +0100509 if (smp->type == SMP_T_IPV6) {
510 if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, *len))
511 return NULL;
512 }
513 else {
514 if (!inet_ntop(AF_INET, &smp->data.ipv4, kdata->buf, *len))
515 return NULL;
516 }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200517
518 *len = strlen((const char *)kdata->buf);
519 return (void *)kdata->buf;
520}
521
Emeric Brun8ac33d92012-10-17 13:36:06 +0200522static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
523{
524 unsigned char c;
525 int ptr = 0;
Willy Tarreauf22180f2012-12-09 11:08:14 +0100526 int max = *len;
527 int size = 0;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200528
Willy Tarreauf22180f2012-12-09 11:08:14 +0100529 while (ptr < smp->data.str.len && size <= max - 2) {
Emeric Brun8ac33d92012-10-17 13:36:06 +0200530 c = smp->data.str.str[ptr++];
Willy Tarreauf22180f2012-12-09 11:08:14 +0100531 kdata->buf[size++] = hextab[(c >> 4) & 0xF];
532 kdata->buf[size++] = hextab[c & 0xF];
Emeric Brun8ac33d92012-10-17 13:36:06 +0200533 }
Willy Tarreauf22180f2012-12-09 11:08:14 +0100534 *len = size;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200535 return (void *)kdata->buf;
536}
537
Willy Tarreau342acb42012-04-23 22:03:39 +0200538static void *k_int2str(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200539{
540 void *key;
541
Willy Tarreauf22180f2012-12-09 11:08:14 +0100542 key = (void *)ultoa_r(smp->data.uint, kdata->buf, *len);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200543 if (!key)
544 return NULL;
545
546 *len = strlen((const char *)key);
547 return key;
548}
549
Willy Tarreau342acb42012-04-23 22:03:39 +0200550static void *k_str2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200551{
Willy Tarreau342acb42012-04-23 22:03:39 +0200552 if (!buf2ip(smp->data.str.str, smp->data.str.len, &kdata->ip))
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200553 return NULL;
554
555 return (void *)&kdata->ip.s_addr;
556}
557
Willy Tarreau342acb42012-04-23 22:03:39 +0200558static void *k_str2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len)
David du Colombier4f92d322011-03-24 11:09:31 +0100559{
Willy Tarreau342acb42012-04-23 22:03:39 +0200560 if (!inet_pton(AF_INET6, smp->data.str.str, &kdata->ipv6))
David du Colombier4f92d322011-03-24 11:09:31 +0100561 return NULL;
562
563 return (void *)&kdata->ipv6.s6_addr;
564}
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200565
Willy Tarreau342acb42012-04-23 22:03:39 +0200566static void *k_str2int(struct sample *smp, union stktable_key_data *kdata, size_t *len)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200567{
568 int i;
569
570 kdata->integer = 0;
Willy Tarreau342acb42012-04-23 22:03:39 +0200571 for (i = 0; i < smp->data.str.len; i++) {
572 uint32_t val = smp->data.str.str[i] - '0';
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200573
574 if (val > 9)
575 break;
576
577 kdata->integer = kdata->integer * 10 + val;
578 }
579 return (void *)&kdata->integer;
580}
581
582/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200583/* typed sample to typed table key matrix: */
584/* sample_to_key[from sample type][to table key type] */
585/* NULL pointer used for impossible sample casts */
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200586/*****************************************************************/
587
Willy Tarreau12785782012-04-27 21:37:17 +0200588typedef void *(*sample_to_key_fct)(struct sample *smp, union stktable_key_data *kdata, size_t *len);
589static sample_to_key_fct sample_to_key[SMP_TYPES][STKTABLE_TYPES] = {
David du Colombier4f92d322011-03-24 11:09:31 +0100590/* table type: IP IPV6 INTEGER STRING BINARY */
Willy Tarreau422aa072012-04-20 20:49:27 +0200591/* patt. type: BOOL */ { NULL, NULL, k_int2int, k_int2str, NULL },
592/* UINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
593/* SINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL },
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100594/* ADDR */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200595/* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau803685f2013-12-02 23:17:27 +0100596/* IPV6 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200597/* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str },
Emeric Brun8ac33d92012-10-17 13:36:06 +0200598/* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200599};
600
601
602/*
Willy Tarreau12785782012-04-27 21:37:17 +0200603 * Process a fetch + format conversion as defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200604 * on request or response considering the <opt> parameter. Returns either NULL if
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200605 * no key could be extracted, or a pointer to the converted result stored in
Willy Tarreaud0083942014-06-25 16:20:53 +0200606 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
607 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreauf94735e2014-07-30 08:56:35 +0200608 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
609 * without SMP_OPT_FINAL). The output will be usable like this :
610 *
611 * return MAY_CHANGE FINAL Meaning for the sample
612 * NULL 0 * Not present and will never be (eg: header)
613 * NULL 1 0 Not present or unstable, could change (eg: req_len)
614 * NULL 1 1 Not present, will not change anymore
615 * smp 0 * Present and will not change (eg: header)
616 * smp 1 0 not possible
617 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200618 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200619struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7,
Willy Tarreaud0083942014-06-25 16:20:53 +0200620 unsigned int opt, struct sample_expr *expr, struct sample *smp)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200621{
Willy Tarreaud0083942014-06-25 16:20:53 +0200622 if (smp)
623 memset(smp, 0, sizeof(*smp));
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200624
Willy Tarreaud0083942014-06-25 16:20:53 +0200625 smp = sample_process(px, l4, l7, opt, expr, smp);
Willy Tarreau342acb42012-04-23 22:03:39 +0200626 if (!smp)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200627 return NULL;
628
Willy Tarreau67ff7e02013-12-05 02:19:58 +0100629 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
630 return NULL; /* we can only use stable samples */
631
Willy Tarreau12785782012-04-27 21:37:17 +0200632 if (!sample_to_key[smp->type][t->type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200633 return NULL;
634
Willy Tarreau07115412012-10-29 21:56:59 +0100635 static_table_key->key_len = t->key_size;
636 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 +0200637
Willy Tarreau07115412012-10-29 21:56:59 +0100638 if (!static_table_key->key)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200639 return NULL;
640
Willy Tarreau07115412012-10-29 21:56:59 +0100641 if (static_table_key->key_len == 0)
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200642 return NULL;
643
Willy Tarreau07115412012-10-29 21:56:59 +0100644 if ((static_table_key->key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200645 /* need padding with null */
646
647 /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf)
648 cause t->key_size is necessary less than sizeof(static_table_key.data) */
649
Willy Tarreau07115412012-10-29 21:56:59 +0100650 if ((char *)static_table_key->key > (char *)&static_table_key->data &&
651 (char *)static_table_key->key < (char *)&static_table_key->data + global.tune.bufsize) {
Emeric Brun485479d2010-09-23 18:02:19 +0200652 /* key buffer is part of the static_table_key private data buffer, but is not aligned */
653
Willy Tarreau07115412012-10-29 21:56:59 +0100654 if (global.tune.bufsize - ((char *)static_table_key->key - (char *)&static_table_key->data) < t->key_size) {
655 /* if not remain enough place for padding , process a realign */
656 memmove(static_table_key->data.buf, static_table_key->key, static_table_key->key_len);
657 static_table_key->key = static_table_key->data.buf;
Emeric Brun485479d2010-09-23 18:02:19 +0200658 }
659 }
Willy Tarreau07115412012-10-29 21:56:59 +0100660 else if (static_table_key->key != static_table_key->data.buf) {
Emeric Brun485479d2010-09-23 18:02:19 +0200661 /* key definitly not part of the static_table_key private data buffer */
662
Willy Tarreau07115412012-10-29 21:56:59 +0100663 memcpy(static_table_key->data.buf, static_table_key->key, static_table_key->key_len);
664 static_table_key->key = static_table_key->data.buf;
Emeric Brun485479d2010-09-23 18:02:19 +0200665 }
666
Willy Tarreau07115412012-10-29 21:56:59 +0100667 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 +0200668 }
669
Willy Tarreau07115412012-10-29 21:56:59 +0100670 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200671}
672
673/*
Willy Tarreau12785782012-04-27 21:37:17 +0200674 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200675 * type <table_type>, otherwise zero. Used in configuration check.
676 */
Willy Tarreau12785782012-04-27 21:37:17 +0200677int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200678{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100679 int out_type;
680
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200681 if (table_type >= STKTABLE_TYPES)
682 return 0;
683
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100684 out_type = smp_expr_output_type(expr);
685 if (!sample_to_key[out_type][table_type])
686 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200687
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200688 return 1;
689}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100690
Willy Tarreau08d5f982010-06-06 13:34:54 +0200691/* Extra data types processing */
692struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200693 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
694 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200695 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200696 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
697 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
698 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
699 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
700 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
701 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
702 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
703 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
704 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
705 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
706 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
707 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
708 [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 +0200709};
710
711/*
712 * Returns the data type number for the stktable_data_type whose name is <name>,
713 * or <0 if not found.
714 */
715int stktable_get_data_type(char *name)
716{
717 int type;
718
719 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
720 if (strcmp(name, stktable_data_types[type].name) == 0)
721 return type;
722 }
723 return -1;
724}
725
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200726/* Returns pointer to proxy containing table <name> or NULL if not found */
727struct proxy *find_stktable(const char *name)
728{
729 struct proxy *px;
Willy Tarreau991610d2014-03-15 08:03:57 +0100730 struct ebpt_node *node;
731
732 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
733 px = container_of(node, struct proxy, conf.by_name);
734
735 if (strcmp(px->id, name) != 0)
736 break;
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200737
Willy Tarreau991610d2014-03-15 08:03:57 +0100738 if (px->table.size)
Willy Tarreau4a0347a2010-06-18 17:26:50 +0200739 return px;
740 }
741 return NULL;
742}