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