blob: 2d94e36ecc4a8fc6ec99bd08976f65b625f389e1 [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>
27
28#include <types/filters.h>
29#include <types/freq_ctr.h>
30#include <types/proxy.h>
31#include <types/sample.h>
32#include <types/stream.h>
33#include <types/task.h>
34
35/* Flags set on the SPOE agent */
36#define SPOE_FL_CONT_ON_ERR 0x00000001 /* Do not stop events processing when an error occurred */
Christopher Faulet305c6072017-02-23 16:17:53 +010037#define SPOE_FL_PIPELINING 0x00000002 /* Set when SPOE agent supports pipelining (set by default) */
38#define SPOE_FL_ASYNC 0x00000004 /* Set when SPOE agent supports async (set by default) */
Christopher Fauletcecd8522017-02-24 22:11:21 +010039#define SPOE_FL_SND_FRAGMENTATION 0x00000008 /* Set when SPOE agent supports sending fragmented payload */
40#define SPOE_FL_RCV_FRAGMENTATION 0x00000010 /* Set when SPOE agent supports receiving fragmented payload */
Christopher Faulet1f40b912017-02-17 09:32:19 +010041
42/* Flags set on the SPOE context */
43#define SPOE_CTX_FL_CLI_CONNECTED 0x00000001 /* Set after that on-client-session event was processed */
44#define SPOE_CTX_FL_SRV_CONNECTED 0x00000002 /* Set after that on-server-session event was processed */
45#define SPOE_CTX_FL_REQ_PROCESS 0x00000004 /* Set when SPOE is processing the request */
46#define SPOE_CTX_FL_RSP_PROCESS 0x00000008 /* Set when SPOE is processing the response */
47#define SPOE_CTX_FL_FRAGMENTED 0x00000010 /* Set when a fragmented frame is processing */
48
49#define SPOE_CTX_FL_PROCESS (SPOE_CTX_FL_REQ_PROCESS|SPOE_CTX_FL_RSP_PROCESS)
50
51/* Flags set on the SPOE applet */
52#define SPOE_APPCTX_FL_PIPELINING 0x00000001 /* Set if pipelining is supported */
53#define SPOE_APPCTX_FL_ASYNC 0x00000002 /* Set if asynchronus frames is supported */
54#define SPOE_APPCTX_FL_FRAGMENTATION 0x00000004 /* Set if fragmentation is supported */
55#define SPOE_APPCTX_FL_PERSIST 0x00000008 /* Set if the applet is persistent */
56
57#define SPOE_APPCTX_ERR_NONE 0x00000000 /* no error yet, leave it to zero */
58#define SPOE_APPCTX_ERR_TOUT 0x00000001 /* SPOE applet timeout */
59
60/* Flags set on the SPOE frame */
61#define SPOE_FRM_FL_FIN 0x00000001
62#define SPOE_FRM_FL_ABRT 0x00000002
63
64/* All possible states for a SPOE context */
65enum spoe_ctx_state {
66 SPOE_CTX_ST_NONE = 0,
67 SPOE_CTX_ST_READY,
68 SPOE_CTX_ST_ENCODING_MSGS,
69 SPOE_CTX_ST_SENDING_MSGS,
70 SPOE_CTX_ST_WAITING_ACK,
71 SPOE_CTX_ST_DONE,
72 SPOE_CTX_ST_ERROR,
73};
74
75/* All possible states for a SPOE applet */
76enum spoe_appctx_state {
77 SPOE_APPCTX_ST_CONNECT = 0,
78 SPOE_APPCTX_ST_CONNECTING,
79 SPOE_APPCTX_ST_IDLE,
80 SPOE_APPCTX_ST_PROCESSING,
81 SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY,
82 SPOE_APPCTX_ST_WAITING_SYNC_ACK,
83 SPOE_APPCTX_ST_DISCONNECT,
84 SPOE_APPCTX_ST_DISCONNECTING,
85 SPOE_APPCTX_ST_EXIT,
86 SPOE_APPCTX_ST_END,
87};
88
89/* All supported SPOE actions */
90enum spoe_action_type {
91 SPOE_ACT_T_SET_VAR = 1,
92 SPOE_ACT_T_UNSET_VAR,
93 SPOE_ACT_TYPES,
94};
95
96/* All supported SPOE events */
97enum spoe_event {
98 SPOE_EV_NONE = 0,
99
100 /* Request events */
101 SPOE_EV_ON_CLIENT_SESS = 1,
102 SPOE_EV_ON_TCP_REQ_FE,
103 SPOE_EV_ON_TCP_REQ_BE,
104 SPOE_EV_ON_HTTP_REQ_FE,
105 SPOE_EV_ON_HTTP_REQ_BE,
106
107 /* Response events */
108 SPOE_EV_ON_SERVER_SESS,
109 SPOE_EV_ON_TCP_RSP,
110 SPOE_EV_ON_HTTP_RSP,
111
112 SPOE_EV_EVENTS
113};
114
115/* Errors triggered by streams */
116enum spoe_context_error {
117 SPOE_CTX_ERR_NONE = 0,
118 SPOE_CTX_ERR_TOUT,
119 SPOE_CTX_ERR_RES,
120 SPOE_CTX_ERR_TOO_BIG,
121 SPOE_CTX_ERR_FRAG_FRAME_ABRT,
122 SPOE_CTX_ERR_UNKNOWN = 255,
123 SPOE_CTX_ERRS,
124};
125
126/* Errors triggerd by SPOE applet */
127enum spoe_frame_error {
128 SPOE_FRM_ERR_NONE = 0,
129 SPOE_FRM_ERR_IO,
130 SPOE_FRM_ERR_TOUT,
131 SPOE_FRM_ERR_TOO_BIG,
132 SPOE_FRM_ERR_INVALID,
133 SPOE_FRM_ERR_NO_VSN,
134 SPOE_FRM_ERR_NO_FRAME_SIZE,
135 SPOE_FRM_ERR_NO_CAP,
136 SPOE_FRM_ERR_BAD_VSN,
137 SPOE_FRM_ERR_BAD_FRAME_SIZE,
138 SPOE_FRM_ERR_FRAG_NOT_SUPPORTED,
139 SPOE_FRM_ERR_INTERLACED_FRAMES,
140 SPOE_FRM_ERR_FRAMEID_NOTFOUND,
141 SPOE_FRM_ERR_RES,
142 SPOE_FRM_ERR_UNKNOWN = 99,
143 SPOE_FRM_ERRS,
144};
145
146/* Scopes used for variables set by agents. It is a way to be agnotic to vars
147 * scope. */
148enum spoe_vars_scope {
149 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
150 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
151 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
152 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
153 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
154};
155
156
157/* Describe an argument that will be linked to a message. It is a sample fetch,
158 * with an optional name. */
159struct spoe_arg {
160 char *name; /* Name of the argument, may be NULL */
161 unsigned int name_len; /* The name length, 0 if NULL */
162 struct sample_expr *expr; /* Sample expression */
163 struct list list; /* Used to chain SPOE args */
164};
165
166/* Used during the config parsing only because, when a SPOE agent section is
167 * parsed, messages can be undefined. */
168struct spoe_msg_placeholder {
169 char *id; /* SPOE message placeholder id */
170 struct list list; /* Use to chain SPOE message placeholders */
171};
172
173/* Describe a message that will be sent in a NOTIFY frame. A message has a name,
174 * an argument list (see above) and it is linked to a specific event. */
175struct spoe_message {
176 char *id; /* SPOE message id */
177 unsigned int id_len; /* The message id length */
178 struct spoe_agent *agent; /* SPOE agent owning this SPOE message */
179 struct {
180 char *file; /* file where the SPOE message appears */
181 int line; /* line where the SPOE message appears */
182 } conf; /* config information */
183 unsigned int nargs; /* # of arguments */
184 struct list args; /* Arguments added when the SPOE messages is sent */
185 struct list list; /* Used to chain SPOE messages */
186
Christopher Faulet57583e42017-09-04 15:41:09 +0200187 struct list acls; /* ACL declared on this message */
188 struct acl_cond *cond; /* acl condition to meet */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100189 enum spoe_event event; /* SPOE_EV_* */
190};
191
192/* Describe a SPOE agent. */
193struct spoe_agent {
194 char *id; /* SPOE agent id (name) */
195 struct {
196 char *file; /* file where the SPOE agent appears */
197 int line; /* line where the SPOE agent appears */
198 } conf; /* config information */
199 union {
200 struct proxy *be; /* Backend used by this agent */
201 char *name; /* Backend name used during conf parsing */
202 } b;
203 struct {
204 unsigned int hello; /* Max time to receive AGENT-HELLO frame (in SPOE applet) */
205 unsigned int idle; /* Max Idle timeout (in SPOE applet) */
206 unsigned int processing; /* Max time to process an event (in the main stream) */
207 } timeout;
208
209 /* Config info */
210 char *engine_id; /* engine-id string */
211 char *var_pfx; /* Prefix used for vars set by the agent */
212 char *var_on_error; /* Variable to set when an error occurred, in the TXN scope */
213 unsigned int flags; /* SPOE_FL_* */
214 unsigned int cps_max; /* Maximum # of connections per second */
215 unsigned int eps_max; /* Maximum # of errors per second */
216 unsigned int max_frame_size; /* Maximum frame size for this agent, before any negotiation */
217 unsigned int min_applets; /* Minimum # applets alive at a time */
218 unsigned int max_fpa; /* Maximum # of frames handled per applet at once */
219
220 struct list messages[SPOE_EV_EVENTS]; /* List of SPOE messages that will be sent
221 * for each supported events */
222
223 /* running info */
224 unsigned int frame_size; /* current maximum frame size, only used to encode messages */
225 unsigned int applets_act; /* # of applets alive at a time */
226 unsigned int applets_idle; /* # of applets in the state SPOE_APPCTX_ST_IDLE */
227 unsigned int sending_rate; /* the global sending rate */
228
229 struct freq_ctr conn_per_sec; /* connections per second */
230 struct freq_ctr err_per_sec; /* connetion errors per second */
231
232 struct list applets; /* List of available SPOE applets */
233 struct list sending_queue; /* Queue of streams waiting to send data */
234 struct list waiting_queue; /* Queue of streams waiting for a ack, in async mode */
235
236};
237
238/* SPOE filter configuration */
239struct spoe_config {
Christopher Faulet7ee86672017-09-19 11:08:28 +0200240 char *id; /* The SPOE engine name. If undefined in HAProxy config,
241 * it will be set with the SPOE agent name */
Christopher Faulet1f40b912017-02-17 09:32:19 +0100242 struct proxy *proxy; /* Proxy owning the filter */
243 struct spoe_agent *agent; /* Agent used by this filter */
244 struct proxy agent_fe; /* Agent frontend */
245};
246
247/* SPOE context attached to a stream. It is the main structure that handles the
248 * processing offload */
249struct spoe_context {
250 struct filter *filter; /* The SPOE filter */
251 struct stream *strm; /* The stream that should be offloaded */
252
253 struct list *messages; /* List of messages that will be sent during the stream processing */
254 struct buffer *buffer; /* Buffer used to store a encoded messages */
255 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
256 struct list list;
257
258 enum spoe_ctx_state state; /* SPOE_CTX_ST_* */
259 unsigned int flags; /* SPOE_CTX_FL_* */
260 unsigned int status_code; /* SPOE_CTX_ERR_* */
261
262 unsigned int stream_id; /* stream_id and frame_id are used */
263 unsigned int frame_id; /* to map NOTIFY and ACK frames */
264 unsigned int process_exp; /* expiration date to process an event */
265
266 struct {
267 struct spoe_appctx *spoe_appctx; /* SPOE appctx sending the fragmented frame */
268 struct spoe_message *curmsg; /* SPOE message from which to resume encoding */
269 struct spoe_arg *curarg; /* SPOE arg in <curmsg> from which to resume encoding */
270 unsigned int curoff; /* offset in <curarg> from which to resume encoding */
271 unsigned int flags; /* SPOE_FRM_FL_* */
272 } frag_ctx; /* Info about fragmented frames, valid on if SPOE_CTX_FL_FRAGMENTED is set */
273};
274
275/* SPOE context inside a appctx */
276struct spoe_appctx {
277 struct appctx *owner; /* the owner */
278 struct task *task; /* task to handle applet timeouts */
279 struct spoe_agent *agent; /* agent on which the applet is attached */
280
281 unsigned int version; /* the negotiated version */
282 unsigned int max_frame_size; /* the negotiated max-frame-size value */
283 unsigned int flags; /* SPOE_APPCTX_FL_* */
284
285 unsigned int status_code; /* SPOE_FRM_ERR_* */
286#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
287 char *reason; /* Error message, used for debugging only */
288 int rlen; /* reason length */
289#endif
290
291 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 waiting_queue; /* list of streams waiting for a ACK frame, in sync and pipelining mode */
294 struct list list; /* next spoe appctx for the same agent */
295
296 struct {
297 struct spoe_context *ctx; /* SPOE context owning the fragmented frame */
298 unsigned int cursid; /* stream-id of the fragmented frame. used if the processing is aborted */
299 unsigned int curfid; /* frame-id of the fragmented frame. used if the processing is aborted */
300 } frag_ctx; /* Info about fragmented frames, unused for unfragmented frames */
301};
302
303/* Frame Types sent by HAProxy and by agents */
304enum spoe_frame_type {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100305 SPOE_FRM_T_UNSET = 0,
306
Christopher Faulet1f40b912017-02-17 09:32:19 +0100307 /* Frames sent by HAProxy */
308 SPOE_FRM_T_HAPROXY_HELLO = 1,
309 SPOE_FRM_T_HAPROXY_DISCON,
310 SPOE_FRM_T_HAPROXY_NOTIFY,
311
312 /* Frames sent by the agents */
313 SPOE_FRM_T_AGENT_HELLO = 101,
314 SPOE_FRM_T_AGENT_DISCON,
315 SPOE_FRM_T_AGENT_ACK
316};
317
318/* All supported data types */
319enum spoe_data_type {
320 SPOE_DATA_T_NULL = 0,
321 SPOE_DATA_T_BOOL,
322 SPOE_DATA_T_INT32,
323 SPOE_DATA_T_UINT32,
324 SPOE_DATA_T_INT64,
325 SPOE_DATA_T_UINT64,
326 SPOE_DATA_T_IPV4,
327 SPOE_DATA_T_IPV6,
328 SPOE_DATA_T_STR,
329 SPOE_DATA_T_BIN,
330 SPOE_DATA_TYPES
331};
332
333/* Masks to get data type or flags value */
334#define SPOE_DATA_T_MASK 0x0F
335#define SPOE_DATA_FL_MASK 0xF0
336
337/* Flags to set Boolean values */
338#define SPOE_DATA_FL_FALSE 0x00
339#define SPOE_DATA_FL_TRUE 0x10
340
341
342#endif /* _TYPES_SPOE_H */