blob: 392cc94ef26c2e88c91509b4a0326f27f4613543 [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
180/* Describe a message that will be sent in a NOTIFY frame. A message has a name,
181 * an argument list (see above) and it is linked to a specific event. */
182struct spoe_message {
Christopher Faulet11610f32017-09-21 10:23:10 +0200183 char *id; /* SPOE message id */
184 unsigned int id_len; /* The message id length */
185 struct spoe_agent *agent; /* SPOE agent owning this SPOE message */
186 struct spoe_group *group; /* SPOE group owning this SPOE message (can be NULL) */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100187 struct {
Christopher Faulet11610f32017-09-21 10:23:10 +0200188 char *file; /* file where the SPOE message appears */
189 int line; /* line where the SPOE message appears */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100190 } conf; /* config information */
Christopher Faulet11610f32017-09-21 10:23:10 +0200191 unsigned int nargs; /* # of arguments */
192 struct list args; /* Arguments added when the SPOE messages is sent */
193 struct list list; /* Used to chain SPOE messages */
194 struct list by_evt; /* By event list */
195 struct list by_grp; /* By group list */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100196
Christopher Faulet11610f32017-09-21 10:23:10 +0200197 struct list acls; /* ACL declared on this message */
198 struct acl_cond *cond; /* acl condition to meet */
199 enum spoe_event event; /* SPOE_EV_* */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100200};
201
Christopher Faulet11610f32017-09-21 10:23:10 +0200202/* Describe a group of messages that will be sent in a NOTIFY frame. A group has
203 * a name and a list of messages. It can be used by HAProxy, outside events
204 * processing, mainly in (tcp|http) rules. */
205struct spoe_group {
206 char *id; /* SPOE group id */
207 struct spoe_agent *agent; /* SPOE agent owning this SPOE group */
208 struct {
209 char *file; /* file where the SPOE group appears */
210 int line; /* line where the SPOE group appears */
211 } conf; /* config information */
212
213 struct list phs; /* List of placeholders used during conf parsing */
214 struct list messages; /* List of SPOE messages that will be sent by this
215 * group */
216
217 struct list list; /* Used to chain SPOE groups */
218};
219
Christopher Faulet1f40b912017-02-17 09:32:19 +0100220/* Describe a SPOE agent. */
221struct spoe_agent {
222 char *id; /* SPOE agent id (name) */
223 struct {
224 char *file; /* file where the SPOE agent appears */
225 int line; /* line where the SPOE agent appears */
226 } conf; /* config information */
227 union {
228 struct proxy *be; /* Backend used by this agent */
229 char *name; /* Backend name used during conf parsing */
230 } b;
231 struct {
232 unsigned int hello; /* Max time to receive AGENT-HELLO frame (in SPOE applet) */
233 unsigned int idle; /* Max Idle timeout (in SPOE applet) */
234 unsigned int processing; /* Max time to process an event (in the main stream) */
235 } timeout;
236
237 /* Config info */
238 char *engine_id; /* engine-id string */
239 char *var_pfx; /* Prefix used for vars set by the agent */
240 char *var_on_error; /* Variable to set when an error occurred, in the TXN scope */
241 unsigned int flags; /* SPOE_FL_* */
242 unsigned int cps_max; /* Maximum # of connections per second */
243 unsigned int eps_max; /* Maximum # of errors per second */
244 unsigned int max_frame_size; /* Maximum frame size for this agent, before any negotiation */
245 unsigned int min_applets; /* Minimum # applets alive at a time */
246 unsigned int max_fpa; /* Maximum # of frames handled per applet at once */
247
Christopher Faulet11610f32017-09-21 10:23:10 +0200248 struct list events[SPOE_EV_EVENTS]; /* List of SPOE messages that will be sent
Christopher Faulet1f40b912017-02-17 09:32:19 +0100249 * for each supported events */
250
Christopher Faulet11610f32017-09-21 10:23:10 +0200251 struct list groups; /* List of available SPOE groups */
252
253 struct list messages; /* list of all messages attached to this SPOE agent */
254
Christopher Faulet1f40b912017-02-17 09:32:19 +0100255 /* running info */
Christopher Faulet24289f22017-09-25 14:48:02 +0200256 struct {
257 unsigned int frame_size; /* current maximum frame size, only used to encode messages */
258 unsigned int applets_act; /* # of applets alive at a time */
259 unsigned int applets_idle; /* # of applets in the state SPOE_APPCTX_ST_IDLE */
260 unsigned int sending_rate; /* the global sending rate */
261
262 struct freq_ctr conn_per_sec; /* connections per second */
263 struct freq_ctr err_per_sec; /* connetion errors per second */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100264
Christopher Faulet24289f22017-09-25 14:48:02 +0200265 struct list applets; /* List of available SPOE applets */
266 struct list sending_queue; /* Queue of streams waiting to send data */
267 struct list waiting_queue; /* Queue of streams waiting for a ack, in async mode */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100268 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200269 } *rt;
Christopher Faulet1f40b912017-02-17 09:32:19 +0100270
271};
272
273/* SPOE filter configuration */
274struct spoe_config {
Christopher Faulet7ee86672017-09-19 11:08:28 +0200275 char *id; /* The SPOE engine name. If undefined in HAProxy config,
276 * it will be set with the SPOE agent name */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100277 struct proxy *proxy; /* Proxy owning the filter */
278 struct spoe_agent *agent; /* Agent used by this filter */
279 struct proxy agent_fe; /* Agent frontend */
280};
281
282/* SPOE context attached to a stream. It is the main structure that handles the
283 * processing offload */
284struct spoe_context {
285 struct filter *filter; /* The SPOE filter */
286 struct stream *strm; /* The stream that should be offloaded */
287
Christopher Faulet11610f32017-09-21 10:23:10 +0200288 struct list *events; /* List of messages that will be sent during the stream processing */
Christopher Faulet76c09ef2017-09-21 11:03:52 +0200289 struct list *groups; /* List of available SPOE group */
Christopher Faulet11610f32017-09-21 10:23:10 +0200290
Christopher Faulet1f40b912017-02-17 09:32:19 +0100291 struct buffer *buffer; /* Buffer used to store a encoded messages */
292 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
293 struct list list;
294
295 enum spoe_ctx_state state; /* SPOE_CTX_ST_* */
296 unsigned int flags; /* SPOE_CTX_FL_* */
297 unsigned int status_code; /* SPOE_CTX_ERR_* */
298
299 unsigned int stream_id; /* stream_id and frame_id are used */
300 unsigned int frame_id; /* to map NOTIFY and ACK frames */
301 unsigned int process_exp; /* expiration date to process an event */
302
303 struct {
304 struct spoe_appctx *spoe_appctx; /* SPOE appctx sending the fragmented frame */
305 struct spoe_message *curmsg; /* SPOE message from which to resume encoding */
306 struct spoe_arg *curarg; /* SPOE arg in <curmsg> from which to resume encoding */
307 unsigned int curoff; /* offset in <curarg> from which to resume encoding */
308 unsigned int flags; /* SPOE_FRM_FL_* */
309 } frag_ctx; /* Info about fragmented frames, valid on if SPOE_CTX_FL_FRAGMENTED is set */
310};
311
312/* SPOE context inside a appctx */
313struct spoe_appctx {
314 struct appctx *owner; /* the owner */
315 struct task *task; /* task to handle applet timeouts */
316 struct spoe_agent *agent; /* agent on which the applet is attached */
317
318 unsigned int version; /* the negotiated version */
319 unsigned int max_frame_size; /* the negotiated max-frame-size value */
320 unsigned int flags; /* SPOE_APPCTX_FL_* */
321
322 unsigned int status_code; /* SPOE_FRM_ERR_* */
323#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
324 char *reason; /* Error message, used for debugging only */
325 int rlen; /* reason length */
326#endif
327
328 struct buffer *buffer; /* Buffer used to store a encoded messages */
329 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
330 struct list waiting_queue; /* list of streams waiting for a ACK frame, in sync and pipelining mode */
331 struct list list; /* next spoe appctx for the same agent */
332
333 struct {
334 struct spoe_context *ctx; /* SPOE context owning the fragmented frame */
335 unsigned int cursid; /* stream-id of the fragmented frame. used if the processing is aborted */
336 unsigned int curfid; /* frame-id of the fragmented frame. used if the processing is aborted */
337 } frag_ctx; /* Info about fragmented frames, unused for unfragmented frames */
338};
339
340/* Frame Types sent by HAProxy and by agents */
341enum spoe_frame_type {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100342 SPOE_FRM_T_UNSET = 0,
343
Christopher Faulet1f40b912017-02-17 09:32:19 +0100344 /* Frames sent by HAProxy */
345 SPOE_FRM_T_HAPROXY_HELLO = 1,
346 SPOE_FRM_T_HAPROXY_DISCON,
347 SPOE_FRM_T_HAPROXY_NOTIFY,
348
349 /* Frames sent by the agents */
350 SPOE_FRM_T_AGENT_HELLO = 101,
351 SPOE_FRM_T_AGENT_DISCON,
352 SPOE_FRM_T_AGENT_ACK
353};
354
355/* All supported data types */
356enum spoe_data_type {
357 SPOE_DATA_T_NULL = 0,
358 SPOE_DATA_T_BOOL,
359 SPOE_DATA_T_INT32,
360 SPOE_DATA_T_UINT32,
361 SPOE_DATA_T_INT64,
362 SPOE_DATA_T_UINT64,
363 SPOE_DATA_T_IPV4,
364 SPOE_DATA_T_IPV6,
365 SPOE_DATA_T_STR,
366 SPOE_DATA_T_BIN,
367 SPOE_DATA_TYPES
368};
369
370/* Masks to get data type or flags value */
371#define SPOE_DATA_T_MASK 0x0F
372#define SPOE_DATA_FL_MASK 0xF0
373
374/* Flags to set Boolean values */
375#define SPOE_DATA_FL_FALSE 0x00
376#define SPOE_DATA_FL_TRUE 0x10
377
378
379#endif /* _TYPES_SPOE_H */