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 | |
| 25 | #include <types/stick_table.h> |
| 26 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 27 | #include <proto/pattern.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 28 | #include <proto/proxy.h> |
| 29 | #include <proto/session.h> |
| 30 | #include <proto/task.h> |
| 31 | |
| 32 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 33 | /* static structure used to return a table key built from a pattern */ |
| 34 | static struct stktable_key static_table_key; |
| 35 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 36 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 37 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 38 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 39 | */ |
| 40 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 41 | { |
| 42 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 43 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 47 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 48 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 49 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 50 | 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] | 51 | { |
| 52 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 53 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 54 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 55 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 56 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 62 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 63 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 64 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 65 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 66 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 67 | memset((void *)ts - t->data_size, 0, t->data_size); |
| 68 | ts->sid = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 69 | ts->key.node.leaf_p = NULL; |
| 70 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 71 | return ts; |
| 72 | } |
| 73 | |
| 74 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 75 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 76 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 77 | */ |
| 78 | static int stktable_trash_oldest(struct stktable *t, int to_batch) |
| 79 | { |
| 80 | struct stksess *ts; |
| 81 | struct eb32_node *eb; |
| 82 | int batched = 0; |
| 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 |
| 91 | * half. Let's loop back to the beginning of the tree now. |
| 92 | */ |
| 93 | eb = eb32_first(&t->exps); |
| 94 | if (likely(!eb)) |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 99 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 100 | eb = eb32_next(eb); |
| 101 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 102 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 103 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 104 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 105 | if (!tick_isset(ts->expire)) |
| 106 | continue; |
| 107 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 108 | ts->exp.key = ts->expire; |
| 109 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 110 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 111 | if (!eb || eb->key > ts->exp.key) |
| 112 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 113 | |
| 114 | continue; |
| 115 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 116 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 117 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 118 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 119 | stksess_free(t, ts); |
| 120 | batched++; |
| 121 | } |
| 122 | |
| 123 | return batched; |
| 124 | } |
| 125 | |
| 126 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 127 | * Allocate and initialise a new sticky session. |
| 128 | * The new sticky session is returned or NULL in case of lack of memory. |
| 129 | * Sticky sessions should only be allocated this way, and must be freed using |
| 130 | * stksess_free(). Increase table <t> sticky session counter. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 131 | */ |
| 132 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 133 | { |
| 134 | struct stksess *ts; |
| 135 | |
| 136 | if (unlikely(t->current == t->size)) { |
| 137 | if ( t->nopurge ) |
| 138 | return NULL; |
| 139 | |
| 140 | if (!stktable_trash_oldest(t, t->size >> 8)) |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 144 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 145 | if (ts) { |
| 146 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 147 | stksess_init(t, ts); |
| 148 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return ts; |
| 152 | } |
| 153 | |
| 154 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 155 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 156 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 157 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 158 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 159 | { |
| 160 | struct ebmb_node *eb; |
| 161 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 162 | if (t->type == STKTABLE_TYPE_STRING) |
| 163 | eb = ebst_lookup_len(&t->keys, key->key, key->key_len); |
| 164 | else |
| 165 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 166 | |
| 167 | if (unlikely(!eb)) { |
| 168 | /* no session found */ |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 172 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 175 | /* |
| 176 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 177 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 178 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 179 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 180 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 181 | struct ebmb_node *eb; |
| 182 | |
| 183 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 184 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 185 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 186 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 187 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 188 | if (unlikely(!eb)) |
| 189 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 190 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 191 | return ebmb_entry(eb, struct stksess, key); |
| 192 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 193 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 194 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 195 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 196 | * is set. <ts> is returned. |
| 197 | */ |
| 198 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts) |
| 199 | { |
| 200 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 201 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 202 | ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 203 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 204 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 205 | if (t->expire) { |
| 206 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 207 | task_queue(t->exp_task); |
| 208 | } |
| 209 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 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 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 477 | /* Extra data types processing */ |
| 478 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 69b870f | 2010-06-06 14:30:13 +0200 | [diff] [blame] | 479 | [STKTABLE_DT_CONN_CUM] = { .name = "conn_cum", .data_length = stktable_data_size(conn_cum) }, |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | /* |
| 483 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 484 | * or <0 if not found. |
| 485 | */ |
| 486 | int stktable_get_data_type(char *name) |
| 487 | { |
| 488 | int type; |
| 489 | |
| 490 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
| 491 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 492 | return type; |
| 493 | } |
| 494 | return -1; |
| 495 | } |
| 496 | |