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