blob: dc772ae220c1c0e7721483bf2f9da92d05cff1c9 [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
2 * Stick tables management functions.
3 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau08d5f982010-06-06 13:34:54 +02005 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01006 *
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 Tarreauf13ebdf2016-11-22 18:00:53 +010015#include <errno.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010016
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 Tarreauf13ebdf2016-11-22 18:00:53 +010026#include <types/cli.h>
27#include <types/stats.h>
28
Willy Tarreaud9f316a2014-07-10 14:03:38 +020029#include <proto/arg.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010030#include <proto/cli.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020031#include <proto/proto_http.h>
32#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010033#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020035#include <proto/stream.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010036#include <proto/stream_interface.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020037#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010038#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020039#include <proto/peers.h>
40#include <types/global.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010041
Willy Tarreau12785782012-04-27 21:37:17 +020042/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010043struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020044
Emeric Brun3bd697e2010-01-04 15:23:48 +010045/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020046 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
47 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010048 */
49void stksess_free(struct stktable *t, struct stksess *ts)
50{
51 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020052 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010053}
54
55/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020056 * Kill an stksess (only if its ref_cnt is zero).
57 */
58void stksess_kill(struct stktable *t, struct stksess *ts)
59{
60 if (ts->ref_cnt)
61 return;
62
63 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020064 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020065 ebmb_delete(&ts->key);
66 stksess_free(t, ts);
67}
68
69/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020070 * Initialize or update the key in the sticky session <ts> present in table <t>
71 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010072 */
Willy Tarreau393379c2010-06-06 12:11:37 +020073void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010074{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020075 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020076 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010077 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020078 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 Brun3bd697e2010-01-04 15:23:48 +010080 }
81}
82
83
84/*
Willy Tarreau393379c2010-06-06 12:11:37 +020085 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
86 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010087 */
Willy Tarreau393379c2010-06-06 12:11:37 +020088static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010089{
Willy Tarreau393379c2010-06-06 12:11:37 +020090 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020091 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020092 ts->key.node.leaf_p = NULL;
93 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020094 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010095 return ts;
96}
97
98/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020099 * Trash oldest <to_batch> sticky sessions from table <t>
100 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100101 */
Willy Tarreau3a925c12013-09-04 17:54:01 +0200102int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100103{
104 struct stksess *ts;
105 struct eb32_node *eb;
106 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200107 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100108
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 Tarreaue7f3d7a2010-06-14 14:53:07 +0200116 * half. Let's loop back to the beginning of the tree now if we
117 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100118 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200119 if (looped)
120 break;
121 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100122 eb = eb32_first(&t->exps);
123 if (likely(!eb))
124 break;
125 }
126
127 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200128 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100129 eb = eb32_next(eb);
130
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200131 /* don't delete an entry which is currently referenced */
132 if (ts->ref_cnt)
133 continue;
134
Willy Tarreau86257dc2010-06-06 12:57:10 +0200135 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100136
Willy Tarreau86257dc2010-06-06 12:57:10 +0200137 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100138 if (!tick_isset(ts->expire))
139 continue;
140
Willy Tarreau86257dc2010-06-06 12:57:10 +0200141 ts->exp.key = ts->expire;
142 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143
Willy Tarreau86257dc2010-06-06 12:57:10 +0200144 if (!eb || eb->key > ts->exp.key)
145 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100146
147 continue;
148 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100149
Willy Tarreauaea940e2010-06-06 11:56:36 +0200150 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200151 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200152 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 stksess_free(t, ts);
154 batched++;
155 }
156
157 return batched;
158}
159
160/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200161 * 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 Tarreaua7b46b52013-04-11 16:55:37 +0200164 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
165 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100166 */
167struct 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 Brunfbce6d02010-09-23 18:10:00 +0200175 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100176 return NULL;
177 }
178
Vincent Bernatef8f4fe2016-11-17 15:42:40 +0100179 ts = pool_alloc2(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180 if (ts) {
181 t->current++;
Willy Tarreau51791462016-11-18 18:21:39 +0100182 ts = (void *)ts + t->data_size;
Willy Tarreau393379c2010-06-06 12:11:37 +0200183 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200184 if (key)
185 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100186 }
187
188 return ts;
189}
190
191/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200192 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200193 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200195struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100196{
197 struct ebmb_node *eb;
198
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200199 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200200 eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100201 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 Tarreau86257dc2010-06-06 12:57:10 +0200209 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100210}
211
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200212/* 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 */
217struct 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 Brun85e77c72010-09-23 18:16:52 +0200223 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200224
225 /* entry does not exist, initialize a new one */
226 ts = stksess_new(table, key);
227 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200228 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200229 return ts;
230}
231
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200232/*
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 Brun3bd697e2010-01-04 15:23:48 +0100235 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200236struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238 struct ebmb_node *eb;
239
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200240 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200241 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100242 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200243 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100244
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200245 if (unlikely(!eb))
246 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100247
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200248 return ebmb_entry(eb, struct stksess, key);
249}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100250
Willy Tarreaucb183642010-06-06 17:58:34 +0200251/* 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écaille523cc9e2016-10-12 17:30:30 +0200254struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts,
255 int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200256{
Emeric Brun85e77c72010-09-23 18:16:52 +0200257 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200258 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200259 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 Brun85e77c72010-09-23 18:16:52 +0200263
Emeric Brunaaf58602015-06-15 17:23:30 +0200264 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200265 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200266 /* 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 Brunaaf58602015-06-15 17:23:30 +0200271 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 Brun85e77c72010-09-23 18:16:52 +0200279 }
280 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
281 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200282 return ts;
283}
284
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200285/* 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 */
289struct 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 Tarreauf16d2b82010-06-06 15:38:59 +0200296/* 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 Brun85e77c72010-09-23 18:16:52 +0200300struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200301{
302 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200303 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200304 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200305 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200306 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100307}
308
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200309/* 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 */
313struct 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 Tarreau9ba2dcc2010-06-14 21:04:55 +0200323/* 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 */
327struct 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 Brun85e77c72010-09-23 18:16:52 +0200340 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200341 }
342 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200343 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200344 return ts;
345}
346
Emeric Brun3bd697e2010-01-04 15:23:48 +0100347/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200348 * Trash expired sticky sessions from table <t>. The next expiration date is
349 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100350 */
351static int stktable_trash_expired(struct stktable *t)
352{
353 struct stksess *ts;
354 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200355 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100356
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 Tarreaue7f3d7a2010-06-14 14:53:07 +0200363 * half. Let's loop back to the beginning of the tree now if we
364 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100365 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200366 if (looped)
367 break;
368 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100369 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 Tarreau86257dc2010-06-06 12:57:10 +0200381 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100382 eb = eb32_next(eb);
383
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200384 /* don't delete an entry which is currently referenced */
385 if (ts->ref_cnt)
386 continue;
387
Willy Tarreau86257dc2010-06-06 12:57:10 +0200388 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100389
390 if (!tick_is_expired(ts->expire, now_ms)) {
391 if (!tick_isset(ts->expire))
392 continue;
393
Willy Tarreau86257dc2010-06-06 12:57:10 +0200394 ts->exp.key = ts->expire;
395 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100396
Willy Tarreau86257dc2010-06-06 12:57:10 +0200397 if (!eb || eb->key > ts->exp.key)
398 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100399 continue;
400 }
401
402 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200403 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200404 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100405 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 Tarreauaea940e2010-06-06 11:56:36 +0200414 * Task processing function to trash expired sticky sessions. A pointer to the
415 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100416 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200417static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100418{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200419 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100420
421 task->expire = stktable_trash_expired(t);
422 return task;
423}
424
Willy Tarreauaea940e2010-06-06 11:56:36 +0200425/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100426int 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 Brun1c6235d2015-12-16 15:28:12 +0100431 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100432
Willy Tarreau393379c2010-06-06 12:11:37 +0200433 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100434
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 Tarreauc8b67912015-05-01 18:29:57 +0200442 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200443 peers_register_table(t->peers.p, t);
444 }
445
Emeric Brun3bd697e2010-01-04 15:23:48 +0100446 return t->pool != NULL;
447 }
448 return 1;
449}
450
451/*
452 * Configuration keywords of known table types
453 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200454struct 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 Brun3bd697e2010-01-04 15:23:48 +0100461
462/*
463 * Parse table type configuration.
464 * Returns 0 on successful parsing, else 1.
465 * <myidx> is set at next configuration <args> index.
466 */
467int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
468{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200469 for (*type = 0; *type < SMP_TYPES; (*type)++) {
470 if (!stktable_types[*type].kw)
471 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100472 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
473 continue;
474
475 *key_size = stktable_types[*type].default_size;
476 (*myidx)++;
477
Willy Tarreauaea940e2010-06-06 11:56:36 +0200478 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100479 if (strcmp("len", args[*myidx]) == 0) {
480 (*myidx)++;
481 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200482 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100483 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200484 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200485 /* null terminated string needs +1 for '\0'. */
486 (*key_size)++;
487 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100488 (*myidx)++;
489 }
490 }
491 return 0;
492 }
493 return 1;
494}
495
Willy Tarreau8fed9032014-07-03 17:02:46 +0200496/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200497 * 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 Tarreau8fed9032014-07-03 17:02:46 +0200499 * 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 Tarreauf0b38bf2010-06-06 13:22:23 +0200502 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200503struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200504{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200505 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200506 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200507 return NULL;
508
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200509 /* Fill static_table_key. */
510 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200511
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200512 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200513 static_table_key->key = &smp->data.u.ipv4;
514 static_table_key->key_len = 4;
515 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200516
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200517 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200518 static_table_key->key = &smp->data.u.ipv6;
519 static_table_key->key_len = 16;
520 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200521
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200522 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200523 /* 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 FOURNIER5d24ebc2015-07-24 08:46:42 +0200531 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200532 if (!smp_make_safe(smp))
533 return NULL;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200534 static_table_key->key = smp->data.u.str.str;
535 static_table_key->key_len = smp->data.u.str.len;
536 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200537
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200538 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200539 if (smp->data.u.str.len < t->key_size) {
540 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +0200541 if (!smp_make_rw(smp))
542 return NULL;
543
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200544 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 Brun485479d2010-09-23 18:02:19 +0200552 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200553 static_table_key->key = smp->data.u.str.str;
554 static_table_key->key_len = smp->data.u.str.len;
555 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200556
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200557 default: /* impossible case. */
558 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200559 }
560
Willy Tarreau07115412012-10-29 21:56:59 +0100561 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200562}
563
564/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200565 * 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 Tarreau6bcb0a82014-07-30 08:56:35 +0200570 * 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 Tarreau8fed9032014-07-03 17:02:46 +0200580 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200581struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200582 unsigned int opt, struct sample_expr *expr, struct sample *smp)
583{
584 if (smp)
585 memset(smp, 0, sizeof(*smp));
586
Willy Tarreau192252e2015-04-04 01:47:55 +0200587 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200588 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 Tarreau12785782012-04-27 21:37:17 +0200598 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200599 * type <table_type>, otherwise zero. Used in configuration check.
600 */
Willy Tarreau12785782012-04-27 21:37:17 +0200601int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200602{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100603 int out_type;
604
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200605 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200606 return 0;
607
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100608 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200609
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200610 /* Convert sample. */
611 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100612 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200613
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200614 return 1;
615}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100616
Willy Tarreauedee1d62014-07-15 16:44:27 +0200617/* 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 Tarreau08d5f982010-06-06 13:34:54 +0200621struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200622 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200623 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200624 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200625 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200626 [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 Tarreau08d5f982010-06-06 13:34:54 +0200639};
640
Willy Tarreauedee1d62014-07-15 16:44:27 +0200641/* 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 */
647int 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 Tarreau08d5f982010-06-06 13:34:54 +0200671/*
672 * Returns the data type number for the stktable_data_type whose name is <name>,
673 * or <0 if not found.
674 */
675int stktable_get_data_type(char *name)
676{
677 int type;
678
679 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200680 if (!stktable_data_types[type].name)
681 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200682 if (strcmp(name, stktable_data_types[type].name) == 0)
683 return type;
684 }
685 return -1;
686}
687
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200688/* 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200695static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200696{
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 FOURNIER8c542ca2015-08-19 09:00:18 +0200709 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200710 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200711 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200721static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200722{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200734 ts = stktable_lookup_key(t, key);
735
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200736 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200737 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200739
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200740 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 FOURNIER136f9d32015-08-19 09:07:19 +0200747 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200748 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200758static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200759{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200771 ts = stktable_lookup_key(t, key);
772
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200773 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200774 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200775 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200776
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200777 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 FOURNIER136f9d32015-08-19 09:07:19 +0200784 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200785 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200794static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200795{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200807 ts = stktable_lookup_key(t, key);
808
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200809 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200810 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200811 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200812
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200813 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 FOURNIER136f9d32015-08-19 09:07:19 +0200820 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200821 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200830static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200831{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200843 ts = stktable_lookup_key(t, key);
844
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200845 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200846 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200847 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200848
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200849 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 FOURNIER136f9d32015-08-19 09:07:19 +0200856 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200857 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200867static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200868{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200880 ts = stktable_lookup_key(t, key);
881
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200882 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200883 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200884 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200885
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200886 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 FOURNIER136f9d32015-08-19 09:07:19 +0200893 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200894 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 FOURNIER236657b2015-08-19 08:25:14 +0200899 * 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 */
904static 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 Tarreauf0c730a2016-05-25 17:07:56 +0200917 ts = stktable_lookup_key(t, key);
918
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200919 smp->flags = SMP_F_VOL_TEST;
920 smp->data.type = SMP_T_SINT;
921 smp->data.u.sint = 0;
922
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200923 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 Tarreaud9f316a2014-07-10 14:03:38 +0200935 * 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200940static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200941{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200953 ts = stktable_lookup_key(t, key);
954
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200955 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200956 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200957 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200958
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200959 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 FOURNIER136f9d32015-08-19 09:07:19 +0200966 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200967 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200976static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200977{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200989 ts = stktable_lookup_key(t, key);
990
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200991 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200992 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200993 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200994
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200995 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 FOURNIER136f9d32015-08-19 09:07:19 +02001002 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001003 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001013static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001014{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001026 ts = stktable_lookup_key(t, key);
1027
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001028 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001029 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001030 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001031
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001032 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 FOURNIER136f9d32015-08-19 09:07:19 +02001039 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001040 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001049static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001050{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001062 ts = stktable_lookup_key(t, key);
1063
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001064 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001065 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001066 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001067
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001068 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 FOURNIER136f9d32015-08-19 09:07:19 +02001075 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001076 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001086static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001087{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001099 ts = stktable_lookup_key(t, key);
1100
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001101 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001102 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001103 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001104
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001105 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 FOURNIER136f9d32015-08-19 09:07:19 +02001112 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001113 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001122static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001123{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001135 ts = stktable_lookup_key(t, key);
1136
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001137 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001138 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001139 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001140
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001141 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 FOURNIER136f9d32015-08-19 09:07:19 +02001148 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001149 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001159static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001160{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001172 ts = stktable_lookup_key(t, key);
1173
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001174 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001175 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001176 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001177
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001178 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 FOURNIER136f9d32015-08-19 09:07:19 +02001185 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001186 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001195static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001196{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001208 ts = stktable_lookup_key(t, key);
1209
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001210 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001211 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001212 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001213
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001214 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 FOURNIER136f9d32015-08-19 09:07:19 +02001221 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001222 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001231static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001232{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001244 ts = stktable_lookup_key(t, key);
1245
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001246 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001247 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001248 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001249
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001250 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 FOURNIER136f9d32015-08-19 09:07:19 +02001257 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001258 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001267static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001268{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001280 ts = stktable_lookup_key(t, key);
1281
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001282 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001283 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001284 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001285
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001286 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 FOURNIER136f9d32015-08-19 09:07:19 +02001293 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001294 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001303static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001304{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001316 ts = stktable_lookup_key(t, key);
1317
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001318 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001319 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001320 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001321
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001322 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 FOURNIER136f9d32015-08-19 09:07:19 +02001329 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001330 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001340static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001341{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001352 ts = stktable_lookup_key(t, key);
1353
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001354 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001355 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001356 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001357
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001358 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001359 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001360
1361 return 1;
1362}
1363
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001364/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001365static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001366 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001367{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001368 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 Tarreau79c1e912016-01-25 14:54:45 +01001376
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001377 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001378 if (ts) {
1379 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001380
Willy Tarreau79c1e912016-01-25 14:54:45 +01001381 /* 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 FOURNIERe0627bd2015-08-04 08:20:33 +02001386
Willy Tarreau79c1e912016-01-25 14:54:45 +01001387 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 FOURNIERe0627bd2015-08-04 08:20:33 +02001395 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 */
1407static 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 FOURNIER42148732015-09-02 17:17:33 +02001436 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001437 rule->action_ptr = action_inc_gpc0;
1438 return ACT_RET_PRS_OK;
1439}
1440
1441/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001442static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001443 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001444{
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 Tarreau79c1e912016-01-25 14:54:45 +01001454
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001455 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 Tarreau79c1e912016-01-25 14:54:45 +01001461 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001462 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001463 stktable_touch(stkctr->table, ts, 1);
1464 }
1465
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001466 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 */
1478static 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 FOURNIER42148732015-09-02 17:17:33 +02001517 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001518 rule->action_ptr = action_set_gpt0;
1519
1520 return ACT_RET_PRS_OK;
1521}
1522
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01001523
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
1531enum {
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 */
1541static 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 */
1566static 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 */
1639static 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 */
1816static 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 */
1858static 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
1892err_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 */
1915static 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
2068static 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 */
2077static 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 FOURNIER236657b2015-08-19 08:25:14 +02002085static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002086 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002087 { "sc-set-gpt0", parse_set_gpt0, 1 },
2088 { /* END */ }
2089}};
2090
Willy Tarreau620408f2016-10-21 16:37:51 +02002091static 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 FOURNIER236657b2015-08-19 08:25:14 +02002097static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002098 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002099 { "sc-set-gpt0", parse_set_gpt0, 1 },
2100 { /* END */ }
2101}};
2102
2103static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002104 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002105 { "sc-set-gpt0", parse_set_gpt0, 1 },
2106 { /* END */ }
2107}};
2108
2109static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002110 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002111 { "sc-set-gpt0", parse_set_gpt0, 1 },
2112 { /* END */ }
2113}};
2114
2115static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002116 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002117 { "sc-set-gpt0", parse_set_gpt0, 1 },
2118 { /* END */ }
2119}};
2120
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002121/* Note: must not be declared <const> as its list will be overwritten */
2122static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02002123 { "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 Tarreaud9f316a2014-07-10 14:03:38 +02002142 { /* END */ },
2143}};
2144
2145__attribute__((constructor))
2146static void __stick_table_init(void)
2147{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002148 /* register som action keywords. */
2149 tcp_req_conn_keywords_register(&tcp_conn_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +02002150 tcp_req_sess_keywords_register(&tcp_sess_kws);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002151 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 Tarreaud9f316a2014-07-10 14:03:38 +02002156 /* register sample fetch and format conversion keywords */
2157 sample_register_convs(&sample_conv_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002158 cli_register_kw(&cli_kws);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002159}