blob: dbc9d30755bf79eb998d0755306c42a0071db062 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
21
22#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020023#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010024#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025
Christopher Faulet57583e42017-09-04 15:41:09 +020026#include <proto/acl.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020027#include <proto/arg.h>
28#include <proto/backend.h>
29#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010030#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020031#include <proto/frontend.h>
32#include <proto/log.h>
33#include <proto/proto_http.h>
34#include <proto/proxy.h>
35#include <proto/sample.h>
36#include <proto/session.h>
37#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010038#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020039#include <proto/stream.h>
40#include <proto/stream_interface.h>
41#include <proto/task.h>
42#include <proto/vars.h>
43
44#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
45#define SPOE_PRINTF(x...) fprintf(x)
46#else
47#define SPOE_PRINTF(x...)
48#endif
49
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010050/* Reserved 4 bytes to the frame size. So a frame and its size can be written
51 * together in a buffer */
52#define MAX_FRAME_SIZE global.tune.bufsize - 4
53
54/* The minimum size for a frame */
55#define MIN_FRAME_SIZE 256
56
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010057/* Reserved for the metadata and the frame type.
58 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010059#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020060
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010061/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010062#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
63
Christopher Faulet3b386a32017-02-23 10:17:15 +010064/* SPOE filter id. Used to identify SPOE filters */
65const char *spoe_filter_id = "SPOE filter";
66
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067/* Set if the handle on SIGUSR1 is registered */
68static int sighandler_registered = 0;
69
70/* proxy used during the parsing */
71struct proxy *curproxy = NULL;
72
73/* The name of the SPOE engine, used during the parsing */
74char *curengine = NULL;
75
76/* SPOE agent used during the parsing */
77struct spoe_agent *curagent = NULL;
78
79/* SPOE message used during the parsing */
80struct spoe_message *curmsg = NULL;
81
82/* list of SPOE messages and placeholders used during the parsing */
83struct list curmsgs;
84struct list curmps;
85
Christopher Faulet42bfa462017-01-04 14:14:19 +010086/* Pools used to allocate SPOE structs */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020087static struct pool_head *pool2_spoe_ctx = NULL;
Christopher Faulet42bfa462017-01-04 14:14:19 +010088static struct pool_head *pool2_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020089
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020090struct flt_ops spoe_ops;
91
Christopher Faulet8ef75252017-02-20 22:56:03 +010092static int spoe_queue_context(struct spoe_context *ctx);
93static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
94static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
96/********************************************************************
97 * helper functions/globals
98 ********************************************************************/
99static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100100spoe_release_msg_placeholder(struct spoe_msg_placeholder *mp)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101{
102 if (!mp)
103 return;
104 free(mp->id);
105 free(mp);
106}
107
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200108static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100109spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110{
Christopher Faulet57583e42017-09-04 15:41:09 +0200111 struct spoe_arg *arg, *argback;
112 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113
114 if (!msg)
115 return;
116 free(msg->id);
117 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200118 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119 release_sample_expr(arg->expr);
120 free(arg->name);
121 LIST_DEL(&arg->list);
122 free(arg);
123 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200124 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
125 LIST_DEL(&acl->list);
126 prune_acl(acl);
127 free(acl);
128 }
129 if (msg->cond) {
130 prune_acl_cond(msg->cond);
131 free(msg->cond);
132 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200133 free(msg);
134}
135
136static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100137spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200138{
139 struct spoe_message *msg, *back;
140 int i;
141
142 if (!agent)
143 return;
144 free(agent->id);
145 free(agent->conf.file);
146 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100147 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100148 free(agent->var_on_error);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200149 for (i = 0; i < SPOE_EV_EVENTS; ++i) {
150 list_for_each_entry_safe(msg, back, &agent->messages[i], list) {
151 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100152 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200153 }
154 }
155 free(agent);
156}
157
158static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100159 [SPOE_FRM_ERR_NONE] = "normal",
160 [SPOE_FRM_ERR_IO] = "I/O error",
161 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
162 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
163 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
164 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
165 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
166 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
167 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
168 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
169 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
170 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100171 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100172 [SPOE_FRM_ERR_RES] = "resource allocation error",
173 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200174};
175
176static const char *spoe_event_str[SPOE_EV_EVENTS] = {
177 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
178 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
179 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
180 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
181 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
182
183 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
184 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
185 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
186};
187
188
189#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
190
191static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_CTX_ST_NONE] = "NONE",
193 [SPOE_CTX_ST_READY] = "READY",
194 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
195 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
196 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
197 [SPOE_CTX_ST_DONE] = "DONE",
198 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200199};
200
201static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100202 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
203 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
204 [SPOE_APPCTX_ST_IDLE] = "IDLE",
205 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
206 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
207 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
208 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
209 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
210 [SPOE_APPCTX_ST_EXIT] = "EXIT",
211 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200212};
213
214#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100215
Christopher Faulet8ef75252017-02-20 22:56:03 +0100216/* Used to generates a unique id for an engine. On success, it returns a
217 * allocated string. So it is the caller's reponsibility to release it. If the
218 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100219static char *
220generate_pseudo_uuid()
221{
222 static int init = 0;
223
224 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
225 const char uuid_chr[] = "0123456789ABCDEF-";
226 char *uuid;
227 int i;
228
229 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
230 return NULL;
231
232 if (!init) {
233 srand(now_ms);
234 init = 1;
235 }
236
237 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
238 int r = rand () % 16;
239
240 switch (uuid_fmt[i]) {
241 case 'x' : uuid[i] = uuid_chr[r]; break;
242 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
243 default : uuid[i] = uuid_fmt[i]; break;
244 }
245 }
246 return uuid;
247}
248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Returns the minimum number of appets alive at a time. This function is used
250 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100251static inline unsigned int
252min_applets_act(struct spoe_agent *agent)
253{
254 unsigned int nbsrv;
255
Christopher Faulet8ef75252017-02-20 22:56:03 +0100256 /* TODO: Add a config parameter to customize this value. Always 0 for
257 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100258 if (agent->min_applets)
259 return agent->min_applets;
260
Christopher Faulet8ef75252017-02-20 22:56:03 +0100261 /* Get the number of active servers for the backend */
262 nbsrv = (agent->b.be->srv_act
263 ? agent->b.be->srv_act
264 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100265 return 2*nbsrv;
266}
267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200268/********************************************************************
269 * Functions that encode/decode SPOE frames
270 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/* Helper to get static string length, excluding the terminating null byte */
272#define SLEN(str) (sizeof(str)-1)
273
274/* Predefined key used in HELLO/DISCONNECT frames */
275#define SUPPORTED_VERSIONS_KEY "supported-versions"
276#define VERSION_KEY "version"
277#define MAX_FRAME_SIZE_KEY "max-frame-size"
278#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100280#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281#define STATUS_CODE_KEY "status-code"
282#define MSG_KEY "message"
283
284struct spoe_version {
285 char *str;
286 int min;
287 int max;
288};
289
290/* All supported versions */
291static struct spoe_version supported_versions[] = {
292 {"1.0", 1000, 1000},
293 {NULL, 0, 0}
294};
295
296/* Comma-separated list of supported versions */
297#define SUPPORTED_VERSIONS_VAL "1.0"
298
Christopher Faulet8ef75252017-02-20 22:56:03 +0100299/* Convert a string to a SPOE version value. The string must follow the format
300 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
301 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200302static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100303spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305 const char *p, *end;
306 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 p = str;
309 end = str+len;
310 maj = min = 0;
311 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200312
Christopher Faulet8ef75252017-02-20 22:56:03 +0100313 /* skip leading spaces */
314 while (p < end && isspace(*p))
315 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200316
Christopher Faulet8ef75252017-02-20 22:56:03 +0100317 /* parse Major number, until the '.' */
318 while (*p != '.') {
319 if (p >= end || *p < '0' || *p > '9')
320 goto out;
321 maj *= 10;
322 maj += (*p - '0');
323 p++;
324 }
325
326 /* check Major version */
327 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328 goto out;
329
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 p++; /* skip the '.' */
331 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
332 goto out;
333
334 /* Parse Minor number */
335 while (p < end) {
336 if (*p < '0' || *p > '9')
337 break;
338 min *= 10;
339 min += (*p - '0');
340 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342
343 /* check Minor number */
344 if (min > 999)
345 goto out;
346
347 /* skip trailing spaces */
348 while (p < end && isspace(*p))
349 p++;
350 if (p != end)
351 goto out;
352
353 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200354 out:
355 return vsn;
356}
357
Christopher Faulet8ef75252017-02-20 22:56:03 +0100358/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
359 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
360 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200361static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200363{
Christopher Faulet305c6072017-02-23 16:17:53 +0100364 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100365 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 char *p, *end;
367 unsigned int flags = SPOE_FRM_FL_FIN;
368 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370 p = frame;
371 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 /* Set Frame type */
374 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100376 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100377 memcpy(p, (char *)&flags, 4);
378 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379
380 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100381 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382
383 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
384 * and "capabilities" */
385
386 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387 sz = SLEN(SUPPORTED_VERSIONS_KEY);
388 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
389 goto too_big;
390
391 *p++ = SPOE_DATA_T_STR;
392 sz = SLEN(SUPPORTED_VERSIONS_VAL);
393 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
394 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395
396 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100397 sz = SLEN(MAX_FRAME_SIZE_KEY);
398 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
399 goto too_big;
400
401 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200402 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200404
405 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 sz = SLEN(CAPABILITIES_KEY);
407 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
408 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100411 chk = get_trash_chunk();
412 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
413 memcpy(chk->str, "pipelining", 10);
414 chk->len += 10;
415 }
416 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
417 if (chk->len) chk->str[chk->len++] = ',';
418 memcpy(chk->str+chk->len, "async", 5);
419 chk->len += 5;
420 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100421 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
422 if (chk->len) chk->str[chk->len++] = ',';
423 memcpy(chk->str+chk->len, "fragmentation", 13);
424 chk->len += 5;
425 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100426 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100427 goto too_big;
428
429 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100430 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 sz = SLEN(ENGINE_ID_KEY);
432 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
433 goto too_big;
434
435 *p++ = SPOE_DATA_T_STR;
436 sz = strlen(agent->engine_id);
437 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
438 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100439 }
440
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 return (p - frame);
442
443 too_big:
444 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
445 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200446}
447
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
449 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
450 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200451static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100454 const char *reason;
455 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100456 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 p = frame;
460 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200461
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 /* Set Frame type */
463 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200464
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100465 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 memcpy(p, (char *)&flags, 4);
467 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
469 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470 *p++ = 0; *p++ = 0;
471
472 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
473 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
475 /* There are 2 mandatory items: "status-code" and "message" */
476
477 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 sz = SLEN(STATUS_CODE_KEY);
479 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
480 goto too_big;
481
482 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200483 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200485
486 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 sz = SLEN(MSG_KEY);
488 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
489 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 /*Get the message corresponding to the status code */
492 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
493
494 *p++ = SPOE_DATA_T_STR;
495 sz = strlen(reason);
496 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
497 goto too_big;
498
499 return (p - frame);
500
501 too_big:
502 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
503 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200504}
505
Christopher Faulet8ef75252017-02-20 22:56:03 +0100506/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
507 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
508 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100510spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100511 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513 char *p, *end;
514 unsigned int stream_id, frame_id;
515 unsigned int flags = SPOE_FRM_FL_FIN;
516 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 p = frame;
519 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100521 stream_id = ctx->stream_id;
522 frame_id = ctx->frame_id;
523
524 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
525 /* The fragmentation is not supported by the applet */
526 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
527 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
528 return -1;
529 }
530 flags = ctx->frag_ctx.flags;
531 }
532
533 /* Set Frame type */
534 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
535
536 /* Set flags */
537 memcpy(p, (char *)&flags, 4);
538 p += 4;
539
540 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200541 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100542 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200543 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100544 goto too_big;
545
546 /* Copy encoded messages, if possible */
547 sz = ctx->buffer->i;
548 if (p + sz >= end)
549 goto too_big;
550 memcpy(p, ctx->buffer->p, sz);
551 p += sz;
552
553 return (p - frame);
554
555 too_big:
556 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
557 return 0;
558}
559
560/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
561 * returns the number of encoded bytes in the frame on success, 0 if an encoding
562 * error occurred and -1 if a fatal error occurred. */
563static int
564spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
565 char *frame, size_t size)
566{
567 char *p, *end;
568 unsigned int stream_id, frame_id;
569 unsigned int flags;
570 size_t sz;
571
572 p = frame;
573 end = frame+size;
574
Christopher Faulet8ef75252017-02-20 22:56:03 +0100575 /* <ctx> is null when the stream has aborted the processing of a
576 * fragmented frame. In this case, we must notify the corresponding
577 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100578 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100579 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100580 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
581 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
582 }
583 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100584 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100585 stream_id = ctx->stream_id;
586 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100587 }
588
Christopher Faulet8ef75252017-02-20 22:56:03 +0100589 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100590 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100591
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100592 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100593 memcpy(p, (char *)&flags, 4);
594 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200595
596 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200597 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200599 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200601
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100602 if (ctx == NULL)
603 goto end;
604
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100606 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 if (p + sz >= end)
608 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100611
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613 return (p - frame);
614
615 too_big:
616 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
617 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200618}
619
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620/* Decode and process the HELLO frame sent by an agent. It returns the number of
621 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
622 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200623static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200625{
Christopher Faulet305c6072017-02-23 16:17:53 +0100626 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
627 char *p, *end;
628 int vsn, max_frame_size;
629 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630
631 p = frame;
632 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633
634 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
636 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200637 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 if (size < 7 /* TYPE + METADATA */) {
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
642 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643 }
644
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100645 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646 memcpy((char *)&flags, p, 4);
647 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100649 /* Fragmentation is not supported for HELLO frame */
650 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100652 return -1;
653 }
654
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 if (*p != 0 || *(p+1) != 0) {
657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
658 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661
662 /* There are 3 mandatory items: "version", "max-frame-size" and
663 * "capabilities" */
664
665 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100666 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 while (p < end) {
668 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200669 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671
672 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 ret = spoe_decode_buffer(&p, end, &str, &sz);
674 if (ret == -1 || !sz) {
675 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
676 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200679 /* Check "version" K/V item */
680 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 int i, type = *p++;
682
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
685 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
686 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
689 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
690 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691 }
692
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696 return -1;
697 }
698 for (i = 0; supported_versions[i].str != NULL; ++i) {
699 if (vsn >= supported_versions[i].min &&
700 vsn <= supported_versions[i].max)
701 break;
702 }
703 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 return -1;
706 }
707 }
708 /* Check "max-frame-size" K/V item */
709 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100710 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200711
712 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
714 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
715 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
716 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
718 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200720 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100721 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
722 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200723 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100724 if (sz < MIN_FRAME_SIZE ||
725 sz > SPOE_APPCTX(appctx)->max_frame_size) {
726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 return -1;
728 }
729 max_frame_size = sz;
730 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100731 /* Check "capabilities" K/V item */
732 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100734
735 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100736 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
738 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
741 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
742 return 0;
743 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100744
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100746 char *delim;
747
748 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
752 str += 10; sz -= 10;
753 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100754 flags |= SPOE_APPCTX_FL_PIPELINING;
755 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 else if (sz >= 5 && !strncmp(str, "async", 5)) {
757 str += 5; sz -= 5;
758 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759 flags |= SPOE_APPCTX_FL_ASYNC;
760 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
762 str += 13; sz -= 13;
763 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100764 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
765 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 /* Get the next comma or break */
768 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 delim++;
771 sz -= (delim - str);
772 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100773 }
774 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200775 else {
776 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 if (spoe_skip_data(&p, end) == -1) {
778 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
779 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200780 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200781 }
782 }
783
784 /* Final checks */
785 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200787 return -1;
788 }
789 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100790 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 return -1;
792 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100793 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
794 flags &= ~SPOE_APPCTX_FL_PIPELINING;
795 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
796 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797
Christopher Faulet42bfa462017-01-04 14:14:19 +0100798 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
799 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
800 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100801
802 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200803}
804
805/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
806 * bytes on success, 0 if the frame can be ignored and -1 if an error
807 * occurred. */
808static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100809spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100812 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100813
814 p = frame;
815 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816
817 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
819 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 if (size < 7 /* TYPE + METADATA */) {
824 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
825 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 }
827
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100828 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829 memcpy((char *)&flags, p, 4);
830 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100832 /* Fragmentation is not supported for DISCONNECT frame */
833 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100835 return -1;
836 }
837
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 if (*p != 0 || *(p+1) != 0) {
840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
841 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200844
845 /* There are 2 mandatory items: "status-code" and "message" */
846
847 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 while (p < end) {
849 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200850 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
853 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 ret = spoe_decode_buffer(&p, end, &str, &sz);
855 if (ret == -1 || !sz) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
859
860 /* Check "status-code" K/V item */
861 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863
864 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
866 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
867 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
868 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
870 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200872 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
874 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877 }
878
879 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 else if (!memcmp(str, MSG_KEY, sz)) {
881 int type = *p++;
882
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100888 ret = spoe_decode_buffer(&p, end, &str, &sz);
889 if (ret == -1 || sz > 255) {
890 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
891 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100893#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
894 SPOE_APPCTX(appctx)->reason = str;
895 SPOE_APPCTX(appctx)->rlen = sz;
896#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 }
898 else {
899 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if (spoe_skip_data(&p, end) == -1) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200904 }
905 }
906
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908}
909
910
Christopher Fauleta1cda022016-12-21 08:58:06 +0100911/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200912 * success, 0 if the frame can be ignored and -1 if an error occurred. */
913static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100914spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100915 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100917 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100919 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100921 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100922
923 p = frame;
924 end = frame + size;
925 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926
927 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
929 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 if (size < 7 /* TYPE + METADATA */) {
934 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
935 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200936 }
937
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100938 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 memcpy((char *)&flags, p, 4);
940 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100942 /* Fragmentation is not supported for now */
943 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100945 return -1;
946 }
947
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200949 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100951 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200953 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100957
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100959 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100960 list_for_each_entry((*ctx), &agent->waiting_queue, list) {
961 if ((*ctx)->stream_id == (unsigned int)stream_id &&
962 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100963 goto found;
964 }
965 }
966 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100967 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
968 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100970 goto found;
971 }
972 }
973
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100974 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
975 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
976 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
977
978 /* ABRT bit is set for an unfinished fragmented frame */
979 if (flags & SPOE_FRM_FL_ABRT) {
980 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
981 (*ctx)->frag_ctx.spoe_appctx = NULL;
982 (*ctx)->state = SPOE_CTX_ST_ERROR;
983 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
984 /* Ignore the payload */
985 goto end;
986 }
987 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
988 /* For now, we ignore the ack */
989 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
990 return 0;
991 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100992
Christopher Fauleta1cda022016-12-21 08:58:06 +0100993 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100994 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
995 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100996 " - stream-id=%u - frame-id=%u\n",
997 (int)now.tv_sec, (int)now.tv_usec, agent->id,
998 __FUNCTION__, appctx,
999 (unsigned int)stream_id, (unsigned int)frame_id);
1000
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001001 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001002 return 0;
1003
1004 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001005 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1006 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001007 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001008 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001009 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001010
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001011 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001012 len = (end - p);
1013 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1014 SPOE_APPCTX(appctx)->buffer->i = len;
1015 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001016
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001017 /* Transfer the buffer ownership to the SPOE context */
1018 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1019 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001020
Christopher Faulet8ef75252017-02-20 22:56:03 +01001021 (*ctx)->state = SPOE_CTX_ST_DONE;
1022
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001023 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001024 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001025 " - ACK frame received"
1026 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001027 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1029 (*ctx)->frame_id, flags);
1030 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001031}
1032
Christopher Fauletba7bc162016-11-07 21:07:38 +01001033/* This function is used in cfgparse.c and declared in proto/checks.h. It
1034 * prepare the request to send to agents during a healthcheck. It returns 0 on
1035 * success and -1 if an error occurred. */
1036int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001038{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 struct appctx appctx;
1040 struct spoe_appctx spoe_appctx;
1041 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1042 size_t sz;
1043 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001044
Christopher Faulet42bfa462017-01-04 14:14:19 +01001045 memset(&appctx, 0, sizeof(appctx));
1046 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001047 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001048
1049 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001050 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001051
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1053 end = frame + MAX_FRAME_SIZE;
1054
1055 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1056 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001058 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 /* Add "healthcheck" K/V item */
1061 sz = SLEN(HEALTHCHECK_KEY);
1062 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1063 return -1;
1064 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065
Christopher Faulet8ef75252017-02-20 22:56:03 +01001066 *len = frame - buf;
1067 sz = htonl(*len - 4);
1068 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001072 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073 return 0;
1074}
1075
1076/* This function is used in checks.c and declared in proto/checks.h. It decode
1077 * the response received from an agent during a healthcheck. It returns 0 on
1078 * success and -1 if an error occurred. */
1079int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001082 struct appctx appctx;
1083 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084
Christopher Faulet42bfa462017-01-04 14:14:19 +01001085 memset(&appctx, 0, sizeof(appctx));
1086 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087
Christopher Faulet42bfa462017-01-04 14:14:19 +01001088 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001089 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1092 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 goto error;
1094 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1096 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097
1098 return 0;
1099
1100 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1102 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1103 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104 return -1;
1105}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001106
Christopher Fauleta1cda022016-12-21 08:58:06 +01001107/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1108 * the frame can be ignored, 1 to retry later, and the frame legnth on
1109 * success. */
1110static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001112{
1113 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114 int ret;
1115 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001116
1117 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001119
Christopher Faulet8ef75252017-02-20 22:56:03 +01001120 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1121 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001122 netint = htonl(framesz);
1123 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001124 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001125
1126 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127 if (ret == -1) {
1128 retry:
1129 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001130 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001131 }
1132 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133 return -1; /* error */
1134 }
1135 return framesz;
1136}
1137
1138/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1139 * when the frame can be ignored, 1 to retry later and the frame length on
1140 * success. */
1141static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143{
1144 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001145 int ret;
1146 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147
1148 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001149 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150
Willy Tarreau06d80a92017-10-19 14:32:15 +02001151 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 if (ret > 0) {
1153 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001154 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001155 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 return -1;
1157 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001158 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 }
1160 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161 if (ret == 0) {
1162 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 }
1165 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166 return -1; /* error */
1167 }
1168 return framesz;
1169}
1170
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001171/********************************************************************
1172 * Functions that manage the SPOE applet
1173 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001174static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001176{
1177 si_applet_want_get(appctx->owner);
1178 si_applet_want_put(appctx->owner);
1179 appctx_wakeup(appctx);
1180 return 1;
1181}
1182
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001183/* Callback function that catches applet timeouts. If a timeout occurred, we set
1184 * <appctx->st1> flag and the SPOE applet is woken up. */
1185static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001187{
1188 struct appctx *appctx = task->context;
1189
1190 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1191 if (tick_is_expired(task->expire, now_ms)) {
1192 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001193 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001194 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001195 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001196 return task;
1197}
1198
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199/* Callback function that releases a SPOE applet. This happens when the
1200 * connection with the agent is closed. */
1201static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001202spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001203{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001204 struct stream_interface *si = appctx->owner;
1205 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1206 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001207 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208
1209 if (spoe_appctx == NULL)
1210 return;
1211
1212 appctx->ctx.spoe.ptr = NULL;
1213 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001214
1215 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1216 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1217 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219 /* Remove applet from the list of running applets */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220 agent->applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1222 LIST_DEL(&spoe_appctx->list);
1223 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001224 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001227 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001228 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
1229 agent->applets_idle--;
1230
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001231 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001232 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1233 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001234
1235 si_shutw(si);
1236 si_shutr(si);
1237 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001238 }
1239
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 /* Destroy the task attached to this applet */
1241 if (spoe_appctx->task) {
1242 task_delete(spoe_appctx->task);
1243 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001244 }
1245
Christopher Faulet8ef75252017-02-20 22:56:03 +01001246 /* Notify all waiting streams */
1247 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001248 LIST_DEL(&ctx->list);
1249 LIST_INIT(&ctx->list);
1250 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001253 }
1254
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 /* If the applet was processing a fragmented frame, notify the
1256 * corresponding stream. */
1257 if (spoe_appctx->frag_ctx.ctx) {
1258 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001259 ctx->frag_ctx.spoe_appctx = NULL;
1260 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001262 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1263 }
1264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 /* Release allocated memory */
1266 spoe_release_buffer(&spoe_appctx->buffer,
1267 &spoe_appctx->buffer_wait);
1268 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001269
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 if (!LIST_ISEMPTY(&agent->applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001271 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001272
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273 /* If this was the last running applet, notify all waiting streams */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) {
1275 LIST_DEL(&ctx->list);
1276 LIST_INIT(&ctx->list);
1277 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001279 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001280 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001281 list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) {
1282 LIST_DEL(&ctx->list);
1283 LIST_INIT(&ctx->list);
1284 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001286 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1287 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001288
1289 end:
1290 /* Update runtinme agent info */
1291 agent->frame_size = agent->max_frame_size;
1292 list_for_each_entry(spoe_appctx, &agent->applets, list)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 agent->frame_size = MIN(spoe_appctx->max_frame_size,
1294 agent->frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001295}
1296
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001297static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001299{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001301 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001302 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001303 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001304
Christopher Fauleta1cda022016-12-21 08:58:06 +01001305 if (si->state <= SI_ST_CON) {
1306 si_applet_want_put(si);
1307 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1308 goto stop;
1309 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001310 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001313 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001314
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001316 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1317 " - Connection timed out\n",
1318 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1319 __FUNCTION__, appctx);
1320 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001322 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001323
Christopher Faulet42bfa462017-01-04 14:14:19 +01001324 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001325 SPOE_APPCTX(appctx)->task->expire =
1326 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327
Christopher Faulet8ef75252017-02-20 22:56:03 +01001328 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1329 * length. */
1330 buf = trash.str; frame = buf+4;
1331 ret = spoe_prepare_hahello_frame(appctx, frame,
1332 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001334 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335
1336 switch (ret) {
1337 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 case 0: /* ignore => an error, cannot be ignored */
1339 goto exit;
1340
1341 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 goto stop;
1343
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 default:
1345 /* HELLO frame successfully sent, now wait for the
1346 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1348 goto next;
1349 }
1350
1351 next:
1352 return 0;
1353 stop:
1354 return 1;
1355 exit:
1356 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1357 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001358}
1359
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001360static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001362{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001364 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 char *frame;
1366 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001367
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001368
Christopher Fauletb067b062017-01-04 16:39:11 +01001369 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001371 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001372 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001373
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1376 " - Connection timed out\n",
1377 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1378 __FUNCTION__, appctx);
1379 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 goto exit;
1381 }
1382
Christopher Faulet8ef75252017-02-20 22:56:03 +01001383 frame = trash.str; trash.len = 0;
1384 ret = spoe_recv_frame(appctx, frame,
1385 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001386 if (ret > 1) {
1387 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1388 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1389 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001390 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 trash.len = ret + 4;
1392 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394
Christopher Fauleta1cda022016-12-21 08:58:06 +01001395 switch (ret) {
1396 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001397 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001398 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1399 goto next;
1400
1401 case 1: /* retry later */
1402 goto stop;
1403
1404 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001405 /* HELLO handshake is finished, set the idle timeout and
1406 * add the applet in the list of running applets. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 agent->applets_idle++;
1408 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001409 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1410 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001411
1412 /* Update runtinme agent info */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001413 agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size,
1414 agent->frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001416 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001417
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001419 /* Do not forget to remove processed frame from the output buffer */
1420 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001421 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422
1423 SPOE_APPCTX(appctx)->task->expire =
1424 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 return 0;
1426 stop:
1427 return 1;
1428 exit:
1429 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1430 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431}
1432
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001433
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001435spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001437 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1438 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 char *frame, *buf;
1440 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001441
Christopher Faulet8ef75252017-02-20 22:56:03 +01001442 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1443 * length. */
1444 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001445
1446 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1447 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1448 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1449 SPOE_APPCTX(appctx)->max_frame_size);
1450 }
1451 else if (LIST_ISEMPTY(&agent->sending_queue)) {
1452 *skip = 1;
1453 ret = 1;
1454 goto end;
1455 }
1456 else {
1457 ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list);
1458 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1459 SPOE_APPCTX(appctx)->max_frame_size);
1460
1461 }
1462
Christopher Faulet8ef75252017-02-20 22:56:03 +01001463 if (ret > 1)
1464 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001465
Christopher Faulet8ef75252017-02-20 22:56:03 +01001466 switch (ret) {
1467 case -1: /* error */
1468 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1469 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001470
Christopher Faulet8ef75252017-02-20 22:56:03 +01001471 case 0: /* ignore */
1472 if (ctx == NULL)
1473 goto abort_frag_frame;
1474
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 LIST_DEL(&ctx->list);
1477 LIST_INIT(&ctx->list);
1478 ctx->state = SPOE_CTX_ST_ERROR;
1479 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1480 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1481 break;
1482
1483 case 1: /* retry */
1484 *skip = 1;
1485 break;
1486
1487 default:
1488 if (ctx == NULL)
1489 goto abort_frag_frame;
1490
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001491 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 LIST_DEL(&ctx->list);
1493 LIST_INIT(&ctx->list);
1494 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1495 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1496 goto no_frag_frame_sent;
1497 else {
1498 *skip = 1;
1499 goto frag_frame_sent;
1500 }
1501 }
1502 goto end;
1503
1504 frag_frame_sent:
1505 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1506 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1507 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1508 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1509
1510 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1511 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1512 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1513 goto end;
1514
1515 no_frag_frame_sent:
1516 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1517 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1518 LIST_ADDQ(&agent->waiting_queue, &ctx->list);
1519 }
1520 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1521 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1522 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1523 }
1524 else {
1525 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1526 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1527 }
1528 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1529 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1530 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1531
1532 ctx->frag_ctx.spoe_appctx = NULL;
1533 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1534 goto end;
1535
1536 abort_frag_frame:
1537 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1538 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1539 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1540 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1541 goto end;
1542
1543 end:
1544 return ret;
1545}
1546
1547static int
1548spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1549{
1550 struct spoe_context *ctx = NULL;
1551 char *frame;
1552 int ret;
1553
1554 frame = trash.str; trash.len = 0;
1555 ret = spoe_recv_frame(appctx, frame,
1556 SPOE_APPCTX(appctx)->max_frame_size);
1557 if (ret > 1) {
1558 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1559 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1560 goto end;
1561 }
1562 trash.len = ret + 4;
1563 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1564 }
1565 switch (ret) {
1566 case -1: /* error */
1567 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1568 break;
1569
1570 case 0: /* ignore */
1571 break;
1572
1573 case 1: /* retry */
1574 *skip = 1;
1575 break;
1576
1577 default:
1578 LIST_DEL(&ctx->list);
1579 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001580
1581 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1582 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1583 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1584 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1585 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1586 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1587 }
1588 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1589 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1590
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1592 break;
1593 }
1594
1595 /* Do not forget to remove processed frame from the output buffer */
1596 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001597 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 end:
1599 return ret;
1600}
1601
1602static int
1603spoe_handle_processing_appctx(struct appctx *appctx)
1604{
1605 struct stream_interface *si = appctx->owner;
1606 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001607 unsigned int fpa = 0;
1608 int ret, skip_sending = 0, skip_receiving = 0;
1609
1610 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1611 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1612 goto exit;
1613 }
1614
1615 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1616 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1617 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1618 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1619 goto next;
1620 }
1621
1622 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001623 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1624 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1625 " - appctx-state=%s\n",
1626 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1627 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 skip_sending, skip_receiving,
1629 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001630
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001631 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001632 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001633 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001634 if (skip_receiving)
1635 goto stop;
1636 goto recv_frame;
1637 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001638
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001639 /* send_frame */
1640 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001641 switch (ret) {
1642 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001643 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001644
Christopher Fauleta1cda022016-12-21 08:58:06 +01001645 case 0: /* ignore */
1646 agent->sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001647 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001648 break;
1649
Christopher Fauleta1cda022016-12-21 08:58:06 +01001650 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001651 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001652
Christopher Fauleta1cda022016-12-21 08:58:06 +01001653 default:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001654 agent->sending_rate++;
1655 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001656 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001657 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001658 if (fpa > agent->max_fpa)
1659 goto stop;
1660
1661 recv_frame:
1662 if (skip_receiving)
1663 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001664 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001665 switch (ret) {
1666 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001667 goto next;
1668
1669 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001670 fpa++;
1671 break;
1672
1673 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 break;
1675
1676 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001677 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001678 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679 }
1680 goto process;
1681
1682 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001683 SPOE_APPCTX(appctx)->task->expire =
1684 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001685 return 0;
1686 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001687 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001688 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001689 agent->applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001691 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
1692 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1693 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001694 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001695 SPOE_APPCTX(appctx)->task->expire =
1696 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697 }
1698 return 1;
1699
1700 exit:
1701 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1702 return 0;
1703}
1704
1705static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001706spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707{
1708 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001709 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001710 char *frame, *buf;
1711 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001712
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1714 goto exit;
1715
1716 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1717 goto exit;
1718
Christopher Faulet8ef75252017-02-20 22:56:03 +01001719 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1720 * length. */
1721 buf = trash.str; frame = buf+4;
1722 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1723 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001725 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001726
1727 switch (ret) {
1728 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001729 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001730 goto exit;
1731
1732 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001733 goto stop;
1734
1735 default:
1736 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1737 " - disconnected by HAProxy (%d): %s\n",
1738 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001739 __FUNCTION__, appctx,
1740 SPOE_APPCTX(appctx)->status_code,
1741 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742
1743 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1744 goto next;
1745 }
1746
1747 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748 SPOE_APPCTX(appctx)->task->expire =
1749 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750 return 0;
1751 stop:
1752 return 1;
1753 exit:
1754 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1755 return 0;
1756}
1757
1758static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001760{
1761 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 char *frame;
1763 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764
Christopher Fauletb067b062017-01-04 16:39:11 +01001765 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001766 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001768 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769
Christopher Fauletb067b062017-01-04 16:39:11 +01001770 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774
Christopher Faulet8ef75252017-02-20 22:56:03 +01001775 frame = trash.str; trash.len = 0;
1776 ret = spoe_recv_frame(appctx, frame,
1777 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 trash.len = ret + 4;
1780 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781 }
1782
1783 switch (ret) {
1784 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1786 " - error on frame (%s)\n",
1787 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001788 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 goto exit;
1792
1793 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001794 goto next;
1795
1796 case 1: /* retry */
1797 goto stop;
1798
1799 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001800 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001803 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1805 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 goto exit;
1807 }
1808
1809 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 /* Do not forget to remove processed frame from the output buffer */
1811 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001812 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 return 0;
1815 stop:
1816 return 1;
1817 exit:
1818 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1819 return 0;
1820}
1821
1822/* I/O Handler processing messages exchanged with the agent */
1823static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001826 struct stream_interface *si = appctx->owner;
1827 struct spoe_agent *agent;
1828
1829 if (SPOE_APPCTX(appctx) == NULL)
1830 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831
Christopher Faulet8ef75252017-02-20 22:56:03 +01001832 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1833 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001834
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 switchstate:
1836 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1837 " - appctx-state=%s\n",
1838 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1839 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1840
1841 switch (appctx->st0) {
1842 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001843 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001844 goto out;
1845 goto switchstate;
1846
1847 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 goto out;
1850 goto switchstate;
1851
1852 case SPOE_APPCTX_ST_IDLE:
1853 if (stopping &&
1854 LIST_ISEMPTY(&agent->sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001855 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 SPOE_APPCTX(appctx)->task->expire =
1857 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001859 goto switchstate;
1860 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001861 agent->applets_idle--;
1862 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1863 /* fall through */
1864
1865 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001866 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1867 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001868 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869 goto out;
1870 goto switchstate;
1871
1872 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001873 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 goto out;
1875 goto switchstate;
1876
1877 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001879 goto out;
1880 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001881
1882 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 appctx->st0 = SPOE_APPCTX_ST_END;
1884 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1885
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001886 si_shutw(si);
1887 si_shutr(si);
1888 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001889 /* fall through */
1890
1891 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001892 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001893 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001895 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1896 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001897 si_oc(si)->flags |= CF_READ_DONTWAIT;
1898 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001899}
1900
1901struct applet spoe_applet = {
1902 .obj_type = OBJ_TYPE_APPLET,
1903 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 .fct = spoe_handle_appctx,
1905 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001906};
1907
1908/* Create a SPOE applet. On success, the created applet is returned, else
1909 * NULL. */
1910static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001911spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001912{
1913 struct appctx *appctx;
1914 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001915 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916
1917 if ((appctx = appctx_new(&spoe_applet)) == NULL)
1918 goto out_error;
1919
Christopher Faulet42bfa462017-01-04 14:14:19 +01001920 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1921 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001923 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001924
Christopher Faulet42bfa462017-01-04 14:14:19 +01001925 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1926 if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL)
1927 goto out_free_spoe_appctx;
1928
1929 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001931 SPOE_APPCTX(appctx)->task->context = appctx;
1932 SPOE_APPCTX(appctx)->agent = conf->agent;
1933 SPOE_APPCTX(appctx)->version = 0;
1934 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1935 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001936 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001937 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1938
1939 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1940 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001941 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001942
1943 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1944 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945
Willy Tarreau5820a362016-12-22 15:59:02 +01001946 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001947 if (!sess)
1948 goto out_free_spoe;
1949
Willy Tarreau87787ac2017-08-28 16:22:54 +02001950 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951 goto out_free_sess;
1952
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001953 stream_set_backend(strm, conf->agent->b.be);
1954
1955 /* applet is waiting for data */
1956 si_applet_cant_get(&strm->si[0]);
1957 appctx_wakeup(appctx);
1958
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 strm->do_log = NULL;
1960 strm->res.flags |= CF_READ_DONTWAIT;
1961
Christopher Faulet42bfa462017-01-04 14:14:19 +01001962 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
1963 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001964 conf->agent->applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001965
Willy Tarreau87787ac2017-08-28 16:22:54 +02001966 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 return appctx;
1968
1969 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001970 out_free_sess:
1971 session_free(sess);
1972 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001973 task_free(SPOE_APPCTX(appctx)->task);
1974 out_free_spoe_appctx:
1975 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001976 out_free_appctx:
1977 appctx_free(appctx);
1978 out_error:
1979 return NULL;
1980}
1981
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001982static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001983spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984{
1985 struct spoe_config *conf = FLT_CONF(ctx->filter);
1986 struct spoe_agent *agent = conf->agent;
1987 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001988 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001989 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990
Christopher Fauleta1cda022016-12-21 08:58:06 +01001991 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001994 if (agent->applets_act >= min_applets &&
1995 agent->applets_idle &&
1996 agent->sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001997 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998
1999 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002000 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002001 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2002 ctx->strm);
2003
Christopher Fauleta1cda022016-12-21 08:58:06 +01002004 /* Do not try to create a new applet if there is no server up for the
2005 * agent's backend. */
2006 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2007 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2008 " - cannot create SPOE appctx: no server up\n",
2009 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2010 __FUNCTION__, ctx->strm);
2011 goto end;
2012 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013
Christopher Fauleta1cda022016-12-21 08:58:06 +01002014 /* Do not try to create a new applet if we have reached the maximum of
2015 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002016 if (agent->cps_max > 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002017 if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) {
2018 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2019 " - cannot create SPOE appctx: max CPS reached\n",
2020 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2021 __FUNCTION__, ctx->strm);
2022 goto end;
2023 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 }
2025
Christopher Faulet8ef75252017-02-20 22:56:03 +01002026 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002027 if (appctx == NULL) {
2028 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2029 " - failed to create SPOE appctx\n",
2030 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2031 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002032 send_log(ctx->strm->be, LOG_EMERG,
2033 "SPOE: [%s] failed to create SPOE applet\n",
2034 agent->id);
2035
Christopher Fauleta1cda022016-12-21 08:58:06 +01002036 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002037 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 if (agent->applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002039 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 /* Increase the per-process number of cumulated connections */
2042 if (agent->cps_max > 0)
2043 update_freq_ctr(&agent->conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002044
Christopher Fauleta1cda022016-12-21 08:58:06 +01002045 end:
2046 /* The only reason to return an error is when there is no applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002047 if (LIST_ISEMPTY(&agent->applets)) {
2048 ctx->status_code = SPOE_CTX_ERR_RES;
2049 return -1;
2050 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 /* Add the SPOE context in the sending queue and update all running
2053 * info */
2054 LIST_ADDQ(&agent->sending_queue, &ctx->list);
2055 if (agent->sending_rate)
2056 agent->sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002057
2058 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002059 " - Add stream in sending queue"
2060 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002062 ctx->strm, agent->applets_act, agent->applets_idle,
2063 agent->sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002064
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 /* Finally try to wakeup the first IDLE applet found and move it at the
2066 * end of the list. */
Christopher Faulet42bfa462017-01-04 14:14:19 +01002067 list_for_each_entry(spoe_appctx, &agent->applets, list) {
2068 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002070 spoe_wakeup_appctx(appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002071 LIST_DEL(&spoe_appctx->list);
2072 LIST_ADDQ(&agent->applets, &spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002073 break;
2074 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002075 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002076 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077}
2078
2079/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002080 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002081 **************************************************************************/
Christopher Faulet8ef75252017-02-20 22:56:03 +01002082/* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any,
2083 * are used to handle fragmented content. On success it returns 1. If an error
Christopher Faulet57583e42017-09-04 15:41:09 +02002084 * occurred, -1 is returned. If nothing has been encoded, it returns 0 (this is
2085 * only possible for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002087spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002088 struct list *messages, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002089{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002090 struct spoe_config *conf = FLT_CONF(ctx->filter);
2091 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092 struct spoe_message *msg;
2093 struct sample *smp;
2094 struct spoe_arg *arg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002095 char *p, *end;
2096 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002097
Christopher Faulet8ef75252017-02-20 22:56:03 +01002098 p = ctx->buffer->p;
2099 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002100
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002101 /* Resume encoding of a SPOE message */
2102 if (ctx->frag_ctx.curmsg != NULL) {
2103 msg = ctx->frag_ctx.curmsg;
2104 goto encode_message;
2105 }
2106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107 /* Loop on messages */
2108 list_for_each_entry(msg, messages, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002109 ctx->frag_ctx.curmsg = msg;
2110 ctx->frag_ctx.curarg = NULL;
2111 ctx->frag_ctx.curoff = UINT_MAX;
2112
2113 encode_message:
Christopher Faulet57583e42017-09-04 15:41:09 +02002114 if (msg->cond) {
2115 int ret = 1;
2116
2117 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2118 ret = acl_pass(ret);
2119 if (msg->cond->pol == ACL_COND_UNLESS)
2120 ret = !ret;
2121
2122 /* the rule does not match */
2123 if (!ret)
2124 continue;
2125 }
2126
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002127 /* Resume encoding of a SPOE argument */
2128 if (ctx->frag_ctx.curarg != NULL) {
2129 arg = ctx->frag_ctx.curarg;
2130 goto encode_argument;
2131 }
2132
2133 if (ctx->frag_ctx.curoff != UINT_MAX)
2134 goto encode_msg_payload;
2135
Christopher Faulet8ef75252017-02-20 22:56:03 +01002136 /* Check if there is enough space for the message name and the
2137 * number of arguments. It implies <msg->id_len> is encoded on 2
2138 * bytes, at most (< 2288). */
2139 if (p + 2 + msg->id_len + 1 > end)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002140 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002141
Christopher Faulet8ef75252017-02-20 22:56:03 +01002142 /* Encode the message name */
2143 if (spoe_encode_buffer(msg->id, msg->id_len, &p, end) == -1)
2144 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002145
Christopher Faulet8ef75252017-02-20 22:56:03 +01002146 /* Set the number of arguments for this message */
2147 *p++ = msg->nargs;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002148
2149 ctx->frag_ctx.curoff = 0;
2150 encode_msg_payload:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002151
2152 /* Loop on arguments */
2153 list_for_each_entry(arg, &msg->args, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002154 ctx->frag_ctx.curarg = arg;
2155 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002156
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002157 encode_argument:
2158 if (ctx->frag_ctx.curoff != UINT_MAX)
2159 goto encode_arg_value;
2160
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002161 /* Encode the arguement name as a string. It can by NULL */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002162 if (spoe_encode_buffer(arg->name, arg->name_len, &p, end) == -1)
2163 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002164
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002165 ctx->frag_ctx.curoff = 0;
2166 encode_arg_value:
2167
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002168 /* Fetch the arguement value */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002169 smp = sample_process(s->be, s->sess, s,
2170 dir|SMP_OPT_FINAL, arg->expr, NULL);
2171 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, &p, end);
2172 if (ret == -1 || ctx->frag_ctx.curoff)
2173 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002174 }
2175 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002176
Christopher Faulet57583e42017-09-04 15:41:09 +02002177 /* nothing has been encoded for an unfragmented payload */
2178 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2179 goto skip;
2180
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002181 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002182 " - encode %s messages - spoe_appctx=%p"
2183 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002184 (int)now.tv_sec, (int)now.tv_usec,
2185 agent->id, __FUNCTION__, s,
2186 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002187 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2188 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002189
Christopher Faulet8ef75252017-02-20 22:56:03 +01002190 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002191 ctx->frag_ctx.curmsg = NULL;
2192 ctx->frag_ctx.curarg = NULL;
2193 ctx->frag_ctx.curoff = 0;
2194 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002195
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002196 return 1;
2197
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002198 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002199 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2200 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2201 return -1;
2202 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002203
2204 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002205 " - encode fragmented messages - spoe_appctx=%p"
2206 " - curmsg=%p - curarg=%p - curoff=%u"
2207 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002208 (int)now.tv_sec, (int)now.tv_usec,
2209 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2210 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002211 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002212
Christopher Faulet8ef75252017-02-20 22:56:03 +01002213 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002214 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2215 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2216 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002217
2218 skip:
2219 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2220 " - skip the frame because nothing has been encoded\n",
2221 (int)now.tv_sec, (int)now.tv_usec,
2222 agent->id, __FUNCTION__, s);
2223 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002224}
2225
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002226
2227/***************************************************************************
2228 * Functions that handle SPOE actions
2229 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002230/* Helper function to set a variable */
2231static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002232spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002233 struct sample *smp)
2234{
2235 struct spoe_config *conf = FLT_CONF(ctx->filter);
2236 struct spoe_agent *agent = conf->agent;
2237 char varname[64];
2238
2239 memset(varname, 0, sizeof(varname));
2240 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2241 scope, agent->var_pfx, len, name);
2242 vars_set_by_name_ifexist(varname, len, smp);
2243}
2244
2245/* Helper function to unset a variable */
2246static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002247spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002248 struct sample *smp)
2249{
2250 struct spoe_config *conf = FLT_CONF(ctx->filter);
2251 struct spoe_agent *agent = conf->agent;
2252 char varname[64];
2253
2254 memset(varname, 0, sizeof(varname));
2255 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2256 scope, agent->var_pfx, len, name);
2257 vars_unset_by_name_ifexist(varname, len, smp);
2258}
2259
2260
Christopher Faulet8ef75252017-02-20 22:56:03 +01002261static inline int
2262spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2263 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 char *str, *scope, *p = *buf;
2266 struct sample smp;
2267 uint64_t sz;
2268 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002269
Christopher Faulet8ef75252017-02-20 22:56:03 +01002270 if (p + 2 >= end)
2271 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002272
Christopher Faulet8ef75252017-02-20 22:56:03 +01002273 /* SET-VAR requires 3 arguments */
2274 if (*p++ != 3)
2275 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002276
Christopher Faulet8ef75252017-02-20 22:56:03 +01002277 switch (*p++) {
2278 case SPOE_SCOPE_PROC: scope = "proc"; break;
2279 case SPOE_SCOPE_SESS: scope = "sess"; break;
2280 case SPOE_SCOPE_TXN : scope = "txn"; break;
2281 case SPOE_SCOPE_REQ : scope = "req"; break;
2282 case SPOE_SCOPE_RES : scope = "res"; break;
2283 default: goto skip;
2284 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002285
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2287 goto skip;
2288 memset(&smp, 0, sizeof(smp));
2289 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002290
Christopher Faulet8ef75252017-02-20 22:56:03 +01002291 if (spoe_decode_data(&p, end, &smp) == -1)
2292 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002293
Christopher Faulet8ef75252017-02-20 22:56:03 +01002294 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2295 " - set-var '%s.%s.%.*s'\n",
2296 (int)now.tv_sec, (int)now.tv_usec,
2297 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2298 __FUNCTION__, s, scope,
2299 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2300 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301
Christopher Faulet8ef75252017-02-20 22:56:03 +01002302 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002303
Christopher Faulet8ef75252017-02-20 22:56:03 +01002304 ret = (p - *buf);
2305 *buf = p;
2306 return ret;
2307 skip:
2308 return 0;
2309}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002310
Christopher Faulet8ef75252017-02-20 22:56:03 +01002311static inline int
2312spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2313 char **buf, char *end, int dir)
2314{
2315 char *str, *scope, *p = *buf;
2316 struct sample smp;
2317 uint64_t sz;
2318 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002319
Christopher Faulet8ef75252017-02-20 22:56:03 +01002320 if (p + 2 >= end)
2321 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002322
Christopher Faulet8ef75252017-02-20 22:56:03 +01002323 /* UNSET-VAR requires 2 arguments */
2324 if (*p++ != 2)
2325 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326
Christopher Faulet8ef75252017-02-20 22:56:03 +01002327 switch (*p++) {
2328 case SPOE_SCOPE_PROC: scope = "proc"; break;
2329 case SPOE_SCOPE_SESS: scope = "sess"; break;
2330 case SPOE_SCOPE_TXN : scope = "txn"; break;
2331 case SPOE_SCOPE_REQ : scope = "req"; break;
2332 case SPOE_SCOPE_RES : scope = "res"; break;
2333 default: goto skip;
2334 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002335
Christopher Faulet8ef75252017-02-20 22:56:03 +01002336 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2337 goto skip;
2338 memset(&smp, 0, sizeof(smp));
2339 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002340
Christopher Faulet8ef75252017-02-20 22:56:03 +01002341 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2342 " - unset-var '%s.%s.%.*s'\n",
2343 (int)now.tv_sec, (int)now.tv_usec,
2344 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2345 __FUNCTION__, s, scope,
2346 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2347 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 spoe_unset_var(ctx, scope, str, sz, &smp);
2350
2351 ret = (p - *buf);
2352 *buf = p;
2353 return ret;
2354 skip:
2355 return 0;
2356}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358/* Process SPOE actions for a specific event. It returns 1 on success. If an
2359 * error occurred, 0 is returned. */
2360static int
2361spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2362 enum spoe_event ev, int dir)
2363{
2364 char *p, *end;
2365 int ret;
2366
2367 p = ctx->buffer->p;
2368 end = p + ctx->buffer->i;
2369
2370 while (p < end) {
2371 enum spoe_action_type type;
2372
2373 type = *p++;
2374 switch (type) {
2375 case SPOE_ACT_T_SET_VAR:
2376 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2377 if (!ret)
2378 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002379 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 case SPOE_ACT_T_UNSET_VAR:
2382 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2383 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002384 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385 break;
2386
2387 default:
2388 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389 }
2390 }
2391
2392 return 1;
2393 skip:
2394 return 0;
2395}
2396
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002397/***************************************************************************
2398 * Functions that process SPOE events
2399 **************************************************************************/
2400static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002402{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002403 /* If a process is already started for this SPOE context, retry
2404 * later. */
2405 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002406 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002407
2408 /* Set the right flag to prevent request and response processing
2409 * in same time. */
2410 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2411 ? SPOE_CTX_FL_REQ_PROCESS
2412 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002413 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002414}
2415
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002416static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002418{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002419 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2420
2421 if (sa) {
2422 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002424 }
2425
Christopher Fauleta1cda022016-12-21 08:58:06 +01002426 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002427 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002428
Christopher Fauletb067b062017-01-04 16:39:11 +01002429 ctx->status_code = 0;
2430
Christopher Fauleta1cda022016-12-21 08:58:06 +01002431 /* Reset processing timer */
2432 ctx->process_exp = TICK_ETERNITY;
2433
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002435
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002436 ctx->frag_ctx.spoe_appctx = NULL;
2437 ctx->frag_ctx.curmsg = NULL;
2438 ctx->frag_ctx.curarg = NULL;
2439 ctx->frag_ctx.curoff = 0;
2440 ctx->frag_ctx.flags = 0;
2441
Christopher Fauleta1cda022016-12-21 08:58:06 +01002442 if (!LIST_ISEMPTY(&ctx->list)) {
2443 LIST_DEL(&ctx->list);
2444 LIST_INIT(&ctx->list);
2445 }
2446}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002447
2448/* Process a SPOE event. First, this functions will process messages attached to
2449 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2450 * ACK frame to process corresponding actions. During all the processing, it
2451 * returns 0 and it returns 1 when the processing is finished. If an error
2452 * occurred, -1 is returned. */
2453static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002454spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002455 enum spoe_event ev)
2456{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002457 struct spoe_config *conf = FLT_CONF(ctx->filter);
2458 struct spoe_agent *agent = conf->agent;
2459 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460
2461 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2462 " - ctx-state=%s - event=%s\n",
2463 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002464 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002465 spoe_event_str[ev]);
2466
2467 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2468
2469 if (LIST_ISEMPTY(&(ctx->messages[ev])))
2470 goto out;
2471
2472 if (ctx->state == SPOE_CTX_ST_ERROR)
2473 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002474
2475 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2476 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2477 " - failed to process event '%s': timeout\n",
2478 (int)now.tv_sec, (int)now.tv_usec,
2479 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002480 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002481 goto error;
2482 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002483
2484 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002485 if (agent->eps_max > 0) {
2486 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2487 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2488 " - skip event '%s': max EPS reached\n",
2489 (int)now.tv_sec, (int)now.tv_usec,
2490 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2491 goto skip;
2492 }
2493 }
2494
Christopher Fauletf7a30922016-11-10 15:04:51 +01002495 if (!tick_isset(ctx->process_exp)) {
2496 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2497 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2498 ctx->process_exp);
2499 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002500 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002501 if (!ret)
2502 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002503
Christopher Faulet8ef75252017-02-20 22:56:03 +01002504 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002505 goto error;
2506
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002507 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002508 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002509 }
2510
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002511 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002512 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002513 goto out;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002514 ret = spoe_encode_messages(s, ctx, &(ctx->messages[ev]), dir);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002515 if (ret < 0)
2516 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002517 if (!ret)
2518 goto skip;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002519 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2520 }
2521
2522 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2523 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002524 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002525 ret = 0;
2526 goto out;
2527 }
2528
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002529 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2530 ret = 0;
2531 goto out;
2532 }
2533
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002534 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002535 spoe_process_actions(s, ctx, ev, dir);
2536 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002537 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002538 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002539 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002540 }
2541
2542 out:
2543 return ret;
2544
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002545 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002546 if (agent->eps_max > 0)
2547 update_freq_ctr(&agent->err_per_sec, 1);
2548
Christopher Faulet985532d2016-11-16 15:36:19 +01002549 if (agent->var_on_error) {
2550 struct sample smp;
2551
2552 memset(&smp, 0, sizeof(smp));
2553 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002554 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002555 smp.data.type = SMP_T_BOOL;
2556
Christopher Faulet8ef75252017-02-20 22:56:03 +01002557 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002558 strlen(agent->var_on_error), &smp);
2559 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002560 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2561 " - failed to create process event '%s': code=%u\n",
2562 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2563 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2564 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002565 send_log(ctx->strm->be, LOG_WARNING,
2566 "SPOE: [%s] failed to process event '%s': code=%u\n",
2567 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002568
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002569 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2570 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002571 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002572 ret = 1;
2573 goto end;
2574
2575 skip:
2576 ctx->state = SPOE_CTX_ST_READY;
2577 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002578
Christopher Fauleta1cda022016-12-21 08:58:06 +01002579 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002580 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581 return ret;
2582}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002583
2584/***************************************************************************
2585 * Functions that create/destroy SPOE contexts
2586 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002588spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002589{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002590 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002591 return 1;
2592
Christopher Faulet4596fb72017-01-11 14:05:19 +01002593 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2594 LIST_DEL(&buffer_wait->list);
2595 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596 }
2597
Christopher Faulet4596fb72017-01-11 14:05:19 +01002598 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599 return 1;
2600
Christopher Faulet4596fb72017-01-11 14:05:19 +01002601 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002602 return 0;
2603}
2604
2605static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002606spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002607{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002608 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2609 LIST_DEL(&buffer_wait->list);
2610 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002611 }
2612
2613 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002614 if (*buf != &buf_empty) {
2615 b_free(buf);
2616 offer_buffers(buffer_wait->target,
2617 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002618 }
2619}
2620
Christopher Faulet4596fb72017-01-11 14:05:19 +01002621static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002622spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002623{
2624 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2625 return 1;
2626}
2627
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002628static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002629spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002630{
2631 struct spoe_config *conf = FLT_CONF(filter);
2632 struct spoe_context *ctx;
2633
2634 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2635 if (ctx == NULL) {
2636 return NULL;
2637 }
2638 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002639 ctx->filter = filter;
2640 ctx->state = SPOE_CTX_ST_NONE;
2641 ctx->status_code = SPOE_CTX_ERR_NONE;
2642 ctx->flags = 0;
2643 ctx->messages = conf->agent->messages;
2644 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002645 LIST_INIT(&ctx->buffer_wait.list);
2646 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002647 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002648 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002649
Christopher Fauletf7a30922016-11-10 15:04:51 +01002650 ctx->stream_id = 0;
2651 ctx->frame_id = 1;
2652 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002653
2654 return ctx;
2655}
2656
2657static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002658spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002659{
2660 if (!ctx)
2661 return;
2662
Christopher Faulet8ef75252017-02-20 22:56:03 +01002663 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002664 pool_free2(pool2_spoe_ctx, ctx);
2665}
2666
2667static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002668spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002669{
2670 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002671 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002672}
2673
2674
2675/***************************************************************************
2676 * Hooks that manage the filter lifecycle (init/check/deinit)
2677 **************************************************************************/
2678/* Signal handler: Do a soft stop, wakeup SPOE applet */
2679static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002680spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002681{
2682 struct proxy *p;
2683
2684 p = proxy;
2685 while (p) {
2686 struct flt_conf *fconf;
2687
2688 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002689 struct spoe_config *conf;
2690 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002691 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002692
Christopher Faulet3b386a32017-02-23 10:17:15 +01002693 if (fconf->id != spoe_filter_id)
2694 continue;
2695
2696 conf = fconf->conf;
2697 agent = conf->agent;
2698
Christopher Faulet42bfa462017-01-04 14:14:19 +01002699 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002700 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002701 }
2702 }
2703 p = p->next;
2704 }
2705}
2706
2707
2708/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2709static int
2710spoe_init(struct proxy *px, struct flt_conf *fconf)
2711{
2712 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713
2714 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2715 init_new_proxy(&conf->agent_fe);
2716 conf->agent_fe.parent = conf->agent;
2717 conf->agent_fe.last_change = now.tv_sec;
2718 conf->agent_fe.id = conf->agent->id;
2719 conf->agent_fe.cap = PR_CAP_FE;
2720 conf->agent_fe.mode = PR_MODE_TCP;
2721 conf->agent_fe.maxconn = 0;
2722 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2723 conf->agent_fe.conn_retries = CONN_RETRIES;
2724 conf->agent_fe.accept = frontend_accept;
2725 conf->agent_fe.srv = NULL;
2726 conf->agent_fe.timeout.client = TICK_ETERNITY;
2727 conf->agent_fe.default_target = &spoe_applet.obj_type;
2728 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2729
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002730 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002731 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002732 sighandler_registered = 1;
2733 }
2734
2735 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002736}
2737
2738/* Free ressources allocated by the SPOE filter. */
2739static void
2740spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2741{
2742 struct spoe_config *conf = fconf->conf;
2743
2744 if (conf) {
2745 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002746
Christopher Faulet8ef75252017-02-20 22:56:03 +01002747 spoe_release_agent(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002748 free(conf);
2749 }
2750 fconf->conf = NULL;
2751}
2752
2753/* Check configuration of a SPOE filter for a specified proxy.
2754 * Return 1 on error, else 0. */
2755static int
2756spoe_check(struct proxy *px, struct flt_conf *fconf)
2757{
2758 struct spoe_config *conf = fconf->conf;
2759 struct proxy *target;
2760
2761 target = proxy_be_by_name(conf->agent->b.name);
2762 if (target == NULL) {
2763 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2764 " declared at %s:%d.\n",
2765 px->id, conf->agent->b.name, conf->agent->id,
2766 conf->agent->conf.file, conf->agent->conf.line);
2767 return 1;
2768 }
2769 if (target->mode != PR_MODE_TCP) {
2770 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2771 " at %s:%d does not support HTTP mode.\n",
2772 px->id, target->id, conf->agent->id,
2773 conf->agent->conf.file, conf->agent->conf.line);
2774 return 1;
2775 }
2776
2777 free(conf->agent->b.name);
2778 conf->agent->b.name = NULL;
2779 conf->agent->b.be = target;
2780 return 0;
2781}
2782
2783/**************************************************************************
2784 * Hooks attached to a stream
2785 *************************************************************************/
2786/* Called when a filter instance is created and attach to a stream. It creates
2787 * the context that will be used to process this stream. */
2788static int
2789spoe_start(struct stream *s, struct filter *filter)
2790{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002791 struct spoe_config *conf = FLT_CONF(filter);
2792 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002793 struct spoe_context *ctx;
2794
2795 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002796 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002797 __FUNCTION__, s);
2798
Christopher Faulet8ef75252017-02-20 22:56:03 +01002799 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002800 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002801 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2802 " - failed to create SPOE context\n",
2803 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002804 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002805 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002806 "SPOE: [%s] failed to create SPOE context\n",
2807 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002808 return 0;
2809 }
2810
2811 ctx->strm = s;
2812 ctx->state = SPOE_CTX_ST_READY;
2813 filter->ctx = ctx;
2814
2815 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_FE]))
2816 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2817
2818 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_BE]))
2819 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2820
2821 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_RSP]))
2822 filter->pre_analyzers |= AN_RES_INSPECT;
2823
2824 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_FE]))
2825 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2826
2827 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_BE]))
2828 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2829
2830 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_RSP]))
2831 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2832
2833 return 1;
2834}
2835
2836/* Called when a filter instance is detached from a stream. It release the
2837 * attached SPOE context. */
2838static void
2839spoe_stop(struct stream *s, struct filter *filter)
2840{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002841 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2842 (int)now.tv_sec, (int)now.tv_usec,
2843 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2844 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002845 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002846}
2847
Christopher Fauletf7a30922016-11-10 15:04:51 +01002848
2849/*
2850 * Called when the stream is woken up because of expired timer.
2851 */
2852static void
2853spoe_check_timeouts(struct stream *s, struct filter *filter)
2854{
2855 struct spoe_context *ctx = filter->ctx;
2856
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002857 if (tick_is_expired(ctx->process_exp, now_ms)) {
2858 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002859 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002860 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002861}
2862
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002863/* Called when we are ready to filter data on a channel */
2864static int
2865spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2866{
2867 struct spoe_context *ctx = filter->ctx;
2868 int ret = 1;
2869
2870 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2871 " - ctx-flags=0x%08x\n",
2872 (int)now.tv_sec, (int)now.tv_usec,
2873 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2874 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2875
Christopher Fauletb067b062017-01-04 16:39:11 +01002876 if (ctx->state == SPOE_CTX_ST_NONE)
2877 goto out;
2878
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002879 if (!(chn->flags & CF_ISRESP)) {
2880 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2881 chn->analysers |= AN_REQ_INSPECT_FE;
2882 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2883 chn->analysers |= AN_REQ_INSPECT_BE;
2884
2885 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2886 goto out;
2887
2888 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002889 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002890 if (!ret)
2891 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002892 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2893 }
2894 else {
2895 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2896 chn->analysers |= AN_RES_INSPECT;
2897
2898 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2899 goto out;
2900
Christopher Faulet8ef75252017-02-20 22:56:03 +01002901 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002902 if (!ret) {
2903 channel_dont_read(chn);
2904 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002905 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002906 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002907 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 }
2909
2910 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002911 return ret;
2912}
2913
2914/* Called before a processing happens on a given channel */
2915static int
2916spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2917 struct channel *chn, unsigned an_bit)
2918{
2919 struct spoe_context *ctx = filter->ctx;
2920 int ret = 1;
2921
2922 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2923 " - ctx-flags=0x%08x - ana=0x%08x\n",
2924 (int)now.tv_sec, (int)now.tv_usec,
2925 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2926 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2927 ctx->flags, an_bit);
2928
Christopher Fauletb067b062017-01-04 16:39:11 +01002929 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002930 goto out;
2931
2932 switch (an_bit) {
2933 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002934 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935 break;
2936 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002937 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002938 break;
2939 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002941 break;
2942 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002943 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944 break;
2945 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002946 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002947 break;
2948 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002949 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002950 break;
2951 }
2952
2953 out:
2954 if (!ret) {
2955 channel_dont_read(chn);
2956 channel_dont_close(chn);
2957 }
2958 return ret;
2959}
2960
2961/* Called when the filtering on the channel ends. */
2962static int
2963spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2964{
2965 struct spoe_context *ctx = filter->ctx;
2966
2967 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2968 " - ctx-flags=0x%08x\n",
2969 (int)now.tv_sec, (int)now.tv_usec,
2970 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2971 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2972
2973 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002974 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002975 }
2976
2977 return 1;
2978}
2979
2980/********************************************************************
2981 * Functions that manage the filter initialization
2982 ********************************************************************/
2983struct flt_ops spoe_ops = {
2984 /* Manage SPOE filter, called for each filter declaration */
2985 .init = spoe_init,
2986 .deinit = spoe_deinit,
2987 .check = spoe_check,
2988
2989 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01002990 .attach = spoe_start,
2991 .detach = spoe_stop,
2992 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993
2994 /* Handle channels activity */
2995 .channel_start_analyze = spoe_start_analyze,
2996 .channel_pre_analyze = spoe_chn_pre_analyze,
2997 .channel_end_analyze = spoe_end_analyze,
2998};
2999
3000
3001static int
3002cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3003{
3004 const char *err;
3005 int i, err_code = 0;
3006
3007 if ((cfg_scope == NULL && curengine != NULL) ||
3008 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003009 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 goto out;
3011
3012 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3013 if (!*args[1]) {
3014 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3015 file, linenum);
3016 err_code |= ERR_ALERT | ERR_ABORT;
3017 goto out;
3018 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003019 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3020 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021 goto out;
3022 }
3023
3024 err = invalid_char(args[1]);
3025 if (err) {
3026 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3027 file, linenum, *err, args[0], args[1]);
3028 err_code |= ERR_ALERT | ERR_ABORT;
3029 goto out;
3030 }
3031
3032 if (curagent != NULL) {
3033 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3034 file, linenum);
3035 err_code |= ERR_ALERT | ERR_ABORT;
3036 goto out;
3037 }
3038 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3039 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3040 err_code |= ERR_ALERT | ERR_ABORT;
3041 goto out;
3042 }
3043
3044 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003045
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046 curagent->conf.file = strdup(file);
3047 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003048
3049 curagent->timeout.hello = TICK_ETERNITY;
3050 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003051 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003052
3053 curagent->engine_id = NULL;
3054 curagent->var_pfx = NULL;
3055 curagent->var_on_error = NULL;
Christopher Fauletcecd8522017-02-24 22:11:21 +01003056 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003057 curagent->cps_max = 0;
3058 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003059 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003060 curagent->min_applets = 0;
3061 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062
3063 for (i = 0; i < SPOE_EV_EVENTS; ++i)
3064 LIST_INIT(&curagent->messages[i]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003065
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003066 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003067 curagent->applets_act = 0;
3068 curagent->applets_idle = 0;
3069 curagent->sending_rate = 0;
3070
3071 LIST_INIT(&curagent->applets);
3072 LIST_INIT(&curagent->sending_queue);
3073 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003074 }
3075 else if (!strcmp(args[0], "use-backend")) {
3076 if (!*args[1]) {
3077 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3078 file, linenum, args[0]);
3079 err_code |= ERR_ALERT | ERR_FATAL;
3080 goto out;
3081 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003082 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003083 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003084 free(curagent->b.name);
3085 curagent->b.name = strdup(args[1]);
3086 }
3087 else if (!strcmp(args[0], "messages")) {
3088 int cur_arg = 1;
3089 while (*args[cur_arg]) {
3090 struct spoe_msg_placeholder *mp = NULL;
3091
3092 list_for_each_entry(mp, &curmps, list) {
3093 if (!strcmp(mp->id, args[cur_arg])) {
3094 Alert("parsing [%s:%d]: spoe-message message '%s' already declared.\n",
3095 file, linenum, args[cur_arg]);
3096 err_code |= ERR_ALERT | ERR_FATAL;
3097 goto out;
3098 }
3099 }
3100
3101 if ((mp = calloc(1, sizeof(*mp))) == NULL) {
3102 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3103 err_code |= ERR_ALERT | ERR_ABORT;
3104 goto out;
3105 }
3106 mp->id = strdup(args[cur_arg]);
3107 LIST_ADDQ(&curmps, &mp->list);
3108 cur_arg++;
3109 }
3110 }
3111 else if (!strcmp(args[0], "timeout")) {
3112 unsigned int *tv = NULL;
3113 const char *res;
3114 unsigned timeout;
3115
3116 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003117 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003118 file, linenum);
3119 err_code |= ERR_ALERT | ERR_FATAL;
3120 goto out;
3121 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003122 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3123 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 if (!strcmp(args[1], "hello"))
3125 tv = &curagent->timeout.hello;
3126 else if (!strcmp(args[1], "idle"))
3127 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003128 else if (!strcmp(args[1], "processing"))
3129 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003130 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003131 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132 file, linenum, args[1]);
3133 err_code |= ERR_ALERT | ERR_FATAL;
3134 goto out;
3135 }
3136 if (!*args[2]) {
3137 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3138 file, linenum, args[1]);
3139 err_code |= ERR_ALERT | ERR_FATAL;
3140 goto out;
3141 }
3142 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3143 if (res) {
3144 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3145 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003146 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003147 goto out;
3148 }
3149 *tv = MS_TO_TICKS(timeout);
3150 }
3151 else if (!strcmp(args[0], "option")) {
3152 if (!*args[1]) {
3153 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3154 file, linenum, args[0]);
3155 err_code |= ERR_ALERT | ERR_FATAL;
3156 goto out;
3157 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003158
Christopher Faulet305c6072017-02-23 16:17:53 +01003159 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003160 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003161 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003162 if (kwm == 1)
3163 curagent->flags &= ~SPOE_FL_PIPELINING;
3164 else
3165 curagent->flags |= SPOE_FL_PIPELINING;
3166 goto out;
3167 }
3168 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003169 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003170 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003171 if (kwm == 1)
3172 curagent->flags &= ~SPOE_FL_ASYNC;
3173 else
3174 curagent->flags |= SPOE_FL_ASYNC;
3175 goto out;
3176 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003177 else if (!strcmp(args[1], "send-frag-payload")) {
3178 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3179 goto out;
3180 if (kwm == 1)
3181 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3182 else
3183 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3184 goto out;
3185 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003186
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003187 /* Following options does not support negation */
3188 if (kwm == 1) {
3189 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3190 file, linenum, args[1]);
3191 err_code |= ERR_ALERT | ERR_FATAL;
3192 goto out;
3193 }
3194
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 if (!strcmp(args[1], "var-prefix")) {
3196 char *tmp;
3197
3198 if (!*args[2]) {
3199 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3200 file, linenum, args[0],
3201 args[1]);
3202 err_code |= ERR_ALERT | ERR_FATAL;
3203 goto out;
3204 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003205 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3206 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003207 tmp = args[2];
3208 while (*tmp) {
3209 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3210 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3211 file, linenum, args[0], args[1]);
3212 err_code |= ERR_ALERT | ERR_FATAL;
3213 goto out;
3214 }
3215 tmp++;
3216 }
3217 curagent->var_pfx = strdup(args[2]);
3218 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003219 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003220 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003221 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003222 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3223 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003224 else if (!strcmp(args[1], "set-on-error")) {
3225 char *tmp;
3226
3227 if (!*args[2]) {
3228 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3229 file, linenum, args[0],
3230 args[1]);
3231 err_code |= ERR_ALERT | ERR_FATAL;
3232 goto out;
3233 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003234 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3235 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003236 tmp = args[2];
3237 while (*tmp) {
3238 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3239 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3240 file, linenum, args[0], args[1]);
3241 err_code |= ERR_ALERT | ERR_FATAL;
3242 goto out;
3243 }
3244 tmp++;
3245 }
3246 curagent->var_on_error = strdup(args[2]);
3247 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003248 else {
3249 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3250 file, linenum, args[1]);
3251 err_code |= ERR_ALERT | ERR_FATAL;
3252 goto out;
3253 }
Christopher Faulet48026722016-11-16 15:01:12 +01003254 }
3255 else if (!strcmp(args[0], "maxconnrate")) {
3256 if (!*args[1]) {
3257 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3258 file, linenum, args[0]);
3259 err_code |= ERR_ALERT | ERR_FATAL;
3260 goto out;
3261 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003262 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003263 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003264 curagent->cps_max = atol(args[1]);
3265 }
3266 else if (!strcmp(args[0], "maxerrrate")) {
3267 if (!*args[1]) {
3268 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3269 file, linenum, args[0]);
3270 err_code |= ERR_ALERT | ERR_FATAL;
3271 goto out;
3272 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003273 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003274 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003275 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003277 else if (!strcmp(args[0], "max-frame-size")) {
3278 if (!*args[1]) {
3279 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3280 file, linenum, args[0]);
3281 err_code |= ERR_ALERT | ERR_FATAL;
3282 goto out;
3283 }
3284 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3285 goto out;
3286 curagent->max_frame_size = atol(args[1]);
3287 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3288 curagent->max_frame_size > MAX_FRAME_SIZE) {
3289 Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3290 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
3291 err_code |= ERR_ALERT | ERR_FATAL;
3292 goto out;
3293 }
3294 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295 else if (*args[0]) {
3296 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3297 file, linenum, args[0]);
3298 err_code |= ERR_ALERT | ERR_FATAL;
3299 goto out;
3300 }
3301 out:
3302 return err_code;
3303}
3304
3305static int
3306cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3307{
3308 struct spoe_message *msg;
3309 struct spoe_arg *arg;
3310 const char *err;
3311 char *errmsg = NULL;
3312 int err_code = 0;
3313
3314 if ((cfg_scope == NULL && curengine != NULL) ||
3315 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003316 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 goto out;
3318
3319 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3320 if (!*args[1]) {
3321 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3322 file, linenum);
3323 err_code |= ERR_ALERT | ERR_ABORT;
3324 goto out;
3325 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003326 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3327 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003328 goto out;
3329 }
3330
3331 err = invalid_char(args[1]);
3332 if (err) {
3333 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3334 file, linenum, *err, args[0], args[1]);
3335 err_code |= ERR_ALERT | ERR_ABORT;
3336 goto out;
3337 }
3338
3339 list_for_each_entry(msg, &curmsgs, list) {
3340 if (!strcmp(msg->id, args[1])) {
3341 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3342 " name as another one declared at %s:%d.\n",
3343 file, linenum, args[1], msg->conf.file, msg->conf.line);
3344 err_code |= ERR_ALERT | ERR_FATAL;
3345 goto out;
3346 }
3347 }
3348
3349 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3350 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3351 err_code |= ERR_ALERT | ERR_ABORT;
3352 goto out;
3353 }
3354
3355 curmsg->id = strdup(args[1]);
3356 curmsg->id_len = strlen(curmsg->id);
3357 curmsg->event = SPOE_EV_NONE;
3358 curmsg->conf.file = strdup(file);
3359 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003360 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003362 LIST_INIT(&curmsg->acls);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003363 LIST_ADDQ(&curmsgs, &curmsg->list);
3364 }
3365 else if (!strcmp(args[0], "args")) {
3366 int cur_arg = 1;
3367
3368 curproxy->conf.args.ctx = ARGC_SPOE;
3369 curproxy->conf.args.file = file;
3370 curproxy->conf.args.line = linenum;
3371 while (*args[cur_arg]) {
3372 char *delim = strchr(args[cur_arg], '=');
3373 int idx = 0;
3374
3375 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3376 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3377 err_code |= ERR_ALERT | ERR_ABORT;
3378 goto out;
3379 }
3380
3381 if (!delim) {
3382 arg->name = NULL;
3383 arg->name_len = 0;
3384 delim = args[cur_arg];
3385 }
3386 else {
3387 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3388 arg->name_len = delim - args[cur_arg];
3389 delim++;
3390 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003391 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3392 &idx, file, linenum, &errmsg,
3393 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 if (arg->expr == NULL) {
3395 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3396 err_code |= ERR_ALERT | ERR_FATAL;
3397 free(arg->name);
3398 free(arg);
3399 goto out;
3400 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003401 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003402 LIST_ADDQ(&curmsg->args, &arg->list);
3403 cur_arg++;
3404 }
3405 curproxy->conf.args.file = NULL;
3406 curproxy->conf.args.line = 0;
3407 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003408 else if (!strcmp(args[0], "acl")) {
3409 err = invalid_char(args[1]);
3410 if (err) {
3411 Alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3412 file, linenum, *err, args[1]);
3413 err_code |= ERR_ALERT | ERR_FATAL;
3414 goto out;
3415 }
3416 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
3417 Alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3418 file, linenum, args[1], errmsg);
3419 err_code |= ERR_ALERT | ERR_FATAL;
3420 goto out;
3421 }
3422 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003423 else if (!strcmp(args[0], "event")) {
3424 if (!*args[1]) {
3425 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003426 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003427 goto out;
3428 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003429 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3430 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003431
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3433 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3434 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3435 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3436
3437 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3438 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3439 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3440 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3441 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3442 curmsg->event = SPOE_EV_ON_TCP_RSP;
3443
3444 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3445 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3446 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3447 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3448 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3449 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3450 else {
3451 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3452 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003453 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 goto out;
3455 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003456
3457 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3458 struct acl_cond *cond;
3459
3460 cond = build_acl_cond(file, linenum, &curmsg->acls,
3461 curproxy, (const char **)args+2,
3462 &errmsg);
3463 if (cond == NULL) {
3464 Alert("parsing [%s:%d] : error detected while "
3465 "parsing an 'event %s' condition : %s.\n",
3466 file, linenum, args[1], errmsg);
3467 err_code |= ERR_ALERT | ERR_FATAL;
3468 goto out;
3469 }
3470 curmsg->cond = cond;
3471 }
3472 else if (*args[2]) {
3473 Alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3474 "or 'unless' followed by a condition but found '%s'.\n",
3475 file, linenum, args[1], args[2]);
3476 err_code |= ERR_ALERT | ERR_FATAL;
3477 goto out;
3478 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003479 }
3480 else if (!*args[0]) {
3481 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3482 file, linenum, args[0]);
3483 err_code |= ERR_ALERT | ERR_FATAL;
3484 goto out;
3485 }
3486 out:
3487 free(errmsg);
3488 return err_code;
3489}
3490
3491/* Return -1 on error, else 0 */
3492static int
3493parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3494 struct flt_conf *fconf, char **err, void *private)
3495{
3496 struct list backup_sections;
3497 struct spoe_config *conf;
3498 struct spoe_message *msg, *msgback;
3499 struct spoe_msg_placeholder *mp, *mpback;
3500 char *file = NULL, *engine = NULL;
3501 int ret, pos = *cur_arg + 1;
3502
3503 conf = calloc(1, sizeof(*conf));
3504 if (conf == NULL) {
3505 memprintf(err, "%s: out of memory", args[*cur_arg]);
3506 goto error;
3507 }
3508 conf->proxy = px;
3509
3510 while (*args[pos]) {
3511 if (!strcmp(args[pos], "config")) {
3512 if (!*args[pos+1]) {
3513 memprintf(err, "'%s' : '%s' option without value",
3514 args[*cur_arg], args[pos]);
3515 goto error;
3516 }
3517 file = args[pos+1];
3518 pos += 2;
3519 }
3520 else if (!strcmp(args[pos], "engine")) {
3521 if (!*args[pos+1]) {
3522 memprintf(err, "'%s' : '%s' option without value",
3523 args[*cur_arg], args[pos]);
3524 goto error;
3525 }
3526 engine = args[pos+1];
3527 pos += 2;
3528 }
3529 else {
3530 memprintf(err, "unknown keyword '%s'", args[pos]);
3531 goto error;
3532 }
3533 }
3534 if (file == NULL) {
3535 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3536 goto error;
3537 }
3538
3539 /* backup sections and register SPOE sections */
3540 LIST_INIT(&backup_sections);
3541 cfg_backup_sections(&backup_sections);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003542 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3543 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003544
3545 /* Parse SPOE filter configuration file */
3546 curengine = engine;
3547 curproxy = px;
3548 curagent = NULL;
3549 curmsg = NULL;
3550 ret = readcfgfile(file);
3551 curproxy = NULL;
3552
3553 /* unregister SPOE sections and restore previous sections */
3554 cfg_unregister_sections();
3555 cfg_restore_sections(&backup_sections);
3556
3557 if (ret == -1) {
3558 memprintf(err, "Could not open configuration file %s : %s",
3559 file, strerror(errno));
3560 goto error;
3561 }
3562 if (ret & (ERR_ABORT|ERR_FATAL)) {
3563 memprintf(err, "Error(s) found in configuration file %s", file);
3564 goto error;
3565 }
3566
3567 /* Check SPOE agent */
3568 if (curagent == NULL) {
3569 memprintf(err, "No SPOE agent found in file %s", file);
3570 goto error;
3571 }
3572 if (curagent->b.name == NULL) {
3573 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3574 curagent->id, curagent->conf.file, curagent->conf.line);
3575 goto error;
3576 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003577 if (curagent->timeout.hello == TICK_ETERNITY ||
3578 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003579 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003580 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3581 " | While not properly invalid, you will certainly encounter various problems\n"
3582 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003583 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003584 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3585 }
3586 if (curagent->var_pfx == NULL) {
3587 char *tmp = curagent->id;
3588
3589 while (*tmp) {
3590 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3591 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3592 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3593 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3594 goto error;
3595 }
3596 tmp++;
3597 }
3598 curagent->var_pfx = strdup(curagent->id);
3599 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003600 if (curagent->engine_id == NULL)
3601 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003602
3603 if (LIST_ISEMPTY(&curmps)) {
3604 Warning("Proxy '%s': No message used by SPOE agent '%s' declared at %s:%d.\n",
3605 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3606 goto finish;
3607 }
3608
3609 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3610 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003611 struct spoe_arg *arg;
3612 unsigned int where;
3613
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003614 if (!strcmp(msg->id, mp->id)) {
3615 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3616 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3617 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3618 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3619 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3620 }
3621 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3622 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3623 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3624 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3625 px->id, msg->conf.file, msg->conf.line);
3626 goto next;
3627 }
3628 if (msg->event == SPOE_EV_NONE) {
3629 Warning("Proxy '%s': Ignore SPOE message without event at %s:%d.\n",
3630 px->id, msg->conf.file, msg->conf.line);
3631 goto next;
3632 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003633
3634 where = 0;
3635 switch (msg->event) {
3636 case SPOE_EV_ON_CLIENT_SESS:
3637 where |= SMP_VAL_FE_CON_ACC;
3638 break;
3639
3640 case SPOE_EV_ON_TCP_REQ_FE:
3641 where |= SMP_VAL_FE_REQ_CNT;
3642 break;
3643
3644 case SPOE_EV_ON_HTTP_REQ_FE:
3645 where |= SMP_VAL_FE_HRQ_HDR;
3646 break;
3647
3648 case SPOE_EV_ON_TCP_REQ_BE:
3649 if (px->cap & PR_CAP_FE)
3650 where |= SMP_VAL_FE_REQ_CNT;
3651 if (px->cap & PR_CAP_BE)
3652 where |= SMP_VAL_BE_REQ_CNT;
3653 break;
3654
3655 case SPOE_EV_ON_HTTP_REQ_BE:
3656 if (px->cap & PR_CAP_FE)
3657 where |= SMP_VAL_FE_HRQ_HDR;
3658 if (px->cap & PR_CAP_BE)
3659 where |= SMP_VAL_BE_HRQ_HDR;
3660 break;
3661
3662 case SPOE_EV_ON_SERVER_SESS:
3663 where |= SMP_VAL_BE_SRV_CON;
3664 break;
3665
3666 case SPOE_EV_ON_TCP_RSP:
3667 if (px->cap & PR_CAP_FE)
3668 where |= SMP_VAL_FE_RES_CNT;
3669 if (px->cap & PR_CAP_BE)
3670 where |= SMP_VAL_BE_RES_CNT;
3671 break;
3672
3673 case SPOE_EV_ON_HTTP_RSP:
3674 if (px->cap & PR_CAP_FE)
3675 where |= SMP_VAL_FE_HRS_HDR;
3676 if (px->cap & PR_CAP_BE)
3677 where |= SMP_VAL_BE_HRS_HDR;
3678 break;
3679
3680 default:
3681 break;
3682 }
3683
3684 list_for_each_entry(arg, &msg->args, list) {
3685 if (!(arg->expr->fetch->val & where)) {
3686 Warning("Proxy '%s': Ignore SPOE message at %s:%d: "
3687 "some args extract information from '%s', "
3688 "none of which is available here ('%s').\n",
3689 px->id, msg->conf.file, msg->conf.line,
3690 sample_ckp_names(arg->expr->fetch->use),
3691 sample_ckp_names(where));
3692 goto next;
3693 }
3694 }
3695
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003696 msg->agent = curagent;
3697 LIST_DEL(&msg->list);
3698 LIST_ADDQ(&curagent->messages[msg->event], &msg->list);
3699 goto next;
3700 }
3701 }
3702 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
3703 curagent->id, mp->id, curagent->conf.file, curagent->conf.line);
3704 goto error;
3705 next:
3706 continue;
3707 }
3708
3709 finish:
3710 conf->agent = curagent;
3711 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3712 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003713 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003714 }
3715 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3716 Warning("Proxy '%s': Ignore unused SPOE messages '%s' declared at %s:%d.\n",
3717 px->id, msg->id, msg->conf.file, msg->conf.line);
3718 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003719 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003720 }
3721
3722 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003723 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003724 fconf->ops = &spoe_ops;
3725 fconf->conf = conf;
3726 return 0;
3727
3728 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003729 spoe_release_agent(curagent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003730 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3731 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003732 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003733 }
3734 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3735 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003736 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003737 }
3738 free(conf);
3739 return -1;
3740}
3741
3742
3743/* Declare the filter parser for "spoe" keyword */
3744static struct flt_kw_list flt_kws = { "SPOE", { }, {
3745 { "spoe", parse_spoe_flt, NULL },
3746 { NULL, NULL, NULL },
3747 }
3748};
3749
3750__attribute__((constructor))
3751static void __spoe_init(void)
3752{
3753 flt_register_keywords(&flt_kws);
3754
3755 LIST_INIT(&curmsgs);
3756 LIST_INIT(&curmps);
3757 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003758 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003759}
3760
3761__attribute__((destructor))
3762static void
3763__spoe_deinit(void)
3764{
3765 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003766 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003767}