blob: 2f211978d4929f2e442f48d6367a457cc704d5c7 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * include/types/sample.h
3 * Macros, variables and structures for sample management.
Emeric Brun107ca302010-01-04 16:16:05 +01004 *
5 * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau80aca902013-01-07 15:42:20 +01006 * Copyright (C) 2012-2013 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +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
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023#ifndef _TYPES_SAMPLE_H
24#define _TYPES_SAMPLE_H
Emeric Brun107ca302010-01-04 16:16:05 +010025
Emeric Brun107ca302010-01-04 16:16:05 +010026#include <sys/socket.h>
27#include <netinet/in.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028
29#include <common/chunk.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020030#include <common/mini-clist.h>
Willy Tarreauecfb8e82012-04-20 12:29:52 +020031#include <types/arg.h>
Emeric Brun107ca302010-01-04 16:16:05 +010032
Willy Tarreau422aa072012-04-20 20:49:27 +020033/* input and output sample types */
Emeric Brun107ca302010-01-04 16:16:05 +010034enum {
Willy Tarreau422aa072012-04-20 20:49:27 +020035 SMP_T_BOOL = 0, /* boolean */
36 SMP_T_UINT, /* unsigned 32bits integer type */
37 SMP_T_SINT, /* signed 32bits integer type */
38 SMP_T_IPV4, /* ipv4 type */
39 SMP_T_IPV6, /* ipv6 type */
40 SMP_T_STR, /* char string type */
41 SMP_T_BIN, /* buffer type */
42 SMP_T_CSTR, /* constant char string type, data need dup before conversion */
43 SMP_T_CBIN, /* constant buffer type, data need dup before conversion */
44 SMP_TYPES /* number of types, must always be last */
Emeric Brun107ca302010-01-04 16:16:05 +010045};
46
Willy Tarreau80aca902013-01-07 15:42:20 +010047/* Sample sources are used to establish a relation between fetch keywords and
48 * the location where they're about to be used. They're reserved for internal
49 * use and are not meant to be known outside the sample management code.
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020050 */
51enum {
Willy Tarreau80aca902013-01-07 15:42:20 +010052 SMP_SRC_INTRN, /* internal context-less information */
53 SMP_SRC_LISTN, /* listener which accepted the connection */
54 SMP_SRC_FTEND, /* frontend which accepted the connection */
55 SMP_SRC_L4CLI, /* L4 information about the client */
56 SMP_SRC_L5CLI, /* fetch uses client information from embryonic session */
57 SMP_SRC_TRACK, /* fetch involves track counters */
58 SMP_SRC_L6REQ, /* fetch uses raw information from the request buffer */
59 SMP_SRC_HRQHV, /* fetch uses volatile information about HTTP request headers (eg: value) */
60 SMP_SRC_HRQHP, /* fetch uses persistent information about HTTP request headers (eg: meth) */
61 SMP_SRC_HRQBO, /* fetch uses information about HTTP request body */
62 SMP_SRC_BKEND, /* fetch uses information about the backend */
63 SMP_SRC_SERVR, /* fetch uses information about the selected server */
64 SMP_SRC_L4SRV, /* fetch uses information about the server L4 connection */
65 SMP_SRC_L5SRV, /* fetch uses information about the server L5 connection */
66 SMP_SRC_L6RES, /* fetch uses raw information from the response buffer */
67 SMP_SRC_HRSHV, /* fetch uses volatile information about HTTP response headers (eg: value) */
68 SMP_SRC_HRSHP, /* fetch uses persistent information about HTTP response headers (eg: status) */
69 SMP_SRC_HRSBO, /* fetch uses information about HTTP response body */
70 SMP_SRC_RQFIN, /* final information about request buffer (eg: tot bytes) */
71 SMP_SRC_RSFIN, /* final information about response buffer (eg: tot bytes) */
72 SMP_SRC_TXFIN, /* final information about the transaction (eg: #comp rate) */
73 SMP_SRC_SSFIN, /* final information about the session (eg: #requests, final flags) */
74 SMP_SRC_ENTRIES /* nothing after this */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020075};
76
Willy Tarreaubf8e2512013-03-25 14:52:41 +010077/* Sample checkpoints are a list of places where samples may be used. This is
78 * an internal enum used only to build SMP_VAL_*.
79 */
80enum {
81 SMP_CKP_FE_CON_ACC, /* FE connection accept rules ("tcp request connection") */
82 SMP_CKP_FE_SES_ACC, /* FE session accept rules (to come soon) */
83 SMP_CKP_FE_REQ_CNT, /* FE request content rules ("tcp request content") */
84 SMP_CKP_FE_HRQ_HDR, /* FE HTTP request headers (rules, headers, monitor, stats, redirect) */
85 SMP_CKP_FE_HRQ_BDY, /* FE HTTP request body */
86 SMP_CKP_FE_SET_BCK, /* FE backend switching rules ("use_backend") */
87 SMP_CKP_BE_REQ_CNT, /* BE request content rules ("tcp request content") */
88 SMP_CKP_BE_HRQ_HDR, /* BE HTTP request headers (rules, headers, monitor, stats, redirect) */
89 SMP_CKP_BE_HRQ_BDY, /* BE HTTP request body */
90 SMP_CKP_BE_SET_SRV, /* BE server switching rules ("use_server", "balance", "force-persist", "stick", ...) */
91 SMP_CKP_BE_SRV_CON, /* BE server connect (eg: "source") */
92 SMP_CKP_BE_RES_CNT, /* BE response content rules ("tcp response content") */
93 SMP_CKP_BE_HRS_HDR, /* BE HTTP response headers (rules, headers) */
94 SMP_CKP_BE_HRS_BDY, /* BE HTTP response body (stick-store rules are there) */
95 SMP_CKP_BE_STO_RUL, /* BE stick-store rules */
96 SMP_CKP_FE_RES_CNT, /* FE response content rules ("tcp response content") */
97 SMP_CKP_FE_HRS_HDR, /* FE HTTP response headers (rules, headers) */
98 SMP_CKP_FE_HRS_BDY, /* FE HTTP response body */
99 SMP_CKP_FE_LOG_END, /* FE log at the end of the txn/session */
100 SMP_CKP_ENTRIES /* nothing after this */
101};
102
Willy Tarreau80aca902013-01-07 15:42:20 +0100103/* SMP_USE_* are flags used to declare fetch keywords. Fetch methods are
104 * associated with bitfields composed of these values, generally only one, to
105 * indicate where the contents may be sampled. Some fetches are ambiguous as
106 * they apply to either the request or the response depending on the context,
107 * so they will have 2 of these bits (eg: hdr(), payload(), ...). These are
108 * stored in smp->use.
109 */
110enum {
111 SMP_USE_INTRN = 1 << SMP_SRC_INTRN, /* internal context-less information */
112 SMP_USE_LISTN = 1 << SMP_SRC_LISTN, /* listener which accepted the connection */
113 SMP_USE_FTEND = 1 << SMP_SRC_FTEND, /* frontend which accepted the connection */
114 SMP_USE_L4CLI = 1 << SMP_SRC_L4CLI, /* L4 information about the client */
115 SMP_USE_L5CLI = 1 << SMP_SRC_L5CLI, /* fetch uses client information from embryonic session */
116 SMP_USE_TRACK = 1 << SMP_SRC_TRACK, /* fetch involves track counters */
117 SMP_USE_L6REQ = 1 << SMP_SRC_L6REQ, /* fetch uses raw information from the request buffer */
118 SMP_USE_HRQHV = 1 << SMP_SRC_HRQHV, /* fetch uses volatile information about HTTP request headers (eg: value) */
119 SMP_USE_HRQHP = 1 << SMP_SRC_HRQHP, /* fetch uses persistent information about HTTP request headers (eg: meth) */
120 SMP_USE_HRQBO = 1 << SMP_SRC_HRQBO, /* fetch uses information about HTTP request body */
121 SMP_USE_BKEND = 1 << SMP_SRC_BKEND, /* fetch uses information about the backend */
122 SMP_USE_SERVR = 1 << SMP_SRC_SERVR, /* fetch uses information about the selected server */
123 SMP_USE_L4SRV = 1 << SMP_SRC_L4SRV, /* fetch uses information about the server L4 connection */
124 SMP_USE_L5SRV = 1 << SMP_SRC_L5SRV, /* fetch uses information about the server L5 connection */
125 SMP_USE_L6RES = 1 << SMP_SRC_L6RES, /* fetch uses raw information from the response buffer */
126 SMP_USE_HRSHV = 1 << SMP_SRC_HRSHV, /* fetch uses volatile information about HTTP response headers (eg: value) */
127 SMP_USE_HRSHP = 1 << SMP_SRC_HRSHP, /* fetch uses persistent information about HTTP response headers (eg: status) */
128 SMP_USE_HRSBO = 1 << SMP_SRC_HRSBO, /* fetch uses information about HTTP response body */
129 SMP_USE_RQFIN = 1 << SMP_SRC_RQFIN, /* final information about request buffer (eg: tot bytes) */
130 SMP_USE_RSFIN = 1 << SMP_SRC_RSFIN, /* final information about response buffer (eg: tot bytes) */
131 SMP_USE_TXFIN = 1 << SMP_SRC_TXFIN, /* final information about the transaction (eg: #comp rate) */
132 SMP_USE_SSFIN = 1 << SMP_SRC_SSFIN, /* final information about the session (eg: #requests, final flags) */
133
134 /* This composite one is useful to detect if an hdr_idx needs to be allocated */
135 SMP_USE_HTTP_ANY = SMP_USE_HRQHV | SMP_USE_HRQHP | SMP_USE_HRQBO |
136 SMP_USE_HRSHV | SMP_USE_HRSHP | SMP_USE_HRSBO,
137};
138
139/* Sample validity is computed from the fetch sources above when keywords
140 * are registered. Each fetch method may be used at different locations. The
141 * configuration parser will check whether the fetches are compatible with the
142 * location where they're used. These are stored in smp->val.
143 */
144enum {
145 SMP_VAL___________ = 0, /* Just used as a visual marker */
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100146 SMP_VAL_FE_CON_ACC = 1 << SMP_CKP_FE_CON_ACC, /* FE connection accept rules ("tcp request connection") */
147 SMP_VAL_FE_SES_ACC = 1 << SMP_CKP_FE_SES_ACC, /* FE session accept rules (to come soon) */
148 SMP_VAL_FE_REQ_CNT = 1 << SMP_CKP_FE_REQ_CNT, /* FE request content rules ("tcp request content") */
149 SMP_VAL_FE_HRQ_HDR = 1 << SMP_CKP_FE_HRQ_HDR, /* FE HTTP request headers (rules, headers, monitor, stats, redirect) */
150 SMP_VAL_FE_HRQ_BDY = 1 << SMP_CKP_FE_HRQ_BDY, /* FE HTTP request body */
151 SMP_VAL_FE_SET_BCK = 1 << SMP_CKP_FE_SET_BCK, /* FE backend switching rules ("use_backend") */
152 SMP_VAL_BE_REQ_CNT = 1 << SMP_CKP_BE_REQ_CNT, /* BE request content rules ("tcp request content") */
153 SMP_VAL_BE_HRQ_HDR = 1 << SMP_CKP_BE_HRQ_HDR, /* BE HTTP request headers (rules, headers, monitor, stats, redirect) */
154 SMP_VAL_BE_HRQ_BDY = 1 << SMP_CKP_BE_HRQ_BDY, /* BE HTTP request body */
155 SMP_VAL_BE_SET_SRV = 1 << SMP_CKP_BE_SET_SRV, /* BE server switching rules ("use_server", "balance", "force-persist", "stick", ...) */
156 SMP_VAL_BE_SRV_CON = 1 << SMP_CKP_BE_SRV_CON, /* BE server connect (eg: "source") */
157 SMP_VAL_BE_RES_CNT = 1 << SMP_CKP_BE_RES_CNT, /* BE response content rules ("tcp response content") */
158 SMP_VAL_BE_HRS_HDR = 1 << SMP_CKP_BE_HRS_HDR, /* BE HTTP response headers (rules, headers) */
159 SMP_VAL_BE_HRS_BDY = 1 << SMP_CKP_BE_HRS_BDY, /* BE HTTP response body (stick-store rules are there) */
160 SMP_VAL_BE_STO_RUL = 1 << SMP_CKP_BE_STO_RUL, /* BE stick-store rules */
161 SMP_VAL_FE_RES_CNT = 1 << SMP_CKP_FE_RES_CNT, /* FE response content rules ("tcp response content") */
162 SMP_VAL_FE_HRS_HDR = 1 << SMP_CKP_FE_HRS_HDR, /* FE HTTP response headers (rules, headers) */
163 SMP_VAL_FE_HRS_BDY = 1 << SMP_CKP_FE_HRS_BDY, /* FE HTTP response body */
164 SMP_VAL_FE_LOG_END = 1 << SMP_CKP_FE_LOG_END, /* FE log at the end of the txn/session */
Willy Tarreau80aca902013-01-07 15:42:20 +0100165
166 /* a few combinations to decide what direction to try to fetch (useful for logs) */
167 SMP_VAL_REQUEST = SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
168 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
169 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
170 SMP_VAL_BE_SET_SRV,
171
172 SMP_VAL_RESPONSE = SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | SMP_VAL_BE_HRS_HDR |
173 SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | SMP_VAL_FE_RES_CNT |
174 SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | SMP_VAL_FE_LOG_END,
175};
176
177extern const unsigned int fetch_cap[SMP_SRC_ENTRIES];
178
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200179/* Sample fetch options are passed to sample fetch functions to add precision
180 * about what is desired :
181 * - fetch direction (req/resp)
182 * - intermediary / final fetch
183 */
184enum {
185 SMP_OPT_DIR_REQ = 0, /* direction = request */
186 SMP_OPT_DIR_RES = 1, /* direction = response */
187 SMP_OPT_DIR = (SMP_OPT_DIR_REQ|SMP_OPT_DIR_RES), /* mask to get direction */
188 SMP_OPT_FINAL = 2, /* final fetch, contents won't change anymore */
Willy Tarreau7a777ed2012-04-26 11:44:02 +0200189 SMP_OPT_ITERATE = 4, /* fetches may be iterated if supported (for ACLs) */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200190};
191
Willy Tarreau16c31b02012-04-23 14:24:58 +0200192/* Flags used to describe fetched samples. MAY_CHANGE indicates that the result
193 * of the fetch might still evolve, for instance because of more data expected,
194 * even if the fetch has failed. VOL_* indicates how long a result may be cached.
195 */
196enum {
197 SMP_F_NOT_LAST = 1 << 0, /* other occurrences might exist for this sample */
198 SMP_F_MAY_CHANGE = 1 << 1, /* sample is unstable and might change (eg: request length) */
199 SMP_F_VOL_TEST = 1 << 2, /* result must not survive longer than the test (eg: time) */
200 SMP_F_VOL_1ST = 1 << 3, /* result sensitive to changes in first line (eg: URI) */
201 SMP_F_VOL_HDR = 1 << 4, /* result sensitive to changes in headers */
202 SMP_F_VOL_TXN = 1 << 5, /* result sensitive to new transaction (eg: HTTP version) */
203 SMP_F_VOL_SESS = 1 << 6, /* result sensitive to new session (eg: src IP) */
204 SMP_F_VOLATILE = (1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6), /* any volatility condition */
Willy Tarreau16c31b02012-04-23 14:24:58 +0200205};
Emeric Brun485479d2010-09-23 18:02:19 +0200206
Willy Tarreauc7e42382012-08-24 19:22:53 +0200207/* needed below */
208struct session;
209
Willy Tarreau16c31b02012-04-23 14:24:58 +0200210/* a sample context might be used by any sample fetch function in order to
211 * store information needed across multiple calls (eg: restart point for a
212 * next occurrence). By definition it may store up to 8 pointers, or any
213 * scalar (double, int, long long).
214 */
215union smp_ctx {
216 void *p; /* any pointer */
217 int i; /* any integer */
218 long long ll; /* any long long or smaller */
219 double d; /* any float or double */
220 void *a[8]; /* any array of up to 8 pointers */
221};
222
223/* a sample is a typed data extracted from a stream. It has a type, contents,
224 * validity constraints, a context for use in iterative calls.
225 */
226struct sample {
227 unsigned int flags; /* SMP_F_* */
Willy Tarreau422aa072012-04-20 20:49:27 +0200228 int type; /* SMP_T_* */
Willy Tarreau342acb42012-04-23 22:03:39 +0200229 union {
230 unsigned int uint; /* used for unsigned 32bits integers and booleans */
231 int sint; /* used for signed 32bits integers */
232 struct in_addr ipv4; /* used for ipv4 addresses */
233 struct in6_addr ipv6; /* used for ipv6 addresses */
234 struct chunk str; /* used for char strings or buffers */
235 } data; /* sample data */
Willy Tarreau16c31b02012-04-23 14:24:58 +0200236 union smp_ctx ctx;
237};
238
Willy Tarreau12785782012-04-27 21:37:17 +0200239/* Descriptor for a sample conversion */
240struct sample_conv {
Emeric Brun107ca302010-01-04 16:16:05 +0100241 const char *kw; /* configuration keyword */
Willy Tarreauecfb8e82012-04-20 12:29:52 +0200242 int (*process)(const struct arg *arg_p,
Willy Tarreau342acb42012-04-23 22:03:39 +0200243 struct sample *smp); /* process function */
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200244 unsigned int arg_mask; /* arguments (ARG*()) */
Willy Tarreau21d68a62012-04-20 15:52:36 +0200245 int (*val_args)(struct arg *arg_p,
Thierry FOURNIER9c1d67e2013-11-21 13:37:41 +0100246 struct sample_conv *smp_conv,
Willy Tarreau21d68a62012-04-20 15:52:36 +0200247 char **err_msg); /* argument validation function */
Willy Tarreau12785782012-04-27 21:37:17 +0200248 unsigned int in_type; /* expected input sample type */
249 unsigned int out_type; /* output sample type */
Emeric Brun107ca302010-01-04 16:16:05 +0100250};
251
Willy Tarreau12785782012-04-27 21:37:17 +0200252/* sample conversion expression */
253struct sample_conv_expr {
254 struct list list; /* member of a sample_expr */
255 struct sample_conv *conv; /* sample conversion used */
256 struct arg *arg_p; /* optional arguments */
Emeric Brun107ca302010-01-04 16:16:05 +0100257};
258
Willy Tarreau12785782012-04-27 21:37:17 +0200259/* Descriptor for a sample fetch method */
260struct sample_fetch {
Emeric Brun107ca302010-01-04 16:16:05 +0100261 const char *kw; /* configuration keyword */
262 int (*process)(struct proxy *px,
263 struct session *l4,
264 void *l7,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200265 unsigned int opt, /* fetch options (SMP_OPT_*) */
266 const struct arg *arg_p,
Willy Tarreauef38c392013-07-22 16:29:32 +0200267 struct sample *smp,
268 const char *kw); /* fetch processing function */
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200269 unsigned int arg_mask; /* arguments (ARG*()) */
Willy Tarreau21d68a62012-04-20 15:52:36 +0200270 int (*val_args)(struct arg *arg_p,
271 char **err_msg); /* argument validation function */
Willy Tarreau12785782012-04-27 21:37:17 +0200272 unsigned long out_type; /* output sample type */
Willy Tarreau80aca902013-01-07 15:42:20 +0100273 unsigned int use; /* fetch source (SMP_USE_*) */
274 unsigned int val; /* fetch validity (SMP_VAL_*) */
Emeric Brun107ca302010-01-04 16:16:05 +0100275};
276
Willy Tarreau12785782012-04-27 21:37:17 +0200277/* sample expression */
278struct sample_expr {
279 struct list list; /* member of list of sample, currently not used */
280 struct sample_fetch *fetch; /* sample fetch method */
281 struct arg *arg_p; /* optional pointer to arguments to fetch function */
Emeric Brun107ca302010-01-04 16:16:05 +0100282 struct list conv_exprs; /* list of conversion expression to apply */
283};
284
Willy Tarreau12785782012-04-27 21:37:17 +0200285/* sample fetch keywords list */
286struct sample_fetch_kw_list {
287 struct list list; /* head of sample fetch keyword list */
288 struct sample_fetch kw[VAR_ARRAY]; /* array of sample fetch descriptors */
Emeric Brun107ca302010-01-04 16:16:05 +0100289};
290
Willy Tarreau12785782012-04-27 21:37:17 +0200291/* sample conversion keywords list */
292struct sample_conv_kw_list {
293 struct list list; /* head of sample conversion keyword list */
294 struct sample_conv kw[VAR_ARRAY]; /* array of sample conversion descriptors */
Emeric Brun107ca302010-01-04 16:16:05 +0100295};
296
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100297typedef int (*sample_cast_fct)(struct sample *smp);
298extern sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES];
299
Willy Tarreaucd3b0942012-04-27 21:52:18 +0200300#endif /* _TYPES_SAMPLE_H */