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