Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Stick tables management functions. |
| 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 6 | * |
| 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 Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 25 | #include <proto/pattern.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 26 | #include <proto/proxy.h> |
| 27 | #include <proto/session.h> |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 28 | #include <proto/stick_table.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 29 | #include <proto/task.h> |
| 30 | |
| 31 | |
Willy Tarreau | 41883e2 | 2010-06-06 17:39:30 +0200 | [diff] [blame] | 32 | /* structure used to return a table key built from a pattern */ |
| 33 | struct stktable_key static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 34 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 35 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 36 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 37 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 38 | */ |
| 39 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 40 | { |
| 41 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 42 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 46 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 47 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 48 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 49 | void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 50 | { |
| 51 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 52 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 53 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 54 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 55 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 61 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 62 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 63 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 64 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 65 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 66 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 67 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 68 | ts->key.node.leaf_p = NULL; |
| 69 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 70 | return ts; |
| 71 | } |
| 72 | |
| 73 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 74 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 75 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 76 | */ |
| 77 | static int stktable_trash_oldest(struct stktable *t, int to_batch) |
| 78 | { |
| 79 | struct stksess *ts; |
| 80 | struct eb32_node *eb; |
| 81 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 82 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 83 | |
| 84 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 85 | |
| 86 | while (batched < to_batch) { |
| 87 | |
| 88 | if (unlikely(!eb)) { |
| 89 | /* we might have reached the end of the tree, typically because |
| 90 | * <now_ms> is in the first half and we're first scanning the last |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 91 | * half. Let's loop back to the beginning of the tree now if we |
| 92 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 93 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 94 | if (looped) |
| 95 | break; |
| 96 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 97 | eb = eb32_first(&t->exps); |
| 98 | if (likely(!eb)) |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 103 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 104 | eb = eb32_next(eb); |
| 105 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 106 | /* don't delete an entry which is currently referenced */ |
| 107 | if (ts->ref_cnt) |
| 108 | continue; |
| 109 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 110 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 111 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 112 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 113 | if (!tick_isset(ts->expire)) |
| 114 | continue; |
| 115 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 116 | ts->exp.key = ts->expire; |
| 117 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 118 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 119 | if (!eb || eb->key > ts->exp.key) |
| 120 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 121 | |
| 122 | continue; |
| 123 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 125 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 126 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 127 | stksess_free(t, ts); |
| 128 | batched++; |
| 129 | } |
| 130 | |
| 131 | return batched; |
| 132 | } |
| 133 | |
| 134 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 135 | * Allocate and initialise a new sticky session. |
| 136 | * The new sticky session is returned or NULL in case of lack of memory. |
| 137 | * Sticky sessions should only be allocated this way, and must be freed using |
| 138 | * stksess_free(). Increase table <t> sticky session counter. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 139 | */ |
| 140 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 141 | { |
| 142 | struct stksess *ts; |
| 143 | |
| 144 | if (unlikely(t->current == t->size)) { |
| 145 | if ( t->nopurge ) |
| 146 | return NULL; |
| 147 | |
| 148 | if (!stktable_trash_oldest(t, t->size >> 8)) |
| 149 | return NULL; |
| 150 | } |
| 151 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 152 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 153 | if (ts) { |
| 154 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 155 | stksess_init(t, ts); |
| 156 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return ts; |
| 160 | } |
| 161 | |
| 162 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 163 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 164 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 165 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 166 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 167 | { |
| 168 | struct ebmb_node *eb; |
| 169 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 170 | if (t->type == STKTABLE_TYPE_STRING) |
| 171 | eb = ebst_lookup_len(&t->keys, key->key, key->key_len); |
| 172 | else |
| 173 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 174 | |
| 175 | if (unlikely(!eb)) { |
| 176 | /* no session found */ |
| 177 | return NULL; |
| 178 | } |
| 179 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 180 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 185 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 186 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 187 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 188 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 189 | struct ebmb_node *eb; |
| 190 | |
| 191 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 192 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 193 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 194 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 195 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 196 | if (unlikely(!eb)) |
| 197 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 198 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 199 | return ebmb_entry(eb, struct stksess, key); |
| 200 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 201 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 202 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 203 | * The table's expiration timer is updated if set. |
| 204 | */ |
| 205 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts) |
| 206 | { |
| 207 | ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 208 | if (t->expire) { |
| 209 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 210 | task_queue(t->exp_task); |
| 211 | } |
| 212 | return ts; |
| 213 | } |
| 214 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 215 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 216 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 217 | * is set. <ts> is returned. |
| 218 | */ |
| 219 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts) |
| 220 | { |
| 221 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 222 | stktable_touch(t, ts); |
| 223 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 224 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 225 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 228 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 229 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 230 | * could be created. The entry's expiration is updated. |
| 231 | */ |
| 232 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 233 | { |
| 234 | struct stksess *ts; |
| 235 | |
| 236 | if (!key) |
| 237 | return NULL; |
| 238 | |
| 239 | ts = stktable_lookup_key(table, key); |
| 240 | if (ts == NULL) { |
| 241 | /* entry does not exist, initialize a new one */ |
| 242 | ts = stksess_new(table, key); |
| 243 | if (!ts) |
| 244 | return NULL; |
| 245 | stktable_store(table, ts); |
| 246 | } |
| 247 | else |
| 248 | stktable_touch(table, ts); |
| 249 | return ts; |
| 250 | } |
| 251 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 252 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 253 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 254 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 255 | */ |
| 256 | static int stktable_trash_expired(struct stktable *t) |
| 257 | { |
| 258 | struct stksess *ts; |
| 259 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 260 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 261 | |
| 262 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 263 | |
| 264 | while (1) { |
| 265 | if (unlikely(!eb)) { |
| 266 | /* we might have reached the end of the tree, typically because |
| 267 | * <now_ms> is in the first half and we're first scanning the last |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 268 | * half. Let's loop back to the beginning of the tree now if we |
| 269 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 270 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 271 | if (looped) |
| 272 | break; |
| 273 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 274 | eb = eb32_first(&t->exps); |
| 275 | if (likely(!eb)) |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 280 | /* timer not expired yet, revisit it later */ |
| 281 | t->exp_next = eb->key; |
| 282 | return t->exp_next; |
| 283 | } |
| 284 | |
| 285 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 286 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 287 | eb = eb32_next(eb); |
| 288 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 289 | /* don't delete an entry which is currently referenced */ |
| 290 | if (ts->ref_cnt) |
| 291 | continue; |
| 292 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 293 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 294 | |
| 295 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 296 | if (!tick_isset(ts->expire)) |
| 297 | continue; |
| 298 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 299 | ts->exp.key = ts->expire; |
| 300 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 301 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 302 | if (!eb || eb->key > ts->exp.key) |
| 303 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 304 | continue; |
| 305 | } |
| 306 | |
| 307 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 308 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 309 | stksess_free(t, ts); |
| 310 | } |
| 311 | |
| 312 | /* We have found no task to expire in any tree */ |
| 313 | t->exp_next = TICK_ETERNITY; |
| 314 | return t->exp_next; |
| 315 | } |
| 316 | |
| 317 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 318 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 319 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 320 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 321 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 322 | { |
| 323 | struct stktable *t = (struct stktable *)task->context; |
| 324 | |
| 325 | task->expire = stktable_trash_expired(t); |
| 326 | return task; |
| 327 | } |
| 328 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 329 | /* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 330 | int stktable_init(struct stktable *t) |
| 331 | { |
| 332 | if (t->size) { |
| 333 | memset(&t->keys, 0, sizeof(t->keys)); |
| 334 | memset(&t->exps, 0, sizeof(t->exps)); |
| 335 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 336 | t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 337 | |
| 338 | t->exp_next = TICK_ETERNITY; |
| 339 | if ( t->expire ) { |
| 340 | t->exp_task = task_new(); |
| 341 | t->exp_task->process = process_table_expire; |
| 342 | t->exp_task->expire = TICK_ETERNITY; |
| 343 | t->exp_task->context = (void *)t; |
| 344 | } |
| 345 | return t->pool != NULL; |
| 346 | } |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Configuration keywords of known table types |
| 352 | */ |
| 353 | struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } , |
| 354 | { "integer", 0, 4 }, |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 355 | { "string", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 356 | |
| 357 | |
| 358 | /* |
| 359 | * Parse table type configuration. |
| 360 | * Returns 0 on successful parsing, else 1. |
| 361 | * <myidx> is set at next configuration <args> index. |
| 362 | */ |
| 363 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 364 | { |
| 365 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 366 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 367 | continue; |
| 368 | |
| 369 | *key_size = stktable_types[*type].default_size; |
| 370 | (*myidx)++; |
| 371 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 372 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 373 | if (strcmp("len", args[*myidx]) == 0) { |
| 374 | (*myidx)++; |
| 375 | *key_size = atol(args[*myidx]); |
| 376 | if ( !*key_size ) |
| 377 | break; |
| 378 | /* null terminated string needs +1 for '\0'. */ |
| 379 | (*key_size)++; |
| 380 | (*myidx)++; |
| 381 | } |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | return 1; |
| 386 | } |
| 387 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 388 | /*****************************************************************/ |
| 389 | /* typed pattern to typed table key functions */ |
| 390 | /*****************************************************************/ |
| 391 | |
| 392 | static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 393 | { |
| 394 | return (void *)&pdata->integer; |
| 395 | } |
| 396 | |
| 397 | static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 398 | { |
| 399 | return (void *)&pdata->ip.s_addr; |
| 400 | } |
| 401 | |
| 402 | static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 403 | { |
| 404 | kdata->integer = ntohl(pdata->ip.s_addr); |
| 405 | return (void *)&kdata->integer; |
| 406 | } |
| 407 | |
| 408 | static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 409 | { |
| 410 | kdata->ip.s_addr = htonl(pdata->integer); |
| 411 | return (void *)&kdata->ip.s_addr; |
| 412 | } |
| 413 | |
| 414 | static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 415 | { |
| 416 | *len = pdata->str.len; |
| 417 | return (void *)pdata->str.str; |
| 418 | } |
| 419 | |
| 420 | static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 421 | { |
| 422 | if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf))) |
| 423 | return NULL; |
| 424 | |
| 425 | *len = strlen((const char *)kdata->buf); |
| 426 | return (void *)kdata->buf; |
| 427 | } |
| 428 | |
| 429 | static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 430 | { |
| 431 | void *key; |
| 432 | |
| 433 | key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf)); |
| 434 | if (!key) |
| 435 | return NULL; |
| 436 | |
| 437 | *len = strlen((const char *)key); |
| 438 | return key; |
| 439 | } |
| 440 | |
| 441 | static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 442 | { |
| 443 | if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip)) |
| 444 | return NULL; |
| 445 | |
| 446 | return (void *)&kdata->ip.s_addr; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 451 | { |
| 452 | int i; |
| 453 | |
| 454 | kdata->integer = 0; |
| 455 | for (i = 0; i < pdata->str.len; i++) { |
| 456 | uint32_t val = pdata->str.str[i] - '0'; |
| 457 | |
| 458 | if (val > 9) |
| 459 | break; |
| 460 | |
| 461 | kdata->integer = kdata->integer * 10 + val; |
| 462 | } |
| 463 | return (void *)&kdata->integer; |
| 464 | } |
| 465 | |
| 466 | /*****************************************************************/ |
| 467 | /* typed pattern to typed table key matrix: */ |
| 468 | /* pattern_to_key[from pattern type][to table key type] */ |
| 469 | /* NULL pointer used for impossible pattern casts */ |
| 470 | /*****************************************************************/ |
| 471 | |
| 472 | typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len); |
| 473 | static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = { |
| 474 | { k_ip2ip, k_ip2int, k_ip2str }, |
| 475 | { k_int2ip, k_int2int, k_int2str }, |
| 476 | { k_str2ip, k_str2int, k_str2str }, |
| 477 | }; |
| 478 | |
| 479 | |
| 480 | /* |
| 481 | * Process a fetch + format conversion as defined by the pattern expression <expr> |
| 482 | * on request or response considering the <dir> parameter. Returns either NULL if |
| 483 | * no key could be extracted, or a pointer to the converted result stored in |
| 484 | * static_table_key in format <table_type>. |
| 485 | */ |
| 486 | struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir, |
| 487 | struct pattern_expr *expr, unsigned long table_type) |
| 488 | { |
| 489 | struct pattern *ptrn; |
| 490 | |
| 491 | ptrn = pattern_process(px, l4, l7, dir, expr, NULL); |
| 492 | if (!ptrn) |
| 493 | return NULL; |
| 494 | |
| 495 | static_table_key.key_len = (size_t)-1; |
| 496 | static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len); |
| 497 | |
| 498 | if (!static_table_key.key) |
| 499 | return NULL; |
| 500 | |
| 501 | return &static_table_key; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Returns 1 if pattern expression <expr> result can be converted to table key of |
| 506 | * type <table_type>, otherwise zero. Used in configuration check. |
| 507 | */ |
| 508 | int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type) |
| 509 | { |
| 510 | if (table_type >= STKTABLE_TYPES) |
| 511 | return 0; |
| 512 | |
| 513 | if (LIST_ISEMPTY(&expr->conv_exprs)) { |
| 514 | if (!pattern_to_key[expr->fetch->out_type][table_type]) |
| 515 | return 0; |
| 516 | } else { |
| 517 | struct pattern_conv_expr *conv_expr; |
| 518 | conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list); |
| 519 | |
| 520 | if (!pattern_to_key[conv_expr->conv->out_type][table_type]) |
| 521 | return 0; |
| 522 | } |
| 523 | return 1; |
| 524 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 525 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 526 | /* Extra data types processing */ |
| 527 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 8fb12c4 | 2010-06-18 20:16:39 +0200 | [diff] [blame] | 528 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .data_length = stktable_data_size(conn_cnt) }, |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 529 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) }, |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | /* |
| 533 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 534 | * or <0 if not found. |
| 535 | */ |
| 536 | int stktable_get_data_type(char *name) |
| 537 | { |
| 538 | int type; |
| 539 | |
| 540 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
| 541 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 542 | return type; |
| 543 | } |
| 544 | return -1; |
| 545 | } |
| 546 | |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 547 | /* Returns pointer to proxy containing table <name> or NULL if not found */ |
| 548 | struct proxy *find_stktable(const char *name) |
| 549 | { |
| 550 | struct proxy *px; |
| 551 | |
| 552 | for (px = proxy; px; px = px->next) { |
| 553 | if (px->table.size && strcmp(px->id, name) == 0) |
| 554 | return px; |
| 555 | } |
| 556 | return NULL; |
| 557 | } |