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> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <common/config.h> |
| 16 | #include <common/memory.h> |
| 17 | #include <common/mini-clist.h> |
| 18 | #include <common/standard.h> |
| 19 | #include <common/time.h> |
| 20 | |
| 21 | #include <ebmbtree.h> |
| 22 | #include <ebsttree.h> |
| 23 | |
| 24 | #include <types/stick_table.h> |
| 25 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame^] | 26 | #include <proto/pattern.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 27 | #include <proto/proxy.h> |
| 28 | #include <proto/session.h> |
| 29 | #include <proto/task.h> |
| 30 | |
| 31 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame^] | 32 | /* static structure used to return a table key built from a pattern */ |
| 33 | static struct stktable_key static_table_key; |
| 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); |
| 67 | ts->sid = 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; |
| 82 | |
| 83 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 84 | |
| 85 | while (batched < to_batch) { |
| 86 | |
| 87 | if (unlikely(!eb)) { |
| 88 | /* we might have reached the end of the tree, typically because |
| 89 | * <now_ms> is in the first half and we're first scanning the last |
| 90 | * half. Let's loop back to the beginning of the tree now. |
| 91 | */ |
| 92 | eb = eb32_first(&t->exps); |
| 93 | if (likely(!eb)) |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 98 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 99 | eb = eb32_next(eb); |
| 100 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 101 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 102 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 103 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 104 | if (!tick_isset(ts->expire)) |
| 105 | continue; |
| 106 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 107 | ts->exp.key = ts->expire; |
| 108 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 109 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 110 | if (!eb || eb->key > ts->exp.key) |
| 111 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 112 | |
| 113 | continue; |
| 114 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 116 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 117 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 118 | stksess_free(t, ts); |
| 119 | batched++; |
| 120 | } |
| 121 | |
| 122 | return batched; |
| 123 | } |
| 124 | |
| 125 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 126 | * Allocate and initialise a new sticky session. |
| 127 | * The new sticky session is returned or NULL in case of lack of memory. |
| 128 | * Sticky sessions should only be allocated this way, and must be freed using |
| 129 | * stksess_free(). Increase table <t> sticky session counter. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 130 | */ |
| 131 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 132 | { |
| 133 | struct stksess *ts; |
| 134 | |
| 135 | if (unlikely(t->current == t->size)) { |
| 136 | if ( t->nopurge ) |
| 137 | return NULL; |
| 138 | |
| 139 | if (!stktable_trash_oldest(t, t->size >> 8)) |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 143 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 144 | if (ts) { |
| 145 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 146 | stksess_init(t, ts); |
| 147 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return ts; |
| 151 | } |
| 152 | |
| 153 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 154 | * Looks in table <t> for a sticky session matching <key>. |
| 155 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 156 | */ |
| 157 | struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key) |
| 158 | { |
| 159 | struct ebmb_node *eb; |
| 160 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 161 | if (t->type == STKTABLE_TYPE_STRING) |
| 162 | eb = ebst_lookup_len(&t->keys, key->key, key->key_len); |
| 163 | else |
| 164 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 165 | |
| 166 | if (unlikely(!eb)) { |
| 167 | /* no session found */ |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 171 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 174 | /* Try to store sticky session <ts> in the table. If another entry already |
| 175 | * exists with the same key, its server ID is updated with <sid> and a non |
| 176 | * zero value is returned so that the caller knows it can release its stksess. |
| 177 | * If no similar entry was present, <ts> is inserted into the tree and assigned |
| 178 | * server ID <sid>. Zero is returned in this case, and the caller must not |
| 179 | * release the stksess. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 180 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 181 | int stktable_store(struct stktable *t, struct stksess *ts, int sid) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 182 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 183 | struct ebmb_node *eb; |
| 184 | |
| 185 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 186 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 187 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 188 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 189 | |
| 190 | if (unlikely(!eb)) { |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 191 | /* no existing session, insert ours */ |
| 192 | ts->sid = sid; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 193 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 194 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 195 | ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 196 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 197 | |
| 198 | if (t->expire) { |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 199 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 200 | task_queue(t->exp_task); |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 205 | ts = ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 206 | |
| 207 | if ( ts->sid != sid ) |
| 208 | ts->sid = sid; |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 213 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 214 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 215 | */ |
| 216 | static int stktable_trash_expired(struct stktable *t) |
| 217 | { |
| 218 | struct stksess *ts; |
| 219 | struct eb32_node *eb; |
| 220 | |
| 221 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 222 | |
| 223 | while (1) { |
| 224 | if (unlikely(!eb)) { |
| 225 | /* we might have reached the end of the tree, typically because |
| 226 | * <now_ms> is in the first half and we're first scanning the last |
| 227 | * half. Let's loop back to the beginning of the tree now. |
| 228 | */ |
| 229 | eb = eb32_first(&t->exps); |
| 230 | if (likely(!eb)) |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 235 | /* timer not expired yet, revisit it later */ |
| 236 | t->exp_next = eb->key; |
| 237 | return t->exp_next; |
| 238 | } |
| 239 | |
| 240 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 241 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 242 | eb = eb32_next(eb); |
| 243 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 244 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 245 | |
| 246 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 247 | if (!tick_isset(ts->expire)) |
| 248 | continue; |
| 249 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 250 | ts->exp.key = ts->expire; |
| 251 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 252 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 253 | if (!eb || eb->key > ts->exp.key) |
| 254 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 255 | continue; |
| 256 | } |
| 257 | |
| 258 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 259 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 260 | stksess_free(t, ts); |
| 261 | } |
| 262 | |
| 263 | /* We have found no task to expire in any tree */ |
| 264 | t->exp_next = TICK_ETERNITY; |
| 265 | return t->exp_next; |
| 266 | } |
| 267 | |
| 268 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 269 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 270 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 271 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 272 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 273 | { |
| 274 | struct stktable *t = (struct stktable *)task->context; |
| 275 | |
| 276 | task->expire = stktable_trash_expired(t); |
| 277 | return task; |
| 278 | } |
| 279 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 280 | /* 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] | 281 | int stktable_init(struct stktable *t) |
| 282 | { |
| 283 | if (t->size) { |
| 284 | memset(&t->keys, 0, sizeof(t->keys)); |
| 285 | memset(&t->exps, 0, sizeof(t->exps)); |
| 286 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 287 | 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] | 288 | |
| 289 | t->exp_next = TICK_ETERNITY; |
| 290 | if ( t->expire ) { |
| 291 | t->exp_task = task_new(); |
| 292 | t->exp_task->process = process_table_expire; |
| 293 | t->exp_task->expire = TICK_ETERNITY; |
| 294 | t->exp_task->context = (void *)t; |
| 295 | } |
| 296 | return t->pool != NULL; |
| 297 | } |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Configuration keywords of known table types |
| 303 | */ |
| 304 | struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } , |
| 305 | { "integer", 0, 4 }, |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 306 | { "string", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 307 | |
| 308 | |
| 309 | /* |
| 310 | * Parse table type configuration. |
| 311 | * Returns 0 on successful parsing, else 1. |
| 312 | * <myidx> is set at next configuration <args> index. |
| 313 | */ |
| 314 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 315 | { |
| 316 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 317 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 318 | continue; |
| 319 | |
| 320 | *key_size = stktable_types[*type].default_size; |
| 321 | (*myidx)++; |
| 322 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 323 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 324 | if (strcmp("len", args[*myidx]) == 0) { |
| 325 | (*myidx)++; |
| 326 | *key_size = atol(args[*myidx]); |
| 327 | if ( !*key_size ) |
| 328 | break; |
| 329 | /* null terminated string needs +1 for '\0'. */ |
| 330 | (*key_size)++; |
| 331 | (*myidx)++; |
| 332 | } |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | return 1; |
| 337 | } |
| 338 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame^] | 339 | /*****************************************************************/ |
| 340 | /* typed pattern to typed table key functions */ |
| 341 | /*****************************************************************/ |
| 342 | |
| 343 | static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 344 | { |
| 345 | return (void *)&pdata->integer; |
| 346 | } |
| 347 | |
| 348 | static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 349 | { |
| 350 | return (void *)&pdata->ip.s_addr; |
| 351 | } |
| 352 | |
| 353 | static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 354 | { |
| 355 | kdata->integer = ntohl(pdata->ip.s_addr); |
| 356 | return (void *)&kdata->integer; |
| 357 | } |
| 358 | |
| 359 | static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 360 | { |
| 361 | kdata->ip.s_addr = htonl(pdata->integer); |
| 362 | return (void *)&kdata->ip.s_addr; |
| 363 | } |
| 364 | |
| 365 | static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 366 | { |
| 367 | *len = pdata->str.len; |
| 368 | return (void *)pdata->str.str; |
| 369 | } |
| 370 | |
| 371 | static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 372 | { |
| 373 | if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf))) |
| 374 | return NULL; |
| 375 | |
| 376 | *len = strlen((const char *)kdata->buf); |
| 377 | return (void *)kdata->buf; |
| 378 | } |
| 379 | |
| 380 | static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 381 | { |
| 382 | void *key; |
| 383 | |
| 384 | key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf)); |
| 385 | if (!key) |
| 386 | return NULL; |
| 387 | |
| 388 | *len = strlen((const char *)key); |
| 389 | return key; |
| 390 | } |
| 391 | |
| 392 | static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 393 | { |
| 394 | if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip)) |
| 395 | return NULL; |
| 396 | |
| 397 | return (void *)&kdata->ip.s_addr; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 402 | { |
| 403 | int i; |
| 404 | |
| 405 | kdata->integer = 0; |
| 406 | for (i = 0; i < pdata->str.len; i++) { |
| 407 | uint32_t val = pdata->str.str[i] - '0'; |
| 408 | |
| 409 | if (val > 9) |
| 410 | break; |
| 411 | |
| 412 | kdata->integer = kdata->integer * 10 + val; |
| 413 | } |
| 414 | return (void *)&kdata->integer; |
| 415 | } |
| 416 | |
| 417 | /*****************************************************************/ |
| 418 | /* typed pattern to typed table key matrix: */ |
| 419 | /* pattern_to_key[from pattern type][to table key type] */ |
| 420 | /* NULL pointer used for impossible pattern casts */ |
| 421 | /*****************************************************************/ |
| 422 | |
| 423 | typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len); |
| 424 | static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = { |
| 425 | { k_ip2ip, k_ip2int, k_ip2str }, |
| 426 | { k_int2ip, k_int2int, k_int2str }, |
| 427 | { k_str2ip, k_str2int, k_str2str }, |
| 428 | }; |
| 429 | |
| 430 | |
| 431 | /* |
| 432 | * Process a fetch + format conversion as defined by the pattern expression <expr> |
| 433 | * on request or response considering the <dir> parameter. Returns either NULL if |
| 434 | * no key could be extracted, or a pointer to the converted result stored in |
| 435 | * static_table_key in format <table_type>. |
| 436 | */ |
| 437 | struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir, |
| 438 | struct pattern_expr *expr, unsigned long table_type) |
| 439 | { |
| 440 | struct pattern *ptrn; |
| 441 | |
| 442 | ptrn = pattern_process(px, l4, l7, dir, expr, NULL); |
| 443 | if (!ptrn) |
| 444 | return NULL; |
| 445 | |
| 446 | static_table_key.key_len = (size_t)-1; |
| 447 | static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len); |
| 448 | |
| 449 | if (!static_table_key.key) |
| 450 | return NULL; |
| 451 | |
| 452 | return &static_table_key; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Returns 1 if pattern expression <expr> result can be converted to table key of |
| 457 | * type <table_type>, otherwise zero. Used in configuration check. |
| 458 | */ |
| 459 | int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type) |
| 460 | { |
| 461 | if (table_type >= STKTABLE_TYPES) |
| 462 | return 0; |
| 463 | |
| 464 | if (LIST_ISEMPTY(&expr->conv_exprs)) { |
| 465 | if (!pattern_to_key[expr->fetch->out_type][table_type]) |
| 466 | return 0; |
| 467 | } else { |
| 468 | struct pattern_conv_expr *conv_expr; |
| 469 | conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list); |
| 470 | |
| 471 | if (!pattern_to_key[conv_expr->conv->out_type][table_type]) |
| 472 | return 0; |
| 473 | } |
| 474 | return 1; |
| 475 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 476 | |