blob: a00f1b64586da90361880398c84dc7dde5e0e3f3 [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>
Willy Tarreau39713102016-11-25 15:49:32 +010027#include <types/global.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010028#include <types/stats.h>
29
Willy Tarreaud9f316a2014-07-10 14:03:38 +020030#include <proto/arg.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010031#include <proto/cli.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020032#include <proto/proto_http.h>
Willy Tarreau7d562212016-11-25 16:10:05 +010033#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010034#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020035#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020036#include <proto/stream.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010037#include <proto/stream_interface.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020038#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010039#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020040#include <proto/peers.h>
Willy Tarreau39713102016-11-25 15:49:32 +010041#include <proto/tcp_rules.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010042
Willy Tarreau12785782012-04-27 21:37:17 +020043/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010044struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020045
Emeric Brun3bd697e2010-01-04 15:23:48 +010046/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020047 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
48 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010049 */
50void stksess_free(struct stktable *t, struct stksess *ts)
51{
52 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020053 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010054}
55
56/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020057 * Kill an stksess (only if its ref_cnt is zero).
58 */
59void stksess_kill(struct stktable *t, struct stksess *ts)
60{
61 if (ts->ref_cnt)
62 return;
63
64 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020065 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020066 ebmb_delete(&ts->key);
67 stksess_free(t, ts);
68}
69
70/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020071 * Initialize or update the key in the sticky session <ts> present in table <t>
72 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010073 */
Willy Tarreau393379c2010-06-06 12:11:37 +020074void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010075{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020076 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020077 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010078 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020079 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
80 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010081 }
82}
83
84
85/*
Willy Tarreau393379c2010-06-06 12:11:37 +020086 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
87 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010088 */
Willy Tarreau393379c2010-06-06 12:11:37 +020089static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010090{
Willy Tarreau393379c2010-06-06 12:11:37 +020091 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020092 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020093 ts->key.node.leaf_p = NULL;
94 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020095 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010096 return ts;
97}
98
99/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200100 * Trash oldest <to_batch> sticky sessions from table <t>
101 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102 */
Willy Tarreau3a925c12013-09-04 17:54:01 +0200103int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100104{
105 struct stksess *ts;
106 struct eb32_node *eb;
107 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200108 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100109
110 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
111
112 while (batched < to_batch) {
113
114 if (unlikely(!eb)) {
115 /* we might have reached the end of the tree, typically because
116 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200117 * half. Let's loop back to the beginning of the tree now if we
118 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100119 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200120 if (looped)
121 break;
122 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100123 eb = eb32_first(&t->exps);
124 if (likely(!eb))
125 break;
126 }
127
128 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130 eb = eb32_next(eb);
131
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200132 /* don't delete an entry which is currently referenced */
133 if (ts->ref_cnt)
134 continue;
135
Willy Tarreau86257dc2010-06-06 12:57:10 +0200136 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreau86257dc2010-06-06 12:57:10 +0200138 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100139 if (!tick_isset(ts->expire))
140 continue;
141
Willy Tarreau86257dc2010-06-06 12:57:10 +0200142 ts->exp.key = ts->expire;
143 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100144
Willy Tarreau86257dc2010-06-06 12:57:10 +0200145 if (!eb || eb->key > ts->exp.key)
146 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147
148 continue;
149 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100150
Willy Tarreauaea940e2010-06-06 11:56:36 +0200151 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200152 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200153 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100154 stksess_free(t, ts);
155 batched++;
156 }
157
158 return batched;
159}
160
161/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200162 * Allocate and initialise a new sticky session.
163 * The new sticky session is returned or NULL in case of lack of memory.
164 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200165 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
166 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100167 */
168struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
169{
170 struct stksess *ts;
171
172 if (unlikely(t->current == t->size)) {
173 if ( t->nopurge )
174 return NULL;
175
Emeric Brunfbce6d02010-09-23 18:10:00 +0200176 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100177 return NULL;
178 }
179
Vincent Bernatef8f4fe2016-11-17 15:42:40 +0100180 ts = pool_alloc2(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100181 if (ts) {
182 t->current++;
Willy Tarreau51791462016-11-18 18:21:39 +0100183 ts = (void *)ts + t->data_size;
Willy Tarreau393379c2010-06-06 12:11:37 +0200184 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200185 if (key)
186 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 }
188
189 return ts;
190}
191
192/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200193 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200194 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100195 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200196struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100197{
198 struct ebmb_node *eb;
199
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200200 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200201 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 +0100202 else
203 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
204
205 if (unlikely(!eb)) {
206 /* no session found */
207 return NULL;
208 }
209
Willy Tarreau86257dc2010-06-06 12:57:10 +0200210 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100211}
212
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200213/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
214 * This is mainly used for situations where we want to refresh a key's usage so
215 * that it does not expire, and we want to have it created if it was not there.
216 * The stksess is returned, or NULL if it could not be created.
217 */
218struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
219{
220 struct stksess *ts;
221
222 ts = stktable_lookup_key(table, key);
223 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200224 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200225
226 /* entry does not exist, initialize a new one */
227 ts = stksess_new(table, key);
228 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200229 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200230 return ts;
231}
232
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200233/*
234 * Looks in table <t> for a sticky session with same key as <ts>.
235 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100236 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200237struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100239 struct ebmb_node *eb;
240
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200241 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200242 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200244 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100245
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200246 if (unlikely(!eb))
247 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100248
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200249 return ebmb_entry(eb, struct stksess, key);
250}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100251
Willy Tarreaucb183642010-06-06 17:58:34 +0200252/* Update the expiration timer for <ts> but do not touch its expiration node.
253 * The table's expiration timer is updated if set.
254 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200255struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts,
256 int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200257{
Emeric Brun85e77c72010-09-23 18:16:52 +0200258 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200259 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200260 if (t->expire) {
261 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
262 task_queue(t->exp_task);
263 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200264
Emeric Brunaaf58602015-06-15 17:23:30 +0200265 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200266 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200267 /* If this entry is not in the tree
268 or not scheduled for at least one peer */
269 if (!ts->upd.node.leaf_p
270 || (int)(t->commitupdate - ts->upd.key) >= 0
271 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200272 ts->upd.key = ++t->update;
273 t->localupdate = t->update;
274 eb32_delete(&ts->upd);
275 eb = eb32_insert(&t->updates, &ts->upd);
276 if (eb != &ts->upd) {
277 eb32_delete(eb);
278 eb32_insert(&t->updates, &ts->upd);
279 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200280 }
281 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
282 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200283 return ts;
284}
285
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200286/* Update the expiration timer for <ts> but do not touch its expiration node.
287 * The table's expiration timer is updated if set. The date of expiration coming from
288 * <t> stick-table configuration.
289 */
290struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
291{
292 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
293
294 return stktable_touch_with_exp(t, ts, local, expire);
295}
296
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200297/* Insert new sticky session <ts> in the table. It is assumed that it does not
298 * yet exist (the caller must check this). The table's timeout is updated if it
299 * is set. <ts> is returned.
300 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200301struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200302{
303 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200304 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200305 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200306 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200307 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100308}
309
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200310/* Same function as stktable_store(), but with <expire> as supplementary argument
311 * to set the date of expiration of <ts> new sticky session thanks to
312 * stktable_touch_with_exp().
313 */
314struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts,
315 int local, int expire)
316{
317 ebmb_insert(&t->keys, &ts->key, t->key_size);
318 stktable_touch_with_exp(t, ts, local, expire);
319 ts->exp.key = ts->expire;
320 eb32_insert(&t->exps, &ts->exp);
321 return ts;
322}
323
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200324/* Returns a valid or initialized stksess for the specified stktable_key in the
325 * specified table, or NULL if the key was NULL, or if no entry was found nor
326 * could be created. The entry's expiration is updated.
327 */
328struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
329{
330 struct stksess *ts;
331
332 if (!key)
333 return NULL;
334
335 ts = stktable_lookup_key(table, key);
336 if (ts == NULL) {
337 /* entry does not exist, initialize a new one */
338 ts = stksess_new(table, key);
339 if (!ts)
340 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200341 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200342 }
343 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200344 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200345 return ts;
346}
347
Emeric Brun3bd697e2010-01-04 15:23:48 +0100348/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200349 * Trash expired sticky sessions from table <t>. The next expiration date is
350 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100351 */
352static int stktable_trash_expired(struct stktable *t)
353{
354 struct stksess *ts;
355 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200356 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357
358 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
359
360 while (1) {
361 if (unlikely(!eb)) {
362 /* we might have reached the end of the tree, typically because
363 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200364 * half. Let's loop back to the beginning of the tree now if we
365 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100366 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200367 if (looped)
368 break;
369 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100370 eb = eb32_first(&t->exps);
371 if (likely(!eb))
372 break;
373 }
374
375 if (likely(tick_is_lt(now_ms, eb->key))) {
376 /* timer not expired yet, revisit it later */
377 t->exp_next = eb->key;
378 return t->exp_next;
379 }
380
381 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200382 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383 eb = eb32_next(eb);
384
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200385 /* don't delete an entry which is currently referenced */
386 if (ts->ref_cnt)
387 continue;
388
Willy Tarreau86257dc2010-06-06 12:57:10 +0200389 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
391 if (!tick_is_expired(ts->expire, now_ms)) {
392 if (!tick_isset(ts->expire))
393 continue;
394
Willy Tarreau86257dc2010-06-06 12:57:10 +0200395 ts->exp.key = ts->expire;
396 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100397
Willy Tarreau86257dc2010-06-06 12:57:10 +0200398 if (!eb || eb->key > ts->exp.key)
399 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100400 continue;
401 }
402
403 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200404 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200405 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100406 stksess_free(t, ts);
407 }
408
409 /* We have found no task to expire in any tree */
410 t->exp_next = TICK_ETERNITY;
411 return t->exp_next;
412}
413
414/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200415 * Task processing function to trash expired sticky sessions. A pointer to the
416 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100417 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200418static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100419{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200420 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100421
422 task->expire = stktable_trash_expired(t);
423 return task;
424}
425
Willy Tarreauaea940e2010-06-06 11:56:36 +0200426/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100427int stktable_init(struct stktable *t)
428{
429 if (t->size) {
430 memset(&t->keys, 0, sizeof(t->keys));
431 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100432 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100433
Willy Tarreau393379c2010-06-06 12:11:37 +0200434 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100435
436 t->exp_next = TICK_ETERNITY;
437 if ( t->expire ) {
438 t->exp_task = task_new();
439 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100440 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 Tarreau7d562212016-11-25 16:10:05 +01001523/* set temp integer to the number of used entries in the table pointed to by expr.
1524 * Accepts exactly 1 argument of type table.
1525 */
1526static int
1527smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1528{
1529 smp->flags = SMP_F_VOL_TEST;
1530 smp->data.type = SMP_T_SINT;
1531 smp->data.u.sint = args->data.prx->table.current;
1532 return 1;
1533}
1534
1535/* set temp integer to the number of free entries in the table pointed to by expr.
1536 * Accepts exactly 1 argument of type table.
1537 */
1538static int
1539smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
1540{
1541 struct proxy *px;
1542
1543 px = args->data.prx;
1544 smp->flags = SMP_F_VOL_TEST;
1545 smp->data.type = SMP_T_SINT;
1546 smp->data.u.sint = px->table.size - px->table.current;
1547 return 1;
1548}
1549
1550/* Returns a pointer to a stkctr depending on the fetch keyword name.
1551 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
1552 * sc[0-9]_* will return a pointer to the respective field in the
1553 * stream <l4>. sc_* requires an UINT argument specifying the stick
1554 * counter number. src_* will fill a locally allocated structure with
1555 * the table and entry corresponding to what is specified with src_*.
1556 * NULL may be returned if the designated stkctr is not tracked. For
1557 * the sc_* and sc[0-9]_* forms, an optional table argument may be
1558 * passed. When present, the currently tracked key is then looked up
1559 * in the specified table instead of the current table. The purpose is
1560 * to be able to convery multiple values per key (eg: have gpc0 from
1561 * multiple tables). <strm> is allowed to be NULL, in which case only
1562 * the session will be consulted.
1563 */
1564struct stkctr *
1565smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
1566{
1567 static struct stkctr stkctr;
1568 struct stkctr *stkptr;
1569 struct stksess *stksess;
1570 unsigned int num = kw[2] - '0';
1571 int arg = 0;
1572
1573 if (num == '_' - '0') {
1574 /* sc_* variant, args[0] = ctr# (mandatory) */
1575 num = args[arg++].data.sint;
1576 if (num >= MAX_SESS_STKCTR)
1577 return NULL;
1578 }
1579 else if (num > 9) { /* src_* variant, args[0] = table */
1580 struct stktable_key *key;
1581 struct connection *conn = objt_conn(sess->origin);
1582 struct sample smp;
1583
1584 if (!conn)
1585 return NULL;
1586
1587 /* Fetch source adress in a sample. */
1588 smp.px = NULL;
1589 smp.sess = sess;
1590 smp.strm = strm;
1591 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
1592 return NULL;
1593
1594 /* Converts into key. */
1595 key = smp_to_stkey(&smp, &args->data.prx->table);
1596 if (!key)
1597 return NULL;
1598
1599 stkctr.table = &args->data.prx->table;
1600 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
1601 return &stkctr;
1602 }
1603
1604 /* Here, <num> contains the counter number from 0 to 9 for
1605 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
1606 * args[arg] is the first optional argument. We first lookup the
1607 * ctr form the stream, then from the session if it was not there.
1608 */
1609
1610 if (strm)
1611 stkptr = &strm->stkctr[num];
1612 if (!strm || !stkctr_entry(stkptr)) {
1613 stkptr = &sess->stkctr[num];
1614 if (!stkctr_entry(stkptr))
1615 return NULL;
1616 }
1617
1618 stksess = stkctr_entry(stkptr);
1619 if (!stksess)
1620 return NULL;
1621
1622 if (unlikely(args[arg].type == ARGT_TAB)) {
1623 /* an alternate table was specified, let's look up the same key there */
1624 stkctr.table = &args[arg].data.prx->table;
1625 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
1626 return &stkctr;
1627 }
1628 return stkptr;
1629}
1630
1631/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
1632 * the entry if it doesn't exist yet. This is needed for a few fetch
1633 * functions which need to create an entry, such as src_inc_gpc* and
1634 * src_clr_gpc*.
1635 */
1636struct stkctr *
1637smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
1638{
1639 static struct stkctr stkctr;
1640 struct stktable_key *key;
1641 struct connection *conn = objt_conn(sess->origin);
1642 struct sample smp;
1643
1644 if (strncmp(kw, "src_", 4) != 0)
1645 return NULL;
1646
1647 if (!conn)
1648 return NULL;
1649
1650 /* Fetch source adress in a sample. */
1651 smp.px = NULL;
1652 smp.sess = sess;
1653 smp.strm = strm;
1654 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
1655 return NULL;
1656
1657 /* Converts into key. */
1658 key = smp_to_stkey(&smp, &args->data.prx->table);
1659 if (!key)
1660 return NULL;
1661
1662 stkctr.table = &args->data.prx->table;
1663 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
1664 return &stkctr;
1665}
1666
1667/* set return a boolean indicating if the requested stream counter is
1668 * currently being tracked or not.
1669 * Supports being called as "sc[0-9]_tracked" only.
1670 */
1671static int
1672smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1673{
1674 smp->flags = SMP_F_VOL_TEST;
1675 smp->data.type = SMP_T_BOOL;
1676 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1677 return 1;
1678}
1679
1680/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
1681 * frontend counters or from the src.
1682 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
1683 * zero is returned if the key is new.
1684 */
1685static int
1686smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1687{
1688 struct stkctr *stkctr;
1689
1690 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1691 if (!stkctr)
1692 return 0;
1693
1694 smp->flags = SMP_F_VOL_TEST;
1695 smp->data.type = SMP_T_SINT;
1696 smp->data.u.sint = 0;
1697
1698 if (stkctr_entry(stkctr) != NULL) {
1699 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
1700 if (!ptr)
1701 return 0; /* parameter not stored */
1702 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
1703 }
1704 return 1;
1705}
1706
1707/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
1708 * frontend counters or from the src.
1709 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
1710 * zero is returned if the key is new.
1711 */
1712static int
1713smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1714{
1715 struct stkctr *stkctr;
1716
1717 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1718 if (!stkctr)
1719 return 0;
1720
1721 smp->flags = SMP_F_VOL_TEST;
1722 smp->data.type = SMP_T_SINT;
1723 smp->data.u.sint = 0;
1724
1725 if (stkctr_entry(stkctr) != NULL) {
1726 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
1727 if (!ptr)
1728 return 0; /* parameter not stored */
1729 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
1730 }
1731 return 1;
1732}
1733
1734/* set <smp> to the General Purpose Counter 0's event rate from the stream's
1735 * tracked frontend counters or from the src.
1736 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
1737 * Value zero is returned if the key is new.
1738 */
1739static int
1740smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
1741{
1742 struct stkctr *stkctr;
1743
1744 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1745 if (!stkctr)
1746 return 0;
1747
1748 smp->flags = SMP_F_VOL_TEST;
1749 smp->data.type = SMP_T_SINT;
1750 smp->data.u.sint = 0;
1751 if (stkctr_entry(stkctr) != NULL) {
1752 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
1753 if (!ptr)
1754 return 0; /* parameter not stored */
1755 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
1756 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
1757 }
1758 return 1;
1759}
1760
1761/* Increment the General Purpose Counter 0 value from the stream's tracked
1762 * frontend counters and return it into temp integer.
1763 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
1764 */
1765static int
1766smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1767{
1768 struct stkctr *stkctr;
1769
1770 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1771 if (!stkctr)
1772 return 0;
1773
1774 smp->flags = SMP_F_VOL_TEST;
1775 smp->data.type = SMP_T_SINT;
1776 smp->data.u.sint = 0;
1777
1778 if (stkctr_entry(stkctr) == NULL)
1779 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
1780
1781 if (stkctr && stkctr_entry(stkctr)) {
1782 void *ptr1,*ptr2;
1783
1784 /* First, update gpc0_rate if it's tracked. Second, update its
1785 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
1786 */
1787 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
1788 if (ptr1) {
1789 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1790 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
1791 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
1792 }
1793
1794 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
1795 if (ptr2)
1796 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
1797
1798 /* If data was modified, we need to touch to re-schedule sync */
1799 if (ptr1 || ptr2)
1800 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
1801 }
1802 return 1;
1803}
1804
1805/* Clear the General Purpose Counter 0 value from the stream's tracked
1806 * frontend counters and return its previous value into temp integer.
1807 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
1808 */
1809static int
1810smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1811{
1812 struct stkctr *stkctr;
1813
1814 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1815 if (!stkctr)
1816 return 0;
1817
1818 smp->flags = SMP_F_VOL_TEST;
1819 smp->data.type = SMP_T_SINT;
1820 smp->data.u.sint = 0;
1821
1822 if (stkctr_entry(stkctr) == NULL)
1823 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
1824
1825 if (stkctr_entry(stkctr) != NULL) {
1826 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
1827 if (!ptr)
1828 return 0; /* parameter not stored */
1829 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
1830 stktable_data_cast(ptr, gpc0) = 0;
1831 /* If data was modified, we need to touch to re-schedule sync */
1832 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
1833 }
1834 return 1;
1835}
1836
1837/* set <smp> to the cumulated number of connections from the stream's tracked
1838 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
1839 * "src_conn_cnt" only.
1840 */
1841static int
1842smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1843{
1844 struct stkctr *stkctr;
1845
1846 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1847 if (!stkctr)
1848 return 0;
1849
1850 smp->flags = SMP_F_VOL_TEST;
1851 smp->data.type = SMP_T_SINT;
1852 smp->data.u.sint = 0;
1853 if (stkctr_entry(stkctr) != NULL) {
1854 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
1855 if (!ptr)
1856 return 0; /* parameter not stored */
1857 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
1858 }
1859 return 1;
1860}
1861
1862/* set <smp> to the connection rate from the stream's tracked frontend
1863 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
1864 * only.
1865 */
1866static int
1867smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
1868{
1869 struct stkctr *stkctr;
1870
1871 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1872 if (!stkctr)
1873 return 0;
1874
1875 smp->flags = SMP_F_VOL_TEST;
1876 smp->data.type = SMP_T_SINT;
1877 smp->data.u.sint = 0;
1878 if (stkctr_entry(stkctr) != NULL) {
1879 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
1880 if (!ptr)
1881 return 0; /* parameter not stored */
1882 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
1883 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
1884 }
1885 return 1;
1886}
1887
1888/* set temp integer to the number of connections from the stream's source address
1889 * in the table pointed to by expr, after updating it.
1890 * Accepts exactly 1 argument of type table.
1891 */
1892static int
1893smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1894{
1895 struct connection *conn = objt_conn(smp->sess->origin);
1896 struct stksess *ts;
1897 struct stktable_key *key;
1898 void *ptr;
1899 struct proxy *px;
1900
1901 if (!conn)
1902 return 0;
1903
1904 /* Fetch source adress in a sample. */
1905 if (!smp_fetch_src(NULL, smp, NULL, NULL))
1906 return 0;
1907
1908 /* Converts into key. */
1909 key = smp_to_stkey(smp, &args->data.prx->table);
1910 if (!key)
1911 return 0;
1912
1913 px = args->data.prx;
1914
1915 if ((ts = stktable_update_key(&px->table, key)) == NULL)
1916 /* entry does not exist and could not be created */
1917 return 0;
1918
1919 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
1920 if (!ptr)
1921 return 0; /* parameter not stored in this table */
1922
1923 smp->data.type = SMP_T_SINT;
1924 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
1925 /* Touch was previously performed by stktable_update_key */
1926 smp->flags = SMP_F_VOL_TEST;
1927 return 1;
1928}
1929
1930/* set <smp> to the number of concurrent connections from the stream's tracked
1931 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
1932 * "src_conn_cur" only.
1933 */
1934static int
1935smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
1936{
1937 struct stkctr *stkctr;
1938
1939 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1940 if (!stkctr)
1941 return 0;
1942
1943 smp->flags = SMP_F_VOL_TEST;
1944 smp->data.type = SMP_T_SINT;
1945 smp->data.u.sint = 0;
1946 if (stkctr_entry(stkctr) != NULL) {
1947 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
1948 if (!ptr)
1949 return 0; /* parameter not stored */
1950 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
1951 }
1952 return 1;
1953}
1954
1955/* set <smp> to the cumulated number of streams from the stream's tracked
1956 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
1957 * "src_sess_cnt" only.
1958 */
1959static int
1960smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1961{
1962 struct stkctr *stkctr;
1963
1964 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1965 if (!stkctr)
1966 return 0;
1967
1968 smp->flags = SMP_F_VOL_TEST;
1969 smp->data.type = SMP_T_SINT;
1970 smp->data.u.sint = 0;
1971 if (stkctr_entry(stkctr) != NULL) {
1972 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
1973 if (!ptr)
1974 return 0; /* parameter not stored */
1975 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
1976 }
1977 return 1;
1978}
1979
1980/* set <smp> to the stream rate from the stream's tracked frontend counters.
1981 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
1982 */
1983static int
1984smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
1985{
1986 struct stkctr *stkctr;
1987
1988 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1989 if (!stkctr)
1990 return 0;
1991
1992 smp->flags = SMP_F_VOL_TEST;
1993 smp->data.type = SMP_T_SINT;
1994 smp->data.u.sint = 0;
1995 if (stkctr_entry(stkctr) != NULL) {
1996 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
1997 if (!ptr)
1998 return 0; /* parameter not stored */
1999 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2000 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
2001 }
2002 return 1;
2003}
2004
2005/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
2006 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2007 * "src_http_req_cnt" only.
2008 */
2009static int
2010smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2011{
2012 struct stkctr *stkctr;
2013
2014 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2015 if (!stkctr)
2016 return 0;
2017
2018 smp->flags = SMP_F_VOL_TEST;
2019 smp->data.type = SMP_T_SINT;
2020 smp->data.u.sint = 0;
2021 if (stkctr_entry(stkctr) != NULL) {
2022 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
2023 if (!ptr)
2024 return 0; /* parameter not stored */
2025 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
2026 }
2027 return 1;
2028}
2029
2030/* set <smp> to the HTTP request rate from the stream's tracked frontend
2031 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2032 * "src_http_req_rate" only.
2033 */
2034static int
2035smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2036{
2037 struct stkctr *stkctr;
2038
2039 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2040 if (!stkctr)
2041 return 0;
2042
2043 smp->flags = SMP_F_VOL_TEST;
2044 smp->data.type = SMP_T_SINT;
2045 smp->data.u.sint = 0;
2046 if (stkctr_entry(stkctr) != NULL) {
2047 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
2048 if (!ptr)
2049 return 0; /* parameter not stored */
2050 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2051 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2052 }
2053 return 1;
2054}
2055
2056/* set <smp> to the cumulated number of HTTP requests errors from the stream's
2057 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2058 * "src_http_err_cnt" only.
2059 */
2060static int
2061smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2062{
2063 struct stkctr *stkctr;
2064
2065 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2066 if (!stkctr)
2067 return 0;
2068
2069 smp->flags = SMP_F_VOL_TEST;
2070 smp->data.type = SMP_T_SINT;
2071 smp->data.u.sint = 0;
2072 if (stkctr_entry(stkctr) != NULL) {
2073 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
2074 if (!ptr)
2075 return 0; /* parameter not stored */
2076 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
2077 }
2078 return 1;
2079}
2080
2081/* set <smp> to the HTTP request error rate from the stream's tracked frontend
2082 * counters. Supports being called as "sc[0-9]_http_err_rate" or
2083 * "src_http_err_rate" only.
2084 */
2085static int
2086smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2087{
2088 struct stkctr *stkctr;
2089
2090 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2091 if (!stkctr)
2092 return 0;
2093
2094 smp->flags = SMP_F_VOL_TEST;
2095 smp->data.type = SMP_T_SINT;
2096 smp->data.u.sint = 0;
2097 if (stkctr_entry(stkctr) != NULL) {
2098 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
2099 if (!ptr)
2100 return 0; /* parameter not stored */
2101 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2102 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2103 }
2104 return 1;
2105}
2106
2107/* set <smp> to the number of kbytes received from clients, as found in the
2108 * stream's tracked frontend counters. Supports being called as
2109 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
2110 */
2111static int
2112smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
2113{
2114 struct stkctr *stkctr;
2115
2116 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2117 if (!stkctr)
2118 return 0;
2119
2120 smp->flags = SMP_F_VOL_TEST;
2121 smp->data.type = SMP_T_SINT;
2122 smp->data.u.sint = 0;
2123 if (stkctr_entry(stkctr) != NULL) {
2124 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
2125 if (!ptr)
2126 return 0; /* parameter not stored */
2127 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2128 }
2129 return 1;
2130}
2131
2132/* set <smp> to the data rate received from clients in bytes/s, as found
2133 * in the stream's tracked frontend counters. Supports being called as
2134 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
2135 */
2136static int
2137smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2138{
2139 struct stkctr *stkctr;
2140
2141 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2142 if (!stkctr)
2143 return 0;
2144
2145 smp->flags = SMP_F_VOL_TEST;
2146 smp->data.type = SMP_T_SINT;
2147 smp->data.u.sint = 0;
2148 if (stkctr_entry(stkctr) != NULL) {
2149 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
2150 if (!ptr)
2151 return 0; /* parameter not stored */
2152 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2153 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2154 }
2155 return 1;
2156}
2157
2158/* set <smp> to the number of kbytes sent to clients, as found in the
2159 * stream's tracked frontend counters. Supports being called as
2160 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
2161 */
2162static int
2163smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
2164{
2165 struct stkctr *stkctr;
2166
2167 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2168 if (!stkctr)
2169 return 0;
2170
2171 smp->flags = SMP_F_VOL_TEST;
2172 smp->data.type = SMP_T_SINT;
2173 smp->data.u.sint = 0;
2174 if (stkctr_entry(stkctr) != NULL) {
2175 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
2176 if (!ptr)
2177 return 0; /* parameter not stored */
2178 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
2179 }
2180 return 1;
2181}
2182
2183/* set <smp> to the data rate sent to clients in bytes/s, as found in the
2184 * stream's tracked frontend counters. Supports being called as
2185 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
2186 */
2187static int
2188smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2189{
2190 struct stkctr *stkctr;
2191
2192 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2193 if (!stkctr)
2194 return 0;
2195
2196 smp->flags = SMP_F_VOL_TEST;
2197 smp->data.type = SMP_T_SINT;
2198 smp->data.u.sint = 0;
2199 if (stkctr_entry(stkctr) != NULL) {
2200 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
2201 if (!ptr)
2202 return 0; /* parameter not stored */
2203 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
2204 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
2205 }
2206 return 1;
2207}
2208
2209/* set <smp> to the number of active trackers on the SC entry in the stream's
2210 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
2211 */
2212static int
2213smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
2214{
2215 struct stkctr *stkctr;
2216
2217 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2218 if (!stkctr)
2219 return 0;
2220
2221 smp->flags = SMP_F_VOL_TEST;
2222 smp->data.type = SMP_T_SINT;
2223 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
2224 return 1;
2225}
2226
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002227
2228/* The functions below are used to manipulate table contents from the CLI.
2229 * There are 3 main actions, "clear", "set" and "show". The code is shared
2230 * between all actions, and the action is encoded in the void *private in
2231 * the appctx as well as in the keyword registration, among one of the
2232 * following values.
2233 */
2234
2235enum {
2236 STK_CLI_ACT_CLR,
2237 STK_CLI_ACT_SET,
2238 STK_CLI_ACT_SHOW,
2239};
2240
2241/* Dump the status of a table to a stream interface's
2242 * read buffer. It returns 0 if the output buffer is full
2243 * and needs to be called again, otherwise non-zero.
2244 */
2245static int table_dump_head_to_buffer(struct chunk *msg, struct stream_interface *si,
2246 struct proxy *proxy, struct proxy *target)
2247{
2248 struct stream *s = si_strm(si);
2249
2250 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
2251 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
2252
2253 /* any other information should be dumped here */
2254
William Lallemand07a62f72017-05-24 00:57:40 +02002255 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002256 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
2257
2258 if (bi_putchk(si_ic(si), msg) == -1) {
2259 si_applet_cant_put(si);
2260 return 0;
2261 }
2262
2263 return 1;
2264}
2265
2266/* Dump a table entry to a stream interface's
2267 * read buffer. It returns 0 if the output buffer is full
2268 * and needs to be called again, otherwise non-zero.
2269 */
2270static int table_dump_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
2271 struct proxy *proxy, struct stksess *entry)
2272{
2273 int dt;
2274
2275 chunk_appendf(msg, "%p:", entry);
2276
2277 if (proxy->table.type == SMP_T_IPV4) {
2278 char addr[INET_ADDRSTRLEN];
2279 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
2280 chunk_appendf(msg, " key=%s", addr);
2281 }
2282 else if (proxy->table.type == SMP_T_IPV6) {
2283 char addr[INET6_ADDRSTRLEN];
2284 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
2285 chunk_appendf(msg, " key=%s", addr);
2286 }
2287 else if (proxy->table.type == SMP_T_SINT) {
2288 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
2289 }
2290 else if (proxy->table.type == SMP_T_STR) {
2291 chunk_appendf(msg, " key=");
2292 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
2293 }
2294 else {
2295 chunk_appendf(msg, " key=");
2296 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
2297 }
2298
2299 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
2300
2301 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
2302 void *ptr;
2303
2304 if (proxy->table.data_ofs[dt] == 0)
2305 continue;
2306 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
2307 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
2308 else
2309 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
2310
2311 ptr = stktable_data_ptr(&proxy->table, entry, dt);
2312 switch (stktable_data_types[dt].std_type) {
2313 case STD_T_SINT:
2314 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
2315 break;
2316 case STD_T_UINT:
2317 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
2318 break;
2319 case STD_T_ULL:
2320 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
2321 break;
2322 case STD_T_FRQP:
2323 chunk_appendf(msg, "%d",
2324 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
2325 proxy->table.data_arg[dt].u));
2326 break;
2327 }
2328 }
2329 chunk_appendf(msg, "\n");
2330
2331 if (bi_putchk(si_ic(si), msg) == -1) {
2332 si_applet_cant_put(si);
2333 return 0;
2334 }
2335
2336 return 1;
2337}
2338
2339
2340/* Processes a single table entry matching a specific key passed in argument.
2341 * returns 0 if wants to be called again, 1 if has ended processing.
2342 */
2343static int table_process_entry_per_key(struct appctx *appctx, char **args)
2344{
2345 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002346 struct proxy *px = appctx->ctx.table.target;
2347 struct stksess *ts;
2348 uint32_t uint32_key;
2349 unsigned char ip6_key[sizeof(struct in6_addr)];
2350 long long value;
2351 int data_type;
2352 int cur_arg;
2353 void *ptr;
2354 struct freq_ctr_period *frqp;
2355
2356 if (!*args[4]) {
2357 appctx->ctx.cli.msg = "Key value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002358 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002359 return 1;
2360 }
2361
2362 switch (px->table.type) {
2363 case SMP_T_IPV4:
2364 uint32_key = htonl(inetaddr_host(args[4]));
2365 static_table_key->key = &uint32_key;
2366 break;
2367 case SMP_T_IPV6:
2368 inet_pton(AF_INET6, args[4], ip6_key);
2369 static_table_key->key = &ip6_key;
2370 break;
2371 case SMP_T_SINT:
2372 {
2373 char *endptr;
2374 unsigned long val;
2375 errno = 0;
2376 val = strtoul(args[4], &endptr, 10);
2377 if ((errno == ERANGE && val == ULONG_MAX) ||
2378 (errno != 0 && val == 0) || endptr == args[4] ||
2379 val > 0xffffffff) {
2380 appctx->ctx.cli.msg = "Invalid key\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002381 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002382 return 1;
2383 }
2384 uint32_key = (uint32_t) val;
2385 static_table_key->key = &uint32_key;
2386 break;
2387 }
2388 break;
2389 case SMP_T_STR:
2390 static_table_key->key = args[4];
2391 static_table_key->key_len = strlen(args[4]);
2392 break;
2393 default:
Willy Tarreaua24bc782016-12-14 15:50:35 +01002394 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002395 case STK_CLI_ACT_SHOW:
2396 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
2397 break;
2398 case STK_CLI_ACT_CLR:
2399 appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
2400 break;
2401 case STK_CLI_ACT_SET:
2402 appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n";
2403 break;
2404 default:
2405 appctx->ctx.cli.msg = "Unknown action\n";
2406 break;
2407 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002408 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002409 return 1;
2410 }
2411
2412 /* check permissions */
2413 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2414 return 1;
2415
2416 ts = stktable_lookup_key(&px->table, static_table_key);
2417
Willy Tarreaua24bc782016-12-14 15:50:35 +01002418 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002419 case STK_CLI_ACT_SHOW:
2420 if (!ts)
2421 return 1;
2422 chunk_reset(&trash);
2423 if (!table_dump_head_to_buffer(&trash, si, px, px))
2424 return 0;
2425 if (!table_dump_entry_to_buffer(&trash, si, px, ts))
2426 return 0;
2427 break;
2428
2429 case STK_CLI_ACT_CLR:
2430 if (!ts)
2431 return 1;
2432 if (ts->ref_cnt) {
2433 /* don't delete an entry which is currently referenced */
2434 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002435 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002436 return 1;
2437 }
2438 stksess_kill(&px->table, ts);
2439 break;
2440
2441 case STK_CLI_ACT_SET:
2442 if (ts)
2443 stktable_touch(&px->table, ts, 1);
2444 else {
2445 ts = stksess_new(&px->table, static_table_key);
2446 if (!ts) {
2447 /* don't delete an entry which is currently referenced */
2448 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002449 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002450 return 1;
2451 }
2452 stktable_store(&px->table, ts, 1);
2453 }
2454
2455 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
2456 if (strncmp(args[cur_arg], "data.", 5) != 0) {
2457 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002458 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002459 return 1;
2460 }
2461
2462 data_type = stktable_get_data_type(args[cur_arg] + 5);
2463 if (data_type < 0) {
2464 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002465 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002466 return 1;
2467 }
2468
2469 if (!px->table.data_ofs[data_type]) {
2470 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002471 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002472 return 1;
2473 }
2474
2475 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
2476 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002477 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002478 return 1;
2479 }
2480
2481 ptr = stktable_data_ptr(&px->table, ts, data_type);
2482
2483 switch (stktable_data_types[data_type].std_type) {
2484 case STD_T_SINT:
2485 stktable_data_cast(ptr, std_t_sint) = value;
2486 break;
2487 case STD_T_UINT:
2488 stktable_data_cast(ptr, std_t_uint) = value;
2489 break;
2490 case STD_T_ULL:
2491 stktable_data_cast(ptr, std_t_ull) = value;
2492 break;
2493 case STD_T_FRQP:
2494 /* We set both the current and previous values. That way
2495 * the reported frequency is stable during all the period
2496 * then slowly fades out. This allows external tools to
2497 * push measures without having to update them too often.
2498 */
2499 frqp = &stktable_data_cast(ptr, std_t_frqp);
2500 frqp->curr_tick = now_ms;
2501 frqp->prev_ctr = 0;
2502 frqp->curr_ctr = value;
2503 break;
2504 }
2505 }
2506 break;
2507
2508 default:
2509 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002510 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002511 break;
2512 }
2513 return 1;
2514}
2515
2516/* Prepares the appctx fields with the data-based filters from the command line.
2517 * Returns 0 if the dump can proceed, 1 if has ended processing.
2518 */
2519static int table_prepare_data_request(struct appctx *appctx, char **args)
2520{
Willy Tarreaua24bc782016-12-14 15:50:35 +01002521 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002522 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002523 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002524 return 1;
2525 }
2526
2527 /* condition on stored data value */
2528 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
2529 if (appctx->ctx.table.data_type < 0) {
2530 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002531 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002532 return 1;
2533 }
2534
2535 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
2536 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002537 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002538 return 1;
2539 }
2540
2541 appctx->ctx.table.data_op = get_std_op(args[4]);
2542 if (appctx->ctx.table.data_op < 0) {
2543 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002544 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002545 return 1;
2546 }
2547
2548 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
2549 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002550 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002551 return 1;
2552 }
2553
2554 /* OK we're done, all the fields are set */
2555 return 0;
2556}
2557
2558/* returns 0 if wants to be called, 1 if has ended processing */
2559static int cli_parse_table_req(char **args, struct appctx *appctx, void *private)
2560{
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002561 appctx->ctx.table.data_type = -1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002562 appctx->ctx.table.target = NULL;
2563 appctx->ctx.table.proxy = NULL;
2564 appctx->ctx.table.entry = NULL;
Willy Tarreaua24bc782016-12-14 15:50:35 +01002565 appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002566
2567 if (*args[2]) {
2568 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
2569 if (!appctx->ctx.table.target) {
2570 appctx->ctx.cli.msg = "No such table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002571 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002572 return 1;
2573 }
2574 }
2575 else {
Willy Tarreaua24bc782016-12-14 15:50:35 +01002576 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002577 goto err_args;
2578 return 0;
2579 }
2580
2581 if (strcmp(args[3], "key") == 0)
2582 return table_process_entry_per_key(appctx, args);
2583 else if (strncmp(args[3], "data.", 5) == 0)
2584 return table_prepare_data_request(appctx, args);
2585 else if (*args[3])
2586 goto err_args;
2587
2588 return 0;
2589
2590err_args:
Willy Tarreaua24bc782016-12-14 15:50:35 +01002591 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002592 case STK_CLI_ACT_SHOW:
2593 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
2594 break;
2595 case STK_CLI_ACT_CLR:
2596 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
2597 break;
2598 case STK_CLI_ACT_SET:
2599 appctx->ctx.cli.msg = "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n";
2600 break;
2601 default:
2602 appctx->ctx.cli.msg = "Unknown action\n";
2603 break;
2604 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002605 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002606 return 1;
2607}
2608
2609/* This function is used to deal with table operations (dump or clear depending
2610 * on the action stored in appctx->private). It returns 0 if the output buffer is
2611 * full and it needs to be called again, otherwise non-zero.
2612 */
2613static int cli_io_handler_table(struct appctx *appctx)
2614{
2615 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002616 struct stream *s = si_strm(si);
2617 struct ebmb_node *eb;
2618 int dt;
2619 int skip_entry;
Willy Tarreaua24bc782016-12-14 15:50:35 +01002620 int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002621
2622 /*
2623 * We have 3 possible states in appctx->st2 :
2624 * - STAT_ST_INIT : the first call
2625 * - STAT_ST_INFO : the proxy pointer points to the next table to
2626 * dump, the entry pointer is NULL ;
2627 * - STAT_ST_LIST : the proxy pointer points to the current table
2628 * and the entry pointer points to the next entry to be dumped,
2629 * and the refcount on the next entry is held ;
2630 * - STAT_ST_END : nothing left to dump, the buffer may contain some
2631 * data though.
2632 */
2633
2634 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2635 /* in case of abort, remove any refcount we might have set on an entry */
2636 if (appctx->st2 == STAT_ST_LIST) {
2637 appctx->ctx.table.entry->ref_cnt--;
2638 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2639 }
2640 return 1;
2641 }
2642
2643 chunk_reset(&trash);
2644
2645 while (appctx->st2 != STAT_ST_FIN) {
2646 switch (appctx->st2) {
2647 case STAT_ST_INIT:
2648 appctx->ctx.table.proxy = appctx->ctx.table.target;
2649 if (!appctx->ctx.table.proxy)
2650 appctx->ctx.table.proxy = proxy;
2651
2652 appctx->ctx.table.entry = NULL;
2653 appctx->st2 = STAT_ST_INFO;
2654 break;
2655
2656 case STAT_ST_INFO:
2657 if (!appctx->ctx.table.proxy ||
2658 (appctx->ctx.table.target &&
2659 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
2660 appctx->st2 = STAT_ST_END;
2661 break;
2662 }
2663
2664 if (appctx->ctx.table.proxy->table.size) {
2665 if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.target))
2666 return 0;
2667
2668 if (appctx->ctx.table.target &&
William Lallemand07a62f72017-05-24 00:57:40 +02002669 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002670 /* dump entries only if table explicitly requested */
2671 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
2672 if (eb) {
2673 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2674 appctx->ctx.table.entry->ref_cnt++;
2675 appctx->st2 = STAT_ST_LIST;
2676 break;
2677 }
2678 }
2679 }
2680 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2681 break;
2682
2683 case STAT_ST_LIST:
2684 skip_entry = 0;
2685
2686 if (appctx->ctx.table.data_type >= 0) {
2687 /* we're filtering on some data contents */
2688 void *ptr;
2689 long long data;
2690
2691 dt = appctx->ctx.table.data_type;
2692 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
2693 appctx->ctx.table.entry,
2694 dt);
2695
2696 data = 0;
2697 switch (stktable_data_types[dt].std_type) {
2698 case STD_T_SINT:
2699 data = stktable_data_cast(ptr, std_t_sint);
2700 break;
2701 case STD_T_UINT:
2702 data = stktable_data_cast(ptr, std_t_uint);
2703 break;
2704 case STD_T_ULL:
2705 data = stktable_data_cast(ptr, std_t_ull);
2706 break;
2707 case STD_T_FRQP:
2708 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
2709 appctx->ctx.table.proxy->table.data_arg[dt].u);
2710 break;
2711 }
2712
2713 /* skip the entry if the data does not match the test and the value */
2714 if ((data < appctx->ctx.table.value &&
2715 (appctx->ctx.table.data_op == STD_OP_EQ ||
2716 appctx->ctx.table.data_op == STD_OP_GT ||
2717 appctx->ctx.table.data_op == STD_OP_GE)) ||
2718 (data == appctx->ctx.table.value &&
2719 (appctx->ctx.table.data_op == STD_OP_NE ||
2720 appctx->ctx.table.data_op == STD_OP_GT ||
2721 appctx->ctx.table.data_op == STD_OP_LT)) ||
2722 (data > appctx->ctx.table.value &&
2723 (appctx->ctx.table.data_op == STD_OP_EQ ||
2724 appctx->ctx.table.data_op == STD_OP_LT ||
2725 appctx->ctx.table.data_op == STD_OP_LE)))
2726 skip_entry = 1;
2727 }
2728
2729 if (show && !skip_entry &&
2730 !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.entry))
2731 return 0;
2732
2733 appctx->ctx.table.entry->ref_cnt--;
2734
2735 eb = ebmb_next(&appctx->ctx.table.entry->key);
2736 if (eb) {
2737 struct stksess *old = appctx->ctx.table.entry;
2738 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2739 if (show)
2740 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
2741 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2742 stksess_kill(&appctx->ctx.table.proxy->table, old);
2743 appctx->ctx.table.entry->ref_cnt++;
2744 break;
2745 }
2746
2747
2748 if (show)
2749 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2750 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2751 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2752
2753 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2754 appctx->st2 = STAT_ST_INFO;
2755 break;
2756
2757 case STAT_ST_END:
2758 appctx->st2 = STAT_ST_FIN;
2759 break;
2760 }
2761 }
2762 return 1;
2763}
2764
2765static void cli_release_show_table(struct appctx *appctx)
2766{
2767 if (appctx->st2 == STAT_ST_LIST) {
2768 appctx->ctx.table.entry->ref_cnt--;
2769 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2770 }
2771}
2772
2773/* register cli keywords */
2774static struct cli_kw_list cli_kws = {{ },{
2775 { { "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 },
2776 { { "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 },
2777 { { "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 },
2778 {{},}
2779}};
2780
2781
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002782static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002783 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002784 { "sc-set-gpt0", parse_set_gpt0, 1 },
2785 { /* END */ }
2786}};
2787
Willy Tarreau620408f2016-10-21 16:37:51 +02002788static struct action_kw_list tcp_sess_kws = { { }, {
2789 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
2790 { "sc-set-gpt0", parse_set_gpt0, 1 },
2791 { /* END */ }
2792}};
2793
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002794static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002795 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002796 { "sc-set-gpt0", parse_set_gpt0, 1 },
2797 { /* END */ }
2798}};
2799
2800static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002801 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002802 { "sc-set-gpt0", parse_set_gpt0, 1 },
2803 { /* END */ }
2804}};
2805
2806static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002807 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002808 { "sc-set-gpt0", parse_set_gpt0, 1 },
2809 { /* END */ }
2810}};
2811
2812static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002813 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002814 { "sc-set-gpt0", parse_set_gpt0, 1 },
2815 { /* END */ }
2816}};
2817
Willy Tarreau7d562212016-11-25 16:10:05 +01002818///* Note: must not be declared <const> as its list will be overwritten.
2819// * Please take care of keeping this list alphabetically sorted.
2820// */
2821//static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
2822// { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2823// { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2824// { /* END */ },
2825//}};
2826/* Note: must not be declared <const> as its list will be overwritten.
2827 * Please take care of keeping this list alphabetically sorted.
2828 */
2829static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
2830 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2831 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2832 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2833 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2834 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2835 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01002836 { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01002837 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2838 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2839 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2840 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2841 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2842 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2843 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2844 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2845 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2846 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2847 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2848 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2849 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2850 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2851 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2852 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2853 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2854 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2855 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01002856 { "sc0_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01002857 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2858 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2859 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2860 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2861 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2862 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2863 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2864 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2865 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2866 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2867 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2868 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2869 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2870 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2871 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2872 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2873 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2874 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2875 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01002876 { "sc1_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01002877 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2878 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2879 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2880 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2881 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2882 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2883 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2884 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2885 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2886 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2887 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2888 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2889 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2890 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2891 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2892 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2893 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2894 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2895 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01002896 { "sc2_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01002897 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2898 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2899 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2900 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2901 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2902 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2903 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2904 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2905 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2906 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2907 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2908 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2909 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2910 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2911 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2912 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2913 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2914 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2915 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01002916 { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01002917 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2918 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2919 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2920 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2921 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2922 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2923 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2924 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2925 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2926 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2927 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2928 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2929 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2930 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2931 { /* END */ },
2932}};
2933
2934
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002935/* Note: must not be declared <const> as its list will be overwritten */
2936static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02002937 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
2938 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2939 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2940 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2941 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2942 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2943 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2944 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2945 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2946 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2947 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2948 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2949 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2950 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2951 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2952 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2953 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2954 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2955 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002956 { /* END */ },
2957}};
2958
2959__attribute__((constructor))
2960static void __stick_table_init(void)
2961{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002962 /* register som action keywords. */
2963 tcp_req_conn_keywords_register(&tcp_conn_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +02002964 tcp_req_sess_keywords_register(&tcp_sess_kws);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002965 tcp_req_cont_keywords_register(&tcp_req_kws);
2966 tcp_res_cont_keywords_register(&tcp_res_kws);
2967 http_req_keywords_register(&http_req_kws);
2968 http_res_keywords_register(&http_res_kws);
2969
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002970 /* register sample fetch and format conversion keywords */
Willy Tarreau7d562212016-11-25 16:10:05 +01002971 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002972 sample_register_convs(&sample_conv_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002973 cli_register_kw(&cli_kws);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002974}