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> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 15 | #include <errno.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/memory.h> |
| 19 | #include <common/mini-clist.h> |
| 20 | #include <common/standard.h> |
| 21 | #include <common/time.h> |
| 22 | |
| 23 | #include <ebmbtree.h> |
| 24 | #include <ebsttree.h> |
| 25 | |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 26 | #include <types/cli.h> |
| 27 | #include <types/stats.h> |
| 28 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 29 | #include <proto/arg.h> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 30 | #include <proto/cli.h> |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 31 | #include <proto/proto_http.h> |
| 32 | #include <proto/proto_tcp.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 33 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 34 | #include <proto/sample.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 35 | #include <proto/stream.h> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 36 | #include <proto/stream_interface.h> |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 37 | #include <proto/stick_table.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 38 | #include <proto/task.h> |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 39 | #include <proto/peers.h> |
| 40 | #include <types/global.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 42 | /* structure used to return a table key built from a sample */ |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 43 | struct stktable_key *static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 44 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 45 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 46 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 47 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 48 | */ |
| 49 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 50 | { |
| 51 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 52 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 56 | * Kill an stksess (only if its ref_cnt is zero). |
| 57 | */ |
| 58 | void stksess_kill(struct stktable *t, struct stksess *ts) |
| 59 | { |
| 60 | if (ts->ref_cnt) |
| 61 | return; |
| 62 | |
| 63 | eb32_delete(&ts->exp); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 64 | eb32_delete(&ts->upd); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 65 | ebmb_delete(&ts->key); |
| 66 | stksess_free(t, ts); |
| 67 | } |
| 68 | |
| 69 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 70 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 71 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 72 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 73 | 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] | 74 | { |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 75 | if (t->type != SMP_T_STR) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 76 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 77 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 78 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 79 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 85 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 86 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 87 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 88 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 89 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 90 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 91 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 92 | ts->key.node.leaf_p = NULL; |
| 93 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 94 | ts->upd.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 95 | return ts; |
| 96 | } |
| 97 | |
| 98 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 99 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 100 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 101 | */ |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 102 | int stktable_trash_oldest(struct stktable *t, int to_batch) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 103 | { |
| 104 | struct stksess *ts; |
| 105 | struct eb32_node *eb; |
| 106 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 107 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 108 | |
| 109 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 110 | |
| 111 | while (batched < to_batch) { |
| 112 | |
| 113 | if (unlikely(!eb)) { |
| 114 | /* we might have reached the end of the tree, typically because |
| 115 | * <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] | 116 | * half. Let's loop back to the beginning of the tree now if we |
| 117 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 118 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 119 | if (looped) |
| 120 | break; |
| 121 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 122 | eb = eb32_first(&t->exps); |
| 123 | if (likely(!eb)) |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 128 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 129 | eb = eb32_next(eb); |
| 130 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 131 | /* don't delete an entry which is currently referenced */ |
| 132 | if (ts->ref_cnt) |
| 133 | continue; |
| 134 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 135 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 136 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 137 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 138 | if (!tick_isset(ts->expire)) |
| 139 | continue; |
| 140 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 141 | ts->exp.key = ts->expire; |
| 142 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 143 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 144 | if (!eb || eb->key > ts->exp.key) |
| 145 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 146 | |
| 147 | continue; |
| 148 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 149 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 150 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 151 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 152 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 153 | stksess_free(t, ts); |
| 154 | batched++; |
| 155 | } |
| 156 | |
| 157 | return batched; |
| 158 | } |
| 159 | |
| 160 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 161 | * Allocate and initialise a new sticky session. |
| 162 | * The new sticky session is returned or NULL in case of lack of memory. |
| 163 | * 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] | 164 | * stksess_free(). Table <t>'s sticky session counter is increased. If <key> |
| 165 | * is not NULL, it is assigned to the new session. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 166 | */ |
| 167 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 168 | { |
| 169 | struct stksess *ts; |
| 170 | |
| 171 | if (unlikely(t->current == t->size)) { |
| 172 | if ( t->nopurge ) |
| 173 | return NULL; |
| 174 | |
Emeric Brun | fbce6d0 | 2010-09-23 18:10:00 +0200 | [diff] [blame] | 175 | if (!stktable_trash_oldest(t, (t->size >> 8) + 1)) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | |
Vincent Bernat | ef8f4fe | 2016-11-17 15:42:40 +0100 | [diff] [blame] | 179 | ts = pool_alloc2(t->pool); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 180 | if (ts) { |
| 181 | t->current++; |
Willy Tarreau | 5179146 | 2016-11-18 18:21:39 +0100 | [diff] [blame] | 182 | ts = (void *)ts + t->data_size; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 183 | stksess_init(t, ts); |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 184 | if (key) |
| 185 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | return ts; |
| 189 | } |
| 190 | |
| 191 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 192 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 193 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 194 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 195 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 196 | { |
| 197 | struct ebmb_node *eb; |
| 198 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 199 | if (t->type == SMP_T_STR) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 200 | 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] | 201 | else |
| 202 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 203 | |
| 204 | if (unlikely(!eb)) { |
| 205 | /* no session found */ |
| 206 | return NULL; |
| 207 | } |
| 208 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 209 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 212 | /* Lookup and touch <key> in <table>, or create the entry if it does not exist. |
| 213 | * This is mainly used for situations where we want to refresh a key's usage so |
| 214 | * that it does not expire, and we want to have it created if it was not there. |
| 215 | * The stksess is returned, or NULL if it could not be created. |
| 216 | */ |
| 217 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key) |
| 218 | { |
| 219 | struct stksess *ts; |
| 220 | |
| 221 | ts = stktable_lookup_key(table, key); |
| 222 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 223 | return stktable_touch(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 224 | |
| 225 | /* entry does not exist, initialize a new one */ |
| 226 | ts = stksess_new(table, key); |
| 227 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 228 | stktable_store(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 229 | return ts; |
| 230 | } |
| 231 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 232 | /* |
| 233 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 234 | * Returns pointer on requested sticky session or NULL if none was found. |
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 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 237 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 238 | struct ebmb_node *eb; |
| 239 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 240 | if (t->type == SMP_T_STR) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 241 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 242 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 243 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 245 | if (unlikely(!eb)) |
| 246 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 247 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 248 | return ebmb_entry(eb, struct stksess, key); |
| 249 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 250 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 251 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 252 | * The table's expiration timer is updated if set. |
| 253 | */ |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 254 | struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts, |
| 255 | int local, int expire) |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 256 | { |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 257 | struct eb32_node * eb; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 258 | ts->expire = expire; |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 259 | if (t->expire) { |
| 260 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 261 | task_queue(t->exp_task); |
| 262 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 263 | |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 264 | /* If sync is enabled and update is local */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 265 | if (t->sync_task && local) { |
Emeric Brun | c703a9d | 2015-09-22 15:05:06 +0200 | [diff] [blame] | 266 | /* If this entry is not in the tree |
| 267 | or not scheduled for at least one peer */ |
| 268 | if (!ts->upd.node.leaf_p |
| 269 | || (int)(t->commitupdate - ts->upd.key) >= 0 |
| 270 | || (int)(ts->upd.key - t->localupdate) >= 0) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 271 | ts->upd.key = ++t->update; |
| 272 | t->localupdate = t->update; |
| 273 | eb32_delete(&ts->upd); |
| 274 | eb = eb32_insert(&t->updates, &ts->upd); |
| 275 | if (eb != &ts->upd) { |
| 276 | eb32_delete(eb); |
| 277 | eb32_insert(&t->updates, &ts->upd); |
| 278 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 279 | } |
| 280 | task_wakeup(t->sync_task, TASK_WOKEN_MSG); |
| 281 | } |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 282 | return ts; |
| 283 | } |
| 284 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 285 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 286 | * The table's expiration timer is updated if set. The date of expiration coming from |
| 287 | * <t> stick-table configuration. |
| 288 | */ |
| 289 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local) |
| 290 | { |
| 291 | int expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 292 | |
| 293 | return stktable_touch_with_exp(t, ts, local, expire); |
| 294 | } |
| 295 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 296 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 297 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 298 | * is set. <ts> is returned. |
| 299 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 300 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 301 | { |
| 302 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 303 | stktable_touch(t, ts, local); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 304 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 305 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 306 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 309 | /* Same function as stktable_store(), but with <expire> as supplementary argument |
| 310 | * to set the date of expiration of <ts> new sticky session thanks to |
| 311 | * stktable_touch_with_exp(). |
| 312 | */ |
| 313 | struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts, |
| 314 | int local, int expire) |
| 315 | { |
| 316 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
| 317 | stktable_touch_with_exp(t, ts, local, expire); |
| 318 | ts->exp.key = ts->expire; |
| 319 | eb32_insert(&t->exps, &ts->exp); |
| 320 | return ts; |
| 321 | } |
| 322 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 323 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 324 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 325 | * could be created. The entry's expiration is updated. |
| 326 | */ |
| 327 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 328 | { |
| 329 | struct stksess *ts; |
| 330 | |
| 331 | if (!key) |
| 332 | return NULL; |
| 333 | |
| 334 | ts = stktable_lookup_key(table, key); |
| 335 | if (ts == NULL) { |
| 336 | /* entry does not exist, initialize a new one */ |
| 337 | ts = stksess_new(table, key); |
| 338 | if (!ts) |
| 339 | return NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 340 | stktable_store(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 341 | } |
| 342 | else |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 343 | stktable_touch(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 344 | return ts; |
| 345 | } |
| 346 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 347 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 348 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 349 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 350 | */ |
| 351 | static int stktable_trash_expired(struct stktable *t) |
| 352 | { |
| 353 | struct stksess *ts; |
| 354 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 355 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 356 | |
| 357 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 358 | |
| 359 | while (1) { |
| 360 | if (unlikely(!eb)) { |
| 361 | /* we might have reached the end of the tree, typically because |
| 362 | * <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] | 363 | * half. Let's loop back to the beginning of the tree now if we |
| 364 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 365 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 366 | if (looped) |
| 367 | break; |
| 368 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 369 | eb = eb32_first(&t->exps); |
| 370 | if (likely(!eb)) |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 375 | /* timer not expired yet, revisit it later */ |
| 376 | t->exp_next = eb->key; |
| 377 | return t->exp_next; |
| 378 | } |
| 379 | |
| 380 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 381 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 382 | eb = eb32_next(eb); |
| 383 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 384 | /* don't delete an entry which is currently referenced */ |
| 385 | if (ts->ref_cnt) |
| 386 | continue; |
| 387 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 388 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 389 | |
| 390 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 391 | if (!tick_isset(ts->expire)) |
| 392 | continue; |
| 393 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 394 | ts->exp.key = ts->expire; |
| 395 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 396 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 397 | if (!eb || eb->key > ts->exp.key) |
| 398 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | |
| 402 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 403 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 404 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 405 | stksess_free(t, ts); |
| 406 | } |
| 407 | |
| 408 | /* We have found no task to expire in any tree */ |
| 409 | t->exp_next = TICK_ETERNITY; |
| 410 | return t->exp_next; |
| 411 | } |
| 412 | |
| 413 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 414 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 415 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 416 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 417 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 418 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 419 | struct stktable *t = task->context; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 420 | |
| 421 | task->expire = stktable_trash_expired(t); |
| 422 | return task; |
| 423 | } |
| 424 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 425 | /* 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] | 426 | int stktable_init(struct stktable *t) |
| 427 | { |
| 428 | if (t->size) { |
| 429 | memset(&t->keys, 0, sizeof(t->keys)); |
| 430 | memset(&t->exps, 0, sizeof(t->exps)); |
Emeric Brun | 1c6235d | 2015-12-16 15:28:12 +0100 | [diff] [blame] | 431 | t->updates = EB_ROOT_UNIQUE; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 432 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 433 | 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] | 434 | |
| 435 | t->exp_next = TICK_ETERNITY; |
| 436 | if ( t->expire ) { |
| 437 | t->exp_task = task_new(); |
| 438 | t->exp_task->process = process_table_expire; |
| 439 | t->exp_task->expire = TICK_ETERNITY; |
| 440 | t->exp_task->context = (void *)t; |
| 441 | } |
Willy Tarreau | c8b6791 | 2015-05-01 18:29:57 +0200 | [diff] [blame] | 442 | 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] | 443 | peers_register_table(t->peers.p, t); |
| 444 | } |
| 445 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 446 | return t->pool != NULL; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Configuration keywords of known table types |
| 453 | */ |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 454 | struct stktable_type stktable_types[SMP_TYPES] = { |
| 455 | [SMP_T_SINT] = { "integer", 0, 4 }, |
| 456 | [SMP_T_IPV4] = { "ip", 0, 4 }, |
| 457 | [SMP_T_IPV6] = { "ipv6", 0, 16 }, |
| 458 | [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 }, |
| 459 | [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 } |
| 460 | }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Parse table type configuration. |
| 464 | * Returns 0 on successful parsing, else 1. |
| 465 | * <myidx> is set at next configuration <args> index. |
| 466 | */ |
| 467 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 468 | { |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 469 | for (*type = 0; *type < SMP_TYPES; (*type)++) { |
| 470 | if (!stktable_types[*type].kw) |
| 471 | continue; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 472 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 473 | continue; |
| 474 | |
| 475 | *key_size = stktable_types[*type].default_size; |
| 476 | (*myidx)++; |
| 477 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 478 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 479 | if (strcmp("len", args[*myidx]) == 0) { |
| 480 | (*myidx)++; |
| 481 | *key_size = atol(args[*myidx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 482 | if (!*key_size) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 483 | break; |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 484 | if (*type == SMP_T_STR) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 485 | /* null terminated string needs +1 for '\0'. */ |
| 486 | (*key_size)++; |
| 487 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 488 | (*myidx)++; |
| 489 | } |
| 490 | } |
| 491 | return 0; |
| 492 | } |
| 493 | return 1; |
| 494 | } |
| 495 | |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 496 | /* Prepares a stktable_key from a sample <smp> to search into table <t>. |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 497 | * Note that the sample *is* modified and that the returned key may point |
| 498 | * to it, so the sample must not be modified afterwards before the lookup. |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 499 | * Returns NULL if the sample could not be converted (eg: no matching type), |
| 500 | * otherwise a pointer to the static stktable_key filled with what is needed |
| 501 | * for the lookup. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 502 | */ |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 503 | struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 504 | { |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 505 | /* Convert sample. */ |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 506 | if (!sample_convert(smp, t->type)) |
Willy Tarreau | 7fc1c6e | 2012-04-26 11:03:17 +0200 | [diff] [blame] | 507 | return NULL; |
| 508 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 509 | /* Fill static_table_key. */ |
| 510 | switch (t->type) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 511 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 512 | case SMP_T_IPV4: |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 513 | static_table_key->key = &smp->data.u.ipv4; |
| 514 | static_table_key->key_len = 4; |
| 515 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 516 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 517 | case SMP_T_IPV6: |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 518 | static_table_key->key = &smp->data.u.ipv6; |
| 519 | static_table_key->key_len = 16; |
| 520 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 521 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 522 | case SMP_T_SINT: |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 523 | /* The stick table require a 32bit unsigned int, "sint" is a |
| 524 | * signed 64 it, so we can convert it inplace. |
| 525 | */ |
| 526 | *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint; |
| 527 | static_table_key->key = &smp->data.u.sint; |
| 528 | static_table_key->key_len = 4; |
| 529 | break; |
| 530 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 531 | case SMP_T_STR: |
Willy Tarreau | ce6955e | 2016-08-09 11:59:12 +0200 | [diff] [blame] | 532 | if (!smp_make_safe(smp)) |
| 533 | return NULL; |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 534 | static_table_key->key = smp->data.u.str.str; |
| 535 | static_table_key->key_len = smp->data.u.str.len; |
| 536 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 537 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 538 | case SMP_T_BIN: |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 539 | if (smp->data.u.str.len < t->key_size) { |
| 540 | /* This type needs padding with 0. */ |
Willy Tarreau | f65c6c0 | 2016-08-09 12:08:41 +0200 | [diff] [blame] | 541 | if (!smp_make_rw(smp)) |
| 542 | return NULL; |
| 543 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 544 | if (smp->data.u.str.size < t->key_size) |
| 545 | if (!smp_dup(smp)) |
| 546 | return NULL; |
| 547 | if (smp->data.u.str.size < t->key_size) |
| 548 | return NULL; |
| 549 | memset(smp->data.u.str.str + smp->data.u.str.len, 0, |
| 550 | t->key_size - smp->data.u.str.len); |
| 551 | smp->data.u.str.len = t->key_size; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 552 | } |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 553 | static_table_key->key = smp->data.u.str.str; |
| 554 | static_table_key->key_len = smp->data.u.str.len; |
| 555 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 556 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 557 | default: /* impossible case. */ |
| 558 | return NULL; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 561 | return static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 565 | * Process a fetch + format conversion as defined by the sample expression <expr> |
| 566 | * on request or response considering the <opt> parameter. Returns either NULL if |
| 567 | * no key could be extracted, or a pointer to the converted result stored in |
| 568 | * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset |
| 569 | * 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] | 570 | * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present |
| 571 | * without SMP_OPT_FINAL). The output will be usable like this : |
| 572 | * |
| 573 | * return MAY_CHANGE FINAL Meaning for the sample |
| 574 | * NULL 0 * Not present and will never be (eg: header) |
| 575 | * NULL 1 0 Not present or unstable, could change (eg: req_len) |
| 576 | * NULL 1 1 Not present, will not change anymore |
| 577 | * smp 0 * Present and will not change (eg: header) |
| 578 | * smp 1 0 not possible |
| 579 | * smp 1 1 Present, last known value (eg: request length) |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 580 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 581 | 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] | 582 | unsigned int opt, struct sample_expr *expr, struct sample *smp) |
| 583 | { |
| 584 | if (smp) |
| 585 | memset(smp, 0, sizeof(*smp)); |
| 586 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 587 | smp = sample_process(px, sess, strm, opt, expr, smp); |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 588 | if (!smp) |
| 589 | return NULL; |
| 590 | |
| 591 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 592 | return NULL; /* we can only use stable samples */ |
| 593 | |
| 594 | return smp_to_stkey(smp, t); |
| 595 | } |
| 596 | |
| 597 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 598 | * 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] | 599 | * type <table_type>, otherwise zero. Used in configuration check. |
| 600 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 601 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 602 | { |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 603 | int out_type; |
| 604 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 605 | if (table_type >= SMP_TYPES || !stktable_types[table_type].kw) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 606 | return 0; |
| 607 | |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 608 | out_type = smp_expr_output_type(expr); |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 609 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 610 | /* Convert sample. */ |
| 611 | if (!sample_casts[out_type][table_type]) |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 612 | return 0; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 613 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 614 | return 1; |
| 615 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 616 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 617 | /* Extra data types processing : after the last one, some room may remain |
| 618 | * before STKTABLE_DATA_TYPES that may be used to register extra data types |
| 619 | * at run time. |
| 620 | */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 621 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 622 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT }, |
Thierry FOURNIER | 3cf1111 | 2015-07-28 08:57:05 +0200 | [diff] [blame] | 623 | [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT }, |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 624 | [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT }, |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 625 | [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] | 626 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT }, |
| 627 | [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 628 | [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT }, |
| 629 | [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT }, |
| 630 | [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 631 | [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT }, |
| 632 | [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 633 | [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT }, |
| 634 | [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 635 | [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL }, |
| 636 | [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 637 | [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL }, |
| 638 | [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] | 639 | }; |
| 640 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 641 | /* Registers stick-table extra data type with index <idx>, name <name>, type |
| 642 | * <std_type> and arg type <arg_type>. If the index is negative, the next free |
| 643 | * index is automatically allocated. The allocated index is returned, or -1 if |
| 644 | * no free index was found or <name> was already registered. The <name> is used |
| 645 | * directly as a pointer, so if it's not stable, the caller must allocate it. |
| 646 | */ |
| 647 | int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type) |
| 648 | { |
| 649 | if (idx < 0) { |
| 650 | for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) { |
| 651 | if (!stktable_data_types[idx].name) |
| 652 | break; |
| 653 | |
| 654 | if (strcmp(stktable_data_types[idx].name, name) == 0) |
| 655 | return -1; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | if (idx >= STKTABLE_DATA_TYPES) |
| 660 | return -1; |
| 661 | |
| 662 | if (stktable_data_types[idx].name != NULL) |
| 663 | return -1; |
| 664 | |
| 665 | stktable_data_types[idx].name = name; |
| 666 | stktable_data_types[idx].std_type = std_type; |
| 667 | stktable_data_types[idx].arg_type = arg_type; |
| 668 | return idx; |
| 669 | } |
| 670 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 671 | /* |
| 672 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 673 | * or <0 if not found. |
| 674 | */ |
| 675 | int stktable_get_data_type(char *name) |
| 676 | { |
| 677 | int type; |
| 678 | |
| 679 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 680 | if (!stktable_data_types[type].name) |
| 681 | continue; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 682 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 683 | return type; |
| 684 | } |
| 685 | return -1; |
| 686 | } |
| 687 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 688 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 689 | * it up into this table. Returns true if found, false otherwise. The input |
| 690 | * type is STR so that input samples are converted to string (since all types |
| 691 | * can be converted to strings), then the function casts the string again into |
| 692 | * the table's type. This is a double conversion, but in the future we might |
| 693 | * support automatic input types to perform the cast on the fly. |
| 694 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 695 | 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] | 696 | { |
| 697 | struct stktable *t; |
| 698 | struct stktable_key *key; |
| 699 | struct stksess *ts; |
| 700 | |
| 701 | t = &arg_p[0].data.prx->table; |
| 702 | |
| 703 | key = smp_to_stkey(smp, t); |
| 704 | if (!key) |
| 705 | return 0; |
| 706 | |
| 707 | ts = stktable_lookup_key(t, key); |
| 708 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 709 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 710 | smp->data.u.sint = !!ts; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 711 | smp->flags = SMP_F_VOL_TEST; |
| 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 716 | * it up into this table. Returns the data rate received from clients in bytes/s |
| 717 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 718 | * be easily performed. If the inspected parameter is not stored in the table, |
| 719 | * <not found> is returned. |
| 720 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 721 | 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] | 722 | { |
| 723 | struct stktable *t; |
| 724 | struct stktable_key *key; |
| 725 | struct stksess *ts; |
| 726 | void *ptr; |
| 727 | |
| 728 | t = &arg_p[0].data.prx->table; |
| 729 | |
| 730 | key = smp_to_stkey(smp, t); |
| 731 | if (!key) |
| 732 | return 0; |
| 733 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 734 | ts = stktable_lookup_key(t, key); |
| 735 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 736 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 737 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 738 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 739 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 740 | if (!ts) /* key not present */ |
| 741 | return 1; |
| 742 | |
| 743 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE); |
| 744 | if (!ptr) |
| 745 | return 0; /* parameter not stored */ |
| 746 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 747 | 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] | 748 | t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u); |
| 749 | return 1; |
| 750 | } |
| 751 | |
| 752 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 753 | * it up into this table. Returns the cumulated number of connections for the key |
| 754 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 755 | * be easily performed. If the inspected parameter is not stored in the table, |
| 756 | * <not found> is returned. |
| 757 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 758 | 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] | 759 | { |
| 760 | struct stktable *t; |
| 761 | struct stktable_key *key; |
| 762 | struct stksess *ts; |
| 763 | void *ptr; |
| 764 | |
| 765 | t = &arg_p[0].data.prx->table; |
| 766 | |
| 767 | key = smp_to_stkey(smp, t); |
| 768 | if (!key) |
| 769 | return 0; |
| 770 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 771 | ts = stktable_lookup_key(t, key); |
| 772 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 777 | if (!ts) /* key not present */ |
| 778 | return 1; |
| 779 | |
| 780 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT); |
| 781 | if (!ptr) |
| 782 | return 0; /* parameter not stored */ |
| 783 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 784 | smp->data.u.sint = stktable_data_cast(ptr, conn_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 785 | return 1; |
| 786 | } |
| 787 | |
| 788 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 789 | * it up into this table. Returns the number of concurrent connections for the |
| 790 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 791 | * can be easily performed. If the inspected parameter is not stored in the |
| 792 | * table, <not found> is returned. |
| 793 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 794 | 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] | 795 | { |
| 796 | struct stktable *t; |
| 797 | struct stktable_key *key; |
| 798 | struct stksess *ts; |
| 799 | void *ptr; |
| 800 | |
| 801 | t = &arg_p[0].data.prx->table; |
| 802 | |
| 803 | key = smp_to_stkey(smp, t); |
| 804 | if (!key) |
| 805 | return 0; |
| 806 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 807 | ts = stktable_lookup_key(t, key); |
| 808 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 809 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 810 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 811 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 812 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 813 | if (!ts) /* key not present */ |
| 814 | return 1; |
| 815 | |
| 816 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR); |
| 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 = stktable_data_cast(ptr, conn_cur); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 825 | * it up into this table. Returns the rate of incoming connections from the key |
| 826 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 827 | * be easily performed. If the inspected parameter is not stored in the table, |
| 828 | * <not found> is returned. |
| 829 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 830 | 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] | 831 | { |
| 832 | struct stktable *t; |
| 833 | struct stktable_key *key; |
| 834 | struct stksess *ts; |
| 835 | void *ptr; |
| 836 | |
| 837 | t = &arg_p[0].data.prx->table; |
| 838 | |
| 839 | key = smp_to_stkey(smp, t); |
| 840 | if (!key) |
| 841 | return 0; |
| 842 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 843 | ts = stktable_lookup_key(t, key); |
| 844 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 845 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 846 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 847 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 848 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 849 | if (!ts) /* key not present */ |
| 850 | return 1; |
| 851 | |
| 852 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_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, conn_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 857 | t->data_arg[STKTABLE_DT_CONN_RATE].u); |
| 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 862 | * it up into this table. Returns the data rate sent to clients in bytes/s |
| 863 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 864 | * be easily performed. If the inspected parameter is not stored in the table, |
| 865 | * <not found> is returned. |
| 866 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 867 | 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] | 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 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 880 | ts = stktable_lookup_key(t, key); |
| 881 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 882 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 883 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 884 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 885 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 886 | if (!ts) /* key not present */ |
| 887 | return 1; |
| 888 | |
| 889 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE); |
| 890 | if (!ptr) |
| 891 | return 0; /* parameter not stored */ |
| 892 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 893 | 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] | 894 | t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u); |
| 895 | return 1; |
| 896 | } |
| 897 | |
| 898 | /* 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] | 899 | * it up into this table. Returns the value of the GPT0 tag for the key |
| 900 | * if the key is present in the table, otherwise false, so that comparisons can |
| 901 | * be easily performed. If the inspected parameter is not stored in the table, |
| 902 | * <not found> is returned. |
| 903 | */ |
| 904 | static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private) |
| 905 | { |
| 906 | struct stktable *t; |
| 907 | struct stktable_key *key; |
| 908 | struct stksess *ts; |
| 909 | void *ptr; |
| 910 | |
| 911 | t = &arg_p[0].data.prx->table; |
| 912 | |
| 913 | key = smp_to_stkey(smp, t); |
| 914 | if (!key) |
| 915 | return 0; |
| 916 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 917 | ts = stktable_lookup_key(t, key); |
| 918 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 919 | smp->flags = SMP_F_VOL_TEST; |
| 920 | smp->data.type = SMP_T_SINT; |
| 921 | smp->data.u.sint = 0; |
| 922 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 923 | if (!ts) /* key not present */ |
| 924 | return 1; |
| 925 | |
| 926 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0); |
| 927 | if (!ptr) |
| 928 | return 0; /* parameter not stored */ |
| 929 | |
| 930 | smp->data.u.sint = stktable_data_cast(ptr, gpt0); |
| 931 | return 1; |
| 932 | } |
| 933 | |
| 934 | /* 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] | 935 | * it up into this table. Returns the value of the GPC0 counter for the key |
| 936 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 937 | * be easily performed. If the inspected parameter is not stored in the table, |
| 938 | * <not found> is returned. |
| 939 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 940 | 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] | 941 | { |
| 942 | struct stktable *t; |
| 943 | struct stktable_key *key; |
| 944 | struct stksess *ts; |
| 945 | void *ptr; |
| 946 | |
| 947 | t = &arg_p[0].data.prx->table; |
| 948 | |
| 949 | key = smp_to_stkey(smp, t); |
| 950 | if (!key) |
| 951 | return 0; |
| 952 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 953 | ts = stktable_lookup_key(t, key); |
| 954 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 955 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 956 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 957 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 958 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 959 | if (!ts) /* key not present */ |
| 960 | return 1; |
| 961 | |
| 962 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0); |
| 963 | if (!ptr) |
| 964 | return 0; /* parameter not stored */ |
| 965 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 966 | smp->data.u.sint = stktable_data_cast(ptr, gpc0); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 967 | return 1; |
| 968 | } |
| 969 | |
| 970 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 971 | * it up into this table. Returns the event rate of the GPC0 counter for the key |
| 972 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 973 | * be easily performed. If the inspected parameter is not stored in the table, |
| 974 | * <not found> is returned. |
| 975 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 976 | 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] | 977 | { |
| 978 | struct stktable *t; |
| 979 | struct stktable_key *key; |
| 980 | struct stksess *ts; |
| 981 | void *ptr; |
| 982 | |
| 983 | t = &arg_p[0].data.prx->table; |
| 984 | |
| 985 | key = smp_to_stkey(smp, t); |
| 986 | if (!key) |
| 987 | return 0; |
| 988 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 989 | ts = stktable_lookup_key(t, key); |
| 990 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 991 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 992 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 993 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 994 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 995 | if (!ts) /* key not present */ |
| 996 | return 1; |
| 997 | |
| 998 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE); |
| 999 | if (!ptr) |
| 1000 | return 0; /* parameter not stored */ |
| 1001 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1002 | 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] | 1003 | t->data_arg[STKTABLE_DT_GPC0_RATE].u); |
| 1004 | return 1; |
| 1005 | } |
| 1006 | |
| 1007 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1008 | * it up into this table. Returns the cumulated number of HTTP request errors |
| 1009 | * for the key if the key is present in the table, otherwise zero, so that |
| 1010 | * comparisons can be easily performed. If the inspected parameter is not stored |
| 1011 | * in the table, <not found> is returned. |
| 1012 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1013 | 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] | 1014 | { |
| 1015 | struct stktable *t; |
| 1016 | struct stktable_key *key; |
| 1017 | struct stksess *ts; |
| 1018 | void *ptr; |
| 1019 | |
| 1020 | t = &arg_p[0].data.prx->table; |
| 1021 | |
| 1022 | key = smp_to_stkey(smp, t); |
| 1023 | if (!key) |
| 1024 | return 0; |
| 1025 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1026 | ts = stktable_lookup_key(t, key); |
| 1027 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1028 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1029 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1030 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1031 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1032 | if (!ts) /* key not present */ |
| 1033 | return 1; |
| 1034 | |
| 1035 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 1036 | if (!ptr) |
| 1037 | return 0; /* parameter not stored */ |
| 1038 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1039 | smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1040 | return 1; |
| 1041 | } |
| 1042 | |
| 1043 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1044 | * it up into this table. Returns the HTTP request error rate the key |
| 1045 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1046 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1047 | * <not found> is returned. |
| 1048 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1049 | 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] | 1050 | { |
| 1051 | struct stktable *t; |
| 1052 | struct stktable_key *key; |
| 1053 | struct stksess *ts; |
| 1054 | void *ptr; |
| 1055 | |
| 1056 | t = &arg_p[0].data.prx->table; |
| 1057 | |
| 1058 | key = smp_to_stkey(smp, t); |
| 1059 | if (!key) |
| 1060 | return 0; |
| 1061 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1062 | ts = stktable_lookup_key(t, key); |
| 1063 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1064 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1065 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1066 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1067 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1068 | if (!ts) /* key not present */ |
| 1069 | return 1; |
| 1070 | |
| 1071 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 1072 | if (!ptr) |
| 1073 | return 0; /* parameter not stored */ |
| 1074 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1075 | 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] | 1076 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u); |
| 1077 | return 1; |
| 1078 | } |
| 1079 | |
| 1080 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1081 | * it up into this table. Returns the cumulated number of HTTP request for the |
| 1082 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1083 | * can be easily performed. If the inspected parameter is not stored in the |
| 1084 | * table, <not found> is returned. |
| 1085 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1086 | 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] | 1087 | { |
| 1088 | struct stktable *t; |
| 1089 | struct stktable_key *key; |
| 1090 | struct stksess *ts; |
| 1091 | void *ptr; |
| 1092 | |
| 1093 | t = &arg_p[0].data.prx->table; |
| 1094 | |
| 1095 | key = smp_to_stkey(smp, t); |
| 1096 | if (!key) |
| 1097 | return 0; |
| 1098 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1099 | ts = stktable_lookup_key(t, key); |
| 1100 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1101 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1102 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1103 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1104 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1105 | if (!ts) /* key not present */ |
| 1106 | return 1; |
| 1107 | |
| 1108 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1109 | if (!ptr) |
| 1110 | return 0; /* parameter not stored */ |
| 1111 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1112 | smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 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 rate the key if the key is |
| 1118 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1119 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1120 | * is returned. |
| 1121 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1122 | 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] | 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 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1135 | ts = stktable_lookup_key(t, key); |
| 1136 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1137 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1138 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1139 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1140 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1141 | if (!ts) /* key not present */ |
| 1142 | return 1; |
| 1143 | |
| 1144 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1145 | if (!ptr) |
| 1146 | return 0; /* parameter not stored */ |
| 1147 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1148 | 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] | 1149 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u); |
| 1150 | return 1; |
| 1151 | } |
| 1152 | |
| 1153 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1154 | * it up into this table. Returns the volume of datareceived from clients in kbytes |
| 1155 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1156 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1157 | * <not found> is returned. |
| 1158 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1159 | 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] | 1160 | { |
| 1161 | struct stktable *t; |
| 1162 | struct stktable_key *key; |
| 1163 | struct stksess *ts; |
| 1164 | void *ptr; |
| 1165 | |
| 1166 | t = &arg_p[0].data.prx->table; |
| 1167 | |
| 1168 | key = smp_to_stkey(smp, t); |
| 1169 | if (!key) |
| 1170 | return 0; |
| 1171 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1172 | ts = stktable_lookup_key(t, key); |
| 1173 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1174 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1175 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1176 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1177 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1178 | if (!ts) /* key not present */ |
| 1179 | return 1; |
| 1180 | |
| 1181 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT); |
| 1182 | if (!ptr) |
| 1183 | return 0; /* parameter not stored */ |
| 1184 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1185 | smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1186 | return 1; |
| 1187 | } |
| 1188 | |
| 1189 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1190 | * it up into this table. Returns the volume of data sent to clients in kbytes |
| 1191 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1192 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1193 | * <not found> is returned. |
| 1194 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1195 | 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] | 1196 | { |
| 1197 | struct stktable *t; |
| 1198 | struct stktable_key *key; |
| 1199 | struct stksess *ts; |
| 1200 | void *ptr; |
| 1201 | |
| 1202 | t = &arg_p[0].data.prx->table; |
| 1203 | |
| 1204 | key = smp_to_stkey(smp, t); |
| 1205 | if (!key) |
| 1206 | return 0; |
| 1207 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1208 | ts = stktable_lookup_key(t, key); |
| 1209 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1210 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1211 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1212 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1213 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1214 | if (!ts) /* key not present */ |
| 1215 | return 1; |
| 1216 | |
| 1217 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT); |
| 1218 | if (!ptr) |
| 1219 | return 0; /* parameter not stored */ |
| 1220 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1221 | smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1222 | return 1; |
| 1223 | } |
| 1224 | |
| 1225 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1226 | * it up into this table. Returns the server ID associated with the key if the |
| 1227 | * key is present in the table, otherwise zero, so that comparisons can be |
| 1228 | * easily performed. If the inspected parameter is not stored in the table, |
| 1229 | * <not found> is returned. |
| 1230 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1231 | 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] | 1232 | { |
| 1233 | struct stktable *t; |
| 1234 | struct stktable_key *key; |
| 1235 | struct stksess *ts; |
| 1236 | void *ptr; |
| 1237 | |
| 1238 | t = &arg_p[0].data.prx->table; |
| 1239 | |
| 1240 | key = smp_to_stkey(smp, t); |
| 1241 | if (!key) |
| 1242 | return 0; |
| 1243 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1244 | ts = stktable_lookup_key(t, key); |
| 1245 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1246 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1247 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1248 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1249 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1250 | if (!ts) /* key not present */ |
| 1251 | return 1; |
| 1252 | |
| 1253 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID); |
| 1254 | if (!ptr) |
| 1255 | return 0; /* parameter not stored */ |
| 1256 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1257 | smp->data.u.sint = stktable_data_cast(ptr, server_id); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1258 | return 1; |
| 1259 | } |
| 1260 | |
| 1261 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1262 | * it up into this table. Returns the cumulated number of sessions for the |
| 1263 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1264 | * can be easily performed. If the inspected parameter is not stored in the |
| 1265 | * table, <not found> is returned. |
| 1266 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1267 | 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] | 1268 | { |
| 1269 | struct stktable *t; |
| 1270 | struct stktable_key *key; |
| 1271 | struct stksess *ts; |
| 1272 | void *ptr; |
| 1273 | |
| 1274 | t = &arg_p[0].data.prx->table; |
| 1275 | |
| 1276 | key = smp_to_stkey(smp, t); |
| 1277 | if (!key) |
| 1278 | return 0; |
| 1279 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1280 | ts = stktable_lookup_key(t, key); |
| 1281 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1282 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1283 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1284 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1285 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1286 | if (!ts) /* key not present */ |
| 1287 | return 1; |
| 1288 | |
| 1289 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT); |
| 1290 | if (!ptr) |
| 1291 | return 0; /* parameter not stored */ |
| 1292 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1293 | smp->data.u.sint = stktable_data_cast(ptr, sess_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1294 | return 1; |
| 1295 | } |
| 1296 | |
| 1297 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1298 | * it up into this table. Returns the session rate the key if the key is |
| 1299 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1300 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1301 | * is returned. |
| 1302 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1303 | 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] | 1304 | { |
| 1305 | struct stktable *t; |
| 1306 | struct stktable_key *key; |
| 1307 | struct stksess *ts; |
| 1308 | void *ptr; |
| 1309 | |
| 1310 | t = &arg_p[0].data.prx->table; |
| 1311 | |
| 1312 | key = smp_to_stkey(smp, t); |
| 1313 | if (!key) |
| 1314 | return 0; |
| 1315 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1316 | ts = stktable_lookup_key(t, key); |
| 1317 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1318 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1319 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1320 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1321 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1322 | if (!ts) /* key not present */ |
| 1323 | return 1; |
| 1324 | |
| 1325 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE); |
| 1326 | if (!ptr) |
| 1327 | return 0; /* parameter not stored */ |
| 1328 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1329 | 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] | 1330 | t->data_arg[STKTABLE_DT_SESS_RATE].u); |
| 1331 | return 1; |
| 1332 | } |
| 1333 | |
| 1334 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1335 | * it up into this table. Returns the amount of concurrent connections tracking |
| 1336 | * the same key if the key is present in the table, otherwise zero, so that |
| 1337 | * comparisons can be easily performed. If the inspected parameter is not |
| 1338 | * stored in the table, <not found> is returned. |
| 1339 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1340 | 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] | 1341 | { |
| 1342 | struct stktable *t; |
| 1343 | struct stktable_key *key; |
| 1344 | struct stksess *ts; |
| 1345 | |
| 1346 | t = &arg_p[0].data.prx->table; |
| 1347 | |
| 1348 | key = smp_to_stkey(smp, t); |
| 1349 | if (!key) |
| 1350 | return 0; |
| 1351 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1352 | ts = stktable_lookup_key(t, key); |
| 1353 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1354 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1355 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1356 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1357 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1358 | if (ts) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1359 | smp->data.u.sint = ts->ref_cnt; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1360 | |
| 1361 | return 1; |
| 1362 | } |
| 1363 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1364 | /* Always returns 1. */ |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1365 | 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] | 1366 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1367 | { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1368 | struct stksess *ts; |
| 1369 | struct stkctr *stkctr; |
| 1370 | |
| 1371 | /* Extract the stksess, return OK if no stksess available. */ |
| 1372 | if (s) |
| 1373 | stkctr = &s->stkctr[rule->arg.gpc.sc]; |
| 1374 | else |
| 1375 | stkctr = &sess->stkctr[rule->arg.gpc.sc]; |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1376 | |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1377 | ts = stkctr_entry(stkctr); |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1378 | if (ts) { |
| 1379 | void *ptr1, *ptr2; |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1380 | |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1381 | /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */ |
| 1382 | ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE); |
| 1383 | if (ptr1) |
| 1384 | update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate), |
| 1385 | stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1); |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1386 | |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1387 | ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0); |
| 1388 | if (ptr2) |
| 1389 | stktable_data_cast(ptr2, gpc0)++; |
| 1390 | |
| 1391 | /* If data was modified, we need to touch to re-schedule sync */ |
| 1392 | if (ptr1 || ptr2) |
| 1393 | stktable_touch(stkctr->table, ts, 1); |
| 1394 | } |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1395 | return ACT_RET_CONT; |
| 1396 | } |
| 1397 | |
| 1398 | /* This function is a common parser for using variables. It understands |
| 1399 | * the formats: |
| 1400 | * |
| 1401 | * sc-inc-gpc0(<stick-table ID>) |
| 1402 | * |
| 1403 | * It returns 0 if fails and <err> is filled with an error message. Otherwise, |
| 1404 | * it returns 1 and the variable <expr> is filled with the pointer to the |
| 1405 | * expression to execute. |
| 1406 | */ |
| 1407 | static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px, |
| 1408 | struct act_rule *rule, char **err) |
| 1409 | { |
| 1410 | const char *cmd_name = args[*arg-1]; |
| 1411 | char *error; |
| 1412 | |
| 1413 | cmd_name += strlen("sc-inc-gpc0"); |
| 1414 | if (*cmd_name == '\0') { |
| 1415 | /* default stick table id. */ |
| 1416 | rule->arg.gpc.sc = 0; |
| 1417 | } else { |
| 1418 | /* parse the stick table id. */ |
| 1419 | if (*cmd_name != '(') { |
| 1420 | memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]); |
| 1421 | return ACT_RET_PRS_ERR; |
| 1422 | } |
| 1423 | cmd_name++; /* jump the '(' */ |
| 1424 | rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */ |
| 1425 | if (*error != ')') { |
| 1426 | memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]); |
| 1427 | return ACT_RET_PRS_ERR; |
| 1428 | } |
| 1429 | |
| 1430 | if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) { |
| 1431 | memprintf(err, "invalid stick table track ID. The max allowed ID is %d", |
| 1432 | ACT_ACTION_TRK_SCMAX-1); |
| 1433 | return ACT_RET_PRS_ERR; |
| 1434 | } |
| 1435 | } |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1436 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1437 | rule->action_ptr = action_inc_gpc0; |
| 1438 | return ACT_RET_PRS_OK; |
| 1439 | } |
| 1440 | |
| 1441 | /* Always returns 1. */ |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1442 | 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] | 1443 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1444 | { |
| 1445 | void *ptr; |
| 1446 | struct stksess *ts; |
| 1447 | struct stkctr *stkctr; |
| 1448 | |
| 1449 | /* Extract the stksess, return OK if no stksess available. */ |
| 1450 | if (s) |
| 1451 | stkctr = &s->stkctr[rule->arg.gpt.sc]; |
| 1452 | else |
| 1453 | stkctr = &sess->stkctr[rule->arg.gpt.sc]; |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1454 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1455 | ts = stkctr_entry(stkctr); |
| 1456 | if (!ts) |
| 1457 | return ACT_RET_CONT; |
| 1458 | |
| 1459 | /* Store the sample in the required sc, and ignore errors. */ |
| 1460 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0); |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1461 | if (ptr) { |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1462 | stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value; |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1463 | stktable_touch(stkctr->table, ts, 1); |
| 1464 | } |
| 1465 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1466 | return ACT_RET_CONT; |
| 1467 | } |
| 1468 | |
| 1469 | /* This function is a common parser for using variables. It understands |
| 1470 | * the format: |
| 1471 | * |
| 1472 | * set-gpt0(<stick-table ID>) <expression> |
| 1473 | * |
| 1474 | * It returns 0 if fails and <err> is filled with an error message. Otherwise, |
| 1475 | * it returns 1 and the variable <expr> is filled with the pointer to the |
| 1476 | * expression to execute. |
| 1477 | */ |
| 1478 | static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px, |
| 1479 | struct act_rule *rule, char **err) |
| 1480 | |
| 1481 | |
| 1482 | { |
| 1483 | const char *cmd_name = args[*arg-1]; |
| 1484 | char *error; |
| 1485 | |
| 1486 | cmd_name += strlen("sc-set-gpt0"); |
| 1487 | if (*cmd_name == '\0') { |
| 1488 | /* default stick table id. */ |
| 1489 | rule->arg.gpt.sc = 0; |
| 1490 | } else { |
| 1491 | /* parse the stick table id. */ |
| 1492 | if (*cmd_name != '(') { |
| 1493 | memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]); |
| 1494 | return ACT_RET_PRS_ERR; |
| 1495 | } |
| 1496 | cmd_name++; /* jump the '(' */ |
| 1497 | rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */ |
| 1498 | if (*error != ')') { |
| 1499 | memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]); |
| 1500 | return ACT_RET_PRS_ERR; |
| 1501 | } |
| 1502 | |
| 1503 | if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) { |
| 1504 | memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d", |
| 1505 | args[*arg-1], ACT_ACTION_TRK_SCMAX-1); |
| 1506 | return ACT_RET_PRS_ERR; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | rule->arg.gpt.value = strtol(args[*arg], &error, 10); |
| 1511 | if (*error != '\0') { |
| 1512 | memprintf(err, "invalid integer value '%s'", args[*arg]); |
| 1513 | return ACT_RET_PRS_ERR; |
| 1514 | } |
| 1515 | (*arg)++; |
| 1516 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1517 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1518 | rule->action_ptr = action_set_gpt0; |
| 1519 | |
| 1520 | return ACT_RET_PRS_OK; |
| 1521 | } |
| 1522 | |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 1523 | |
| 1524 | /* The functions below are used to manipulate table contents from the CLI. |
| 1525 | * There are 3 main actions, "clear", "set" and "show". The code is shared |
| 1526 | * between all actions, and the action is encoded in the void *private in |
| 1527 | * the appctx as well as in the keyword registration, among one of the |
| 1528 | * following values. |
| 1529 | */ |
| 1530 | |
| 1531 | enum { |
| 1532 | STK_CLI_ACT_CLR, |
| 1533 | STK_CLI_ACT_SET, |
| 1534 | STK_CLI_ACT_SHOW, |
| 1535 | }; |
| 1536 | |
| 1537 | /* Dump the status of a table to a stream interface's |
| 1538 | * read buffer. It returns 0 if the output buffer is full |
| 1539 | * and needs to be called again, otherwise non-zero. |
| 1540 | */ |
| 1541 | static int table_dump_head_to_buffer(struct chunk *msg, struct stream_interface *si, |
| 1542 | struct proxy *proxy, struct proxy *target) |
| 1543 | { |
| 1544 | struct stream *s = si_strm(si); |
| 1545 | |
| 1546 | chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n", |
| 1547 | proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current); |
| 1548 | |
| 1549 | /* any other information should be dumped here */ |
| 1550 | |
| 1551 | if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) |
| 1552 | chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n"); |
| 1553 | |
| 1554 | if (bi_putchk(si_ic(si), msg) == -1) { |
| 1555 | si_applet_cant_put(si); |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | /* Dump a table entry to a stream interface's |
| 1563 | * read buffer. It returns 0 if the output buffer is full |
| 1564 | * and needs to be called again, otherwise non-zero. |
| 1565 | */ |
| 1566 | static int table_dump_entry_to_buffer(struct chunk *msg, struct stream_interface *si, |
| 1567 | struct proxy *proxy, struct stksess *entry) |
| 1568 | { |
| 1569 | int dt; |
| 1570 | |
| 1571 | chunk_appendf(msg, "%p:", entry); |
| 1572 | |
| 1573 | if (proxy->table.type == SMP_T_IPV4) { |
| 1574 | char addr[INET_ADDRSTRLEN]; |
| 1575 | inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr)); |
| 1576 | chunk_appendf(msg, " key=%s", addr); |
| 1577 | } |
| 1578 | else if (proxy->table.type == SMP_T_IPV6) { |
| 1579 | char addr[INET6_ADDRSTRLEN]; |
| 1580 | inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr)); |
| 1581 | chunk_appendf(msg, " key=%s", addr); |
| 1582 | } |
| 1583 | else if (proxy->table.type == SMP_T_SINT) { |
| 1584 | chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key); |
| 1585 | } |
| 1586 | else if (proxy->table.type == SMP_T_STR) { |
| 1587 | chunk_appendf(msg, " key="); |
| 1588 | dump_text(msg, (const char *)entry->key.key, proxy->table.key_size); |
| 1589 | } |
| 1590 | else { |
| 1591 | chunk_appendf(msg, " key="); |
| 1592 | dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size); |
| 1593 | } |
| 1594 | |
| 1595 | chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire)); |
| 1596 | |
| 1597 | for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) { |
| 1598 | void *ptr; |
| 1599 | |
| 1600 | if (proxy->table.data_ofs[dt] == 0) |
| 1601 | continue; |
| 1602 | if (stktable_data_types[dt].arg_type == ARG_T_DELAY) |
| 1603 | chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u); |
| 1604 | else |
| 1605 | chunk_appendf(msg, " %s=", stktable_data_types[dt].name); |
| 1606 | |
| 1607 | ptr = stktable_data_ptr(&proxy->table, entry, dt); |
| 1608 | switch (stktable_data_types[dt].std_type) { |
| 1609 | case STD_T_SINT: |
| 1610 | chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint)); |
| 1611 | break; |
| 1612 | case STD_T_UINT: |
| 1613 | chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint)); |
| 1614 | break; |
| 1615 | case STD_T_ULL: |
| 1616 | chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull)); |
| 1617 | break; |
| 1618 | case STD_T_FRQP: |
| 1619 | chunk_appendf(msg, "%d", |
| 1620 | read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 1621 | proxy->table.data_arg[dt].u)); |
| 1622 | break; |
| 1623 | } |
| 1624 | } |
| 1625 | chunk_appendf(msg, "\n"); |
| 1626 | |
| 1627 | if (bi_putchk(si_ic(si), msg) == -1) { |
| 1628 | si_applet_cant_put(si); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | /* Processes a single table entry matching a specific key passed in argument. |
| 1637 | * returns 0 if wants to be called again, 1 if has ended processing. |
| 1638 | */ |
| 1639 | static int table_process_entry_per_key(struct appctx *appctx, char **args) |
| 1640 | { |
| 1641 | struct stream_interface *si = appctx->owner; |
| 1642 | int action = (long)appctx->private; |
| 1643 | struct proxy *px = appctx->ctx.table.target; |
| 1644 | struct stksess *ts; |
| 1645 | uint32_t uint32_key; |
| 1646 | unsigned char ip6_key[sizeof(struct in6_addr)]; |
| 1647 | long long value; |
| 1648 | int data_type; |
| 1649 | int cur_arg; |
| 1650 | void *ptr; |
| 1651 | struct freq_ctr_period *frqp; |
| 1652 | |
| 1653 | if (!*args[4]) { |
| 1654 | appctx->ctx.cli.msg = "Key value expected\n"; |
| 1655 | appctx->st0 = STAT_CLI_PRINT; |
| 1656 | return 1; |
| 1657 | } |
| 1658 | |
| 1659 | switch (px->table.type) { |
| 1660 | case SMP_T_IPV4: |
| 1661 | uint32_key = htonl(inetaddr_host(args[4])); |
| 1662 | static_table_key->key = &uint32_key; |
| 1663 | break; |
| 1664 | case SMP_T_IPV6: |
| 1665 | inet_pton(AF_INET6, args[4], ip6_key); |
| 1666 | static_table_key->key = &ip6_key; |
| 1667 | break; |
| 1668 | case SMP_T_SINT: |
| 1669 | { |
| 1670 | char *endptr; |
| 1671 | unsigned long val; |
| 1672 | errno = 0; |
| 1673 | val = strtoul(args[4], &endptr, 10); |
| 1674 | if ((errno == ERANGE && val == ULONG_MAX) || |
| 1675 | (errno != 0 && val == 0) || endptr == args[4] || |
| 1676 | val > 0xffffffff) { |
| 1677 | appctx->ctx.cli.msg = "Invalid key\n"; |
| 1678 | appctx->st0 = STAT_CLI_PRINT; |
| 1679 | return 1; |
| 1680 | } |
| 1681 | uint32_key = (uint32_t) val; |
| 1682 | static_table_key->key = &uint32_key; |
| 1683 | break; |
| 1684 | } |
| 1685 | break; |
| 1686 | case SMP_T_STR: |
| 1687 | static_table_key->key = args[4]; |
| 1688 | static_table_key->key_len = strlen(args[4]); |
| 1689 | break; |
| 1690 | default: |
| 1691 | switch (action) { |
| 1692 | case STK_CLI_ACT_SHOW: |
| 1693 | appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n"; |
| 1694 | break; |
| 1695 | case STK_CLI_ACT_CLR: |
| 1696 | appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n"; |
| 1697 | break; |
| 1698 | case STK_CLI_ACT_SET: |
| 1699 | appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n"; |
| 1700 | break; |
| 1701 | default: |
| 1702 | appctx->ctx.cli.msg = "Unknown action\n"; |
| 1703 | break; |
| 1704 | } |
| 1705 | appctx->st0 = STAT_CLI_PRINT; |
| 1706 | return 1; |
| 1707 | } |
| 1708 | |
| 1709 | /* check permissions */ |
| 1710 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1711 | return 1; |
| 1712 | |
| 1713 | ts = stktable_lookup_key(&px->table, static_table_key); |
| 1714 | |
| 1715 | switch (action) { |
| 1716 | case STK_CLI_ACT_SHOW: |
| 1717 | if (!ts) |
| 1718 | return 1; |
| 1719 | chunk_reset(&trash); |
| 1720 | if (!table_dump_head_to_buffer(&trash, si, px, px)) |
| 1721 | return 0; |
| 1722 | if (!table_dump_entry_to_buffer(&trash, si, px, ts)) |
| 1723 | return 0; |
| 1724 | break; |
| 1725 | |
| 1726 | case STK_CLI_ACT_CLR: |
| 1727 | if (!ts) |
| 1728 | return 1; |
| 1729 | if (ts->ref_cnt) { |
| 1730 | /* don't delete an entry which is currently referenced */ |
| 1731 | appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n"; |
| 1732 | appctx->st0 = STAT_CLI_PRINT; |
| 1733 | return 1; |
| 1734 | } |
| 1735 | stksess_kill(&px->table, ts); |
| 1736 | break; |
| 1737 | |
| 1738 | case STK_CLI_ACT_SET: |
| 1739 | if (ts) |
| 1740 | stktable_touch(&px->table, ts, 1); |
| 1741 | else { |
| 1742 | ts = stksess_new(&px->table, static_table_key); |
| 1743 | if (!ts) { |
| 1744 | /* don't delete an entry which is currently referenced */ |
| 1745 | appctx->ctx.cli.msg = "Unable to allocate a new entry\n"; |
| 1746 | appctx->st0 = STAT_CLI_PRINT; |
| 1747 | return 1; |
| 1748 | } |
| 1749 | stktable_store(&px->table, ts, 1); |
| 1750 | } |
| 1751 | |
| 1752 | for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) { |
| 1753 | if (strncmp(args[cur_arg], "data.", 5) != 0) { |
| 1754 | appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n"; |
| 1755 | appctx->st0 = STAT_CLI_PRINT; |
| 1756 | return 1; |
| 1757 | } |
| 1758 | |
| 1759 | data_type = stktable_get_data_type(args[cur_arg] + 5); |
| 1760 | if (data_type < 0) { |
| 1761 | appctx->ctx.cli.msg = "Unknown data type\n"; |
| 1762 | appctx->st0 = STAT_CLI_PRINT; |
| 1763 | return 1; |
| 1764 | } |
| 1765 | |
| 1766 | if (!px->table.data_ofs[data_type]) { |
| 1767 | appctx->ctx.cli.msg = "Data type not stored in this table\n"; |
| 1768 | appctx->st0 = STAT_CLI_PRINT; |
| 1769 | return 1; |
| 1770 | } |
| 1771 | |
| 1772 | if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) { |
| 1773 | appctx->ctx.cli.msg = "Require a valid integer value to store\n"; |
| 1774 | appctx->st0 = STAT_CLI_PRINT; |
| 1775 | return 1; |
| 1776 | } |
| 1777 | |
| 1778 | ptr = stktable_data_ptr(&px->table, ts, data_type); |
| 1779 | |
| 1780 | switch (stktable_data_types[data_type].std_type) { |
| 1781 | case STD_T_SINT: |
| 1782 | stktable_data_cast(ptr, std_t_sint) = value; |
| 1783 | break; |
| 1784 | case STD_T_UINT: |
| 1785 | stktable_data_cast(ptr, std_t_uint) = value; |
| 1786 | break; |
| 1787 | case STD_T_ULL: |
| 1788 | stktable_data_cast(ptr, std_t_ull) = value; |
| 1789 | break; |
| 1790 | case STD_T_FRQP: |
| 1791 | /* We set both the current and previous values. That way |
| 1792 | * the reported frequency is stable during all the period |
| 1793 | * then slowly fades out. This allows external tools to |
| 1794 | * push measures without having to update them too often. |
| 1795 | */ |
| 1796 | frqp = &stktable_data_cast(ptr, std_t_frqp); |
| 1797 | frqp->curr_tick = now_ms; |
| 1798 | frqp->prev_ctr = 0; |
| 1799 | frqp->curr_ctr = value; |
| 1800 | break; |
| 1801 | } |
| 1802 | } |
| 1803 | break; |
| 1804 | |
| 1805 | default: |
| 1806 | appctx->ctx.cli.msg = "Unknown action\n"; |
| 1807 | appctx->st0 = STAT_CLI_PRINT; |
| 1808 | break; |
| 1809 | } |
| 1810 | return 1; |
| 1811 | } |
| 1812 | |
| 1813 | /* Prepares the appctx fields with the data-based filters from the command line. |
| 1814 | * Returns 0 if the dump can proceed, 1 if has ended processing. |
| 1815 | */ |
| 1816 | static int table_prepare_data_request(struct appctx *appctx, char **args) |
| 1817 | { |
| 1818 | int action = (long)appctx->private; |
| 1819 | |
| 1820 | if (action != STK_CLI_ACT_SHOW && action != STK_CLI_ACT_CLR) { |
| 1821 | appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions"; |
| 1822 | appctx->st0 = STAT_CLI_PRINT; |
| 1823 | return 1; |
| 1824 | } |
| 1825 | |
| 1826 | /* condition on stored data value */ |
| 1827 | appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5); |
| 1828 | if (appctx->ctx.table.data_type < 0) { |
| 1829 | appctx->ctx.cli.msg = "Unknown data type\n"; |
| 1830 | appctx->st0 = STAT_CLI_PRINT; |
| 1831 | return 1; |
| 1832 | } |
| 1833 | |
| 1834 | if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) { |
| 1835 | appctx->ctx.cli.msg = "Data type not stored in this table\n"; |
| 1836 | appctx->st0 = STAT_CLI_PRINT; |
| 1837 | return 1; |
| 1838 | } |
| 1839 | |
| 1840 | appctx->ctx.table.data_op = get_std_op(args[4]); |
| 1841 | if (appctx->ctx.table.data_op < 0) { |
| 1842 | appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n"; |
| 1843 | appctx->st0 = STAT_CLI_PRINT; |
| 1844 | return 1; |
| 1845 | } |
| 1846 | |
| 1847 | if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) { |
| 1848 | appctx->ctx.cli.msg = "Require a valid integer value to compare against\n"; |
| 1849 | appctx->st0 = STAT_CLI_PRINT; |
| 1850 | return 1; |
| 1851 | } |
| 1852 | |
| 1853 | /* OK we're done, all the fields are set */ |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
| 1857 | /* returns 0 if wants to be called, 1 if has ended processing */ |
| 1858 | static int cli_parse_table_req(char **args, struct appctx *appctx, void *private) |
| 1859 | { |
| 1860 | int action = (long)private; |
| 1861 | |
| 1862 | appctx->private = private; |
| 1863 | appctx->ctx.table.data_type = -1; |
| 1864 | appctx->st2 = STAT_ST_INIT; |
| 1865 | appctx->ctx.table.target = NULL; |
| 1866 | appctx->ctx.table.proxy = NULL; |
| 1867 | appctx->ctx.table.entry = NULL; |
| 1868 | |
| 1869 | if (*args[2]) { |
| 1870 | appctx->ctx.table.target = proxy_tbl_by_name(args[2]); |
| 1871 | if (!appctx->ctx.table.target) { |
| 1872 | appctx->ctx.cli.msg = "No such table\n"; |
| 1873 | appctx->st0 = STAT_CLI_PRINT; |
| 1874 | return 1; |
| 1875 | } |
| 1876 | } |
| 1877 | else { |
| 1878 | if (action != STK_CLI_ACT_SHOW) |
| 1879 | goto err_args; |
| 1880 | return 0; |
| 1881 | } |
| 1882 | |
| 1883 | if (strcmp(args[3], "key") == 0) |
| 1884 | return table_process_entry_per_key(appctx, args); |
| 1885 | else if (strncmp(args[3], "data.", 5) == 0) |
| 1886 | return table_prepare_data_request(appctx, args); |
| 1887 | else if (*args[3]) |
| 1888 | goto err_args; |
| 1889 | |
| 1890 | return 0; |
| 1891 | |
| 1892 | err_args: |
| 1893 | switch (action) { |
| 1894 | case STK_CLI_ACT_SHOW: |
| 1895 | appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n"; |
| 1896 | break; |
| 1897 | case STK_CLI_ACT_CLR: |
| 1898 | appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n"; |
| 1899 | break; |
| 1900 | case STK_CLI_ACT_SET: |
| 1901 | appctx->ctx.cli.msg = "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n"; |
| 1902 | break; |
| 1903 | default: |
| 1904 | appctx->ctx.cli.msg = "Unknown action\n"; |
| 1905 | break; |
| 1906 | } |
| 1907 | appctx->st0 = STAT_CLI_PRINT; |
| 1908 | return 1; |
| 1909 | } |
| 1910 | |
| 1911 | /* This function is used to deal with table operations (dump or clear depending |
| 1912 | * on the action stored in appctx->private). It returns 0 if the output buffer is |
| 1913 | * full and it needs to be called again, otherwise non-zero. |
| 1914 | */ |
| 1915 | static int cli_io_handler_table(struct appctx *appctx) |
| 1916 | { |
| 1917 | struct stream_interface *si = appctx->owner; |
| 1918 | int action = (long)appctx->private; |
| 1919 | struct stream *s = si_strm(si); |
| 1920 | struct ebmb_node *eb; |
| 1921 | int dt; |
| 1922 | int skip_entry; |
| 1923 | int show = action == STK_CLI_ACT_SHOW; |
| 1924 | |
| 1925 | /* |
| 1926 | * We have 3 possible states in appctx->st2 : |
| 1927 | * - STAT_ST_INIT : the first call |
| 1928 | * - STAT_ST_INFO : the proxy pointer points to the next table to |
| 1929 | * dump, the entry pointer is NULL ; |
| 1930 | * - STAT_ST_LIST : the proxy pointer points to the current table |
| 1931 | * and the entry pointer points to the next entry to be dumped, |
| 1932 | * and the refcount on the next entry is held ; |
| 1933 | * - STAT_ST_END : nothing left to dump, the buffer may contain some |
| 1934 | * data though. |
| 1935 | */ |
| 1936 | |
| 1937 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) { |
| 1938 | /* in case of abort, remove any refcount we might have set on an entry */ |
| 1939 | if (appctx->st2 == STAT_ST_LIST) { |
| 1940 | appctx->ctx.table.entry->ref_cnt--; |
| 1941 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 1942 | } |
| 1943 | return 1; |
| 1944 | } |
| 1945 | |
| 1946 | chunk_reset(&trash); |
| 1947 | |
| 1948 | while (appctx->st2 != STAT_ST_FIN) { |
| 1949 | switch (appctx->st2) { |
| 1950 | case STAT_ST_INIT: |
| 1951 | appctx->ctx.table.proxy = appctx->ctx.table.target; |
| 1952 | if (!appctx->ctx.table.proxy) |
| 1953 | appctx->ctx.table.proxy = proxy; |
| 1954 | |
| 1955 | appctx->ctx.table.entry = NULL; |
| 1956 | appctx->st2 = STAT_ST_INFO; |
| 1957 | break; |
| 1958 | |
| 1959 | case STAT_ST_INFO: |
| 1960 | if (!appctx->ctx.table.proxy || |
| 1961 | (appctx->ctx.table.target && |
| 1962 | appctx->ctx.table.proxy != appctx->ctx.table.target)) { |
| 1963 | appctx->st2 = STAT_ST_END; |
| 1964 | break; |
| 1965 | } |
| 1966 | |
| 1967 | if (appctx->ctx.table.proxy->table.size) { |
| 1968 | if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.target)) |
| 1969 | return 0; |
| 1970 | |
| 1971 | if (appctx->ctx.table.target && |
| 1972 | strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) { |
| 1973 | /* dump entries only if table explicitly requested */ |
| 1974 | eb = ebmb_first(&appctx->ctx.table.proxy->table.keys); |
| 1975 | if (eb) { |
| 1976 | appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key); |
| 1977 | appctx->ctx.table.entry->ref_cnt++; |
| 1978 | appctx->st2 = STAT_ST_LIST; |
| 1979 | break; |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | appctx->ctx.table.proxy = appctx->ctx.table.proxy->next; |
| 1984 | break; |
| 1985 | |
| 1986 | case STAT_ST_LIST: |
| 1987 | skip_entry = 0; |
| 1988 | |
| 1989 | if (appctx->ctx.table.data_type >= 0) { |
| 1990 | /* we're filtering on some data contents */ |
| 1991 | void *ptr; |
| 1992 | long long data; |
| 1993 | |
| 1994 | dt = appctx->ctx.table.data_type; |
| 1995 | ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table, |
| 1996 | appctx->ctx.table.entry, |
| 1997 | dt); |
| 1998 | |
| 1999 | data = 0; |
| 2000 | switch (stktable_data_types[dt].std_type) { |
| 2001 | case STD_T_SINT: |
| 2002 | data = stktable_data_cast(ptr, std_t_sint); |
| 2003 | break; |
| 2004 | case STD_T_UINT: |
| 2005 | data = stktable_data_cast(ptr, std_t_uint); |
| 2006 | break; |
| 2007 | case STD_T_ULL: |
| 2008 | data = stktable_data_cast(ptr, std_t_ull); |
| 2009 | break; |
| 2010 | case STD_T_FRQP: |
| 2011 | data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 2012 | appctx->ctx.table.proxy->table.data_arg[dt].u); |
| 2013 | break; |
| 2014 | } |
| 2015 | |
| 2016 | /* skip the entry if the data does not match the test and the value */ |
| 2017 | if ((data < appctx->ctx.table.value && |
| 2018 | (appctx->ctx.table.data_op == STD_OP_EQ || |
| 2019 | appctx->ctx.table.data_op == STD_OP_GT || |
| 2020 | appctx->ctx.table.data_op == STD_OP_GE)) || |
| 2021 | (data == appctx->ctx.table.value && |
| 2022 | (appctx->ctx.table.data_op == STD_OP_NE || |
| 2023 | appctx->ctx.table.data_op == STD_OP_GT || |
| 2024 | appctx->ctx.table.data_op == STD_OP_LT)) || |
| 2025 | (data > appctx->ctx.table.value && |
| 2026 | (appctx->ctx.table.data_op == STD_OP_EQ || |
| 2027 | appctx->ctx.table.data_op == STD_OP_LT || |
| 2028 | appctx->ctx.table.data_op == STD_OP_LE))) |
| 2029 | skip_entry = 1; |
| 2030 | } |
| 2031 | |
| 2032 | if (show && !skip_entry && |
| 2033 | !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.entry)) |
| 2034 | return 0; |
| 2035 | |
| 2036 | appctx->ctx.table.entry->ref_cnt--; |
| 2037 | |
| 2038 | eb = ebmb_next(&appctx->ctx.table.entry->key); |
| 2039 | if (eb) { |
| 2040 | struct stksess *old = appctx->ctx.table.entry; |
| 2041 | appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key); |
| 2042 | if (show) |
| 2043 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old); |
| 2044 | else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt) |
| 2045 | stksess_kill(&appctx->ctx.table.proxy->table, old); |
| 2046 | appctx->ctx.table.entry->ref_cnt++; |
| 2047 | break; |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | if (show) |
| 2052 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2053 | else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt) |
| 2054 | stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2055 | |
| 2056 | appctx->ctx.table.proxy = appctx->ctx.table.proxy->next; |
| 2057 | appctx->st2 = STAT_ST_INFO; |
| 2058 | break; |
| 2059 | |
| 2060 | case STAT_ST_END: |
| 2061 | appctx->st2 = STAT_ST_FIN; |
| 2062 | break; |
| 2063 | } |
| 2064 | } |
| 2065 | return 1; |
| 2066 | } |
| 2067 | |
| 2068 | static void cli_release_show_table(struct appctx *appctx) |
| 2069 | { |
| 2070 | if (appctx->st2 == STAT_ST_LIST) { |
| 2071 | appctx->ctx.table.entry->ref_cnt--; |
| 2072 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | /* register cli keywords */ |
| 2077 | static struct cli_kw_list cli_kws = {{ },{ |
| 2078 | { { "clear", "table", NULL }, "clear table : remove an entry from a table", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_CLR }, |
| 2079 | { { "set", "table", NULL }, "set table [id] : update or create a table entry's data", cli_parse_table_req, cli_io_handler_table, NULL, (void *)STK_CLI_ACT_SET }, |
| 2080 | { { "show", "table", NULL }, "show table [id]: report table usage stats or dump this table's contents", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_SHOW }, |
| 2081 | {{},} |
| 2082 | }}; |
| 2083 | |
| 2084 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2085 | static struct action_kw_list tcp_conn_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2086 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2087 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2088 | { /* END */ } |
| 2089 | }}; |
| 2090 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2091 | static struct action_kw_list tcp_sess_kws = { { }, { |
| 2092 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
| 2093 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2094 | { /* END */ } |
| 2095 | }}; |
| 2096 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2097 | static struct action_kw_list tcp_req_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2098 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2099 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2100 | { /* END */ } |
| 2101 | }}; |
| 2102 | |
| 2103 | static struct action_kw_list tcp_res_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2104 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2105 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2106 | { /* END */ } |
| 2107 | }}; |
| 2108 | |
| 2109 | static struct action_kw_list http_req_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2110 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2111 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2112 | { /* END */ } |
| 2113 | }}; |
| 2114 | |
| 2115 | static struct action_kw_list http_res_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2116 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2117 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2118 | { /* END */ } |
| 2119 | }}; |
| 2120 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2121 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2122 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Willy Tarreau | 2d17db5 | 2016-05-25 17:16:38 +0200 | [diff] [blame] | 2123 | { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL }, |
| 2124 | { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2125 | { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2126 | { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2127 | { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2128 | { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2129 | { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2130 | { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2131 | { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2132 | { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2133 | { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2134 | { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2135 | { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2136 | { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2137 | { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2138 | { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2139 | { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2140 | { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2141 | { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2142 | { /* END */ }, |
| 2143 | }}; |
| 2144 | |
| 2145 | __attribute__((constructor)) |
| 2146 | static void __stick_table_init(void) |
| 2147 | { |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2148 | /* register som action keywords. */ |
| 2149 | tcp_req_conn_keywords_register(&tcp_conn_kws); |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2150 | tcp_req_sess_keywords_register(&tcp_sess_kws); |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2151 | tcp_req_cont_keywords_register(&tcp_req_kws); |
| 2152 | tcp_res_cont_keywords_register(&tcp_res_kws); |
| 2153 | http_req_keywords_register(&http_req_kws); |
| 2154 | http_res_keywords_register(&http_res_kws); |
| 2155 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2156 | /* register sample fetch and format conversion keywords */ |
| 2157 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2158 | cli_register_kw(&cli_kws); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2159 | } |