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 | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 25 | #include <proto/arg.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 26 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 27 | #include <proto/sample.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 28 | #include <proto/stream.h> |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 29 | #include <proto/stick_table.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 30 | #include <proto/task.h> |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 31 | #include <proto/peers.h> |
| 32 | #include <types/global.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 33 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 34 | /* structure used to return a table key built from a sample */ |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 35 | struct stktable_key *static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 36 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 37 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 38 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 39 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 40 | */ |
| 41 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 42 | { |
| 43 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 44 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 48 | * Kill an stksess (only if its ref_cnt is zero). |
| 49 | */ |
| 50 | void stksess_kill(struct stktable *t, struct stksess *ts) |
| 51 | { |
| 52 | if (ts->ref_cnt) |
| 53 | return; |
| 54 | |
| 55 | eb32_delete(&ts->exp); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 56 | eb32_delete(&ts->upd); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 57 | ebmb_delete(&ts->key); |
| 58 | stksess_free(t, ts); |
| 59 | } |
| 60 | |
| 61 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 62 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 63 | * from the value present in <key>. |
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 | 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] | 66 | { |
| 67 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 68 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 69 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 70 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 71 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 77 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 78 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 79 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 80 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 81 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 82 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 83 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 84 | ts->key.node.leaf_p = NULL; |
| 85 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 86 | ts->upd.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 87 | return ts; |
| 88 | } |
| 89 | |
| 90 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 91 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 92 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 93 | */ |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 94 | int stktable_trash_oldest(struct stktable *t, int to_batch) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 95 | { |
| 96 | struct stksess *ts; |
| 97 | struct eb32_node *eb; |
| 98 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 99 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 100 | |
| 101 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 102 | |
| 103 | while (batched < to_batch) { |
| 104 | |
| 105 | if (unlikely(!eb)) { |
| 106 | /* we might have reached the end of the tree, typically because |
| 107 | * <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] | 108 | * half. Let's loop back to the beginning of the tree now if we |
| 109 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 110 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 111 | if (looped) |
| 112 | break; |
| 113 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 114 | eb = eb32_first(&t->exps); |
| 115 | if (likely(!eb)) |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 120 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 121 | eb = eb32_next(eb); |
| 122 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 123 | /* don't delete an entry which is currently referenced */ |
| 124 | if (ts->ref_cnt) |
| 125 | continue; |
| 126 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 127 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 128 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 129 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 130 | if (!tick_isset(ts->expire)) |
| 131 | continue; |
| 132 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 133 | ts->exp.key = ts->expire; |
| 134 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 136 | if (!eb || eb->key > ts->exp.key) |
| 137 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 138 | |
| 139 | continue; |
| 140 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 141 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 142 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 143 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 144 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 145 | stksess_free(t, ts); |
| 146 | batched++; |
| 147 | } |
| 148 | |
| 149 | return batched; |
| 150 | } |
| 151 | |
| 152 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 153 | * Allocate and initialise a new sticky session. |
| 154 | * The new sticky session is returned or NULL in case of lack of memory. |
| 155 | * Sticky sessions should only be allocated this way, and must be freed using |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 156 | * stksess_free(). Table <t>'s sticky session counter is increased. If <key> |
| 157 | * is not NULL, it is assigned to the new session. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 158 | */ |
| 159 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 160 | { |
| 161 | struct stksess *ts; |
| 162 | |
| 163 | if (unlikely(t->current == t->size)) { |
| 164 | if ( t->nopurge ) |
| 165 | return NULL; |
| 166 | |
Emeric Brun | fbce6d0 | 2010-09-23 18:10:00 +0200 | [diff] [blame] | 167 | if (!stktable_trash_oldest(t, (t->size >> 8) + 1)) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 168 | return NULL; |
| 169 | } |
| 170 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 171 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 172 | if (ts) { |
| 173 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 174 | stksess_init(t, ts); |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 175 | if (key) |
| 176 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return ts; |
| 180 | } |
| 181 | |
| 182 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 183 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 184 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 185 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 186 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 187 | { |
| 188 | struct ebmb_node *eb; |
| 189 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 190 | if (t->type == STKTABLE_TYPE_STRING) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 191 | 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] | 192 | else |
| 193 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 194 | |
| 195 | if (unlikely(!eb)) { |
| 196 | /* no session found */ |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 200 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 203 | /* Lookup and touch <key> in <table>, or create the entry if it does not exist. |
| 204 | * This is mainly used for situations where we want to refresh a key's usage so |
| 205 | * that it does not expire, and we want to have it created if it was not there. |
| 206 | * The stksess is returned, or NULL if it could not be created. |
| 207 | */ |
| 208 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key) |
| 209 | { |
| 210 | struct stksess *ts; |
| 211 | |
| 212 | ts = stktable_lookup_key(table, key); |
| 213 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 214 | return stktable_touch(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 215 | |
| 216 | /* entry does not exist, initialize a new one */ |
| 217 | ts = stksess_new(table, key); |
| 218 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 219 | stktable_store(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 220 | return ts; |
| 221 | } |
| 222 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 223 | /* |
| 224 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 225 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 226 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 227 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 228 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 229 | struct ebmb_node *eb; |
| 230 | |
| 231 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 232 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 233 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 234 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
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 | if (unlikely(!eb)) |
| 237 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 238 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 239 | return ebmb_entry(eb, struct stksess, key); |
| 240 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 241 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 242 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 243 | * The table's expiration timer is updated if set. |
| 244 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 245 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 246 | { |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 247 | struct eb32_node * eb; |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 248 | ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 249 | if (t->expire) { |
| 250 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 251 | task_queue(t->exp_task); |
| 252 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 253 | |
| 254 | if (t->sync_task && local) { |
| 255 | ts->upd.key = ++t->update; |
| 256 | t->localupdate = t->update; |
| 257 | eb32_delete(&ts->upd); |
| 258 | eb = eb32_insert(&t->updates, &ts->upd); |
| 259 | if (eb != &ts->upd) { |
| 260 | eb32_delete(eb); |
| 261 | eb32_insert(&t->updates, &ts->upd); |
| 262 | } |
| 263 | task_wakeup(t->sync_task, TASK_WOKEN_MSG); |
| 264 | } |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 265 | return ts; |
| 266 | } |
| 267 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 268 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 269 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 270 | * is set. <ts> is returned. |
| 271 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 272 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 273 | { |
| 274 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 275 | stktable_touch(t, ts, local); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 276 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 277 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 278 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 281 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 282 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 283 | * could be created. The entry's expiration is updated. |
| 284 | */ |
| 285 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 286 | { |
| 287 | struct stksess *ts; |
| 288 | |
| 289 | if (!key) |
| 290 | return NULL; |
| 291 | |
| 292 | ts = stktable_lookup_key(table, key); |
| 293 | if (ts == NULL) { |
| 294 | /* entry does not exist, initialize a new one */ |
| 295 | ts = stksess_new(table, key); |
| 296 | if (!ts) |
| 297 | return NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 298 | stktable_store(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 299 | } |
| 300 | else |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 301 | stktable_touch(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 302 | return ts; |
| 303 | } |
| 304 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 305 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 306 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 307 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 308 | */ |
| 309 | static int stktable_trash_expired(struct stktable *t) |
| 310 | { |
| 311 | struct stksess *ts; |
| 312 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 313 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 314 | |
| 315 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 316 | |
| 317 | while (1) { |
| 318 | if (unlikely(!eb)) { |
| 319 | /* we might have reached the end of the tree, typically because |
| 320 | * <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] | 321 | * half. Let's loop back to the beginning of the tree now if we |
| 322 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 323 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 324 | if (looped) |
| 325 | break; |
| 326 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 327 | eb = eb32_first(&t->exps); |
| 328 | if (likely(!eb)) |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 333 | /* timer not expired yet, revisit it later */ |
| 334 | t->exp_next = eb->key; |
| 335 | return t->exp_next; |
| 336 | } |
| 337 | |
| 338 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 339 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 340 | eb = eb32_next(eb); |
| 341 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 342 | /* don't delete an entry which is currently referenced */ |
| 343 | if (ts->ref_cnt) |
| 344 | continue; |
| 345 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 346 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 347 | |
| 348 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 349 | if (!tick_isset(ts->expire)) |
| 350 | continue; |
| 351 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 352 | ts->exp.key = ts->expire; |
| 353 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 354 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 355 | if (!eb || eb->key > ts->exp.key) |
| 356 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 357 | continue; |
| 358 | } |
| 359 | |
| 360 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 361 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 362 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 363 | stksess_free(t, ts); |
| 364 | } |
| 365 | |
| 366 | /* We have found no task to expire in any tree */ |
| 367 | t->exp_next = TICK_ETERNITY; |
| 368 | return t->exp_next; |
| 369 | } |
| 370 | |
| 371 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 372 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 373 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 374 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 375 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 376 | { |
| 377 | struct stktable *t = (struct stktable *)task->context; |
| 378 | |
| 379 | task->expire = stktable_trash_expired(t); |
| 380 | return task; |
| 381 | } |
| 382 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 383 | /* 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] | 384 | int stktable_init(struct stktable *t) |
| 385 | { |
| 386 | if (t->size) { |
| 387 | memset(&t->keys, 0, sizeof(t->keys)); |
| 388 | memset(&t->exps, 0, sizeof(t->exps)); |
| 389 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 390 | 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] | 391 | |
| 392 | t->exp_next = TICK_ETERNITY; |
| 393 | if ( t->expire ) { |
| 394 | t->exp_task = task_new(); |
| 395 | t->exp_task->process = process_table_expire; |
| 396 | t->exp_task->expire = TICK_ETERNITY; |
| 397 | t->exp_task->context = (void *)t; |
| 398 | } |
Willy Tarreau | c8b6791 | 2015-05-01 18:29:57 +0200 | [diff] [blame] | 399 | if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) { |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 400 | peers_register_table(t->peers.p, t); |
| 401 | } |
| 402 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 403 | return t->pool != NULL; |
| 404 | } |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Configuration keywords of known table types |
| 410 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 411 | struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 412 | { "ipv6", 0, 16 }, |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 413 | { "integer", 0, 4 }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 414 | { "string", STK_F_CUSTOM_KEYSIZE, 32 }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 415 | { "binary", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 416 | |
| 417 | |
| 418 | /* |
| 419 | * Parse table type configuration. |
| 420 | * Returns 0 on successful parsing, else 1. |
| 421 | * <myidx> is set at next configuration <args> index. |
| 422 | */ |
| 423 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 424 | { |
| 425 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 426 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 427 | continue; |
| 428 | |
| 429 | *key_size = stktable_types[*type].default_size; |
| 430 | (*myidx)++; |
| 431 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 432 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 433 | if (strcmp("len", args[*myidx]) == 0) { |
| 434 | (*myidx)++; |
| 435 | *key_size = atol(args[*myidx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 436 | if (!*key_size) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 437 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 438 | if (*type == STKTABLE_TYPE_STRING) { |
| 439 | /* null terminated string needs +1 for '\0'. */ |
| 440 | (*key_size)++; |
| 441 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 442 | (*myidx)++; |
| 443 | } |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | return 1; |
| 448 | } |
| 449 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 450 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 451 | /* typed sample to typed table key functions */ |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 452 | /*****************************************************************/ |
| 453 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 454 | 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] | 455 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 456 | return (void *)&smp->data.uint; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 459 | 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] | 460 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 461 | if (smp->type == SMP_T_IPV6) { |
| 462 | v6tov4(&kdata->ip, &smp->data.ipv6); |
| 463 | return (void *)&kdata->ip.s_addr; |
| 464 | } |
| 465 | else { |
| 466 | return (void *)&smp->data.ipv4.s_addr; |
| 467 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 470 | 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] | 471 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 472 | if (smp->type == SMP_T_IPV6) { |
| 473 | return (void *)&smp->data.ipv6.s6_addr; |
| 474 | } |
| 475 | else { |
| 476 | v4tov6(&kdata->ipv6, &smp->data.ipv4); |
| 477 | return (void *)&kdata->ipv6.s6_addr; |
| 478 | } |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 479 | } |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 480 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 481 | 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] | 482 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 483 | if (smp->type == SMP_T_IPV6) { |
| 484 | if (!v6tov4(&kdata->ip, &smp->data.ipv6)) |
| 485 | return NULL; |
| 486 | kdata->integer = ntohl(kdata->ip.s_addr); |
| 487 | } |
| 488 | else { |
| 489 | kdata->integer = ntohl(smp->data.ipv4.s_addr); |
| 490 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 491 | return (void *)&kdata->integer; |
| 492 | } |
| 493 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 494 | 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] | 495 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 496 | kdata->ip.s_addr = htonl(smp->data.uint); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 497 | return (void *)&kdata->ip.s_addr; |
| 498 | } |
| 499 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 500 | 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] | 501 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 502 | *len = smp->data.str.len; |
| 503 | return (void *)smp->data.str.str; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 506 | 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] | 507 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 508 | if (smp->type == SMP_T_IPV6) { |
| 509 | if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, *len)) |
| 510 | return NULL; |
| 511 | } |
| 512 | else { |
| 513 | if (!inet_ntop(AF_INET, &smp->data.ipv4, kdata->buf, *len)) |
| 514 | return NULL; |
| 515 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 516 | |
| 517 | *len = strlen((const char *)kdata->buf); |
| 518 | return (void *)kdata->buf; |
| 519 | } |
| 520 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 521 | static void *k_ip2bin(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
| 522 | { |
| 523 | if (smp->type == SMP_T_IPV4) { |
| 524 | if (*len > 4) |
| 525 | *len = 4; |
| 526 | memcpy(kdata->buf, &smp->data.ipv4, *len); |
| 527 | } |
| 528 | else if (smp->type == SMP_T_IPV6) { |
| 529 | if (*len > 16) |
| 530 | *len = 16; |
| 531 | memcpy(kdata->buf, &smp->data.ipv6, *len); |
| 532 | } |
| 533 | else |
| 534 | *len = 0; |
| 535 | return (void *)kdata->buf; |
| 536 | } |
| 537 | |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 538 | static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
| 539 | { |
| 540 | unsigned char c; |
| 541 | int ptr = 0; |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 542 | int max = *len; |
| 543 | int size = 0; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 544 | |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 545 | while (ptr < smp->data.str.len && size <= max - 2) { |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 546 | c = smp->data.str.str[ptr++]; |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 547 | kdata->buf[size++] = hextab[(c >> 4) & 0xF]; |
| 548 | kdata->buf[size++] = hextab[c & 0xF]; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 549 | } |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 550 | *len = size; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 551 | return (void *)kdata->buf; |
| 552 | } |
| 553 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 554 | 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] | 555 | { |
| 556 | void *key; |
| 557 | |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 558 | key = (void *)ultoa_r(smp->data.uint, kdata->buf, *len); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 559 | if (!key) |
| 560 | return NULL; |
| 561 | |
| 562 | *len = strlen((const char *)key); |
| 563 | return key; |
| 564 | } |
| 565 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 566 | 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] | 567 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 568 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &kdata->ip)) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 569 | return NULL; |
| 570 | |
| 571 | return (void *)&kdata->ip.s_addr; |
| 572 | } |
| 573 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 574 | 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] | 575 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 576 | if (!inet_pton(AF_INET6, smp->data.str.str, &kdata->ipv6)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 577 | return NULL; |
| 578 | |
| 579 | return (void *)&kdata->ipv6.s6_addr; |
| 580 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 581 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 582 | 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] | 583 | { |
| 584 | int i; |
| 585 | |
| 586 | kdata->integer = 0; |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 587 | for (i = 0; i < smp->data.str.len; i++) { |
| 588 | uint32_t val = smp->data.str.str[i] - '0'; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 589 | |
| 590 | if (val > 9) |
| 591 | break; |
| 592 | |
| 593 | kdata->integer = kdata->integer * 10 + val; |
| 594 | } |
| 595 | return (void *)&kdata->integer; |
| 596 | } |
| 597 | |
| 598 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 599 | /* typed sample to typed table key matrix: */ |
| 600 | /* sample_to_key[from sample type][to table key type] */ |
| 601 | /* NULL pointer used for impossible sample casts */ |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 602 | /*****************************************************************/ |
| 603 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 604 | typedef void *(*sample_to_key_fct)(struct sample *smp, union stktable_key_data *kdata, size_t *len); |
| 605 | 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] | 606 | /* table type: IP IPV6 INTEGER STRING BINARY */ |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 607 | /* patt. type: BOOL */ { NULL, NULL, k_int2int, k_int2str, NULL }, |
| 608 | /* UINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL }, |
| 609 | /* SINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL }, |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 610 | /* ADDR */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL }, |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 611 | /* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, k_ip2bin }, |
| 612 | /* IPV6 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, k_ip2bin }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 613 | /* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str }, |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 614 | /* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 615 | }; |
| 616 | |
| 617 | |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 618 | /* Prepares a stktable_key from a sample <smp> to search into table <t>. |
| 619 | * Returns NULL if the sample could not be converted (eg: no matching type), |
| 620 | * otherwise a pointer to the static stktable_key filled with what is needed |
| 621 | * for the lookup. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 622 | */ |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 623 | struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 624 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 625 | if (!sample_to_key[smp->type][t->type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 626 | return NULL; |
| 627 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 628 | static_table_key->key_len = t->key_size; |
| 629 | 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] | 630 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 631 | if (!static_table_key->key) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 632 | return NULL; |
| 633 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 634 | if (static_table_key->key_len == 0) |
Willy Tarreau | 7fc1c6e | 2012-04-26 11:03:17 +0200 | [diff] [blame] | 635 | return NULL; |
| 636 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 637 | 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] | 638 | /* need padding with null */ |
| 639 | |
| 640 | /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf) |
| 641 | cause t->key_size is necessary less than sizeof(static_table_key.data) */ |
| 642 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 643 | if ((char *)static_table_key->key > (char *)&static_table_key->data && |
| 644 | (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] | 645 | /* key buffer is part of the static_table_key private data buffer, but is not aligned */ |
| 646 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 647 | if (global.tune.bufsize - ((char *)static_table_key->key - (char *)&static_table_key->data) < t->key_size) { |
| 648 | /* if not remain enough place for padding , process a realign */ |
| 649 | memmove(static_table_key->data.buf, static_table_key->key, static_table_key->key_len); |
| 650 | static_table_key->key = static_table_key->data.buf; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 651 | } |
| 652 | } |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 653 | else if (static_table_key->key != static_table_key->data.buf) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 654 | /* key definitly not part of the static_table_key private data buffer */ |
| 655 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 656 | memcpy(static_table_key->data.buf, static_table_key->key, static_table_key->key_len); |
| 657 | static_table_key->key = static_table_key->data.buf; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 660 | 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] | 661 | } |
| 662 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 663 | return static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 667 | * Process a fetch + format conversion as defined by the sample expression <expr> |
| 668 | * on request or response considering the <opt> parameter. Returns either NULL if |
| 669 | * no key could be extracted, or a pointer to the converted result stored in |
| 670 | * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset |
| 671 | * and its flags will be initialized so that the caller gets a copy of the input |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 672 | * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present |
| 673 | * without SMP_OPT_FINAL). The output will be usable like this : |
| 674 | * |
| 675 | * return MAY_CHANGE FINAL Meaning for the sample |
| 676 | * NULL 0 * Not present and will never be (eg: header) |
| 677 | * NULL 1 0 Not present or unstable, could change (eg: req_len) |
| 678 | * NULL 1 1 Not present, will not change anymore |
| 679 | * smp 0 * Present and will not change (eg: header) |
| 680 | * smp 1 0 not possible |
| 681 | * smp 1 1 Present, last known value (eg: request length) |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 682 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 683 | struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm, |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 684 | unsigned int opt, struct sample_expr *expr, struct sample *smp) |
| 685 | { |
| 686 | if (smp) |
| 687 | memset(smp, 0, sizeof(*smp)); |
| 688 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 689 | smp = sample_process(px, sess, strm, opt, expr, smp); |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 690 | if (!smp) |
| 691 | return NULL; |
| 692 | |
| 693 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 694 | return NULL; /* we can only use stable samples */ |
| 695 | |
| 696 | return smp_to_stkey(smp, t); |
| 697 | } |
| 698 | |
| 699 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 700 | * 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] | 701 | * type <table_type>, otherwise zero. Used in configuration check. |
| 702 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 703 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 704 | { |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 705 | int out_type; |
| 706 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 707 | if (table_type >= STKTABLE_TYPES) |
| 708 | return 0; |
| 709 | |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 710 | out_type = smp_expr_output_type(expr); |
| 711 | if (!sample_to_key[out_type][table_type]) |
| 712 | return 0; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 713 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 714 | return 1; |
| 715 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 716 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 717 | /* Extra data types processing : after the last one, some room may remain |
| 718 | * before STKTABLE_DATA_TYPES that may be used to register extra data types |
| 719 | * at run time. |
| 720 | */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 721 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 722 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT }, |
| 723 | [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT }, |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 724 | [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 725 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT }, |
| 726 | [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 727 | [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT }, |
| 728 | [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT }, |
| 729 | [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 730 | [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT }, |
| 731 | [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 732 | [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT }, |
| 733 | [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 734 | [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL }, |
| 735 | [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 736 | [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL }, |
| 737 | [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] | 738 | }; |
| 739 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 740 | /* Registers stick-table extra data type with index <idx>, name <name>, type |
| 741 | * <std_type> and arg type <arg_type>. If the index is negative, the next free |
| 742 | * index is automatically allocated. The allocated index is returned, or -1 if |
| 743 | * no free index was found or <name> was already registered. The <name> is used |
| 744 | * directly as a pointer, so if it's not stable, the caller must allocate it. |
| 745 | */ |
| 746 | int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type) |
| 747 | { |
| 748 | if (idx < 0) { |
| 749 | for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) { |
| 750 | if (!stktable_data_types[idx].name) |
| 751 | break; |
| 752 | |
| 753 | if (strcmp(stktable_data_types[idx].name, name) == 0) |
| 754 | return -1; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | if (idx >= STKTABLE_DATA_TYPES) |
| 759 | return -1; |
| 760 | |
| 761 | if (stktable_data_types[idx].name != NULL) |
| 762 | return -1; |
| 763 | |
| 764 | stktable_data_types[idx].name = name; |
| 765 | stktable_data_types[idx].std_type = std_type; |
| 766 | stktable_data_types[idx].arg_type = arg_type; |
| 767 | return idx; |
| 768 | } |
| 769 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 770 | /* |
| 771 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 772 | * or <0 if not found. |
| 773 | */ |
| 774 | int stktable_get_data_type(char *name) |
| 775 | { |
| 776 | int type; |
| 777 | |
| 778 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 779 | if (!stktable_data_types[type].name) |
| 780 | continue; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 781 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 782 | return type; |
| 783 | } |
| 784 | return -1; |
| 785 | } |
| 786 | |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 787 | /* Returns pointer to proxy containing table <name> or NULL if not found */ |
| 788 | struct proxy *find_stktable(const char *name) |
| 789 | { |
| 790 | struct proxy *px; |
Willy Tarreau | 991610d | 2014-03-15 08:03:57 +0100 | [diff] [blame] | 791 | struct ebpt_node *node; |
| 792 | |
| 793 | for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) { |
| 794 | px = container_of(node, struct proxy, conf.by_name); |
| 795 | |
| 796 | if (strcmp(px->id, name) != 0) |
| 797 | break; |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 798 | |
Willy Tarreau | 991610d | 2014-03-15 08:03:57 +0100 | [diff] [blame] | 799 | if (px->table.size) |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 800 | return px; |
| 801 | } |
| 802 | return NULL; |
| 803 | } |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 804 | |
| 805 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 806 | * it up into this table. Returns true if found, false otherwise. The input |
| 807 | * type is STR so that input samples are converted to string (since all types |
| 808 | * can be converted to strings), then the function casts the string again into |
| 809 | * the table's type. This is a double conversion, but in the future we might |
| 810 | * support automatic input types to perform the cast on the fly. |
| 811 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 812 | static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 813 | { |
| 814 | struct stktable *t; |
| 815 | struct stktable_key *key; |
| 816 | struct stksess *ts; |
| 817 | |
| 818 | t = &arg_p[0].data.prx->table; |
| 819 | |
| 820 | key = smp_to_stkey(smp, t); |
| 821 | if (!key) |
| 822 | return 0; |
| 823 | |
| 824 | ts = stktable_lookup_key(t, key); |
| 825 | |
| 826 | smp->type = SMP_T_BOOL; |
| 827 | smp->data.uint = !!ts; |
| 828 | smp->flags = SMP_F_VOL_TEST; |
| 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 833 | * it up into this table. Returns the data rate received from clients in bytes/s |
| 834 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 835 | * be easily performed. If the inspected parameter is not stored in the table, |
| 836 | * <not found> is returned. |
| 837 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 838 | static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 839 | { |
| 840 | struct stktable *t; |
| 841 | struct stktable_key *key; |
| 842 | struct stksess *ts; |
| 843 | void *ptr; |
| 844 | |
| 845 | t = &arg_p[0].data.prx->table; |
| 846 | |
| 847 | key = smp_to_stkey(smp, t); |
| 848 | if (!key) |
| 849 | return 0; |
| 850 | |
| 851 | smp->flags = SMP_F_VOL_TEST; |
| 852 | smp->type = SMP_T_UINT; |
| 853 | smp->data.uint = 0; |
| 854 | |
| 855 | ts = stktable_lookup_key(t, key); |
| 856 | if (!ts) /* key not present */ |
| 857 | return 1; |
| 858 | |
| 859 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE); |
| 860 | if (!ptr) |
| 861 | return 0; /* parameter not stored */ |
| 862 | |
| 863 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate), |
| 864 | t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u); |
| 865 | return 1; |
| 866 | } |
| 867 | |
| 868 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 869 | * it up into this table. Returns the cumulated number of connections for the key |
| 870 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 871 | * be easily performed. If the inspected parameter is not stored in the table, |
| 872 | * <not found> is returned. |
| 873 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 874 | static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 875 | { |
| 876 | struct stktable *t; |
| 877 | struct stktable_key *key; |
| 878 | struct stksess *ts; |
| 879 | void *ptr; |
| 880 | |
| 881 | t = &arg_p[0].data.prx->table; |
| 882 | |
| 883 | key = smp_to_stkey(smp, t); |
| 884 | if (!key) |
| 885 | return 0; |
| 886 | |
| 887 | smp->flags = SMP_F_VOL_TEST; |
| 888 | smp->type = SMP_T_UINT; |
| 889 | smp->data.uint = 0; |
| 890 | |
| 891 | ts = stktable_lookup_key(t, key); |
| 892 | if (!ts) /* key not present */ |
| 893 | return 1; |
| 894 | |
| 895 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT); |
| 896 | if (!ptr) |
| 897 | return 0; /* parameter not stored */ |
| 898 | |
| 899 | smp->data.uint = stktable_data_cast(ptr, conn_cnt); |
| 900 | return 1; |
| 901 | } |
| 902 | |
| 903 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 904 | * it up into this table. Returns the number of concurrent connections for the |
| 905 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 906 | * can be easily performed. If the inspected parameter is not stored in the |
| 907 | * table, <not found> is returned. |
| 908 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 909 | static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 910 | { |
| 911 | struct stktable *t; |
| 912 | struct stktable_key *key; |
| 913 | struct stksess *ts; |
| 914 | void *ptr; |
| 915 | |
| 916 | t = &arg_p[0].data.prx->table; |
| 917 | |
| 918 | key = smp_to_stkey(smp, t); |
| 919 | if (!key) |
| 920 | return 0; |
| 921 | |
| 922 | smp->flags = SMP_F_VOL_TEST; |
| 923 | smp->type = SMP_T_UINT; |
| 924 | smp->data.uint = 0; |
| 925 | |
| 926 | ts = stktable_lookup_key(t, key); |
| 927 | if (!ts) /* key not present */ |
| 928 | return 1; |
| 929 | |
| 930 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR); |
| 931 | if (!ptr) |
| 932 | return 0; /* parameter not stored */ |
| 933 | |
| 934 | smp->data.uint = stktable_data_cast(ptr, conn_cur); |
| 935 | return 1; |
| 936 | } |
| 937 | |
| 938 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 939 | * it up into this table. Returns the rate of incoming connections from the key |
| 940 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 941 | * be easily performed. If the inspected parameter is not stored in the table, |
| 942 | * <not found> is returned. |
| 943 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 944 | static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 945 | { |
| 946 | struct stktable *t; |
| 947 | struct stktable_key *key; |
| 948 | struct stksess *ts; |
| 949 | void *ptr; |
| 950 | |
| 951 | t = &arg_p[0].data.prx->table; |
| 952 | |
| 953 | key = smp_to_stkey(smp, t); |
| 954 | if (!key) |
| 955 | return 0; |
| 956 | |
| 957 | smp->flags = SMP_F_VOL_TEST; |
| 958 | smp->type = SMP_T_UINT; |
| 959 | smp->data.uint = 0; |
| 960 | |
| 961 | ts = stktable_lookup_key(t, key); |
| 962 | if (!ts) /* key not present */ |
| 963 | return 1; |
| 964 | |
| 965 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE); |
| 966 | if (!ptr) |
| 967 | return 0; /* parameter not stored */ |
| 968 | |
| 969 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate), |
| 970 | t->data_arg[STKTABLE_DT_CONN_RATE].u); |
| 971 | return 1; |
| 972 | } |
| 973 | |
| 974 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 975 | * it up into this table. Returns the data rate sent to clients in bytes/s |
| 976 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 977 | * be easily performed. If the inspected parameter is not stored in the table, |
| 978 | * <not found> is returned. |
| 979 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 980 | static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 981 | { |
| 982 | struct stktable *t; |
| 983 | struct stktable_key *key; |
| 984 | struct stksess *ts; |
| 985 | void *ptr; |
| 986 | |
| 987 | t = &arg_p[0].data.prx->table; |
| 988 | |
| 989 | key = smp_to_stkey(smp, t); |
| 990 | if (!key) |
| 991 | return 0; |
| 992 | |
| 993 | smp->flags = SMP_F_VOL_TEST; |
| 994 | smp->type = SMP_T_UINT; |
| 995 | smp->data.uint = 0; |
| 996 | |
| 997 | ts = stktable_lookup_key(t, key); |
| 998 | if (!ts) /* key not present */ |
| 999 | return 1; |
| 1000 | |
| 1001 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE); |
| 1002 | if (!ptr) |
| 1003 | return 0; /* parameter not stored */ |
| 1004 | |
| 1005 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate), |
| 1006 | t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u); |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
| 1010 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1011 | * it up into this table. Returns the value of the GPC0 counter for the key |
| 1012 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1013 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1014 | * <not found> is returned. |
| 1015 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1016 | static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1017 | { |
| 1018 | struct stktable *t; |
| 1019 | struct stktable_key *key; |
| 1020 | struct stksess *ts; |
| 1021 | void *ptr; |
| 1022 | |
| 1023 | t = &arg_p[0].data.prx->table; |
| 1024 | |
| 1025 | key = smp_to_stkey(smp, t); |
| 1026 | if (!key) |
| 1027 | return 0; |
| 1028 | |
| 1029 | smp->flags = SMP_F_VOL_TEST; |
| 1030 | smp->type = SMP_T_UINT; |
| 1031 | smp->data.uint = 0; |
| 1032 | |
| 1033 | ts = stktable_lookup_key(t, key); |
| 1034 | if (!ts) /* key not present */ |
| 1035 | return 1; |
| 1036 | |
| 1037 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0); |
| 1038 | if (!ptr) |
| 1039 | return 0; /* parameter not stored */ |
| 1040 | |
| 1041 | smp->data.uint = stktable_data_cast(ptr, gpc0); |
| 1042 | return 1; |
| 1043 | } |
| 1044 | |
| 1045 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1046 | * it up into this table. Returns the event rate of the GPC0 counter for the key |
| 1047 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1048 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1049 | * <not found> is returned. |
| 1050 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1051 | static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1052 | { |
| 1053 | struct stktable *t; |
| 1054 | struct stktable_key *key; |
| 1055 | struct stksess *ts; |
| 1056 | void *ptr; |
| 1057 | |
| 1058 | t = &arg_p[0].data.prx->table; |
| 1059 | |
| 1060 | key = smp_to_stkey(smp, t); |
| 1061 | if (!key) |
| 1062 | return 0; |
| 1063 | |
| 1064 | smp->flags = SMP_F_VOL_TEST; |
| 1065 | smp->type = SMP_T_UINT; |
| 1066 | smp->data.uint = 0; |
| 1067 | |
| 1068 | ts = stktable_lookup_key(t, key); |
| 1069 | if (!ts) /* key not present */ |
| 1070 | return 1; |
| 1071 | |
| 1072 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE); |
| 1073 | if (!ptr) |
| 1074 | return 0; /* parameter not stored */ |
| 1075 | |
| 1076 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate), |
| 1077 | t->data_arg[STKTABLE_DT_GPC0_RATE].u); |
| 1078 | return 1; |
| 1079 | } |
| 1080 | |
| 1081 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1082 | * it up into this table. Returns the cumulated number of HTTP request errors |
| 1083 | * for the key if the key is present in the table, otherwise zero, so that |
| 1084 | * comparisons can be easily performed. If the inspected parameter is not stored |
| 1085 | * in the table, <not found> is returned. |
| 1086 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1087 | static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1088 | { |
| 1089 | struct stktable *t; |
| 1090 | struct stktable_key *key; |
| 1091 | struct stksess *ts; |
| 1092 | void *ptr; |
| 1093 | |
| 1094 | t = &arg_p[0].data.prx->table; |
| 1095 | |
| 1096 | key = smp_to_stkey(smp, t); |
| 1097 | if (!key) |
| 1098 | return 0; |
| 1099 | |
| 1100 | smp->flags = SMP_F_VOL_TEST; |
| 1101 | smp->type = SMP_T_UINT; |
| 1102 | smp->data.uint = 0; |
| 1103 | |
| 1104 | ts = stktable_lookup_key(t, key); |
| 1105 | if (!ts) /* key not present */ |
| 1106 | return 1; |
| 1107 | |
| 1108 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 1109 | if (!ptr) |
| 1110 | return 0; /* parameter not stored */ |
| 1111 | |
| 1112 | smp->data.uint = stktable_data_cast(ptr, http_err_cnt); |
| 1113 | return 1; |
| 1114 | } |
| 1115 | |
| 1116 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1117 | * it up into this table. Returns the HTTP request error rate the key |
| 1118 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1119 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1120 | * <not found> is returned. |
| 1121 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1122 | static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1123 | { |
| 1124 | struct stktable *t; |
| 1125 | struct stktable_key *key; |
| 1126 | struct stksess *ts; |
| 1127 | void *ptr; |
| 1128 | |
| 1129 | t = &arg_p[0].data.prx->table; |
| 1130 | |
| 1131 | key = smp_to_stkey(smp, t); |
| 1132 | if (!key) |
| 1133 | return 0; |
| 1134 | |
| 1135 | smp->flags = SMP_F_VOL_TEST; |
| 1136 | smp->type = SMP_T_UINT; |
| 1137 | smp->data.uint = 0; |
| 1138 | |
| 1139 | ts = stktable_lookup_key(t, key); |
| 1140 | if (!ts) /* key not present */ |
| 1141 | return 1; |
| 1142 | |
| 1143 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 1144 | if (!ptr) |
| 1145 | return 0; /* parameter not stored */ |
| 1146 | |
| 1147 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 1148 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u); |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
| 1152 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1153 | * it up into this table. Returns the cumulated number of HTTP request for the |
| 1154 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1155 | * can be easily performed. If the inspected parameter is not stored in the |
| 1156 | * table, <not found> is returned. |
| 1157 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1158 | static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1159 | { |
| 1160 | struct stktable *t; |
| 1161 | struct stktable_key *key; |
| 1162 | struct stksess *ts; |
| 1163 | void *ptr; |
| 1164 | |
| 1165 | t = &arg_p[0].data.prx->table; |
| 1166 | |
| 1167 | key = smp_to_stkey(smp, t); |
| 1168 | if (!key) |
| 1169 | return 0; |
| 1170 | |
| 1171 | smp->flags = SMP_F_VOL_TEST; |
| 1172 | smp->type = SMP_T_UINT; |
| 1173 | smp->data.uint = 0; |
| 1174 | |
| 1175 | ts = stktable_lookup_key(t, key); |
| 1176 | if (!ts) /* key not present */ |
| 1177 | return 1; |
| 1178 | |
| 1179 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1180 | if (!ptr) |
| 1181 | return 0; /* parameter not stored */ |
| 1182 | |
| 1183 | smp->data.uint = stktable_data_cast(ptr, http_req_cnt); |
| 1184 | return 1; |
| 1185 | } |
| 1186 | |
| 1187 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1188 | * it up into this table. Returns the HTTP request rate the key if the key is |
| 1189 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1190 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1191 | * is returned. |
| 1192 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1193 | static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1194 | { |
| 1195 | struct stktable *t; |
| 1196 | struct stktable_key *key; |
| 1197 | struct stksess *ts; |
| 1198 | void *ptr; |
| 1199 | |
| 1200 | t = &arg_p[0].data.prx->table; |
| 1201 | |
| 1202 | key = smp_to_stkey(smp, t); |
| 1203 | if (!key) |
| 1204 | return 0; |
| 1205 | |
| 1206 | smp->flags = SMP_F_VOL_TEST; |
| 1207 | smp->type = SMP_T_UINT; |
| 1208 | smp->data.uint = 0; |
| 1209 | |
| 1210 | ts = stktable_lookup_key(t, key); |
| 1211 | if (!ts) /* key not present */ |
| 1212 | return 1; |
| 1213 | |
| 1214 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1215 | if (!ptr) |
| 1216 | return 0; /* parameter not stored */ |
| 1217 | |
| 1218 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 1219 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u); |
| 1220 | return 1; |
| 1221 | } |
| 1222 | |
| 1223 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1224 | * it up into this table. Returns the volume of datareceived from clients in kbytes |
| 1225 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1226 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1227 | * <not found> is returned. |
| 1228 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1229 | static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1230 | { |
| 1231 | struct stktable *t; |
| 1232 | struct stktable_key *key; |
| 1233 | struct stksess *ts; |
| 1234 | void *ptr; |
| 1235 | |
| 1236 | t = &arg_p[0].data.prx->table; |
| 1237 | |
| 1238 | key = smp_to_stkey(smp, t); |
| 1239 | if (!key) |
| 1240 | return 0; |
| 1241 | |
| 1242 | smp->flags = SMP_F_VOL_TEST; |
| 1243 | smp->type = SMP_T_UINT; |
| 1244 | smp->data.uint = 0; |
| 1245 | |
| 1246 | ts = stktable_lookup_key(t, key); |
| 1247 | if (!ts) /* key not present */ |
| 1248 | return 1; |
| 1249 | |
| 1250 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT); |
| 1251 | if (!ptr) |
| 1252 | return 0; /* parameter not stored */ |
| 1253 | |
| 1254 | smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10; |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
| 1258 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1259 | * it up into this table. Returns the volume of data sent to clients in kbytes |
| 1260 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1261 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1262 | * <not found> is returned. |
| 1263 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1264 | static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1265 | { |
| 1266 | struct stktable *t; |
| 1267 | struct stktable_key *key; |
| 1268 | struct stksess *ts; |
| 1269 | void *ptr; |
| 1270 | |
| 1271 | t = &arg_p[0].data.prx->table; |
| 1272 | |
| 1273 | key = smp_to_stkey(smp, t); |
| 1274 | if (!key) |
| 1275 | return 0; |
| 1276 | |
| 1277 | smp->flags = SMP_F_VOL_TEST; |
| 1278 | smp->type = SMP_T_UINT; |
| 1279 | smp->data.uint = 0; |
| 1280 | |
| 1281 | ts = stktable_lookup_key(t, key); |
| 1282 | if (!ts) /* key not present */ |
| 1283 | return 1; |
| 1284 | |
| 1285 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT); |
| 1286 | if (!ptr) |
| 1287 | return 0; /* parameter not stored */ |
| 1288 | |
| 1289 | smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10; |
| 1290 | return 1; |
| 1291 | } |
| 1292 | |
| 1293 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1294 | * it up into this table. Returns the server ID associated with the key if the |
| 1295 | * key is present in the table, otherwise zero, so that comparisons can be |
| 1296 | * easily performed. If the inspected parameter is not stored in the table, |
| 1297 | * <not found> is returned. |
| 1298 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1299 | static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1300 | { |
| 1301 | struct stktable *t; |
| 1302 | struct stktable_key *key; |
| 1303 | struct stksess *ts; |
| 1304 | void *ptr; |
| 1305 | |
| 1306 | t = &arg_p[0].data.prx->table; |
| 1307 | |
| 1308 | key = smp_to_stkey(smp, t); |
| 1309 | if (!key) |
| 1310 | return 0; |
| 1311 | |
| 1312 | smp->flags = SMP_F_VOL_TEST; |
| 1313 | smp->type = SMP_T_UINT; |
| 1314 | smp->data.uint = 0; |
| 1315 | |
| 1316 | ts = stktable_lookup_key(t, key); |
| 1317 | if (!ts) /* key not present */ |
| 1318 | return 1; |
| 1319 | |
| 1320 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID); |
| 1321 | if (!ptr) |
| 1322 | return 0; /* parameter not stored */ |
| 1323 | |
| 1324 | smp->data.uint = stktable_data_cast(ptr, server_id); |
| 1325 | return 1; |
| 1326 | } |
| 1327 | |
| 1328 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1329 | * it up into this table. Returns the cumulated number of sessions for the |
| 1330 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1331 | * can be easily performed. If the inspected parameter is not stored in the |
| 1332 | * table, <not found> is returned. |
| 1333 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1334 | static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1335 | { |
| 1336 | struct stktable *t; |
| 1337 | struct stktable_key *key; |
| 1338 | struct stksess *ts; |
| 1339 | void *ptr; |
| 1340 | |
| 1341 | t = &arg_p[0].data.prx->table; |
| 1342 | |
| 1343 | key = smp_to_stkey(smp, t); |
| 1344 | if (!key) |
| 1345 | return 0; |
| 1346 | |
| 1347 | smp->flags = SMP_F_VOL_TEST; |
| 1348 | smp->type = SMP_T_UINT; |
| 1349 | smp->data.uint = 0; |
| 1350 | |
| 1351 | ts = stktable_lookup_key(t, key); |
| 1352 | if (!ts) /* key not present */ |
| 1353 | return 1; |
| 1354 | |
| 1355 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT); |
| 1356 | if (!ptr) |
| 1357 | return 0; /* parameter not stored */ |
| 1358 | |
| 1359 | smp->data.uint = stktable_data_cast(ptr, sess_cnt); |
| 1360 | return 1; |
| 1361 | } |
| 1362 | |
| 1363 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1364 | * it up into this table. Returns the session rate the key if the key is |
| 1365 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1366 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1367 | * is returned. |
| 1368 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1369 | static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1370 | { |
| 1371 | struct stktable *t; |
| 1372 | struct stktable_key *key; |
| 1373 | struct stksess *ts; |
| 1374 | void *ptr; |
| 1375 | |
| 1376 | t = &arg_p[0].data.prx->table; |
| 1377 | |
| 1378 | key = smp_to_stkey(smp, t); |
| 1379 | if (!key) |
| 1380 | return 0; |
| 1381 | |
| 1382 | smp->flags = SMP_F_VOL_TEST; |
| 1383 | smp->type = SMP_T_UINT; |
| 1384 | smp->data.uint = 0; |
| 1385 | |
| 1386 | ts = stktable_lookup_key(t, key); |
| 1387 | if (!ts) /* key not present */ |
| 1388 | return 1; |
| 1389 | |
| 1390 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE); |
| 1391 | if (!ptr) |
| 1392 | return 0; /* parameter not stored */ |
| 1393 | |
| 1394 | smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate), |
| 1395 | t->data_arg[STKTABLE_DT_SESS_RATE].u); |
| 1396 | return 1; |
| 1397 | } |
| 1398 | |
| 1399 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1400 | * it up into this table. Returns the amount of concurrent connections tracking |
| 1401 | * the same key if the key is present in the table, otherwise zero, so that |
| 1402 | * comparisons can be easily performed. If the inspected parameter is not |
| 1403 | * stored in the table, <not found> is returned. |
| 1404 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1405 | static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1406 | { |
| 1407 | struct stktable *t; |
| 1408 | struct stktable_key *key; |
| 1409 | struct stksess *ts; |
| 1410 | |
| 1411 | t = &arg_p[0].data.prx->table; |
| 1412 | |
| 1413 | key = smp_to_stkey(smp, t); |
| 1414 | if (!key) |
| 1415 | return 0; |
| 1416 | |
| 1417 | smp->flags = SMP_F_VOL_TEST; |
| 1418 | smp->type = SMP_T_UINT; |
| 1419 | smp->data.uint = 0; |
| 1420 | |
| 1421 | ts = stktable_lookup_key(t, key); |
| 1422 | if (ts) |
| 1423 | smp->data.uint = ts->ref_cnt; |
| 1424 | |
| 1425 | return 1; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1430 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 1431 | { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_BOOL }, |
| 1432 | { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1433 | { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1434 | { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1435 | { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1436 | { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1437 | { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1438 | { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1439 | { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1440 | { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1441 | { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1442 | { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1443 | { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1444 | { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1445 | { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1446 | { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1447 | { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1448 | { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_UINT }, |
| 1449 | { /* END */ }, |
| 1450 | }}; |
| 1451 | |
| 1452 | __attribute__((constructor)) |
| 1453 | static void __stick_table_init(void) |
| 1454 | { |
| 1455 | /* register sample fetch and format conversion keywords */ |
| 1456 | sample_register_convs(&sample_conv_kws); |
| 1457 | } |