blob: 2632601a7589c38f38a869a9290407160bdd7f1a [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 */
61#define SPOE_APPCTX_FL_PERSIST 0x00000008 /* Set if the applet is persistent */
62
63#define SPOE_APPCTX_ERR_NONE 0x00000000 /* no error yet, leave it to zero */
64#define SPOE_APPCTX_ERR_TOUT 0x00000001 /* SPOE applet timeout */
65
66/* Flags set on the SPOE frame */
67#define SPOE_FRM_FL_FIN 0x00000001
68#define SPOE_FRM_FL_ABRT 0x00000002
69
70/* All possible states for a SPOE context */
71enum spoe_ctx_state {
72 SPOE_CTX_ST_NONE = 0,
73 SPOE_CTX_ST_READY,
74 SPOE_CTX_ST_ENCODING_MSGS,
75 SPOE_CTX_ST_SENDING_MSGS,
76 SPOE_CTX_ST_WAITING_ACK,
77 SPOE_CTX_ST_DONE,
78 SPOE_CTX_ST_ERROR,
79};
80
81/* All possible states for a SPOE applet */
82enum spoe_appctx_state {
83 SPOE_APPCTX_ST_CONNECT = 0,
84 SPOE_APPCTX_ST_CONNECTING,
85 SPOE_APPCTX_ST_IDLE,
86 SPOE_APPCTX_ST_PROCESSING,
87 SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY,
88 SPOE_APPCTX_ST_WAITING_SYNC_ACK,
89 SPOE_APPCTX_ST_DISCONNECT,
90 SPOE_APPCTX_ST_DISCONNECTING,
91 SPOE_APPCTX_ST_EXIT,
92 SPOE_APPCTX_ST_END,
93};
94
95/* All supported SPOE actions */
96enum spoe_action_type {
97 SPOE_ACT_T_SET_VAR = 1,
98 SPOE_ACT_T_UNSET_VAR,
99 SPOE_ACT_TYPES,
100};
101
102/* All supported SPOE events */
103enum spoe_event {
104 SPOE_EV_NONE = 0,
105
106 /* Request events */
107 SPOE_EV_ON_CLIENT_SESS = 1,
108 SPOE_EV_ON_TCP_REQ_FE,
109 SPOE_EV_ON_TCP_REQ_BE,
110 SPOE_EV_ON_HTTP_REQ_FE,
111 SPOE_EV_ON_HTTP_REQ_BE,
112
113 /* Response events */
114 SPOE_EV_ON_SERVER_SESS,
115 SPOE_EV_ON_TCP_RSP,
116 SPOE_EV_ON_HTTP_RSP,
117
118 SPOE_EV_EVENTS
119};
120
121/* Errors triggered by streams */
122enum spoe_context_error {
123 SPOE_CTX_ERR_NONE = 0,
124 SPOE_CTX_ERR_TOUT,
125 SPOE_CTX_ERR_RES,
126 SPOE_CTX_ERR_TOO_BIG,
127 SPOE_CTX_ERR_FRAG_FRAME_ABRT,
Christopher Faulet344c4ab2017-09-22 10:20:13 +0200128 SPOE_CTX_ERR_INTERRUPT,
Christopher Faulet1f40b912017-02-17 09:32:19 +0100129 SPOE_CTX_ERR_UNKNOWN = 255,
130 SPOE_CTX_ERRS,
131};
132
133/* Errors triggerd by SPOE applet */
134enum spoe_frame_error {
135 SPOE_FRM_ERR_NONE = 0,
136 SPOE_FRM_ERR_IO,
137 SPOE_FRM_ERR_TOUT,
138 SPOE_FRM_ERR_TOO_BIG,
139 SPOE_FRM_ERR_INVALID,
140 SPOE_FRM_ERR_NO_VSN,
141 SPOE_FRM_ERR_NO_FRAME_SIZE,
142 SPOE_FRM_ERR_NO_CAP,
143 SPOE_FRM_ERR_BAD_VSN,
144 SPOE_FRM_ERR_BAD_FRAME_SIZE,
145 SPOE_FRM_ERR_FRAG_NOT_SUPPORTED,
146 SPOE_FRM_ERR_INTERLACED_FRAMES,
147 SPOE_FRM_ERR_FRAMEID_NOTFOUND,
148 SPOE_FRM_ERR_RES,
149 SPOE_FRM_ERR_UNKNOWN = 99,
150 SPOE_FRM_ERRS,
151};
152
153/* Scopes used for variables set by agents. It is a way to be agnotic to vars
154 * scope. */
155enum spoe_vars_scope {
156 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
157 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
158 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
159 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
160 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
161};
162
163
164/* Describe an argument that will be linked to a message. It is a sample fetch,
165 * with an optional name. */
166struct spoe_arg {
167 char *name; /* Name of the argument, may be NULL */
168 unsigned int name_len; /* The name length, 0 if NULL */
169 struct sample_expr *expr; /* Sample expression */
170 struct list list; /* Used to chain SPOE args */
171};
172
173/* Used during the config parsing only because, when a SPOE agent section is
Christopher Faulet11610f32017-09-21 10:23:10 +0200174 * parsed, messages/groups can be undefined. */
175struct spoe_placeholder {
176 char *id; /* SPOE placeholder id */
177 struct list list; /* Use to chain SPOE placeholders */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100178};
179
Christopher Faulet336d3ef2017-12-22 10:00:55 +0100180/* Used during the config parsing, when SPOE agent section is parsed, to
181 * register some variable names. */
182struct spoe_var_placeholder {
183 char *name; /* The variable name */
184 struct list list; /* Use to chain SPOE var placeholders */
185};
186
Christopher Faulet1f40b912017-02-17 09:32:19 +0100187/* Describe a message that will be sent in a NOTIFY frame. A message has a name,
188 * an argument list (see above) and it is linked to a specific event. */
189struct spoe_message {
Christopher Faulet11610f32017-09-21 10:23:10 +0200190 char *id; /* SPOE message id */
191 unsigned int id_len; /* The message id length */
192 struct spoe_agent *agent; /* SPOE agent owning this SPOE message */
193 struct spoe_group *group; /* SPOE group owning this SPOE message (can be NULL) */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100194 struct {
Christopher Faulet11610f32017-09-21 10:23:10 +0200195 char *file; /* file where the SPOE message appears */
196 int line; /* line where the SPOE message appears */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100197 } conf; /* config information */
Christopher Faulet11610f32017-09-21 10:23:10 +0200198 unsigned int nargs; /* # of arguments */
199 struct list args; /* Arguments added when the SPOE messages is sent */
200 struct list list; /* Used to chain SPOE messages */
201 struct list by_evt; /* By event list */
202 struct list by_grp; /* By group list */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100203
Christopher Faulet11610f32017-09-21 10:23:10 +0200204 struct list acls; /* ACL declared on this message */
205 struct acl_cond *cond; /* acl condition to meet */
206 enum spoe_event event; /* SPOE_EV_* */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100207};
208
Christopher Faulet11610f32017-09-21 10:23:10 +0200209/* Describe a group of messages that will be sent in a NOTIFY frame. A group has
210 * a name and a list of messages. It can be used by HAProxy, outside events
211 * processing, mainly in (tcp|http) rules. */
212struct spoe_group {
213 char *id; /* SPOE group id */
214 struct spoe_agent *agent; /* SPOE agent owning this SPOE group */
215 struct {
216 char *file; /* file where the SPOE group appears */
217 int line; /* line where the SPOE group appears */
218 } conf; /* config information */
219
220 struct list phs; /* List of placeholders used during conf parsing */
221 struct list messages; /* List of SPOE messages that will be sent by this
222 * group */
223
224 struct list list; /* Used to chain SPOE groups */
225};
226
Christopher Faulet1f40b912017-02-17 09:32:19 +0100227/* Describe a SPOE agent. */
228struct spoe_agent {
229 char *id; /* SPOE agent id (name) */
230 struct {
231 char *file; /* file where the SPOE agent appears */
232 int line; /* line where the SPOE agent appears */
233 } conf; /* config information */
234 union {
235 struct proxy *be; /* Backend used by this agent */
236 char *name; /* Backend name used during conf parsing */
237 } b;
238 struct {
239 unsigned int hello; /* Max time to receive AGENT-HELLO frame (in SPOE applet) */
240 unsigned int idle; /* Max Idle timeout (in SPOE applet) */
241 unsigned int processing; /* Max time to process an event (in the main stream) */
242 } timeout;
243
244 /* Config info */
245 char *engine_id; /* engine-id string */
246 char *var_pfx; /* Prefix used for vars set by the agent */
247 char *var_on_error; /* Variable to set when an error occurred, in the TXN scope */
248 unsigned int flags; /* SPOE_FL_* */
249 unsigned int cps_max; /* Maximum # of connections per second */
250 unsigned int eps_max; /* Maximum # of errors per second */
251 unsigned int max_frame_size; /* Maximum frame size for this agent, before any negotiation */
252 unsigned int min_applets; /* Minimum # applets alive at a time */
253 unsigned int max_fpa; /* Maximum # of frames handled per applet at once */
254
Christopher Faulet11610f32017-09-21 10:23:10 +0200255 struct list events[SPOE_EV_EVENTS]; /* List of SPOE messages that will be sent
Christopher Faulet1f40b912017-02-17 09:32:19 +0100256 * for each supported events */
257
Christopher Faulet11610f32017-09-21 10:23:10 +0200258 struct list groups; /* List of available SPOE groups */
259
260 struct list messages; /* list of all messages attached to this SPOE agent */
261
Christopher Faulet1f40b912017-02-17 09:32:19 +0100262 /* running info */
Christopher Faulet24289f22017-09-25 14:48:02 +0200263 struct {
264 unsigned int frame_size; /* current maximum frame size, only used to encode messages */
265 unsigned int applets_act; /* # of applets alive at a time */
266 unsigned int applets_idle; /* # of applets in the state SPOE_APPCTX_ST_IDLE */
267 unsigned int sending_rate; /* the global sending rate */
268
269 struct freq_ctr conn_per_sec; /* connections per second */
270 struct freq_ctr err_per_sec; /* connetion errors per second */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100271
Christopher Faulet24289f22017-09-25 14:48:02 +0200272 struct list applets; /* List of available SPOE applets */
273 struct list sending_queue; /* Queue of streams waiting to send data */
274 struct list waiting_queue; /* Queue of streams waiting for a ack, in async mode */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100275 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200276 } *rt;
Christopher Faulet1f40b912017-02-17 09:32:19 +0100277
278};
279
280/* SPOE filter configuration */
281struct spoe_config {
Christopher Faulet7ee86672017-09-19 11:08:28 +0200282 char *id; /* The SPOE engine name. If undefined in HAProxy config,
283 * it will be set with the SPOE agent name */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100284 struct proxy *proxy; /* Proxy owning the filter */
285 struct spoe_agent *agent; /* Agent used by this filter */
286 struct proxy agent_fe; /* Agent frontend */
287};
288
289/* SPOE context attached to a stream. It is the main structure that handles the
290 * processing offload */
291struct spoe_context {
292 struct filter *filter; /* The SPOE filter */
293 struct stream *strm; /* The stream that should be offloaded */
294
Christopher Faulet11610f32017-09-21 10:23:10 +0200295 struct list *events; /* List of messages that will be sent during the stream processing */
Christopher Faulet76c09ef2017-09-21 11:03:52 +0200296 struct list *groups; /* List of available SPOE group */
Christopher Faulet11610f32017-09-21 10:23:10 +0200297
Christopher Faulet1f40b912017-02-17 09:32:19 +0100298 struct buffer *buffer; /* Buffer used to store a encoded messages */
299 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
300 struct list list;
301
302 enum spoe_ctx_state state; /* SPOE_CTX_ST_* */
303 unsigned int flags; /* SPOE_CTX_FL_* */
304 unsigned int status_code; /* SPOE_CTX_ERR_* */
305
306 unsigned int stream_id; /* stream_id and frame_id are used */
307 unsigned int frame_id; /* to map NOTIFY and ACK frames */
308 unsigned int process_exp; /* expiration date to process an event */
309
310 struct {
311 struct spoe_appctx *spoe_appctx; /* SPOE appctx sending the fragmented frame */
312 struct spoe_message *curmsg; /* SPOE message from which to resume encoding */
313 struct spoe_arg *curarg; /* SPOE arg in <curmsg> from which to resume encoding */
314 unsigned int curoff; /* offset in <curarg> from which to resume encoding */
315 unsigned int flags; /* SPOE_FRM_FL_* */
316 } frag_ctx; /* Info about fragmented frames, valid on if SPOE_CTX_FL_FRAGMENTED is set */
317};
318
319/* SPOE context inside a appctx */
320struct spoe_appctx {
321 struct appctx *owner; /* the owner */
322 struct task *task; /* task to handle applet timeouts */
323 struct spoe_agent *agent; /* agent on which the applet is attached */
324
325 unsigned int version; /* the negotiated version */
326 unsigned int max_frame_size; /* the negotiated max-frame-size value */
327 unsigned int flags; /* SPOE_APPCTX_FL_* */
328
329 unsigned int status_code; /* SPOE_FRM_ERR_* */
330#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
331 char *reason; /* Error message, used for debugging only */
332 int rlen; /* reason length */
333#endif
334
335 struct buffer *buffer; /* Buffer used to store a encoded messages */
336 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
337 struct list waiting_queue; /* list of streams waiting for a ACK frame, in sync and pipelining mode */
338 struct list list; /* next spoe appctx for the same agent */
339
340 struct {
341 struct spoe_context *ctx; /* SPOE context owning the fragmented frame */
342 unsigned int cursid; /* stream-id of the fragmented frame. used if the processing is aborted */
343 unsigned int curfid; /* frame-id of the fragmented frame. used if the processing is aborted */
344 } frag_ctx; /* Info about fragmented frames, unused for unfragmented frames */
345};
346
347/* Frame Types sent by HAProxy and by agents */
348enum spoe_frame_type {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100349 SPOE_FRM_T_UNSET = 0,
350
Christopher Faulet1f40b912017-02-17 09:32:19 +0100351 /* Frames sent by HAProxy */
352 SPOE_FRM_T_HAPROXY_HELLO = 1,
353 SPOE_FRM_T_HAPROXY_DISCON,
354 SPOE_FRM_T_HAPROXY_NOTIFY,
355
356 /* Frames sent by the agents */
357 SPOE_FRM_T_AGENT_HELLO = 101,
358 SPOE_FRM_T_AGENT_DISCON,
359 SPOE_FRM_T_AGENT_ACK
360};
361
362/* All supported data types */
363enum spoe_data_type {
364 SPOE_DATA_T_NULL = 0,
365 SPOE_DATA_T_BOOL,
366 SPOE_DATA_T_INT32,
367 SPOE_DATA_T_UINT32,
368 SPOE_DATA_T_INT64,
369 SPOE_DATA_T_UINT64,
370 SPOE_DATA_T_IPV4,
371 SPOE_DATA_T_IPV6,
372 SPOE_DATA_T_STR,
373 SPOE_DATA_T_BIN,
374 SPOE_DATA_TYPES
375};
376
377/* Masks to get data type or flags value */
378#define SPOE_DATA_T_MASK 0x0F
379#define SPOE_DATA_FL_MASK 0xF0
380
381/* Flags to set Boolean values */
382#define SPOE_DATA_FL_FALSE 0x00
383#define SPOE_DATA_FL_TRUE 0x10
384
385
386#endif /* _TYPES_SPOE_H */