blob: 32d231146a7568239197ad4ab39095037786fe8f [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
25#include <common/buffer.h>
26#include <common/mini-clist.h>
Christopher Faulet24289f22017-09-25 14:48:02 +020027#include <common/hathreads.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010028
29#include <types/filters.h>
30#include <types/freq_ctr.h>
31#include <types/proxy.h>
32#include <types/sample.h>
33#include <types/stream.h>
34#include <types/task.h>
35
Christopher Fauletc718b822017-09-21 16:50:56 +020036/* Type of list of messages */
37#define SPOE_MSGS_BY_EVENT 0x01
38#define SPOE_MSGS_BY_GROUP 0x02
39
Christopher Faulet1f40b912017-02-17 09:32:19 +010040/* Flags set on the SPOE agent */
41#define SPOE_FL_CONT_ON_ERR 0x00000001 /* Do not stop events processing when an error occurred */
Christopher Faulet305c6072017-02-23 16:17:53 +010042#define SPOE_FL_PIPELINING 0x00000002 /* Set when SPOE agent supports pipelining (set by default) */
43#define SPOE_FL_ASYNC 0x00000004 /* Set when SPOE agent supports async (set by default) */
Christopher Fauletcecd8522017-02-24 22:11:21 +010044#define SPOE_FL_SND_FRAGMENTATION 0x00000008 /* Set when SPOE agent supports sending fragmented payload */
45#define SPOE_FL_RCV_FRAGMENTATION 0x00000010 /* Set when SPOE agent supports receiving fragmented payload */
Etienne Carriereaec89892017-12-14 09:36:40 +000046#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 +010047
48/* Flags set on the SPOE context */
49#define SPOE_CTX_FL_CLI_CONNECTED 0x00000001 /* Set after that on-client-session event was processed */
50#define SPOE_CTX_FL_SRV_CONNECTED 0x00000002 /* Set after that on-server-session event was processed */
51#define SPOE_CTX_FL_REQ_PROCESS 0x00000004 /* Set when SPOE is processing the request */
52#define SPOE_CTX_FL_RSP_PROCESS 0x00000008 /* Set when SPOE is processing the response */
53#define SPOE_CTX_FL_FRAGMENTED 0x00000010 /* Set when a fragmented frame is processing */
54
55#define SPOE_CTX_FL_PROCESS (SPOE_CTX_FL_REQ_PROCESS|SPOE_CTX_FL_RSP_PROCESS)
56
57/* Flags set on the SPOE applet */
58#define SPOE_APPCTX_FL_PIPELINING 0x00000001 /* Set if pipelining is supported */
59#define SPOE_APPCTX_FL_ASYNC 0x00000002 /* Set if asynchronus frames is supported */
60#define SPOE_APPCTX_FL_FRAGMENTATION 0x00000004 /* Set if fragmentation is supported */
Christopher Faulet1f40b912017-02-17 09:32:19 +010061
62#define SPOE_APPCTX_ERR_NONE 0x00000000 /* no error yet, leave it to zero */
63#define SPOE_APPCTX_ERR_TOUT 0x00000001 /* SPOE applet timeout */
64
65/* Flags set on the SPOE frame */
66#define SPOE_FRM_FL_FIN 0x00000001
67#define SPOE_FRM_FL_ABRT 0x00000002
68
69/* All possible states for a SPOE context */
70enum spoe_ctx_state {
71 SPOE_CTX_ST_NONE = 0,
72 SPOE_CTX_ST_READY,
73 SPOE_CTX_ST_ENCODING_MSGS,
74 SPOE_CTX_ST_SENDING_MSGS,
75 SPOE_CTX_ST_WAITING_ACK,
76 SPOE_CTX_ST_DONE,
77 SPOE_CTX_ST_ERROR,
78};
79
80/* All possible states for a SPOE applet */
81enum spoe_appctx_state {
82 SPOE_APPCTX_ST_CONNECT = 0,
83 SPOE_APPCTX_ST_CONNECTING,
84 SPOE_APPCTX_ST_IDLE,
85 SPOE_APPCTX_ST_PROCESSING,
86 SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY,
87 SPOE_APPCTX_ST_WAITING_SYNC_ACK,
88 SPOE_APPCTX_ST_DISCONNECT,
89 SPOE_APPCTX_ST_DISCONNECTING,
90 SPOE_APPCTX_ST_EXIT,
91 SPOE_APPCTX_ST_END,
92};
93
94/* All supported SPOE actions */
95enum spoe_action_type {
96 SPOE_ACT_T_SET_VAR = 1,
97 SPOE_ACT_T_UNSET_VAR,
98 SPOE_ACT_TYPES,
99};
100
101/* All supported SPOE events */
102enum spoe_event {
103 SPOE_EV_NONE = 0,
104
105 /* Request events */
106 SPOE_EV_ON_CLIENT_SESS = 1,
107 SPOE_EV_ON_TCP_REQ_FE,
108 SPOE_EV_ON_TCP_REQ_BE,
109 SPOE_EV_ON_HTTP_REQ_FE,
110 SPOE_EV_ON_HTTP_REQ_BE,
111
112 /* Response events */
113 SPOE_EV_ON_SERVER_SESS,
114 SPOE_EV_ON_TCP_RSP,
115 SPOE_EV_ON_HTTP_RSP,
116
117 SPOE_EV_EVENTS
118};
119
120/* Errors triggered by streams */
121enum spoe_context_error {
122 SPOE_CTX_ERR_NONE = 0,
123 SPOE_CTX_ERR_TOUT,
124 SPOE_CTX_ERR_RES,
125 SPOE_CTX_ERR_TOO_BIG,
126 SPOE_CTX_ERR_FRAG_FRAME_ABRT,
Christopher Faulet344c4ab2017-09-22 10:20:13 +0200127 SPOE_CTX_ERR_INTERRUPT,
Christopher Faulet1f40b912017-02-17 09:32:19 +0100128 SPOE_CTX_ERR_UNKNOWN = 255,
129 SPOE_CTX_ERRS,
130};
131
132/* Errors triggerd by SPOE applet */
133enum spoe_frame_error {
134 SPOE_FRM_ERR_NONE = 0,
135 SPOE_FRM_ERR_IO,
136 SPOE_FRM_ERR_TOUT,
137 SPOE_FRM_ERR_TOO_BIG,
138 SPOE_FRM_ERR_INVALID,
139 SPOE_FRM_ERR_NO_VSN,
140 SPOE_FRM_ERR_NO_FRAME_SIZE,
141 SPOE_FRM_ERR_NO_CAP,
142 SPOE_FRM_ERR_BAD_VSN,
143 SPOE_FRM_ERR_BAD_FRAME_SIZE,
144 SPOE_FRM_ERR_FRAG_NOT_SUPPORTED,
145 SPOE_FRM_ERR_INTERLACED_FRAMES,
146 SPOE_FRM_ERR_FRAMEID_NOTFOUND,
147 SPOE_FRM_ERR_RES,
148 SPOE_FRM_ERR_UNKNOWN = 99,
149 SPOE_FRM_ERRS,
150};
151
152/* Scopes used for variables set by agents. It is a way to be agnotic to vars
153 * scope. */
154enum spoe_vars_scope {
155 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
156 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
157 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
158 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
159 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
160};
161
162
163/* Describe an argument that will be linked to a message. It is a sample fetch,
164 * with an optional name. */
165struct spoe_arg {
166 char *name; /* Name of the argument, may be NULL */
167 unsigned int name_len; /* The name length, 0 if NULL */
168 struct sample_expr *expr; /* Sample expression */
169 struct list list; /* Used to chain SPOE args */
170};
171
172/* Used during the config parsing only because, when a SPOE agent section is
Christopher Faulet11610f32017-09-21 10:23:10 +0200173 * parsed, messages/groups can be undefined. */
174struct spoe_placeholder {
175 char *id; /* SPOE placeholder id */
176 struct list list; /* Use to chain SPOE placeholders */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100177};
178
Christopher Faulet336d3ef2017-12-22 10:00:55 +0100179/* Used during the config parsing, when SPOE agent section is parsed, to
180 * register some variable names. */
181struct spoe_var_placeholder {
182 char *name; /* The variable name */
183 struct list list; /* Use to chain SPOE var placeholders */
184};
185
Christopher Faulet1f40b912017-02-17 09:32:19 +0100186/* Describe a message that will be sent in a NOTIFY frame. A message has a name,
187 * an argument list (see above) and it is linked to a specific event. */
188struct spoe_message {
Christopher Faulet11610f32017-09-21 10:23:10 +0200189 char *id; /* SPOE message id */
190 unsigned int id_len; /* The message id length */
191 struct spoe_agent *agent; /* SPOE agent owning this SPOE message */
192 struct spoe_group *group; /* SPOE group owning this SPOE message (can be NULL) */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100193 struct {
Christopher Faulet11610f32017-09-21 10:23:10 +0200194 char *file; /* file where the SPOE message appears */
195 int line; /* line where the SPOE message appears */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100196 } conf; /* config information */
Christopher Faulet11610f32017-09-21 10:23:10 +0200197 unsigned int nargs; /* # of arguments */
198 struct list args; /* Arguments added when the SPOE messages is sent */
199 struct list list; /* Used to chain SPOE messages */
200 struct list by_evt; /* By event list */
201 struct list by_grp; /* By group list */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100202
Christopher Faulet11610f32017-09-21 10:23:10 +0200203 struct list acls; /* ACL declared on this message */
204 struct acl_cond *cond; /* acl condition to meet */
205 enum spoe_event event; /* SPOE_EV_* */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100206};
207
Christopher Faulet11610f32017-09-21 10:23:10 +0200208/* Describe a group of messages that will be sent in a NOTIFY frame. A group has
209 * a name and a list of messages. It can be used by HAProxy, outside events
210 * processing, mainly in (tcp|http) rules. */
211struct spoe_group {
212 char *id; /* SPOE group id */
213 struct spoe_agent *agent; /* SPOE agent owning this SPOE group */
214 struct {
215 char *file; /* file where the SPOE group appears */
216 int line; /* line where the SPOE group appears */
217 } conf; /* config information */
218
219 struct list phs; /* List of placeholders used during conf parsing */
220 struct list messages; /* List of SPOE messages that will be sent by this
221 * group */
222
223 struct list list; /* Used to chain SPOE groups */
224};
225
Christopher Faulet1f40b912017-02-17 09:32:19 +0100226/* Describe a SPOE agent. */
227struct spoe_agent {
228 char *id; /* SPOE agent id (name) */
229 struct {
230 char *file; /* file where the SPOE agent appears */
231 int line; /* line where the SPOE agent appears */
232 } conf; /* config information */
233 union {
234 struct proxy *be; /* Backend used by this agent */
235 char *name; /* Backend name used during conf parsing */
236 } b;
237 struct {
238 unsigned int hello; /* Max time to receive AGENT-HELLO frame (in SPOE applet) */
239 unsigned int idle; /* Max Idle timeout (in SPOE applet) */
240 unsigned int processing; /* Max time to process an event (in the main stream) */
241 } timeout;
242
243 /* Config info */
244 char *engine_id; /* engine-id string */
245 char *var_pfx; /* Prefix used for vars set by the agent */
246 char *var_on_error; /* Variable to set when an error occurred, in the TXN scope */
247 unsigned int flags; /* SPOE_FL_* */
248 unsigned int cps_max; /* Maximum # of connections per second */
249 unsigned int eps_max; /* Maximum # of errors per second */
250 unsigned int max_frame_size; /* Maximum frame size for this agent, before any negotiation */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100251 unsigned int max_fpa; /* Maximum # of frames handled per applet at once */
252
Christopher Faulet11610f32017-09-21 10:23:10 +0200253 struct list events[SPOE_EV_EVENTS]; /* List of SPOE messages that will be sent
Christopher Faulet1f40b912017-02-17 09:32:19 +0100254 * for each supported events */
255
Christopher Faulet11610f32017-09-21 10:23:10 +0200256 struct list groups; /* List of available SPOE groups */
257
258 struct list messages; /* list of all messages attached to this SPOE agent */
259
Christopher Faulet1f40b912017-02-17 09:32:19 +0100260 /* running info */
Christopher Faulet24289f22017-09-25 14:48:02 +0200261 struct {
262 unsigned int frame_size; /* current maximum frame size, only used to encode messages */
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100263#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
Christopher Faulet24289f22017-09-25 14:48:02 +0200264 unsigned int applets_act; /* # of applets alive at a time */
265 unsigned int applets_idle; /* # of applets in the state SPOE_APPCTX_ST_IDLE */
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100266#endif
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +0100267 unsigned int processing;
268 struct freq_ctr processing_per_sec;
Christopher Faulet24289f22017-09-25 14:48:02 +0200269
270 struct freq_ctr conn_per_sec; /* connections per second */
271 struct freq_ctr err_per_sec; /* connetion errors per second */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100272
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100273 struct eb_root idle_applets; /* idle SPOE applets available to process data */
274 struct list applets; /* all SPOE applets for this agent */
Christopher Faulet24289f22017-09-25 14:48:02 +0200275 struct list sending_queue; /* Queue of streams waiting to send data */
276 struct list waiting_queue; /* Queue of streams waiting for a ack, in async mode */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100277 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200278 } *rt;
Christopher Faulet1f40b912017-02-17 09:32:19 +0100279
280};
281
282/* SPOE filter configuration */
283struct spoe_config {
Christopher Faulet7ee86672017-09-19 11:08:28 +0200284 char *id; /* The SPOE engine name. If undefined in HAProxy config,
285 * it will be set with the SPOE agent name */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100286 struct proxy *proxy; /* Proxy owning the filter */
287 struct spoe_agent *agent; /* Agent used by this filter */
288 struct proxy agent_fe; /* Agent frontend */
289};
290
291/* SPOE context attached to a stream. It is the main structure that handles the
292 * processing offload */
293struct spoe_context {
294 struct filter *filter; /* The SPOE filter */
295 struct stream *strm; /* The stream that should be offloaded */
296
Christopher Faulet11610f32017-09-21 10:23:10 +0200297 struct list *events; /* List of messages that will be sent during the stream processing */
Christopher Faulet76c09ef2017-09-21 11:03:52 +0200298 struct list *groups; /* List of available SPOE group */
Christopher Faulet11610f32017-09-21 10:23:10 +0200299
Christopher Faulet1f40b912017-02-17 09:32:19 +0100300 struct buffer *buffer; /* Buffer used to store a encoded messages */
301 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
302 struct list list;
303
304 enum spoe_ctx_state state; /* SPOE_CTX_ST_* */
305 unsigned int flags; /* SPOE_CTX_FL_* */
306 unsigned int status_code; /* SPOE_CTX_ERR_* */
307
308 unsigned int stream_id; /* stream_id and frame_id are used */
309 unsigned int frame_id; /* to map NOTIFY and ACK frames */
310 unsigned int process_exp; /* expiration date to process an event */
311
Christopher Fauletfce747b2018-01-24 15:59:32 +0100312 struct spoe_appctx *spoe_appctx; /* SPOE appctx sending the current frame */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100313 struct {
Christopher Faulet1f40b912017-02-17 09:32:19 +0100314 struct spoe_message *curmsg; /* SPOE message from which to resume encoding */
315 struct spoe_arg *curarg; /* SPOE arg in <curmsg> from which to resume encoding */
316 unsigned int curoff; /* offset in <curarg> from which to resume encoding */
317 unsigned int flags; /* SPOE_FRM_FL_* */
318 } frag_ctx; /* Info about fragmented frames, valid on if SPOE_CTX_FL_FRAGMENTED is set */
319};
320
321/* SPOE context inside a appctx */
322struct spoe_appctx {
323 struct appctx *owner; /* the owner */
324 struct task *task; /* task to handle applet timeouts */
325 struct spoe_agent *agent; /* agent on which the applet is attached */
326
327 unsigned int version; /* the negotiated version */
328 unsigned int max_frame_size; /* the negotiated max-frame-size value */
329 unsigned int flags; /* SPOE_APPCTX_FL_* */
330
331 unsigned int status_code; /* SPOE_FRM_ERR_* */
332#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
333 char *reason; /* Error message, used for debugging only */
334 int rlen; /* reason length */
335#endif
336
337 struct buffer *buffer; /* Buffer used to store a encoded messages */
338 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
339 struct list waiting_queue; /* list of streams waiting for a ACK frame, in sync and pipelining mode */
340 struct list list; /* next spoe appctx for the same agent */
Christopher Fauletb077cdc2018-01-24 16:37:57 +0100341 struct eb32_node node; /* node used for applets tree */
Christopher Faulet8f82b202018-01-24 16:23:03 +0100342 unsigned int cur_fpa;
Christopher Faulet1f40b912017-02-17 09:32:19 +0100343
344 struct {
345 struct spoe_context *ctx; /* SPOE context owning the fragmented frame */
346 unsigned int cursid; /* stream-id of the fragmented frame. used if the processing is aborted */
347 unsigned int curfid; /* frame-id of the fragmented frame. used if the processing is aborted */
348 } frag_ctx; /* Info about fragmented frames, unused for unfragmented frames */
349};
350
351/* Frame Types sent by HAProxy and by agents */
352enum spoe_frame_type {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100353 SPOE_FRM_T_UNSET = 0,
354
Christopher Faulet1f40b912017-02-17 09:32:19 +0100355 /* Frames sent by HAProxy */
356 SPOE_FRM_T_HAPROXY_HELLO = 1,
357 SPOE_FRM_T_HAPROXY_DISCON,
358 SPOE_FRM_T_HAPROXY_NOTIFY,
359
360 /* Frames sent by the agents */
361 SPOE_FRM_T_AGENT_HELLO = 101,
362 SPOE_FRM_T_AGENT_DISCON,
363 SPOE_FRM_T_AGENT_ACK
364};
365
366/* All supported data types */
367enum spoe_data_type {
368 SPOE_DATA_T_NULL = 0,
369 SPOE_DATA_T_BOOL,
370 SPOE_DATA_T_INT32,
371 SPOE_DATA_T_UINT32,
372 SPOE_DATA_T_INT64,
373 SPOE_DATA_T_UINT64,
374 SPOE_DATA_T_IPV4,
375 SPOE_DATA_T_IPV6,
376 SPOE_DATA_T_STR,
377 SPOE_DATA_T_BIN,
378 SPOE_DATA_TYPES
379};
380
381/* Masks to get data type or flags value */
382#define SPOE_DATA_T_MASK 0x0F
383#define SPOE_DATA_FL_MASK 0xF0
384
385/* Flags to set Boolean values */
386#define SPOE_DATA_FL_FALSE 0x00
387#define SPOE_DATA_FL_TRUE 0x10
388
389
390#endif /* _TYPES_SPOE_H */