blob: 7f3a5a75266c1aa031398bccad22d2bb5405d47b [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 Brunaaf58602015-06-15 17:23:30 +0200258 /* If this entry was already pushed to a peer
259 We want to push it again */
260 if ((int)(ts->upd.key - t->commitupdate) <= 0) {
261 ts->upd.key = ++t->update;
262 t->localupdate = t->update;
263 eb32_delete(&ts->upd);
264 eb = eb32_insert(&t->updates, &ts->upd);
265 if (eb != &ts->upd) {
266 eb32_delete(eb);
267 eb32_insert(&t->updates, &ts->upd);
268 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200269 }
270 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
271 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200272 return ts;
273}
274
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200275/* Insert new sticky session <ts> in the table. It is assumed that it does not
276 * yet exist (the caller must check this). The table's timeout is updated if it
277 * is set. <ts> is returned.
278 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200279struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200280{
281 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200282 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200283 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200284 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200285 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100286}
287
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200288/* Returns a valid or initialized stksess for the specified stktable_key in the
289 * specified table, or NULL if the key was NULL, or if no entry was found nor
290 * could be created. The entry's expiration is updated.
291 */
292struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
293{
294 struct stksess *ts;
295
296 if (!key)
297 return NULL;
298
299 ts = stktable_lookup_key(table, key);
300 if (ts == NULL) {
301 /* entry does not exist, initialize a new one */
302 ts = stksess_new(table, key);
303 if (!ts)
304 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200305 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200306 }
307 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200308 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200309 return ts;
310}
311
Emeric Brun3bd697e2010-01-04 15:23:48 +0100312/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200313 * Trash expired sticky sessions from table <t>. The next expiration date is
314 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100315 */
316static int stktable_trash_expired(struct stktable *t)
317{
318 struct stksess *ts;
319 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200320 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100321
322 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
323
324 while (1) {
325 if (unlikely(!eb)) {
326 /* we might have reached the end of the tree, typically because
327 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200328 * half. Let's loop back to the beginning of the tree now if we
329 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100330 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200331 if (looped)
332 break;
333 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100334 eb = eb32_first(&t->exps);
335 if (likely(!eb))
336 break;
337 }
338
339 if (likely(tick_is_lt(now_ms, eb->key))) {
340 /* timer not expired yet, revisit it later */
341 t->exp_next = eb->key;
342 return t->exp_next;
343 }
344
345 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200346 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100347 eb = eb32_next(eb);
348
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200349 /* don't delete an entry which is currently referenced */
350 if (ts->ref_cnt)
351 continue;
352
Willy Tarreau86257dc2010-06-06 12:57:10 +0200353 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100354
355 if (!tick_is_expired(ts->expire, now_ms)) {
356 if (!tick_isset(ts->expire))
357 continue;
358
Willy Tarreau86257dc2010-06-06 12:57:10 +0200359 ts->exp.key = ts->expire;
360 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100361
Willy Tarreau86257dc2010-06-06 12:57:10 +0200362 if (!eb || eb->key > ts->exp.key)
363 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100364 continue;
365 }
366
367 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200368 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200369 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100370 stksess_free(t, ts);
371 }
372
373 /* We have found no task to expire in any tree */
374 t->exp_next = TICK_ETERNITY;
375 return t->exp_next;
376}
377
378/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200379 * Task processing function to trash expired sticky sessions. A pointer to the
380 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100381 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200382static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383{
384 struct stktable *t = (struct stktable *)task->context;
385
386 task->expire = stktable_trash_expired(t);
387 return task;
388}
389
Willy Tarreauaea940e2010-06-06 11:56:36 +0200390/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100391int stktable_init(struct stktable *t)
392{
393 if (t->size) {
394 memset(&t->keys, 0, sizeof(t->keys));
395 memset(&t->exps, 0, sizeof(t->exps));
396
Willy Tarreau393379c2010-06-06 12:11:37 +0200397 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100398
399 t->exp_next = TICK_ETERNITY;
400 if ( t->expire ) {
401 t->exp_task = task_new();
402 t->exp_task->process = process_table_expire;
403 t->exp_task->expire = TICK_ETERNITY;
404 t->exp_task->context = (void *)t;
405 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200406 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200407 peers_register_table(t->peers.p, t);
408 }
409
Emeric Brun3bd697e2010-01-04 15:23:48 +0100410 return t->pool != NULL;
411 }
412 return 1;
413}
414
415/*
416 * Configuration keywords of known table types
417 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200418struct stktable_type stktable_types[SMP_TYPES] = {
419 [SMP_T_SINT] = { "integer", 0, 4 },
420 [SMP_T_IPV4] = { "ip", 0, 4 },
421 [SMP_T_IPV6] = { "ipv6", 0, 16 },
422 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
423 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
424};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100425
426/*
427 * Parse table type configuration.
428 * Returns 0 on successful parsing, else 1.
429 * <myidx> is set at next configuration <args> index.
430 */
431int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
432{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200433 for (*type = 0; *type < SMP_TYPES; (*type)++) {
434 if (!stktable_types[*type].kw)
435 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100436 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
437 continue;
438
439 *key_size = stktable_types[*type].default_size;
440 (*myidx)++;
441
Willy Tarreauaea940e2010-06-06 11:56:36 +0200442 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100443 if (strcmp("len", args[*myidx]) == 0) {
444 (*myidx)++;
445 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200446 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100447 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200448 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200449 /* null terminated string needs +1 for '\0'. */
450 (*key_size)++;
451 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100452 (*myidx)++;
453 }
454 }
455 return 0;
456 }
457 return 1;
458}
459
Willy Tarreau8fed9032014-07-03 17:02:46 +0200460/* Prepares a stktable_key from a sample <smp> to search into table <t>.
461 * Returns NULL if the sample could not be converted (eg: no matching type),
462 * otherwise a pointer to the static stktable_key filled with what is needed
463 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200464 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200465struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200466{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200467 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200468 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200469 return NULL;
470
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200471 /* Fill static_table_key. */
472 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200473
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200474 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200475 static_table_key->key = &smp->data.u.ipv4;
476 static_table_key->key_len = 4;
477 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200478
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200479 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200480 static_table_key->key = &smp->data.u.ipv6;
481 static_table_key->key_len = 16;
482 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200483
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200484 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200485 /* The stick table require a 32bit unsigned int, "sint" is a
486 * signed 64 it, so we can convert it inplace.
487 */
488 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
489 static_table_key->key = &smp->data.u.sint;
490 static_table_key->key_len = 4;
491 break;
492
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200493 case SMP_T_STR:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200494 /* Must be NULL terminated. */
495 if (smp->data.u.str.len >= smp->data.u.str.size ||
496 smp->data.u.str.str[smp->data.u.str.len] != '\0') {
497 if (!smp_dup(smp))
498 return NULL;
499 if (smp->data.u.str.len >= smp->data.u.str.size)
500 return NULL;
501 smp->data.u.str.str[smp->data.u.str.len] = '\0';
Emeric Brun485479d2010-09-23 18:02:19 +0200502 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200503 static_table_key->key = smp->data.u.str.str;
504 static_table_key->key_len = smp->data.u.str.len;
505 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200506
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200507 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200508 if (smp->data.u.str.len < t->key_size) {
509 /* This type needs padding with 0. */
510 if (smp->data.u.str.size < t->key_size)
511 if (!smp_dup(smp))
512 return NULL;
513 if (smp->data.u.str.size < t->key_size)
514 return NULL;
515 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
516 t->key_size - smp->data.u.str.len);
517 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200518 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200519 static_table_key->key = smp->data.u.str.str;
520 static_table_key->key_len = smp->data.u.str.len;
521 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200522
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200523 default: /* impossible case. */
524 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200525 }
526
Willy Tarreau07115412012-10-29 21:56:59 +0100527 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200528}
529
530/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200531 * Process a fetch + format conversion as defined by the sample expression <expr>
532 * on request or response considering the <opt> parameter. Returns either NULL if
533 * no key could be extracted, or a pointer to the converted result stored in
534 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
535 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200536 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
537 * without SMP_OPT_FINAL). The output will be usable like this :
538 *
539 * return MAY_CHANGE FINAL Meaning for the sample
540 * NULL 0 * Not present and will never be (eg: header)
541 * NULL 1 0 Not present or unstable, could change (eg: req_len)
542 * NULL 1 1 Not present, will not change anymore
543 * smp 0 * Present and will not change (eg: header)
544 * smp 1 0 not possible
545 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200546 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200547struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200548 unsigned int opt, struct sample_expr *expr, struct sample *smp)
549{
550 if (smp)
551 memset(smp, 0, sizeof(*smp));
552
Willy Tarreau192252e2015-04-04 01:47:55 +0200553 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200554 if (!smp)
555 return NULL;
556
557 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
558 return NULL; /* we can only use stable samples */
559
560 return smp_to_stkey(smp, t);
561}
562
563/*
Willy Tarreau12785782012-04-27 21:37:17 +0200564 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200565 * type <table_type>, otherwise zero. Used in configuration check.
566 */
Willy Tarreau12785782012-04-27 21:37:17 +0200567int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200568{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100569 int out_type;
570
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200571 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200572 return 0;
573
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100574 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200575
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200576 /* Convert sample. */
577 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100578 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200579
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200580 return 1;
581}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100582
Willy Tarreauedee1d62014-07-15 16:44:27 +0200583/* Extra data types processing : after the last one, some room may remain
584 * before STKTABLE_DATA_TYPES that may be used to register extra data types
585 * at run time.
586 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200587struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200588 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200589 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200590 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200591 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200592 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
593 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
594 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
595 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
596 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
597 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
598 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
599 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
600 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
601 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
602 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
603 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
604 [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 +0200605};
606
Willy Tarreauedee1d62014-07-15 16:44:27 +0200607/* Registers stick-table extra data type with index <idx>, name <name>, type
608 * <std_type> and arg type <arg_type>. If the index is negative, the next free
609 * index is automatically allocated. The allocated index is returned, or -1 if
610 * no free index was found or <name> was already registered. The <name> is used
611 * directly as a pointer, so if it's not stable, the caller must allocate it.
612 */
613int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
614{
615 if (idx < 0) {
616 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
617 if (!stktable_data_types[idx].name)
618 break;
619
620 if (strcmp(stktable_data_types[idx].name, name) == 0)
621 return -1;
622 }
623 }
624
625 if (idx >= STKTABLE_DATA_TYPES)
626 return -1;
627
628 if (stktable_data_types[idx].name != NULL)
629 return -1;
630
631 stktable_data_types[idx].name = name;
632 stktable_data_types[idx].std_type = std_type;
633 stktable_data_types[idx].arg_type = arg_type;
634 return idx;
635}
636
Willy Tarreau08d5f982010-06-06 13:34:54 +0200637/*
638 * Returns the data type number for the stktable_data_type whose name is <name>,
639 * or <0 if not found.
640 */
641int stktable_get_data_type(char *name)
642{
643 int type;
644
645 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200646 if (!stktable_data_types[type].name)
647 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200648 if (strcmp(name, stktable_data_types[type].name) == 0)
649 return type;
650 }
651 return -1;
652}
653
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200654/* Casts sample <smp> to the type of the table specified in arg(0), and looks
655 * it up into this table. Returns true if found, false otherwise. The input
656 * type is STR so that input samples are converted to string (since all types
657 * can be converted to strings), then the function casts the string again into
658 * the table's type. This is a double conversion, but in the future we might
659 * support automatic input types to perform the cast on the fly.
660 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200661static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200662{
663 struct stktable *t;
664 struct stktable_key *key;
665 struct stksess *ts;
666
667 t = &arg_p[0].data.prx->table;
668
669 key = smp_to_stkey(smp, t);
670 if (!key)
671 return 0;
672
673 ts = stktable_lookup_key(t, key);
674
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200675 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200676 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200677 smp->flags = SMP_F_VOL_TEST;
678 return 1;
679}
680
681/* Casts sample <smp> to the type of the table specified in arg(0), and looks
682 * it up into this table. Returns the data rate received from clients in bytes/s
683 * if the key is present in the table, otherwise zero, so that comparisons can
684 * be easily performed. If the inspected parameter is not stored in the table,
685 * <not found> is returned.
686 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200687static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200688{
689 struct stktable *t;
690 struct stktable_key *key;
691 struct stksess *ts;
692 void *ptr;
693
694 t = &arg_p[0].data.prx->table;
695
696 key = smp_to_stkey(smp, t);
697 if (!key)
698 return 0;
699
700 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
704 ts = stktable_lookup_key(t, key);
705 if (!ts) /* key not present */
706 return 1;
707
708 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
709 if (!ptr)
710 return 0; /* parameter not stored */
711
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200712 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200713 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
714 return 1;
715}
716
717/* Casts sample <smp> to the type of the table specified in arg(0), and looks
718 * it up into this table. Returns the cumulated number of connections for the key
719 * if the key is present in the table, otherwise zero, so that comparisons can
720 * be easily performed. If the inspected parameter is not stored in the table,
721 * <not found> is returned.
722 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200723static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200724{
725 struct stktable *t;
726 struct stktable_key *key;
727 struct stksess *ts;
728 void *ptr;
729
730 t = &arg_p[0].data.prx->table;
731
732 key = smp_to_stkey(smp, t);
733 if (!key)
734 return 0;
735
736 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
740 ts = stktable_lookup_key(t, key);
741 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
771 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200772 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200773 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200774
775 ts = stktable_lookup_key(t, key);
776 if (!ts) /* key not present */
777 return 1;
778
779 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
780 if (!ptr)
781 return 0; /* parameter not stored */
782
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200783 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200784 return 1;
785}
786
787/* Casts sample <smp> to the type of the table specified in arg(0), and looks
788 * it up into this table. Returns the rate of incoming connections from the key
789 * if the key is present in the table, otherwise zero, so that comparisons can
790 * be easily performed. If the inspected parameter is not stored in the table,
791 * <not found> is returned.
792 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200793static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200794{
795 struct stktable *t;
796 struct stktable_key *key;
797 struct stksess *ts;
798 void *ptr;
799
800 t = &arg_p[0].data.prx->table;
801
802 key = smp_to_stkey(smp, t);
803 if (!key)
804 return 0;
805
806 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200807 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200808 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200809
810 ts = stktable_lookup_key(t, key);
811 if (!ts) /* key not present */
812 return 1;
813
814 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
815 if (!ptr)
816 return 0; /* parameter not stored */
817
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200818 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200819 t->data_arg[STKTABLE_DT_CONN_RATE].u);
820 return 1;
821}
822
823/* Casts sample <smp> to the type of the table specified in arg(0), and looks
824 * it up into this table. Returns the data rate sent to clients in bytes/s
825 * if the key is present in the table, otherwise zero, so that comparisons can
826 * be easily performed. If the inspected parameter is not stored in the table,
827 * <not found> is returned.
828 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200829static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200830{
831 struct stktable *t;
832 struct stktable_key *key;
833 struct stksess *ts;
834 void *ptr;
835
836 t = &arg_p[0].data.prx->table;
837
838 key = smp_to_stkey(smp, t);
839 if (!key)
840 return 0;
841
842 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200843 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200844 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200845
846 ts = stktable_lookup_key(t, key);
847 if (!ts) /* key not present */
848 return 1;
849
850 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
851 if (!ptr)
852 return 0; /* parameter not stored */
853
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200854 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200855 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
856 return 1;
857}
858
859/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200860 * it up into this table. Returns the value of the GPT0 tag for the key
861 * if the key is present in the table, otherwise false, so that comparisons can
862 * be easily performed. If the inspected parameter is not stored in the table,
863 * <not found> is returned.
864 */
865static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
866{
867 struct stktable *t;
868 struct stktable_key *key;
869 struct stksess *ts;
870 void *ptr;
871
872 t = &arg_p[0].data.prx->table;
873
874 key = smp_to_stkey(smp, t);
875 if (!key)
876 return 0;
877
878 smp->flags = SMP_F_VOL_TEST;
879 smp->data.type = SMP_T_SINT;
880 smp->data.u.sint = 0;
881
882 ts = stktable_lookup_key(t, key);
883 if (!ts) /* key not present */
884 return 1;
885
886 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
887 if (!ptr)
888 return 0; /* parameter not stored */
889
890 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
891 return 1;
892}
893
894/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200895 * it up into this table. Returns the value of the GPC0 counter for the key
896 * if the key is present in the table, otherwise zero, so that comparisons can
897 * be easily performed. If the inspected parameter is not stored in the table,
898 * <not found> is returned.
899 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200900static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200901{
902 struct stktable *t;
903 struct stktable_key *key;
904 struct stksess *ts;
905 void *ptr;
906
907 t = &arg_p[0].data.prx->table;
908
909 key = smp_to_stkey(smp, t);
910 if (!key)
911 return 0;
912
913 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200914 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200915 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200916
917 ts = stktable_lookup_key(t, key);
918 if (!ts) /* key not present */
919 return 1;
920
921 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
922 if (!ptr)
923 return 0; /* parameter not stored */
924
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200925 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200926 return 1;
927}
928
929/* Casts sample <smp> to the type of the table specified in arg(0), and looks
930 * it up into this table. Returns the event rate of the GPC0 counter for the key
931 * if the key is present in the table, otherwise zero, so that comparisons can
932 * be easily performed. If the inspected parameter is not stored in the table,
933 * <not found> is returned.
934 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200935static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200936{
937 struct stktable *t;
938 struct stktable_key *key;
939 struct stksess *ts;
940 void *ptr;
941
942 t = &arg_p[0].data.prx->table;
943
944 key = smp_to_stkey(smp, t);
945 if (!key)
946 return 0;
947
948 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200949 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200950 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200951
952 ts = stktable_lookup_key(t, key);
953 if (!ts) /* key not present */
954 return 1;
955
956 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
957 if (!ptr)
958 return 0; /* parameter not stored */
959
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200960 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200961 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
962 return 1;
963}
964
965/* Casts sample <smp> to the type of the table specified in arg(0), and looks
966 * it up into this table. Returns the cumulated number of HTTP request errors
967 * for the key if the key is present in the table, otherwise zero, so that
968 * comparisons can be easily performed. If the inspected parameter is not stored
969 * in the table, <not found> is returned.
970 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200971static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200972{
973 struct stktable *t;
974 struct stktable_key *key;
975 struct stksess *ts;
976 void *ptr;
977
978 t = &arg_p[0].data.prx->table;
979
980 key = smp_to_stkey(smp, t);
981 if (!key)
982 return 0;
983
984 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200985 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200986 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200987
988 ts = stktable_lookup_key(t, key);
989 if (!ts) /* key not present */
990 return 1;
991
992 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
993 if (!ptr)
994 return 0; /* parameter not stored */
995
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200996 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200997 return 1;
998}
999
1000/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1001 * it up into this table. Returns the HTTP request error rate the key
1002 * if the key is present in the table, otherwise zero, so that comparisons can
1003 * be easily performed. If the inspected parameter is not stored in the table,
1004 * <not found> is returned.
1005 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001006static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001007{
1008 struct stktable *t;
1009 struct stktable_key *key;
1010 struct stksess *ts;
1011 void *ptr;
1012
1013 t = &arg_p[0].data.prx->table;
1014
1015 key = smp_to_stkey(smp, t);
1016 if (!key)
1017 return 0;
1018
1019 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001020 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001021 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001022
1023 ts = stktable_lookup_key(t, key);
1024 if (!ts) /* key not present */
1025 return 1;
1026
1027 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1028 if (!ptr)
1029 return 0; /* parameter not stored */
1030
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001031 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001032 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1033 return 1;
1034}
1035
1036/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1037 * it up into this table. Returns the cumulated number of HTTP request for the
1038 * key if the key is present in the table, otherwise zero, so that comparisons
1039 * can be easily performed. If the inspected parameter is not stored in the
1040 * table, <not found> is returned.
1041 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001042static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001043{
1044 struct stktable *t;
1045 struct stktable_key *key;
1046 struct stksess *ts;
1047 void *ptr;
1048
1049 t = &arg_p[0].data.prx->table;
1050
1051 key = smp_to_stkey(smp, t);
1052 if (!key)
1053 return 0;
1054
1055 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001056 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001057 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001058
1059 ts = stktable_lookup_key(t, key);
1060 if (!ts) /* key not present */
1061 return 1;
1062
1063 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1064 if (!ptr)
1065 return 0; /* parameter not stored */
1066
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001067 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001068 return 1;
1069}
1070
1071/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1072 * it up into this table. Returns the HTTP request rate the key if the key is
1073 * present in the table, otherwise zero, so that comparisons can be easily
1074 * performed. If the inspected parameter is not stored in the table, <not found>
1075 * is returned.
1076 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001077static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001078{
1079 struct stktable *t;
1080 struct stktable_key *key;
1081 struct stksess *ts;
1082 void *ptr;
1083
1084 t = &arg_p[0].data.prx->table;
1085
1086 key = smp_to_stkey(smp, t);
1087 if (!key)
1088 return 0;
1089
1090 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001091 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001092 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001093
1094 ts = stktable_lookup_key(t, key);
1095 if (!ts) /* key not present */
1096 return 1;
1097
1098 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1099 if (!ptr)
1100 return 0; /* parameter not stored */
1101
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001102 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001103 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1104 return 1;
1105}
1106
1107/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1108 * it up into this table. Returns the volume of datareceived from clients in kbytes
1109 * if the key is present in the table, otherwise zero, so that comparisons can
1110 * be easily performed. If the inspected parameter is not stored in the table,
1111 * <not found> is returned.
1112 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001113static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001114{
1115 struct stktable *t;
1116 struct stktable_key *key;
1117 struct stksess *ts;
1118 void *ptr;
1119
1120 t = &arg_p[0].data.prx->table;
1121
1122 key = smp_to_stkey(smp, t);
1123 if (!key)
1124 return 0;
1125
1126 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001127 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001128 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001129
1130 ts = stktable_lookup_key(t, key);
1131 if (!ts) /* key not present */
1132 return 1;
1133
1134 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1135 if (!ptr)
1136 return 0; /* parameter not stored */
1137
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001138 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001139 return 1;
1140}
1141
1142/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1143 * it up into this table. Returns the volume of data sent to clients in kbytes
1144 * if the key is present in the table, otherwise zero, so that comparisons can
1145 * be easily performed. If the inspected parameter is not stored in the table,
1146 * <not found> is returned.
1147 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001148static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001149{
1150 struct stktable *t;
1151 struct stktable_key *key;
1152 struct stksess *ts;
1153 void *ptr;
1154
1155 t = &arg_p[0].data.prx->table;
1156
1157 key = smp_to_stkey(smp, t);
1158 if (!key)
1159 return 0;
1160
1161 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001162 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001163 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001164
1165 ts = stktable_lookup_key(t, key);
1166 if (!ts) /* key not present */
1167 return 1;
1168
1169 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1170 if (!ptr)
1171 return 0; /* parameter not stored */
1172
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001173 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001174 return 1;
1175}
1176
1177/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1178 * it up into this table. Returns the server ID associated with the key if the
1179 * key is present in the table, otherwise zero, so that comparisons can be
1180 * easily performed. If the inspected parameter is not stored in the table,
1181 * <not found> is returned.
1182 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001183static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001184{
1185 struct stktable *t;
1186 struct stktable_key *key;
1187 struct stksess *ts;
1188 void *ptr;
1189
1190 t = &arg_p[0].data.prx->table;
1191
1192 key = smp_to_stkey(smp, t);
1193 if (!key)
1194 return 0;
1195
1196 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001197 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001198 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001199
1200 ts = stktable_lookup_key(t, key);
1201 if (!ts) /* key not present */
1202 return 1;
1203
1204 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1205 if (!ptr)
1206 return 0; /* parameter not stored */
1207
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001208 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001209 return 1;
1210}
1211
1212/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1213 * it up into this table. Returns the cumulated number of sessions for the
1214 * key if the key is present in the table, otherwise zero, so that comparisons
1215 * can be easily performed. If the inspected parameter is not stored in the
1216 * table, <not found> is returned.
1217 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001218static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001219{
1220 struct stktable *t;
1221 struct stktable_key *key;
1222 struct stksess *ts;
1223 void *ptr;
1224
1225 t = &arg_p[0].data.prx->table;
1226
1227 key = smp_to_stkey(smp, t);
1228 if (!key)
1229 return 0;
1230
1231 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001232 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001233 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001234
1235 ts = stktable_lookup_key(t, key);
1236 if (!ts) /* key not present */
1237 return 1;
1238
1239 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1240 if (!ptr)
1241 return 0; /* parameter not stored */
1242
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001243 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001244 return 1;
1245}
1246
1247/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1248 * it up into this table. Returns the session rate the key if the key is
1249 * present in the table, otherwise zero, so that comparisons can be easily
1250 * performed. If the inspected parameter is not stored in the table, <not found>
1251 * is returned.
1252 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001253static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001254{
1255 struct stktable *t;
1256 struct stktable_key *key;
1257 struct stksess *ts;
1258 void *ptr;
1259
1260 t = &arg_p[0].data.prx->table;
1261
1262 key = smp_to_stkey(smp, t);
1263 if (!key)
1264 return 0;
1265
1266 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001267 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001268 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001269
1270 ts = stktable_lookup_key(t, key);
1271 if (!ts) /* key not present */
1272 return 1;
1273
1274 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1275 if (!ptr)
1276 return 0; /* parameter not stored */
1277
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001278 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001279 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1280 return 1;
1281}
1282
1283/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1284 * it up into this table. Returns the amount of concurrent connections tracking
1285 * the same key if the key is present in the table, otherwise zero, so that
1286 * comparisons can be easily performed. If the inspected parameter is not
1287 * stored in the table, <not found> is returned.
1288 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001289static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001290{
1291 struct stktable *t;
1292 struct stktable_key *key;
1293 struct stksess *ts;
1294
1295 t = &arg_p[0].data.prx->table;
1296
1297 key = smp_to_stkey(smp, t);
1298 if (!key)
1299 return 0;
1300
1301 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001302 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001303 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001304
1305 ts = stktable_lookup_key(t, key);
1306 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001307 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001308
1309 return 1;
1310}
1311
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001312/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001313static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
1314 struct session *sess, struct stream *s)
1315{
1316 void *ptr;
1317 struct stksess *ts;
1318 struct stkctr *stkctr;
1319
1320 /* Extract the stksess, return OK if no stksess available. */
1321 if (s)
1322 stkctr = &s->stkctr[rule->arg.gpc.sc];
1323 else
1324 stkctr = &sess->stkctr[rule->arg.gpc.sc];
1325 ts = stkctr_entry(stkctr);
1326 if (!ts)
1327 return ACT_RET_CONT;
1328
1329 /* Store the sample in the required sc, and ignore errors. */
1330 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1331 if (!ptr)
1332 return ACT_RET_CONT;
1333
1334 stktable_data_cast(ptr, gpc0)++;
1335 return ACT_RET_CONT;
1336}
1337
1338/* This function is a common parser for using variables. It understands
1339 * the formats:
1340 *
1341 * sc-inc-gpc0(<stick-table ID>)
1342 *
1343 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1344 * it returns 1 and the variable <expr> is filled with the pointer to the
1345 * expression to execute.
1346 */
1347static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1348 struct act_rule *rule, char **err)
1349{
1350 const char *cmd_name = args[*arg-1];
1351 char *error;
1352
1353 cmd_name += strlen("sc-inc-gpc0");
1354 if (*cmd_name == '\0') {
1355 /* default stick table id. */
1356 rule->arg.gpc.sc = 0;
1357 } else {
1358 /* parse the stick table id. */
1359 if (*cmd_name != '(') {
1360 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1361 return ACT_RET_PRS_ERR;
1362 }
1363 cmd_name++; /* jump the '(' */
1364 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1365 if (*error != ')') {
1366 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1367 return ACT_RET_PRS_ERR;
1368 }
1369
1370 if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) {
1371 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
1372 ACT_ACTION_TRK_SCMAX-1);
1373 return ACT_RET_PRS_ERR;
1374 }
1375 }
1376 rule->action = ACT_ACTION_CONT;
1377 rule->action_ptr = action_inc_gpc0;
1378 return ACT_RET_PRS_OK;
1379}
1380
1381/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001382static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
1383 struct session *sess, struct stream *s)
1384{
1385 void *ptr;
1386 struct stksess *ts;
1387 struct stkctr *stkctr;
1388
1389 /* Extract the stksess, return OK if no stksess available. */
1390 if (s)
1391 stkctr = &s->stkctr[rule->arg.gpt.sc];
1392 else
1393 stkctr = &sess->stkctr[rule->arg.gpt.sc];
1394 ts = stkctr_entry(stkctr);
1395 if (!ts)
1396 return ACT_RET_CONT;
1397
1398 /* Store the sample in the required sc, and ignore errors. */
1399 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
1400 if (ptr)
1401 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
1402 return ACT_RET_CONT;
1403}
1404
1405/* This function is a common parser for using variables. It understands
1406 * the format:
1407 *
1408 * set-gpt0(<stick-table ID>) <expression>
1409 *
1410 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1411 * it returns 1 and the variable <expr> is filled with the pointer to the
1412 * expression to execute.
1413 */
1414static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
1415 struct act_rule *rule, char **err)
1416
1417
1418{
1419 const char *cmd_name = args[*arg-1];
1420 char *error;
1421
1422 cmd_name += strlen("sc-set-gpt0");
1423 if (*cmd_name == '\0') {
1424 /* default stick table id. */
1425 rule->arg.gpt.sc = 0;
1426 } else {
1427 /* parse the stick table id. */
1428 if (*cmd_name != '(') {
1429 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1430 return ACT_RET_PRS_ERR;
1431 }
1432 cmd_name++; /* jump the '(' */
1433 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1434 if (*error != ')') {
1435 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1436 return ACT_RET_PRS_ERR;
1437 }
1438
1439 if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) {
1440 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
1441 args[*arg-1], ACT_ACTION_TRK_SCMAX-1);
1442 return ACT_RET_PRS_ERR;
1443 }
1444 }
1445
1446 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
1447 if (*error != '\0') {
1448 memprintf(err, "invalid integer value '%s'", args[*arg]);
1449 return ACT_RET_PRS_ERR;
1450 }
1451 (*arg)++;
1452
1453 rule->action = ACT_ACTION_CONT;
1454 rule->action_ptr = action_set_gpt0;
1455
1456 return ACT_RET_PRS_OK;
1457}
1458
1459static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001460 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001461 { "sc-set-gpt0", parse_set_gpt0, 1 },
1462 { /* END */ }
1463}};
1464
1465static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001466 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001467 { "sc-set-gpt0", parse_set_gpt0, 1 },
1468 { /* END */ }
1469}};
1470
1471static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001472 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001473 { "sc-set-gpt0", parse_set_gpt0, 1 },
1474 { /* END */ }
1475}};
1476
1477static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001478 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001479 { "sc-set-gpt0", parse_set_gpt0, 1 },
1480 { /* END */ }
1481}};
1482
1483static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001484 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001485 { "sc-set-gpt0", parse_set_gpt0, 1 },
1486 { /* END */ }
1487}};
1488
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001489/* Note: must not be declared <const> as its list will be overwritten */
1490static struct sample_conv_kw_list sample_conv_kws = {ILH, {
1491 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_BOOL },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001492 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1493 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1494 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1495 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1496 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001497 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001498 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1499 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1500 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1501 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1502 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1503 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1504 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1505 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1506 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1507 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1508 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1509 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001510 { /* END */ },
1511}};
1512
1513__attribute__((constructor))
1514static void __stick_table_init(void)
1515{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001516 /* register som action keywords. */
1517 tcp_req_conn_keywords_register(&tcp_conn_kws);
1518 tcp_req_cont_keywords_register(&tcp_req_kws);
1519 tcp_res_cont_keywords_register(&tcp_res_kws);
1520 http_req_keywords_register(&http_req_kws);
1521 http_res_keywords_register(&http_res_kws);
1522
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001523 /* register sample fetch and format conversion keywords */
1524 sample_register_convs(&sample_conv_kws);
1525}