blob: 13816bbed28acb07012fe9a53fcf33dd2f04b123 [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>
15
16#include <common/config.h>
17#include <common/memory.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
20#include <common/time.h>
21
22#include <ebmbtree.h>
23#include <ebsttree.h>
24
Willy Tarreaud9f316a2014-07-10 14:03:38 +020025#include <proto/arg.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020026#include <proto/proto_http.h>
27#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010028#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020029#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020030#include <proto/stream.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020031#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010032#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020033#include <proto/peers.h>
34#include <types/global.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010035
Willy Tarreau12785782012-04-27 21:37:17 +020036/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010037struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020038
Emeric Brun3bd697e2010-01-04 15:23:48 +010039/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020040 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
41 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010042 */
43void stksess_free(struct stktable *t, struct stksess *ts)
44{
45 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020046 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010047}
48
49/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020050 * Kill an stksess (only if its ref_cnt is zero).
51 */
52void stksess_kill(struct stktable *t, struct stksess *ts)
53{
54 if (ts->ref_cnt)
55 return;
56
57 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020058 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020059 ebmb_delete(&ts->key);
60 stksess_free(t, ts);
61}
62
63/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020064 * Initialize or update the key in the sticky session <ts> present in table <t>
65 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010066 */
Willy Tarreau393379c2010-06-06 12:11:37 +020067void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010068{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020069 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020070 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010071 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020072 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
73 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010074 }
75}
76
77
78/*
Willy Tarreau393379c2010-06-06 12:11:37 +020079 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
80 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010081 */
Willy Tarreau393379c2010-06-06 12:11:37 +020082static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010083{
Willy Tarreau393379c2010-06-06 12:11:37 +020084 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020085 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020086 ts->key.node.leaf_p = NULL;
87 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020088 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010089 return ts;
90}
91
92/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020093 * Trash oldest <to_batch> sticky sessions from table <t>
94 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010095 */
Willy Tarreau3a925c12013-09-04 17:54:01 +020096int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +010097{
98 struct stksess *ts;
99 struct eb32_node *eb;
100 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200101 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102
103 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
104
105 while (batched < to_batch) {
106
107 if (unlikely(!eb)) {
108 /* we might have reached the end of the tree, typically because
109 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200110 * half. Let's loop back to the beginning of the tree now if we
111 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100112 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200113 if (looped)
114 break;
115 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100116 eb = eb32_first(&t->exps);
117 if (likely(!eb))
118 break;
119 }
120
121 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200122 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100123 eb = eb32_next(eb);
124
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200125 /* don't delete an entry which is currently referenced */
126 if (ts->ref_cnt)
127 continue;
128
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130
Willy Tarreau86257dc2010-06-06 12:57:10 +0200131 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100132 if (!tick_isset(ts->expire))
133 continue;
134
Willy Tarreau86257dc2010-06-06 12:57:10 +0200135 ts->exp.key = ts->expire;
136 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreau86257dc2010-06-06 12:57:10 +0200138 if (!eb || eb->key > ts->exp.key)
139 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140
141 continue;
142 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143
Willy Tarreauaea940e2010-06-06 11:56:36 +0200144 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200145 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200146 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147 stksess_free(t, ts);
148 batched++;
149 }
150
151 return batched;
152}
153
154/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200155 * Allocate and initialise a new sticky session.
156 * The new sticky session is returned or NULL in case of lack of memory.
157 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200158 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
159 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100160 */
161struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
162{
163 struct stksess *ts;
164
165 if (unlikely(t->current == t->size)) {
166 if ( t->nopurge )
167 return NULL;
168
Emeric Brunfbce6d02010-09-23 18:10:00 +0200169 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 return NULL;
171 }
172
Willy Tarreau393379c2010-06-06 12:11:37 +0200173 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100174 if (ts) {
175 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200176 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200177 if (key)
178 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179 }
180
181 return ts;
182}
183
184/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200185 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200186 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200188struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189{
190 struct ebmb_node *eb;
191
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200192 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200193 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 +0100194 else
195 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
196
197 if (unlikely(!eb)) {
198 /* no session found */
199 return NULL;
200 }
201
Willy Tarreau86257dc2010-06-06 12:57:10 +0200202 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100203}
204
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200205/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
206 * This is mainly used for situations where we want to refresh a key's usage so
207 * that it does not expire, and we want to have it created if it was not there.
208 * The stksess is returned, or NULL if it could not be created.
209 */
210struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
211{
212 struct stksess *ts;
213
214 ts = stktable_lookup_key(table, key);
215 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200216 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200217
218 /* entry does not exist, initialize a new one */
219 ts = stksess_new(table, key);
220 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200221 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200222 return ts;
223}
224
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200225/*
226 * Looks in table <t> for a sticky session with same key as <ts>.
227 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200229struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100230{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231 struct ebmb_node *eb;
232
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200233 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200234 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200236 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200238 if (unlikely(!eb))
239 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100240
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200241 return ebmb_entry(eb, struct stksess, key);
242}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243
Willy Tarreaucb183642010-06-06 17:58:34 +0200244/* Update the expiration timer for <ts> but do not touch its expiration node.
245 * The table's expiration timer is updated if set.
246 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200247struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200248{
Emeric Brun85e77c72010-09-23 18:16:52 +0200249 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200250 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
251 if (t->expire) {
252 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
253 task_queue(t->exp_task);
254 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200255
Emeric Brunaaf58602015-06-15 17:23:30 +0200256 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200257 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200258 /* If this entry is not in the tree
259 or not scheduled for at least one peer */
260 if (!ts->upd.node.leaf_p
261 || (int)(t->commitupdate - ts->upd.key) >= 0
262 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200263 ts->upd.key = ++t->update;
264 t->localupdate = t->update;
265 eb32_delete(&ts->upd);
266 eb = eb32_insert(&t->updates, &ts->upd);
267 if (eb != &ts->upd) {
268 eb32_delete(eb);
269 eb32_insert(&t->updates, &ts->upd);
270 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200271 }
272 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
273 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200274 return ts;
275}
276
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200277/* Insert new sticky session <ts> in the table. It is assumed that it does not
278 * yet exist (the caller must check this). The table's timeout is updated if it
279 * is set. <ts> is returned.
280 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200281struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200282{
283 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200284 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200285 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200286 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200287 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100288}
289
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200290/* Returns a valid or initialized stksess for the specified stktable_key in the
291 * specified table, or NULL if the key was NULL, or if no entry was found nor
292 * could be created. The entry's expiration is updated.
293 */
294struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
295{
296 struct stksess *ts;
297
298 if (!key)
299 return NULL;
300
301 ts = stktable_lookup_key(table, key);
302 if (ts == NULL) {
303 /* entry does not exist, initialize a new one */
304 ts = stksess_new(table, key);
305 if (!ts)
306 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200307 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200308 }
309 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200310 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200311 return ts;
312}
313
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200315 * Trash expired sticky sessions from table <t>. The next expiration date is
316 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100317 */
318static int stktable_trash_expired(struct stktable *t)
319{
320 struct stksess *ts;
321 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200322 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323
324 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
325
326 while (1) {
327 if (unlikely(!eb)) {
328 /* we might have reached the end of the tree, typically because
329 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200330 * half. Let's loop back to the beginning of the tree now if we
331 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100332 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200333 if (looped)
334 break;
335 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100336 eb = eb32_first(&t->exps);
337 if (likely(!eb))
338 break;
339 }
340
341 if (likely(tick_is_lt(now_ms, eb->key))) {
342 /* timer not expired yet, revisit it later */
343 t->exp_next = eb->key;
344 return t->exp_next;
345 }
346
347 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200348 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100349 eb = eb32_next(eb);
350
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200351 /* don't delete an entry which is currently referenced */
352 if (ts->ref_cnt)
353 continue;
354
Willy Tarreau86257dc2010-06-06 12:57:10 +0200355 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100356
357 if (!tick_is_expired(ts->expire, now_ms)) {
358 if (!tick_isset(ts->expire))
359 continue;
360
Willy Tarreau86257dc2010-06-06 12:57:10 +0200361 ts->exp.key = ts->expire;
362 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363
Willy Tarreau86257dc2010-06-06 12:57:10 +0200364 if (!eb || eb->key > ts->exp.key)
365 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100366 continue;
367 }
368
369 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200370 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200371 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100372 stksess_free(t, ts);
373 }
374
375 /* We have found no task to expire in any tree */
376 t->exp_next = TICK_ETERNITY;
377 return t->exp_next;
378}
379
380/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200381 * Task processing function to trash expired sticky sessions. A pointer to the
382 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200384static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100385{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200386 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100387
388 task->expire = stktable_trash_expired(t);
389 return task;
390}
391
Willy Tarreauaea940e2010-06-06 11:56:36 +0200392/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100393int stktable_init(struct stktable *t)
394{
395 if (t->size) {
396 memset(&t->keys, 0, sizeof(t->keys));
397 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100398 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100399
Willy Tarreau393379c2010-06-06 12:11:37 +0200400 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100401
402 t->exp_next = TICK_ETERNITY;
403 if ( t->expire ) {
404 t->exp_task = task_new();
405 t->exp_task->process = process_table_expire;
406 t->exp_task->expire = TICK_ETERNITY;
407 t->exp_task->context = (void *)t;
408 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200409 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200410 peers_register_table(t->peers.p, t);
411 }
412
Emeric Brun3bd697e2010-01-04 15:23:48 +0100413 return t->pool != NULL;
414 }
415 return 1;
416}
417
418/*
419 * Configuration keywords of known table types
420 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200421struct stktable_type stktable_types[SMP_TYPES] = {
422 [SMP_T_SINT] = { "integer", 0, 4 },
423 [SMP_T_IPV4] = { "ip", 0, 4 },
424 [SMP_T_IPV6] = { "ipv6", 0, 16 },
425 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
426 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
427};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100428
429/*
430 * Parse table type configuration.
431 * Returns 0 on successful parsing, else 1.
432 * <myidx> is set at next configuration <args> index.
433 */
434int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
435{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200436 for (*type = 0; *type < SMP_TYPES; (*type)++) {
437 if (!stktable_types[*type].kw)
438 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100439 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
440 continue;
441
442 *key_size = stktable_types[*type].default_size;
443 (*myidx)++;
444
Willy Tarreauaea940e2010-06-06 11:56:36 +0200445 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100446 if (strcmp("len", args[*myidx]) == 0) {
447 (*myidx)++;
448 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200449 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100450 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200451 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200452 /* null terminated string needs +1 for '\0'. */
453 (*key_size)++;
454 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100455 (*myidx)++;
456 }
457 }
458 return 0;
459 }
460 return 1;
461}
462
Willy Tarreau8fed9032014-07-03 17:02:46 +0200463/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200464 * Note that the sample *is* modified and that the returned key may point
465 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200466 * Returns NULL if the sample could not be converted (eg: no matching type),
467 * otherwise a pointer to the static stktable_key filled with what is needed
468 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200469 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200470struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200471{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200472 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200473 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200474 return NULL;
475
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200476 /* Fill static_table_key. */
477 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200478
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200479 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200480 static_table_key->key = &smp->data.u.ipv4;
481 static_table_key->key_len = 4;
482 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200483
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200484 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200485 static_table_key->key = &smp->data.u.ipv6;
486 static_table_key->key_len = 16;
487 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200488
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200489 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200490 /* The stick table require a 32bit unsigned int, "sint" is a
491 * signed 64 it, so we can convert it inplace.
492 */
493 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
494 static_table_key->key = &smp->data.u.sint;
495 static_table_key->key_len = 4;
496 break;
497
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200498 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200499 if (!smp_make_safe(smp))
500 return NULL;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200501 static_table_key->key = smp->data.u.str.str;
502 static_table_key->key_len = smp->data.u.str.len;
503 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200504
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200505 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200506 if (smp->data.u.str.len < t->key_size) {
507 /* This type needs padding with 0. */
508 if (smp->data.u.str.size < t->key_size)
509 if (!smp_dup(smp))
510 return NULL;
511 if (smp->data.u.str.size < t->key_size)
512 return NULL;
513 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
514 t->key_size - smp->data.u.str.len);
515 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200516 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200517 static_table_key->key = smp->data.u.str.str;
518 static_table_key->key_len = smp->data.u.str.len;
519 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200520
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200521 default: /* impossible case. */
522 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200523 }
524
Willy Tarreau07115412012-10-29 21:56:59 +0100525 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200526}
527
528/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200529 * Process a fetch + format conversion as defined by the sample expression <expr>
530 * on request or response considering the <opt> parameter. Returns either NULL if
531 * no key could be extracted, or a pointer to the converted result stored in
532 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
533 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200534 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
535 * without SMP_OPT_FINAL). The output will be usable like this :
536 *
537 * return MAY_CHANGE FINAL Meaning for the sample
538 * NULL 0 * Not present and will never be (eg: header)
539 * NULL 1 0 Not present or unstable, could change (eg: req_len)
540 * NULL 1 1 Not present, will not change anymore
541 * smp 0 * Present and will not change (eg: header)
542 * smp 1 0 not possible
543 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200544 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200545struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200546 unsigned int opt, struct sample_expr *expr, struct sample *smp)
547{
548 if (smp)
549 memset(smp, 0, sizeof(*smp));
550
Willy Tarreau192252e2015-04-04 01:47:55 +0200551 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200552 if (!smp)
553 return NULL;
554
555 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
556 return NULL; /* we can only use stable samples */
557
558 return smp_to_stkey(smp, t);
559}
560
561/*
Willy Tarreau12785782012-04-27 21:37:17 +0200562 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200563 * type <table_type>, otherwise zero. Used in configuration check.
564 */
Willy Tarreau12785782012-04-27 21:37:17 +0200565int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200566{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100567 int out_type;
568
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200569 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200570 return 0;
571
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100572 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200573
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200574 /* Convert sample. */
575 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100576 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200577
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200578 return 1;
579}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100580
Willy Tarreauedee1d62014-07-15 16:44:27 +0200581/* Extra data types processing : after the last one, some room may remain
582 * before STKTABLE_DATA_TYPES that may be used to register extra data types
583 * at run time.
584 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200585struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200586 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200587 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200588 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200589 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200590 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
591 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
592 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
593 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
594 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
595 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
596 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
597 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
598 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
599 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
600 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
601 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
602 [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 +0200603};
604
Willy Tarreauedee1d62014-07-15 16:44:27 +0200605/* Registers stick-table extra data type with index <idx>, name <name>, type
606 * <std_type> and arg type <arg_type>. If the index is negative, the next free
607 * index is automatically allocated. The allocated index is returned, or -1 if
608 * no free index was found or <name> was already registered. The <name> is used
609 * directly as a pointer, so if it's not stable, the caller must allocate it.
610 */
611int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
612{
613 if (idx < 0) {
614 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
615 if (!stktable_data_types[idx].name)
616 break;
617
618 if (strcmp(stktable_data_types[idx].name, name) == 0)
619 return -1;
620 }
621 }
622
623 if (idx >= STKTABLE_DATA_TYPES)
624 return -1;
625
626 if (stktable_data_types[idx].name != NULL)
627 return -1;
628
629 stktable_data_types[idx].name = name;
630 stktable_data_types[idx].std_type = std_type;
631 stktable_data_types[idx].arg_type = arg_type;
632 return idx;
633}
634
Willy Tarreau08d5f982010-06-06 13:34:54 +0200635/*
636 * Returns the data type number for the stktable_data_type whose name is <name>,
637 * or <0 if not found.
638 */
639int stktable_get_data_type(char *name)
640{
641 int type;
642
643 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200644 if (!stktable_data_types[type].name)
645 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200646 if (strcmp(name, stktable_data_types[type].name) == 0)
647 return type;
648 }
649 return -1;
650}
651
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200652/* Casts sample <smp> to the type of the table specified in arg(0), and looks
653 * it up into this table. Returns true if found, false otherwise. The input
654 * type is STR so that input samples are converted to string (since all types
655 * can be converted to strings), then the function casts the string again into
656 * the table's type. This is a double conversion, but in the future we might
657 * support automatic input types to perform the cast on the fly.
658 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200659static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200660{
661 struct stktable *t;
662 struct stktable_key *key;
663 struct stksess *ts;
664
665 t = &arg_p[0].data.prx->table;
666
667 key = smp_to_stkey(smp, t);
668 if (!key)
669 return 0;
670
671 ts = stktable_lookup_key(t, key);
672
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200673 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200674 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200675 smp->flags = SMP_F_VOL_TEST;
676 return 1;
677}
678
679/* Casts sample <smp> to the type of the table specified in arg(0), and looks
680 * it up into this table. Returns the data rate received from clients in bytes/s
681 * if the key is present in the table, otherwise zero, so that comparisons can
682 * be easily performed. If the inspected parameter is not stored in the table,
683 * <not found> is returned.
684 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200685static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200686{
687 struct stktable *t;
688 struct stktable_key *key;
689 struct stksess *ts;
690 void *ptr;
691
692 t = &arg_p[0].data.prx->table;
693
694 key = smp_to_stkey(smp, t);
695 if (!key)
696 return 0;
697
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200698 ts = stktable_lookup_key(t, key);
699
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200700 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200701 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200702 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200703
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200704 if (!ts) /* key not present */
705 return 1;
706
707 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
708 if (!ptr)
709 return 0; /* parameter not stored */
710
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200711 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200712 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
713 return 1;
714}
715
716/* Casts sample <smp> to the type of the table specified in arg(0), and looks
717 * it up into this table. Returns the cumulated number of connections for the key
718 * if the key is present in the table, otherwise zero, so that comparisons can
719 * be easily performed. If the inspected parameter is not stored in the table,
720 * <not found> is returned.
721 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200722static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200723{
724 struct stktable *t;
725 struct stktable_key *key;
726 struct stksess *ts;
727 void *ptr;
728
729 t = &arg_p[0].data.prx->table;
730
731 key = smp_to_stkey(smp, t);
732 if (!key)
733 return 0;
734
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200735 ts = stktable_lookup_key(t, key);
736
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200737 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200739 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200740
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200741 if (!ts) /* key not present */
742 return 1;
743
744 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
745 if (!ptr)
746 return 0; /* parameter not stored */
747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200749 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 number of concurrent connections for the
754 * key if the key is present in the table, otherwise zero, so that comparisons
755 * can be easily performed. If the inspected parameter is not stored in the
756 * table, <not found> is returned.
757 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200758static int sample_conv_table_conn_cur(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_CUR);
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_cur);
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 rate of incoming connections from the key
790 * if the key is present in the table, otherwise zero, so that comparisons can
791 * be easily performed. If the inspected parameter is not stored in the table,
792 * <not found> is returned.
793 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200794static int sample_conv_table_conn_rate(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_RATE);
817 if (!ptr)
818 return 0; /* parameter not stored */
819
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200820 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200821 t->data_arg[STKTABLE_DT_CONN_RATE].u);
822 return 1;
823}
824
825/* Casts sample <smp> to the type of the table specified in arg(0), and looks
826 * it up into this table. Returns the data rate sent to clients in bytes/s
827 * if the key is present in the table, otherwise zero, so that comparisons can
828 * be easily performed. If the inspected parameter is not stored in the table,
829 * <not found> is returned.
830 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200831static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200832{
833 struct stktable *t;
834 struct stktable_key *key;
835 struct stksess *ts;
836 void *ptr;
837
838 t = &arg_p[0].data.prx->table;
839
840 key = smp_to_stkey(smp, t);
841 if (!key)
842 return 0;
843
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200844 ts = stktable_lookup_key(t, key);
845
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200846 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200847 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200848 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200849
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200850 if (!ts) /* key not present */
851 return 1;
852
853 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
854 if (!ptr)
855 return 0; /* parameter not stored */
856
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200857 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200858 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
859 return 1;
860}
861
862/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200863 * it up into this table. Returns the value of the GPT0 tag for the key
864 * if the key is present in the table, otherwise false, so that comparisons can
865 * be easily performed. If the inspected parameter is not stored in the table,
866 * <not found> is returned.
867 */
868static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
869{
870 struct stktable *t;
871 struct stktable_key *key;
872 struct stksess *ts;
873 void *ptr;
874
875 t = &arg_p[0].data.prx->table;
876
877 key = smp_to_stkey(smp, t);
878 if (!key)
879 return 0;
880
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200881 ts = stktable_lookup_key(t, key);
882
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200883 smp->flags = SMP_F_VOL_TEST;
884 smp->data.type = SMP_T_SINT;
885 smp->data.u.sint = 0;
886
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200887 if (!ts) /* key not present */
888 return 1;
889
890 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
891 if (!ptr)
892 return 0; /* parameter not stored */
893
894 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
895 return 1;
896}
897
898/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200899 * it up into this table. Returns the value of the GPC0 counter for the key
900 * if the key is present in the table, otherwise zero, so that comparisons can
901 * be easily performed. If the inspected parameter is not stored in the table,
902 * <not found> is returned.
903 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200904static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200905{
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
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200919 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200920 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200921 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200922
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200923 if (!ts) /* key not present */
924 return 1;
925
926 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
927 if (!ptr)
928 return 0; /* parameter not stored */
929
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200930 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200931 return 1;
932}
933
934/* Casts sample <smp> to the type of the table specified in arg(0), and looks
935 * it up into this table. Returns the event rate 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_rate(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_RATE);
963 if (!ptr)
964 return 0; /* parameter not stored */
965
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200966 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200967 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
968 return 1;
969}
970
971/* Casts sample <smp> to the type of the table specified in arg(0), and looks
972 * it up into this table. Returns the cumulated number of HTTP request errors
973 * for the key if the key is present in the table, otherwise zero, so that
974 * comparisons can be easily performed. If the inspected parameter is not stored
975 * in the table, <not found> is returned.
976 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200977static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200978{
979 struct stktable *t;
980 struct stktable_key *key;
981 struct stksess *ts;
982 void *ptr;
983
984 t = &arg_p[0].data.prx->table;
985
986 key = smp_to_stkey(smp, t);
987 if (!key)
988 return 0;
989
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200990 ts = stktable_lookup_key(t, key);
991
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200992 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200993 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200994 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200995
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200996 if (!ts) /* key not present */
997 return 1;
998
999 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1000 if (!ptr)
1001 return 0; /* parameter not stored */
1002
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001003 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001004 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 HTTP request error rate the key
1009 * if the key is present in the table, otherwise zero, so that comparisons can
1010 * be easily performed. If the inspected parameter is not stored in the table,
1011 * <not found> is returned.
1012 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001013static int sample_conv_table_http_err_rate(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_RATE);
1036 if (!ptr)
1037 return 0; /* parameter not stored */
1038
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001039 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001040 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1041 return 1;
1042}
1043
1044/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1045 * it up into this table. Returns the cumulated number of HTTP request for the
1046 * key if the key is present in the table, otherwise zero, so that comparisons
1047 * can be easily performed. If the inspected parameter is not stored in the
1048 * table, <not found> is returned.
1049 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001050static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001051{
1052 struct stktable *t;
1053 struct stktable_key *key;
1054 struct stksess *ts;
1055 void *ptr;
1056
1057 t = &arg_p[0].data.prx->table;
1058
1059 key = smp_to_stkey(smp, t);
1060 if (!key)
1061 return 0;
1062
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001063 ts = stktable_lookup_key(t, key);
1064
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001065 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001066 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001067 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001068
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001069 if (!ts) /* key not present */
1070 return 1;
1071
1072 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1073 if (!ptr)
1074 return 0; /* parameter not stored */
1075
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001076 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001077 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 HTTP request rate the key if the key is
1082 * present in the table, otherwise zero, so that comparisons can be easily
1083 * performed. If the inspected parameter is not stored in the table, <not found>
1084 * is returned.
1085 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001086static int sample_conv_table_http_req_rate(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_RATE);
1109 if (!ptr)
1110 return 0; /* parameter not stored */
1111
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001112 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001113 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1114 return 1;
1115}
1116
1117/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1118 * it up into this table. Returns the volume of datareceived from clients in kbytes
1119 * if the key is present in the table, otherwise zero, so that comparisons can
1120 * be easily performed. If the inspected parameter is not stored in the table,
1121 * <not found> is returned.
1122 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001123static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001124{
1125 struct stktable *t;
1126 struct stktable_key *key;
1127 struct stksess *ts;
1128 void *ptr;
1129
1130 t = &arg_p[0].data.prx->table;
1131
1132 key = smp_to_stkey(smp, t);
1133 if (!key)
1134 return 0;
1135
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001136 ts = stktable_lookup_key(t, key);
1137
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001138 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001139 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001140 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001141
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001142 if (!ts) /* key not present */
1143 return 1;
1144
1145 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1146 if (!ptr)
1147 return 0; /* parameter not stored */
1148
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001149 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001150 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 data sent to 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_out(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_OUT_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_out_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 server ID associated with the key if the
1191 * key is present in the table, otherwise zero, so that comparisons can be
1192 * 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_server_id(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_SERVER_ID);
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, server_id);
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 cumulated number of sessions for the
1227 * key if the key is present in the table, otherwise zero, so that comparisons
1228 * can be easily performed. If the inspected parameter is not stored in the
1229 * table, <not found> is returned.
1230 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001231static int sample_conv_table_sess_cnt(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_SESS_CNT);
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, sess_cnt);
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 session rate the key if the key is
1263 * present in the table, otherwise zero, so that comparisons can be easily
1264 * performed. If the inspected parameter is not stored in the table, <not found>
1265 * is returned.
1266 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001267static int sample_conv_table_sess_rate(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_RATE);
1290 if (!ptr)
1291 return 0; /* parameter not stored */
1292
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001293 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001294 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1295 return 1;
1296}
1297
1298/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1299 * it up into this table. Returns the amount of concurrent connections tracking
1300 * the same key if the key is present in the table, otherwise zero, so that
1301 * comparisons can be easily performed. If the inspected parameter is not
1302 * stored in the table, <not found> is returned.
1303 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001304static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001305{
1306 struct stktable *t;
1307 struct stktable_key *key;
1308 struct stksess *ts;
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)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001323 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001324
1325 return 1;
1326}
1327
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001328/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001329static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001330 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001331{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001332 struct stksess *ts;
1333 struct stkctr *stkctr;
1334
1335 /* Extract the stksess, return OK if no stksess available. */
1336 if (s)
1337 stkctr = &s->stkctr[rule->arg.gpc.sc];
1338 else
1339 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001340
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001341 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001342 if (ts) {
1343 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001344
Willy Tarreau79c1e912016-01-25 14:54:45 +01001345 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
1346 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
1347 if (ptr1)
1348 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1349 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001350
Willy Tarreau79c1e912016-01-25 14:54:45 +01001351 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1352 if (ptr2)
1353 stktable_data_cast(ptr2, gpc0)++;
1354
1355 /* If data was modified, we need to touch to re-schedule sync */
1356 if (ptr1 || ptr2)
1357 stktable_touch(stkctr->table, ts, 1);
1358 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001359 return ACT_RET_CONT;
1360}
1361
1362/* This function is a common parser for using variables. It understands
1363 * the formats:
1364 *
1365 * sc-inc-gpc0(<stick-table ID>)
1366 *
1367 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1368 * it returns 1 and the variable <expr> is filled with the pointer to the
1369 * expression to execute.
1370 */
1371static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1372 struct act_rule *rule, char **err)
1373{
1374 const char *cmd_name = args[*arg-1];
1375 char *error;
1376
1377 cmd_name += strlen("sc-inc-gpc0");
1378 if (*cmd_name == '\0') {
1379 /* default stick table id. */
1380 rule->arg.gpc.sc = 0;
1381 } else {
1382 /* parse the stick table id. */
1383 if (*cmd_name != '(') {
1384 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1385 return ACT_RET_PRS_ERR;
1386 }
1387 cmd_name++; /* jump the '(' */
1388 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1389 if (*error != ')') {
1390 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1391 return ACT_RET_PRS_ERR;
1392 }
1393
1394 if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) {
1395 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
1396 ACT_ACTION_TRK_SCMAX-1);
1397 return ACT_RET_PRS_ERR;
1398 }
1399 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001400 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001401 rule->action_ptr = action_inc_gpc0;
1402 return ACT_RET_PRS_OK;
1403}
1404
1405/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001406static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001407 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001408{
1409 void *ptr;
1410 struct stksess *ts;
1411 struct stkctr *stkctr;
1412
1413 /* Extract the stksess, return OK if no stksess available. */
1414 if (s)
1415 stkctr = &s->stkctr[rule->arg.gpt.sc];
1416 else
1417 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001418
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001419 ts = stkctr_entry(stkctr);
1420 if (!ts)
1421 return ACT_RET_CONT;
1422
1423 /* Store the sample in the required sc, and ignore errors. */
1424 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001425 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001426 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001427 stktable_touch(stkctr->table, ts, 1);
1428 }
1429
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001430 return ACT_RET_CONT;
1431}
1432
1433/* This function is a common parser for using variables. It understands
1434 * the format:
1435 *
1436 * set-gpt0(<stick-table ID>) <expression>
1437 *
1438 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1439 * it returns 1 and the variable <expr> is filled with the pointer to the
1440 * expression to execute.
1441 */
1442static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
1443 struct act_rule *rule, char **err)
1444
1445
1446{
1447 const char *cmd_name = args[*arg-1];
1448 char *error;
1449
1450 cmd_name += strlen("sc-set-gpt0");
1451 if (*cmd_name == '\0') {
1452 /* default stick table id. */
1453 rule->arg.gpt.sc = 0;
1454 } else {
1455 /* parse the stick table id. */
1456 if (*cmd_name != '(') {
1457 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1458 return ACT_RET_PRS_ERR;
1459 }
1460 cmd_name++; /* jump the '(' */
1461 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1462 if (*error != ')') {
1463 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1464 return ACT_RET_PRS_ERR;
1465 }
1466
1467 if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) {
1468 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
1469 args[*arg-1], ACT_ACTION_TRK_SCMAX-1);
1470 return ACT_RET_PRS_ERR;
1471 }
1472 }
1473
1474 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
1475 if (*error != '\0') {
1476 memprintf(err, "invalid integer value '%s'", args[*arg]);
1477 return ACT_RET_PRS_ERR;
1478 }
1479 (*arg)++;
1480
Thierry FOURNIER42148732015-09-02 17:17:33 +02001481 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001482 rule->action_ptr = action_set_gpt0;
1483
1484 return ACT_RET_PRS_OK;
1485}
1486
1487static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001488 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001489 { "sc-set-gpt0", parse_set_gpt0, 1 },
1490 { /* END */ }
1491}};
1492
1493static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001494 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001495 { "sc-set-gpt0", parse_set_gpt0, 1 },
1496 { /* END */ }
1497}};
1498
1499static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001500 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001501 { "sc-set-gpt0", parse_set_gpt0, 1 },
1502 { /* END */ }
1503}};
1504
1505static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001506 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001507 { "sc-set-gpt0", parse_set_gpt0, 1 },
1508 { /* END */ }
1509}};
1510
1511static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001512 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001513 { "sc-set-gpt0", parse_set_gpt0, 1 },
1514 { /* END */ }
1515}};
1516
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001517/* Note: must not be declared <const> as its list will be overwritten */
1518static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02001519 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
1520 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1521 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1522 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1523 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1524 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1525 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1526 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1527 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1528 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1529 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1530 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1531 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1532 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1533 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1534 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1535 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1536 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
1537 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001538 { /* END */ },
1539}};
1540
1541__attribute__((constructor))
1542static void __stick_table_init(void)
1543{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001544 /* register som action keywords. */
1545 tcp_req_conn_keywords_register(&tcp_conn_kws);
1546 tcp_req_cont_keywords_register(&tcp_req_kws);
1547 tcp_res_cont_keywords_register(&tcp_res_kws);
1548 http_req_keywords_register(&http_req_kws);
1549 http_res_keywords_register(&http_res_kws);
1550
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001551 /* register sample fetch and format conversion keywords */
1552 sample_register_convs(&sample_conv_kws);
1553}