blob: a5e7520bfa26ee64bd42907055ae3b769c63149e [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
2 * include/proto/stick_table.h
3 * Functions for stick tables management.
4 *
5 * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau08d5f982010-06-06 13:34:54 +02006 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation, version 2.1
11 * exclusively.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef _PROTO_STICK_TABLE_H
24#define _PROTO_STICK_TABLE_H
25
Willy Tarreau888617d2010-06-20 09:11:39 +020026#include <common/errors.h>
Willy Tarreauf6efda12010-08-03 20:34:06 +020027#include <common/ticks.h>
28#include <common/time.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010029#include <types/stick_table.h>
30
Willy Tarreau68129b92010-06-06 16:06:52 +020031#define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type))
32#define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type
33
Willy Tarreau07115412012-10-29 21:56:59 +010034extern struct stktable_key *static_table_key;
Willy Tarreau41883e22010-06-06 17:39:30 +020035
Emeric Brun3bd697e2010-01-04 15:23:48 +010036struct stksess *stksess_new(struct stktable *t, struct stktable_key *key);
Willy Tarreau393379c2010-06-06 12:11:37 +020037void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key);
Emeric Brun3bd697e2010-01-04 15:23:48 +010038void stksess_free(struct stktable *t, struct stksess *ts);
Willy Tarreauf6efda12010-08-03 20:34:06 +020039void stksess_kill(struct stktable *t, struct stksess *ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +010040
41int stktable_init(struct stktable *t);
42int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020043struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key);
Emeric Brun85e77c72010-09-23 18:16:52 +020044struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local);
45struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local);
Willy Tarreauf16d2b82010-06-06 15:38:59 +020046struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts);
47struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key);
Willy Tarreau1f7e9252010-06-20 12:27:21 +020048struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key);
Willy Tarreau8fed9032014-07-03 17:02:46 +020049struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t);
Emeric Brun485479d2010-09-23 18:02:19 +020050struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020051 struct session *l4, void *l7, unsigned int opt,
Willy Tarreaub5975de2014-06-25 16:20:53 +020052 struct sample_expr *expr, struct sample *smp);
Willy Tarreau12785782012-04-27 21:37:17 +020053int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type);
Willy Tarreauedee1d62014-07-15 16:44:27 +020054int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type);
Willy Tarreau08d5f982010-06-06 13:34:54 +020055int stktable_get_data_type(char *name);
Willy Tarreau4a0347a2010-06-18 17:26:50 +020056struct proxy *find_stktable(const char *name);
Willy Tarreau3a925c12013-09-04 17:54:01 +020057int stktable_trash_oldest(struct stktable *t, int to_batch);
Willy Tarreau08d5f982010-06-06 13:34:54 +020058
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020059/* return allocation size for standard data type <type> */
60static inline int stktable_type_size(int type)
61{
62 switch(type) {
63 case STD_T_SINT:
64 case STD_T_UINT:
65 return sizeof(int);
66 case STD_T_ULL:
67 return sizeof(unsigned long long);
68 case STD_T_FRQP:
69 return sizeof(struct freq_ctr_period);
70 }
71 return 0;
72}
73
Willy Tarreau888617d2010-06-20 09:11:39 +020074/* reserve some space for data type <type>, and associate argument at <sa> if
75 * not NULL. Returns PE_NONE (0) if OK or an error code among :
76 * - PE_ENUM_OOR if <type> does not exist
77 * - PE_EXIST if <type> is already registered
Willy Tarreauac782882010-06-20 10:41:54 +020078 * - PE_ARG_NOT_USE if <sa> was provided but not expected
79 * - PE_ARG_MISSING if <sa> was expected but not provided
Willy Tarreau08d5f982010-06-06 13:34:54 +020080 */
Willy Tarreau888617d2010-06-20 09:11:39 +020081static inline int stktable_alloc_data_type(struct stktable *t, int type, const char *sa)
Willy Tarreau08d5f982010-06-06 13:34:54 +020082{
83 if (type >= STKTABLE_DATA_TYPES)
Willy Tarreau888617d2010-06-20 09:11:39 +020084 return PE_ENUM_OOR;
Willy Tarreau08d5f982010-06-06 13:34:54 +020085
86 if (t->data_ofs[type])
87 /* already allocated */
Willy Tarreau888617d2010-06-20 09:11:39 +020088 return PE_EXIST;
Emeric Brun3bd697e2010-01-04 15:23:48 +010089
Willy Tarreauac782882010-06-20 10:41:54 +020090 switch (stktable_data_types[type].arg_type) {
91 case ARG_T_NONE:
92 if (sa)
93 return PE_ARG_NOT_USED;
94 break;
95 case ARG_T_INT:
96 if (!sa)
97 return PE_ARG_MISSING;
98 t->data_arg[type].i = atoi(sa);
99 break;
100 case ARG_T_DELAY:
101 if (!sa)
102 return PE_ARG_MISSING;
103 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
104 if (sa)
105 return PE_ARG_INVC; /* invalid char */
106 break;
107 }
108
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200109 t->data_size += stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau08d5f982010-06-06 13:34:54 +0200110 t->data_ofs[type] = -t->data_size;
Willy Tarreau888617d2010-06-20 09:11:39 +0200111 return PE_NONE;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200112}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100113
Willy Tarreau68129b92010-06-06 16:06:52 +0200114/* return pointer for data type <type> in sticky session <ts> of table <t>, or
115 * NULL if either <ts> is NULL or the type is not stored.
116 */
117static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type)
118{
119 if (type >= STKTABLE_DATA_TYPES)
120 return NULL;
121
122 if (!t->data_ofs[type]) /* type not stored */
123 return NULL;
124
125 if (!ts)
126 return NULL;
127
128 return (void *)ts + t->data_ofs[type];
129}
130
Willy Tarreauf6efda12010-08-03 20:34:06 +0200131/* kill an entry if it's expired and its ref_cnt is zero */
132static inline void stksess_kill_if_expired(struct stktable *t, struct stksess *ts)
133{
Emeric Brunc89a5722010-09-23 18:11:05 +0200134 if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms))
Willy Tarreauf6efda12010-08-03 20:34:06 +0200135 stksess_kill(t, ts);
136}
137
Emeric Brun3bd697e2010-01-04 15:23:48 +0100138#endif /* _PROTO_STICK_TABLE_H */