blob: 9354b5536dd4a098f6dbd484c245dfc38af53625 [file] [log] [blame]
Christopher Faulet1f40b912017-02-17 09:32:19 +01001/*
2 * include/types/spoe.h
3 * Macros, variables and structures for the SPOE filter.
4 *
5 * Copyright (C) 2017 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _TYPES_SPOE_H
23#define _TYPES_SPOE_H
24
Christopher Fauletb2dd1e02018-03-22 09:07:41 +010025#include <sys/time.h>
26
Christopher Faulet1f40b912017-02-17 09:32:19 +010027#include <common/buffer.h>
28#include <common/mini-clist.h>
Christopher Faulet24289f22017-09-25 14:48:02 +020029#include <common/hathreads.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010030
31#include <types/filters.h>
32#include <types/freq_ctr.h>
33#include <types/proxy.h>
34#include <types/sample.h>
35#include <types/stream.h>
36#include <types/task.h>
37
Christopher Fauletc718b822017-09-21 16:50:56 +020038/* Type of list of messages */
39#define SPOE_MSGS_BY_EVENT 0x01
40#define SPOE_MSGS_BY_GROUP 0x02
41
Christopher Faulet1f40b912017-02-17 09:32:19 +010042/* Flags set on the SPOE agent */
43#define SPOE_FL_CONT_ON_ERR 0x00000001 /* Do not stop events processing when an error occurred */
Christopher Faulet305c6072017-02-23 16:17:53 +010044#define SPOE_FL_PIPELINING 0x00000002 /* Set when SPOE agent supports pipelining (set by default) */
45#define SPOE_FL_ASYNC 0x00000004 /* Set when SPOE agent supports async (set by default) */
Christopher Fauletcecd8522017-02-24 22:11:21 +010046#define SPOE_FL_SND_FRAGMENTATION 0x00000008 /* Set when SPOE agent supports sending fragmented payload */
47#define SPOE_FL_RCV_FRAGMENTATION 0x00000010 /* Set when SPOE agent supports receiving fragmented payload */
Etienne Carriereaec89892017-12-14 09:36:40 +000048#define SPOE_FL_FORCE_SET_VAR 0x00000020 /* Set when SPOE agent will set all variables from agent (and not only known variables) */
Christopher Faulet1f40b912017-02-17 09:32:19 +010049
50/* Flags set on the SPOE context */
51#define SPOE_CTX_FL_CLI_CONNECTED 0x00000001 /* Set after that on-client-session event was processed */
52#define SPOE_CTX_FL_SRV_CONNECTED 0x00000002 /* Set after that on-server-session event was processed */
53#define SPOE_CTX_FL_REQ_PROCESS 0x00000004 /* Set when SPOE is processing the request */
54#define SPOE_CTX_FL_RSP_PROCESS 0x00000008 /* Set when SPOE is processing the response */
55#define SPOE_CTX_FL_FRAGMENTED 0x00000010 /* Set when a fragmented frame is processing */
56
57#define SPOE_CTX_FL_PROCESS (SPOE_CTX_FL_REQ_PROCESS|SPOE_CTX_FL_RSP_PROCESS)
58
59/* Flags set on the SPOE applet */
60#define SPOE_APPCTX_FL_PIPELINING 0x00000001 /* Set if pipelining is supported */
61#define SPOE_APPCTX_FL_ASYNC 0x00000002 /* Set if asynchronus frames is supported */
62#define SPOE_APPCTX_FL_FRAGMENTATION 0x00000004 /* Set if fragmentation is supported */
Christopher Faulet1f40b912017-02-17 09:32:19 +010063
64#define SPOE_APPCTX_ERR_NONE 0x00000000 /* no error yet, leave it to zero */
65#define SPOE_APPCTX_ERR_TOUT 0x00000001 /* SPOE applet timeout */
66
67/* Flags set on the SPOE frame */
68#define SPOE_FRM_FL_FIN 0x00000001
69#define SPOE_FRM_FL_ABRT 0x00000002
70
71/* All possible states for a SPOE context */
72enum spoe_ctx_state {
73 SPOE_CTX_ST_NONE = 0,
74 SPOE_CTX_ST_READY,
75 SPOE_CTX_ST_ENCODING_MSGS,
76 SPOE_CTX_ST_SENDING_MSGS,
77 SPOE_CTX_ST_WAITING_ACK,
78 SPOE_CTX_ST_DONE,
79 SPOE_CTX_ST_ERROR,
80};
81
82/* All possible states for a SPOE applet */
83enum spoe_appctx_state {
84 SPOE_APPCTX_ST_CONNECT = 0,
85 SPOE_APPCTX_ST_CONNECTING,
86 SPOE_APPCTX_ST_IDLE,
87 SPOE_APPCTX_ST_PROCESSING,
88 SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY,
89 SPOE_APPCTX_ST_WAITING_SYNC_ACK,
90 SPOE_APPCTX_ST_DISCONNECT,
91 SPOE_APPCTX_ST_DISCONNECTING,
92 SPOE_APPCTX_ST_EXIT,
93 SPOE_APPCTX_ST_END,
94};
95
96/* All supported SPOE actions */
97enum spoe_action_type {
98 SPOE_ACT_T_SET_VAR = 1,
99 SPOE_ACT_T_UNSET_VAR,
100 SPOE_ACT_TYPES,
101};
102
103/* All supported SPOE events */
104enum spoe_event {
105 SPOE_EV_NONE = 0,
106
107 /* Request events */
108 SPOE_EV_ON_CLIENT_SESS = 1,
109 SPOE_EV_ON_TCP_REQ_FE,
110 SPOE_EV_ON_TCP_REQ_BE,
111 SPOE_EV_ON_HTTP_REQ_FE,
112 SPOE_EV_ON_HTTP_REQ_BE,
113
114 /* Response events */
115 SPOE_EV_ON_SERVER_SESS,
116 SPOE_EV_ON_TCP_RSP,
117 SPOE_EV_ON_HTTP_RSP,
118
119 SPOE_EV_EVENTS
120};
121
122/* Errors triggered by streams */
123enum spoe_context_error {
124 SPOE_CTX_ERR_NONE = 0,
125 SPOE_CTX_ERR_TOUT,
126 SPOE_CTX_ERR_RES,
127 SPOE_CTX_ERR_TOO_BIG,
128 SPOE_CTX_ERR_FRAG_FRAME_ABRT,
Christopher Faulet344c4ab2017-09-22 10:20:13 +0200129 SPOE_CTX_ERR_INTERRUPT,
Christopher Faulet1f40b912017-02-17 09:32:19 +0100130 SPOE_CTX_ERR_UNKNOWN = 255,
131 SPOE_CTX_ERRS,
132};
133
134/* Errors triggerd by SPOE applet */
135enum spoe_frame_error {
136 SPOE_FRM_ERR_NONE = 0,
137 SPOE_FRM_ERR_IO,
138 SPOE_FRM_ERR_TOUT,
139 SPOE_FRM_ERR_TOO_BIG,
140 SPOE_FRM_ERR_INVALID,
141 SPOE_FRM_ERR_NO_VSN,
142 SPOE_FRM_ERR_NO_FRAME_SIZE,
143 SPOE_FRM_ERR_NO_CAP,
144 SPOE_FRM_ERR_BAD_VSN,
145 SPOE_FRM_ERR_BAD_FRAME_SIZE,
146 SPOE_FRM_ERR_FRAG_NOT_SUPPORTED,
147 SPOE_FRM_ERR_INTERLACED_FRAMES,
148 SPOE_FRM_ERR_FRAMEID_NOTFOUND,
149 SPOE_FRM_ERR_RES,
150 SPOE_FRM_ERR_UNKNOWN = 99,
151 SPOE_FRM_ERRS,
152};
153
154/* Scopes used for variables set by agents. It is a way to be agnotic to vars
155 * scope. */
156enum spoe_vars_scope {
157 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
158 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
159 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
160 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
161 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
162};
163
164
165/* Describe an argument that will be linked to a message. It is a sample fetch,
166 * with an optional name. */
167struct spoe_arg {
168 char *name; /* Name of the argument, may be NULL */
169 unsigned int name_len; /* The name length, 0 if NULL */
170 struct sample_expr *expr; /* Sample expression */
171 struct list list; /* Used to chain SPOE args */
172};
173
174/* Used during the config parsing only because, when a SPOE agent section is
Christopher Faulet11610f32017-09-21 10:23:10 +0200175 * parsed, messages/groups can be undefined. */
176struct spoe_placeholder {
177 char *id; /* SPOE placeholder id */
178 struct list list; /* Use to chain SPOE placeholders */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100179};
180
Christopher Faulet336d3ef2017-12-22 10:00:55 +0100181/* Used during the config parsing, when SPOE agent section is parsed, to
182 * register some variable names. */
183struct spoe_var_placeholder {
184 char *name; /* The variable name */
185 struct list list; /* Use to chain SPOE var placeholders */
186};
187
Christopher Faulet1f40b912017-02-17 09:32:19 +0100188/* Describe a message that will be sent in a NOTIFY frame. A message has a name,
189 * an argument list (see above) and it is linked to a specific event. */
190struct spoe_message {
Christopher Faulet11610f32017-09-21 10:23:10 +0200191 char *id; /* SPOE message id */
192 unsigned int id_len; /* The message id length */
193 struct spoe_agent *agent; /* SPOE agent owning this SPOE message */
194 struct spoe_group *group; /* SPOE group owning this SPOE message (can be NULL) */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100195 struct {
Christopher Faulet11610f32017-09-21 10:23:10 +0200196 char *file; /* file where the SPOE message appears */
197 int line; /* line where the SPOE message appears */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100198 } conf; /* config information */
Christopher Faulet11610f32017-09-21 10:23:10 +0200199 unsigned int nargs; /* # of arguments */
200 struct list args; /* Arguments added when the SPOE messages is sent */
201 struct list list; /* Used to chain SPOE messages */
202 struct list by_evt; /* By event list */
203 struct list by_grp; /* By group list */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100204
Christopher Faulet11610f32017-09-21 10:23:10 +0200205 struct list acls; /* ACL declared on this message */
206 struct acl_cond *cond; /* acl condition to meet */
207 enum spoe_event event; /* SPOE_EV_* */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100208};
209
Christopher Faulet11610f32017-09-21 10:23:10 +0200210/* Describe a group of messages that will be sent in a NOTIFY frame. A group has
211 * a name and a list of messages. It can be used by HAProxy, outside events
212 * processing, mainly in (tcp|http) rules. */
213struct spoe_group {
214 char *id; /* SPOE group id */
215 struct spoe_agent *agent; /* SPOE agent owning this SPOE group */
216 struct {
217 char *file; /* file where the SPOE group appears */
218 int line; /* line where the SPOE group appears */
219 } conf; /* config information */
220
221 struct list phs; /* List of placeholders used during conf parsing */
222 struct list messages; /* List of SPOE messages that will be sent by this
223 * group */
224
225 struct list list; /* Used to chain SPOE groups */
226};
227
Christopher Faulet1f40b912017-02-17 09:32:19 +0100228/* Describe a SPOE agent. */
229struct spoe_agent {
230 char *id; /* SPOE agent id (name) */
231 struct {
232 char *file; /* file where the SPOE agent appears */
233 int line; /* line where the SPOE agent appears */
234 } conf; /* config information */
235 union {
236 struct proxy *be; /* Backend used by this agent */
237 char *name; /* Backend name used during conf parsing */
238 } b;
239 struct {
240 unsigned int hello; /* Max time to receive AGENT-HELLO frame (in SPOE applet) */
241 unsigned int idle; /* Max Idle timeout (in SPOE applet) */
242 unsigned int processing; /* Max time to process an event (in the main stream) */
243 } timeout;
244
245 /* Config info */
246 char *engine_id; /* engine-id string */
247 char *var_pfx; /* Prefix used for vars set by the agent */
248 char *var_on_error; /* Variable to set when an error occurred, in the TXN scope */
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100249 char *var_t_process; /* Variable to set to report the processing time of the last event/group, in the TXN scope */
250 char *var_t_total; /* Variable to set to report the cumulative processing time, in the TXN scope */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100251 unsigned int flags; /* SPOE_FL_* */
252 unsigned int cps_max; /* Maximum # of connections per second */
253 unsigned int eps_max; /* Maximum # of errors per second */
254 unsigned int max_frame_size; /* Maximum frame size for this agent, before any negotiation */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100255 unsigned int max_fpa; /* Maximum # of frames handled per applet at once */
256
Christopher Faulet11610f32017-09-21 10:23:10 +0200257 struct list events[SPOE_EV_EVENTS]; /* List of SPOE messages that will be sent
Christopher Faulet1f40b912017-02-17 09:32:19 +0100258 * for each supported events */
259
Christopher Faulet11610f32017-09-21 10:23:10 +0200260 struct list groups; /* List of available SPOE groups */
261
262 struct list messages; /* list of all messages attached to this SPOE agent */
263
Christopher Faulet1f40b912017-02-17 09:32:19 +0100264 /* running info */
Christopher Faulet24289f22017-09-25 14:48:02 +0200265 struct {
266 unsigned int frame_size; /* current maximum frame size, only used to encode messages */
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100267#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
Christopher Faulet24289f22017-09-25 14:48:02 +0200268 unsigned int applets_act; /* # of applets alive at a time */
269 unsigned int applets_idle; /* # of applets in the state SPOE_APPCTX_ST_IDLE */
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100270#endif
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +0100271 unsigned int processing;
272 struct freq_ctr processing_per_sec;
Christopher Faulet24289f22017-09-25 14:48:02 +0200273
274 struct freq_ctr conn_per_sec; /* connections per second */
275 struct freq_ctr err_per_sec; /* connetion errors per second */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100276
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100277 struct eb_root idle_applets; /* idle SPOE applets available to process data */
278 struct list applets; /* all SPOE applets for this agent */
Christopher Faulet24289f22017-09-25 14:48:02 +0200279 struct list sending_queue; /* Queue of streams waiting to send data */
280 struct list waiting_queue; /* Queue of streams waiting for a ack, in async mode */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100281 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200282 } *rt;
Christopher Faulet1f40b912017-02-17 09:32:19 +0100283
284};
285
286/* SPOE filter configuration */
287struct spoe_config {
Christopher Faulet7ee86672017-09-19 11:08:28 +0200288 char *id; /* The SPOE engine name. If undefined in HAProxy config,
289 * it will be set with the SPOE agent name */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100290 struct proxy *proxy; /* Proxy owning the filter */
291 struct spoe_agent *agent; /* Agent used by this filter */
292 struct proxy agent_fe; /* Agent frontend */
293};
294
295/* SPOE context attached to a stream. It is the main structure that handles the
296 * processing offload */
297struct spoe_context {
298 struct filter *filter; /* The SPOE filter */
299 struct stream *strm; /* The stream that should be offloaded */
300
Christopher Faulet11610f32017-09-21 10:23:10 +0200301 struct list *events; /* List of messages that will be sent during the stream processing */
Christopher Faulet76c09ef2017-09-21 11:03:52 +0200302 struct list *groups; /* List of available SPOE group */
Christopher Faulet11610f32017-09-21 10:23:10 +0200303
Christopher Faulet1f40b912017-02-17 09:32:19 +0100304 struct buffer *buffer; /* Buffer used to store a encoded messages */
305 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
306 struct list list;
307
308 enum spoe_ctx_state state; /* SPOE_CTX_ST_* */
309 unsigned int flags; /* SPOE_CTX_FL_* */
310 unsigned int status_code; /* SPOE_CTX_ERR_* */
311
312 unsigned int stream_id; /* stream_id and frame_id are used */
313 unsigned int frame_id; /* to map NOTIFY and ACK frames */
314 unsigned int process_exp; /* expiration date to process an event */
315
Christopher Fauletfce747b2018-01-24 15:59:32 +0100316 struct spoe_appctx *spoe_appctx; /* SPOE appctx sending the current frame */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100317 struct {
Christopher Faulet1f40b912017-02-17 09:32:19 +0100318 struct spoe_message *curmsg; /* SPOE message from which to resume encoding */
319 struct spoe_arg *curarg; /* SPOE arg in <curmsg> from which to resume encoding */
320 unsigned int curoff; /* offset in <curarg> from which to resume encoding */
321 unsigned int flags; /* SPOE_FRM_FL_* */
322 } frag_ctx; /* Info about fragmented frames, valid on if SPOE_CTX_FL_FRAGMENTED is set */
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100323
324 struct {
325 struct timeval tv_start; /* start date of the current event/group */
326 struct timeval tv_request; /* date the frame processing starts (reset for each frag) */
327 struct timeval tv_queue; /* date the frame is queued (reset for each frag) */
328 struct timeval tv_wait; /* date the stream starts waiting for a response */
329 struct timeval tv_response; /* date the response processing starts */
330 long t_request; /* delay to encode and push the frame in queue (cumulative for frags) */
331 long t_queue; /* delay before the frame gets out the sending queue (cumulative for frags) */
332 long t_waiting; /* delay before the response is reveived */
333 long t_response; /* delay to process the response (from the stream pov) */
334 long t_process; /* processing time of the last event/group */
335 unsigned long t_total; /* cumulative processing time */
336 } stats; /* Stats for this stream */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100337};
338
339/* SPOE context inside a appctx */
340struct spoe_appctx {
341 struct appctx *owner; /* the owner */
342 struct task *task; /* task to handle applet timeouts */
343 struct spoe_agent *agent; /* agent on which the applet is attached */
344
345 unsigned int version; /* the negotiated version */
346 unsigned int max_frame_size; /* the negotiated max-frame-size value */
347 unsigned int flags; /* SPOE_APPCTX_FL_* */
348
349 unsigned int status_code; /* SPOE_FRM_ERR_* */
350#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
351 char *reason; /* Error message, used for debugging only */
352 int rlen; /* reason length */
353#endif
354
355 struct buffer *buffer; /* Buffer used to store a encoded messages */
356 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
357 struct list waiting_queue; /* list of streams waiting for a ACK frame, in sync and pipelining mode */
358 struct list list; /* next spoe appctx for the same agent */
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100359 struct eb32_node node; /* node used for applets tree */
Christopher Faulet8f82b202018-01-24 16:23:03 +0100360 unsigned int cur_fpa;
Christopher Faulet1f40b912017-02-17 09:32:19 +0100361
362 struct {
363 struct spoe_context *ctx; /* SPOE context owning the fragmented frame */
364 unsigned int cursid; /* stream-id of the fragmented frame. used if the processing is aborted */
365 unsigned int curfid; /* frame-id of the fragmented frame. used if the processing is aborted */
366 } frag_ctx; /* Info about fragmented frames, unused for unfragmented frames */
367};
368
369/* Frame Types sent by HAProxy and by agents */
370enum spoe_frame_type {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100371 SPOE_FRM_T_UNSET = 0,
372
Christopher Faulet1f40b912017-02-17 09:32:19 +0100373 /* Frames sent by HAProxy */
374 SPOE_FRM_T_HAPROXY_HELLO = 1,
375 SPOE_FRM_T_HAPROXY_DISCON,
376 SPOE_FRM_T_HAPROXY_NOTIFY,
377
378 /* Frames sent by the agents */
379 SPOE_FRM_T_AGENT_HELLO = 101,
380 SPOE_FRM_T_AGENT_DISCON,
381 SPOE_FRM_T_AGENT_ACK
382};
383
384/* All supported data types */
385enum spoe_data_type {
386 SPOE_DATA_T_NULL = 0,
387 SPOE_DATA_T_BOOL,
388 SPOE_DATA_T_INT32,
389 SPOE_DATA_T_UINT32,
390 SPOE_DATA_T_INT64,
391 SPOE_DATA_T_UINT64,
392 SPOE_DATA_T_IPV4,
393 SPOE_DATA_T_IPV6,
394 SPOE_DATA_T_STR,
395 SPOE_DATA_T_BIN,
396 SPOE_DATA_TYPES
397};
398
399/* Masks to get data type or flags value */
400#define SPOE_DATA_T_MASK 0x0F
401#define SPOE_DATA_FL_MASK 0xF0
402
403/* Flags to set Boolean values */
404#define SPOE_DATA_FL_FALSE 0x00
405#define SPOE_DATA_FL_TRUE 0x10
406
407
408#endif /* _TYPES_SPOE_H */