blob: a701c70745e74413e3e56bbd612c643b9a4a57a8 [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>
Emeric Bruna1dd2432017-06-21 15:42:52 +020021#include <common/hathreads.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020022
23#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020024#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010025#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020026
Christopher Faulet57583e42017-09-04 15:41:09 +020027#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020028#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020029#include <proto/arg.h>
30#include <proto/backend.h>
31#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010032#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033#include <proto/frontend.h>
34#include <proto/log.h>
35#include <proto/proto_http.h>
36#include <proto/proxy.h>
37#include <proto/sample.h>
38#include <proto/session.h>
39#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010040#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/stream.h>
42#include <proto/stream_interface.h>
43#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020044#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#include <proto/vars.h>
46
47#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
48#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#else
51#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010052#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020053#endif
54
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010055/* Reserved 4 bytes to the frame size. So a frame and its size can be written
56 * together in a buffer */
57#define MAX_FRAME_SIZE global.tune.bufsize - 4
58
59/* The minimum size for a frame */
60#define MIN_FRAME_SIZE 256
61
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Reserved for the metadata and the frame type.
63 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010064#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020065
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010066/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010067#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
68
Christopher Faulet3b386a32017-02-23 10:17:15 +010069/* SPOE filter id. Used to identify SPOE filters */
70const char *spoe_filter_id = "SPOE filter";
71
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020072/* Set if the handle on SIGUSR1 is registered */
73static int sighandler_registered = 0;
74
75/* proxy used during the parsing */
76struct proxy *curproxy = NULL;
77
78/* The name of the SPOE engine, used during the parsing */
79char *curengine = NULL;
80
81/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020082/* SPOE agent/group/message used during the parsing */
83struct spoe_agent *curagent = NULL;
84struct spoe_group *curgrp = NULL;
85struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086
87/* list of SPOE messages and placeholders used during the parsing */
88struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020089struct list curgrps;
90struct list curmphs;
91struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010092struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Faulet42bfa462017-01-04 14:14:19 +010094/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +010095static struct pool_head *pool_head_spoe_ctx = NULL;
96static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020097
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020098struct flt_ops spoe_ops;
99
Christopher Faulet8ef75252017-02-20 22:56:03 +0100100static int spoe_queue_context(struct spoe_context *ctx);
101static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
102static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200103
104/********************************************************************
105 * helper functions/globals
106 ********************************************************************/
107static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200108spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109{
Christopher Faulet11610f32017-09-21 10:23:10 +0200110 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200112 free(ph->id);
113 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114}
115
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100117spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118{
Christopher Faulet57583e42017-09-04 15:41:09 +0200119 struct spoe_arg *arg, *argback;
120 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121
122 if (!msg)
123 return;
124 free(msg->id);
125 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127 release_sample_expr(arg->expr);
128 free(arg->name);
129 LIST_DEL(&arg->list);
130 free(arg);
131 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200132 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
133 LIST_DEL(&acl->list);
134 prune_acl(acl);
135 free(acl);
136 }
137 if (msg->cond) {
138 prune_acl_cond(msg->cond);
139 free(msg->cond);
140 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200141 free(msg);
142}
143
144static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200145spoe_release_group(struct spoe_group *grp)
146{
147 if (!grp)
148 return;
149 free(grp->id);
150 free(grp->conf.file);
151 free(grp);
152}
153
154static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100155spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200156{
Christopher Faulet11610f32017-09-21 10:23:10 +0200157 struct spoe_message *msg, *msgback;
158 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200159 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200160
161 if (!agent)
162 return;
163 free(agent->id);
164 free(agent->conf.file);
165 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100166 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100167 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200168 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
169 LIST_DEL(&msg->list);
170 spoe_release_message(msg);
171 }
172 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
173 LIST_DEL(&grp->list);
174 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200175 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200176 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100177 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200178 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200179 free(agent);
180}
181
182static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100183 [SPOE_FRM_ERR_NONE] = "normal",
184 [SPOE_FRM_ERR_IO] = "I/O error",
185 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
186 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
187 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
188 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
189 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
190 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
191 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
192 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
193 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
194 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100195 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100196 [SPOE_FRM_ERR_RES] = "resource allocation error",
197 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200198};
199
200static const char *spoe_event_str[SPOE_EV_EVENTS] = {
201 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
202 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
203 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
204 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
205 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
206
207 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
208 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
209 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
210};
211
212
213#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
214
215static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100216 [SPOE_CTX_ST_NONE] = "NONE",
217 [SPOE_CTX_ST_READY] = "READY",
218 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
219 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
220 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
221 [SPOE_CTX_ST_DONE] = "DONE",
222 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200223};
224
225static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100226 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
227 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
228 [SPOE_APPCTX_ST_IDLE] = "IDLE",
229 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
230 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
231 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
232 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
233 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
234 [SPOE_APPCTX_ST_EXIT] = "EXIT",
235 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200236};
237
238#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100239
Christopher Faulet8ef75252017-02-20 22:56:03 +0100240/* Used to generates a unique id for an engine. On success, it returns a
241 * allocated string. So it is the caller's reponsibility to release it. If the
242 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100243static char *
244generate_pseudo_uuid()
245{
246 static int init = 0;
247
248 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
249 const char uuid_chr[] = "0123456789ABCDEF-";
250 char *uuid;
251 int i;
252
253 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
254 return NULL;
255
256 if (!init) {
257 srand(now_ms);
258 init = 1;
259 }
260
261 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
262 int r = rand () % 16;
263
264 switch (uuid_fmt[i]) {
265 case 'x' : uuid[i] = uuid_chr[r]; break;
266 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
267 default : uuid[i] = uuid_fmt[i]; break;
268 }
269 }
270 return uuid;
271}
272
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/********************************************************************
274 * Functions that encode/decode SPOE frames
275 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200276/* Helper to get static string length, excluding the terminating null byte */
277#define SLEN(str) (sizeof(str)-1)
278
279/* Predefined key used in HELLO/DISCONNECT frames */
280#define SUPPORTED_VERSIONS_KEY "supported-versions"
281#define VERSION_KEY "version"
282#define MAX_FRAME_SIZE_KEY "max-frame-size"
283#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100284#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100285#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200286#define STATUS_CODE_KEY "status-code"
287#define MSG_KEY "message"
288
289struct spoe_version {
290 char *str;
291 int min;
292 int max;
293};
294
295/* All supported versions */
296static struct spoe_version supported_versions[] = {
297 {"1.0", 1000, 1000},
298 {NULL, 0, 0}
299};
300
301/* Comma-separated list of supported versions */
302#define SUPPORTED_VERSIONS_VAL "1.0"
303
Christopher Faulet8ef75252017-02-20 22:56:03 +0100304/* Convert a string to a SPOE version value. The string must follow the format
305 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
306 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200309{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100310 const char *p, *end;
311 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200312
Christopher Faulet8ef75252017-02-20 22:56:03 +0100313 p = str;
314 end = str+len;
315 maj = min = 0;
316 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200317
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 /* skip leading spaces */
319 while (p < end && isspace(*p))
320 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200321
Christopher Faulet8ef75252017-02-20 22:56:03 +0100322 /* parse Major number, until the '.' */
323 while (*p != '.') {
324 if (p >= end || *p < '0' || *p > '9')
325 goto out;
326 maj *= 10;
327 maj += (*p - '0');
328 p++;
329 }
330
331 /* check Major version */
332 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200333 goto out;
334
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 p++; /* skip the '.' */
336 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
337 goto out;
338
339 /* Parse Minor number */
340 while (p < end) {
341 if (*p < '0' || *p > '9')
342 break;
343 min *= 10;
344 min += (*p - '0');
345 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347
348 /* check Minor number */
349 if (min > 999)
350 goto out;
351
352 /* skip trailing spaces */
353 while (p < end && isspace(*p))
354 p++;
355 if (p != end)
356 goto out;
357
358 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200359 out:
360 return vsn;
361}
362
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
364 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
365 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200368{
Christopher Faulet305c6072017-02-23 16:17:53 +0100369 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100370 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100371 char *p, *end;
372 unsigned int flags = SPOE_FRM_FL_FIN;
373 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200374
Christopher Faulet8ef75252017-02-20 22:56:03 +0100375 p = frame;
376 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200377
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378 /* Set Frame type */
379 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200380
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100381 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100382 memcpy(p, (char *)&flags, 4);
383 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200387
388 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
389 * and "capabilities" */
390
391 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392 sz = SLEN(SUPPORTED_VERSIONS_KEY);
393 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
394 goto too_big;
395
396 *p++ = SPOE_DATA_T_STR;
397 sz = SLEN(SUPPORTED_VERSIONS_VAL);
398 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
399 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
401 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 sz = SLEN(MAX_FRAME_SIZE_KEY);
403 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
404 goto too_big;
405
406 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200407 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
410 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100411 sz = SLEN(CAPABILITIES_KEY);
412 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
413 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200414
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100416 chk = get_trash_chunk();
417 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
418 memcpy(chk->str, "pipelining", 10);
419 chk->len += 10;
420 }
421 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
422 if (chk->len) chk->str[chk->len++] = ',';
423 memcpy(chk->str+chk->len, "async", 5);
424 chk->len += 5;
425 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100426 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
427 if (chk->len) chk->str[chk->len++] = ',';
428 memcpy(chk->str+chk->len, "fragmentation", 13);
429 chk->len += 5;
430 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100431 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 goto too_big;
433
434 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100435 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 sz = SLEN(ENGINE_ID_KEY);
437 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
438 goto too_big;
439
440 *p++ = SPOE_DATA_T_STR;
441 sz = strlen(agent->engine_id);
442 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
443 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100444 }
445
Christopher Faulet8ef75252017-02-20 22:56:03 +0100446 return (p - frame);
447
448 too_big:
449 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
450 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200451}
452
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
454 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
455 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200456static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 const char *reason;
460 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100461 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 p = frame;
465 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200466
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 /* Set Frame type */
468 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100470 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100471 memcpy(p, (char *)&flags, 4);
472 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473
474 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 *p++ = 0; *p++ = 0;
476
477 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
478 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
480 /* There are 2 mandatory items: "status-code" and "message" */
481
482 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 sz = SLEN(STATUS_CODE_KEY);
484 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
485 goto too_big;
486
487 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200488 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 sz = SLEN(MSG_KEY);
493 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
494 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 /*Get the message corresponding to the status code */
497 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
498
499 *p++ = SPOE_DATA_T_STR;
500 sz = strlen(reason);
501 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
502 goto too_big;
503
504 return (p - frame);
505
506 too_big:
507 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
508 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509}
510
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
512 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
513 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100515spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100516 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 char *p, *end;
519 unsigned int stream_id, frame_id;
520 unsigned int flags = SPOE_FRM_FL_FIN;
521 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 p = frame;
524 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100526 stream_id = ctx->stream_id;
527 frame_id = ctx->frame_id;
528
529 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
530 /* The fragmentation is not supported by the applet */
531 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
532 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
533 return -1;
534 }
535 flags = ctx->frag_ctx.flags;
536 }
537
538 /* Set Frame type */
539 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
540
541 /* Set flags */
542 memcpy(p, (char *)&flags, 4);
543 p += 4;
544
545 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200546 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100547 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200548 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100549 goto too_big;
550
551 /* Copy encoded messages, if possible */
552 sz = ctx->buffer->i;
553 if (p + sz >= end)
554 goto too_big;
555 memcpy(p, ctx->buffer->p, sz);
556 p += sz;
557
558 return (p - frame);
559
560 too_big:
561 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
562 return 0;
563}
564
565/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
566 * returns the number of encoded bytes in the frame on success, 0 if an encoding
567 * error occurred and -1 if a fatal error occurred. */
568static int
569spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
570 char *frame, size_t size)
571{
572 char *p, *end;
573 unsigned int stream_id, frame_id;
574 unsigned int flags;
575 size_t sz;
576
577 p = frame;
578 end = frame+size;
579
Christopher Faulet8ef75252017-02-20 22:56:03 +0100580 /* <ctx> is null when the stream has aborted the processing of a
581 * fragmented frame. In this case, we must notify the corresponding
582 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100583 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100584 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100585 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
586 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
587 }
588 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100589 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100590 stream_id = ctx->stream_id;
591 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100592 }
593
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100595 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100597 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 memcpy(p, (char *)&flags, 4);
599 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200600
601 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200602 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200604 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200606
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100607 if (ctx == NULL)
608 goto end;
609
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100611 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 if (p + sz >= end)
613 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100614 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100616
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100617 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 return (p - frame);
619
620 too_big:
621 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
622 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200623}
624
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625/* Decode and process the HELLO frame sent by an agent. It returns the number of
626 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
627 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630{
Christopher Faulet305c6072017-02-23 16:17:53 +0100631 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
632 char *p, *end;
633 int vsn, max_frame_size;
634 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635
636 p = frame;
637 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200638
639 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200642 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (size < 7 /* TYPE + METADATA */) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
647 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648 }
649
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100650 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 memcpy((char *)&flags, p, 4);
652 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100654 /* Fragmentation is not supported for HELLO frame */
655 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100657 return -1;
658 }
659
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200660 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661 if (*p != 0 || *(p+1) != 0) {
662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
663 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666
667 /* There are 3 mandatory items: "version", "max-frame-size" and
668 * "capabilities" */
669
670 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100671 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 while (p < end) {
673 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200674 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200676
677 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 ret = spoe_decode_buffer(&p, end, &str, &sz);
679 if (ret == -1 || !sz) {
680 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
681 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200684 /* Check "version" K/V item */
685 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 int i, type = *p++;
687
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
690 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
691 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
694 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
695 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696 }
697
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 return -1;
702 }
703 for (i = 0; supported_versions[i].str != NULL; ++i) {
704 if (vsn >= supported_versions[i].min &&
705 vsn <= supported_versions[i].max)
706 break;
707 }
708 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710 return -1;
711 }
712 }
713 /* Check "max-frame-size" K/V item */
714 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716
717 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
719 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
720 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
721 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100722 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
723 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200725 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
727 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200728 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 if (sz < MIN_FRAME_SIZE ||
730 sz > SPOE_APPCTX(appctx)->max_frame_size) {
731 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732 return -1;
733 }
734 max_frame_size = sz;
735 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100736 /* Check "capabilities" K/V item */
737 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100738 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739
740 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100741 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
743 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100744 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
748 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749
Christopher Faulet8ef75252017-02-20 22:56:03 +0100750 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100751 char *delim;
752
753 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
757 str += 10; sz -= 10;
758 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759 flags |= SPOE_APPCTX_FL_PIPELINING;
760 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 else if (sz >= 5 && !strncmp(str, "async", 5)) {
762 str += 5; sz -= 5;
763 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 flags |= SPOE_APPCTX_FL_ASYNC;
765 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
767 str += 13; sz -= 13;
768 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100769 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
770 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 /* Get the next comma or break */
773 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 delim++;
776 sz -= (delim - str);
777 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 }
779 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200780 else {
781 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100782 if (spoe_skip_data(&p, end) == -1) {
783 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
784 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200785 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 }
787 }
788
789 /* Final checks */
790 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792 return -1;
793 }
794 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 return -1;
797 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100798 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
799 flags &= ~SPOE_APPCTX_FL_PIPELINING;
800 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
801 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802
Christopher Faulet42bfa462017-01-04 14:14:19 +0100803 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
804 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
805 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806
807 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200808}
809
810/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
811 * bytes on success, 0 if the frame can be ignored and -1 if an error
812 * occurred. */
813static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100814spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100817 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818
819 p = frame;
820 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200821
822 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
824 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828 if (size < 7 /* TYPE + METADATA */) {
829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
830 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831 }
832
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100833 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 memcpy((char *)&flags, p, 4);
835 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100837 /* Fragmentation is not supported for DISCONNECT frame */
838 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100840 return -1;
841 }
842
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 if (*p != 0 || *(p+1) != 0) {
845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
846 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849
850 /* There are 2 mandatory items: "status-code" and "message" */
851
852 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 while (p < end) {
854 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200855 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857
858 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 ret = spoe_decode_buffer(&p, end, &str, &sz);
860 if (ret == -1 || !sz) {
861 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
862 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 }
864
865 /* Check "status-code" K/V item */
866 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868
869 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200870 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
871 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
872 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
873 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
875 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200877 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
879 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100881 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 }
883
884 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 else if (!memcmp(str, MSG_KEY, sz)) {
886 int type = *p++;
887
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
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 ret = spoe_decode_buffer(&p, end, &str, &sz);
894 if (ret == -1 || sz > 255) {
895 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
896 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
899 SPOE_APPCTX(appctx)->reason = str;
900 SPOE_APPCTX(appctx)->rlen = sz;
901#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
903 else {
904 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 if (spoe_skip_data(&p, end) == -1) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 }
910 }
911
Christopher Faulet8ef75252017-02-20 22:56:03 +0100912 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913}
914
915
Christopher Fauleta1cda022016-12-21 08:58:06 +0100916/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 * success, 0 if the frame can be ignored and -1 if an error occurred. */
918static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100919spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100920 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100922 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100924 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100926 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927
928 p = frame;
929 end = frame + size;
930 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200931
932 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
934 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 if (size < 7 /* TYPE + METADATA */) {
939 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
940 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941 }
942
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100943 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 memcpy((char *)&flags, p, 4);
945 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100947 /* Fragmentation is not supported for now */
948 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100950 return -1;
951 }
952
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200953 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200954 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100956 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200958 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100962
Christopher Faulet8ef75252017-02-20 22:56:03 +0100963 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100964 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200965 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100966 if ((*ctx)->stream_id == (unsigned int)stream_id &&
967 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100968 goto found;
969 }
970 }
971 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100972 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
973 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100975 goto found;
976 }
977 }
978
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100979 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
980 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
981 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
982
983 /* ABRT bit is set for an unfinished fragmented frame */
984 if (flags & SPOE_FRM_FL_ABRT) {
985 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100986 (*ctx)->state = SPOE_CTX_ST_ERROR;
987 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
988 /* Ignore the payload */
989 goto end;
990 }
991 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
992 /* For now, we ignore the ack */
993 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
994 return 0;
995 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100996
Christopher Fauleta1cda022016-12-21 08:58:06 +0100997 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100998 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
999 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001000 " - stream-id=%u - frame-id=%u\n",
1001 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1002 __FUNCTION__, appctx,
1003 (unsigned int)stream_id, (unsigned int)frame_id);
1004
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001005 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001006 return 0;
1007
1008 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001009 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1010 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001012 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001013 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001014
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001015 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001016 len = (end - p);
1017 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1018 SPOE_APPCTX(appctx)->buffer->i = len;
1019 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001020
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001021 /* Transfer the buffer ownership to the SPOE context */
1022 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1023 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024
Christopher Faulet8ef75252017-02-20 22:56:03 +01001025 (*ctx)->state = SPOE_CTX_ST_DONE;
1026
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001027 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001028 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 " - ACK frame received"
1030 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1033 (*ctx)->frame_id, flags);
1034 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001035}
1036
Christopher Fauletba7bc162016-11-07 21:07:38 +01001037/* This function is used in cfgparse.c and declared in proto/checks.h. It
1038 * prepare the request to send to agents during a healthcheck. It returns 0 on
1039 * success and -1 if an error occurred. */
1040int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001042{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 struct appctx appctx;
1044 struct spoe_appctx spoe_appctx;
1045 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1046 size_t sz;
1047 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001048
Christopher Faulet42bfa462017-01-04 14:14:19 +01001049 memset(&appctx, 0, sizeof(appctx));
1050 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001051 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001052
1053 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001054 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1057 end = frame + MAX_FRAME_SIZE;
1058
1059 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1060 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001061 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001062 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001063
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064 /* Add "healthcheck" K/V item */
1065 sz = SLEN(HEALTHCHECK_KEY);
1066 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1067 return -1;
1068 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 *len = frame - buf;
1071 sz = htonl(*len - 4);
1072 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001077 return 0;
1078}
1079
1080/* This function is used in checks.c and declared in proto/checks.h. It decode
1081 * the response received from an agent during a healthcheck. It returns 0 on
1082 * success and -1 if an error occurred. */
1083int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001086 struct appctx appctx;
1087 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088
Christopher Faulet42bfa462017-01-04 14:14:19 +01001089 memset(&appctx, 0, sizeof(appctx));
1090 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001091
Christopher Faulet42bfa462017-01-04 14:14:19 +01001092 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001093 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001094
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1096 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097 goto error;
1098 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001099 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1100 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101
1102 return 0;
1103
1104 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001105 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1106 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1107 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108 return -1;
1109}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001110
Christopher Fauleta1cda022016-12-21 08:58:06 +01001111/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1112 * the frame can be ignored, 1 to retry later, and the frame legnth on
1113 * success. */
1114static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001116{
1117 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 int ret;
1119 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001120
Christopher Faulet8ef75252017-02-20 22:56:03 +01001121 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1122 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001123 netint = htonl(framesz);
1124 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001125 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001126 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001127 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130 }
1131 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132 return -1; /* error */
1133 }
1134 return framesz;
1135}
1136
1137/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1138 * when the frame can be ignored, 1 to retry later and the frame length on
1139 * success. */
1140static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142{
1143 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144 int ret;
1145 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146
Willy Tarreau06d80a92017-10-19 14:32:15 +02001147 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001148 if (ret > 0) {
1149 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001150 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001151 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return -1;
1153 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001154 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 }
1156 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 }
1160 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 return -1; /* error */
1162 }
1163 return framesz;
1164}
1165
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001166/********************************************************************
1167 * Functions that manage the SPOE applet
1168 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001169static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001171{
1172 si_applet_want_get(appctx->owner);
1173 si_applet_want_put(appctx->owner);
1174 appctx_wakeup(appctx);
1175 return 1;
1176}
1177
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001178/* Callback function that catches applet timeouts. If a timeout occurred, we set
1179 * <appctx->st1> flag and the SPOE applet is woken up. */
1180static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001182{
1183 struct appctx *appctx = task->context;
1184
1185 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1186 if (tick_is_expired(task->expire, now_ms)) {
1187 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001189 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001190 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001191 return task;
1192}
1193
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001194/* Callback function that releases a SPOE applet. This happens when the
1195 * connection with the agent is closed. */
1196static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001199 struct stream_interface *si = appctx->owner;
1200 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1201 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001202 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001203
1204 if (spoe_appctx == NULL)
1205 return;
1206
1207 appctx->ctx.spoe.ptr = NULL;
1208 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001209
1210 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1211 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1212 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 /* Remove applet from the list of running applets */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001215 SPOE_DEBUG_STMT(agent->rt[tid].applets_act--);
1216 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1218 LIST_DEL(&spoe_appctx->list);
1219 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001221 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001222
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001224 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001225 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1226 eb32_delete(&spoe_appctx->node);
1227 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1228 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001230 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001231 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1232 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001233
1234 si_shutw(si);
1235 si_shutr(si);
1236 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001237 }
1238
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 /* Destroy the task attached to this applet */
1240 if (spoe_appctx->task) {
1241 task_delete(spoe_appctx->task);
1242 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001243 }
1244
Christopher Faulet8ef75252017-02-20 22:56:03 +01001245 /* Notify all waiting streams */
1246 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247 LIST_DEL(&ctx->list);
1248 LIST_INIT(&ctx->list);
1249 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 }
1253
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254 /* If the applet was processing a fragmented frame, notify the
1255 * corresponding stream. */
1256 if (spoe_appctx->frag_ctx.ctx) {
1257 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001258 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001259 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001261 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1262 }
1263
Christopher Faulet8ef75252017-02-20 22:56:03 +01001264 /* Release allocated memory */
1265 spoe_release_buffer(&spoe_appctx->buffer,
1266 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001267 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001268
Christopher Faulet24289f22017-09-25 14:48:02 +02001269 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001270 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001271
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001273 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 LIST_DEL(&ctx->list);
1275 LIST_INIT(&ctx->list);
1276 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001278 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001279 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001280 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001281 LIST_DEL(&ctx->list);
1282 LIST_INIT(&ctx->list);
1283 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001284 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001285 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1286 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001287
1288 end:
1289 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001290 agent->rt[tid].frame_size = agent->max_frame_size;
1291 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1292 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293}
1294
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001295static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001296spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001297{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001298 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001299 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001301 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001302
Christopher Fauleta1cda022016-12-21 08:58:06 +01001303 if (si->state <= SI_ST_CON) {
1304 si_applet_want_put(si);
1305 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1306 goto stop;
1307 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001308 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001309 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001311 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001312
Christopher Fauleta1cda022016-12-21 08:58:06 +01001313 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001314 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1315 " - Connection timed out\n",
1316 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1317 __FUNCTION__, appctx);
1318 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001320 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001321
Christopher Faulet42bfa462017-01-04 14:14:19 +01001322 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 SPOE_APPCTX(appctx)->task->expire =
1324 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001325
Christopher Faulet8ef75252017-02-20 22:56:03 +01001326 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1327 * length. */
1328 buf = trash.str; frame = buf+4;
1329 ret = spoe_prepare_hahello_frame(appctx, frame,
1330 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001332 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333
1334 switch (ret) {
1335 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 case 0: /* ignore => an error, cannot be ignored */
1337 goto exit;
1338
1339 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001340 goto stop;
1341
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 default:
1343 /* HELLO frame successfully sent, now wait for the
1344 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1346 goto next;
1347 }
1348
1349 next:
1350 return 0;
1351 stop:
1352 return 1;
1353 exit:
1354 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1355 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356}
1357
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001358static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001360{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001361 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001362 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001363 char *frame;
1364 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001365
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001366
Christopher Fauletb067b062017-01-04 16:39:11 +01001367 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001368 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001370 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001371
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001373 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1374 " - Connection timed out\n",
1375 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1376 __FUNCTION__, appctx);
1377 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 goto exit;
1379 }
1380
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381 frame = trash.str; trash.len = 0;
1382 ret = spoe_recv_frame(appctx, frame,
1383 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 if (ret > 1) {
1385 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1386 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1387 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 trash.len = ret + 4;
1390 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001391 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001392
Christopher Fauleta1cda022016-12-21 08:58:06 +01001393 switch (ret) {
1394 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001396 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1397 goto next;
1398
1399 case 1: /* retry later */
1400 goto stop;
1401
1402 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 /* HELLO handshake is finished, set the idle timeout and
1404 * add the applet in the list of running applets. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001405 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001407 SPOE_APPCTX(appctx)->node.key = 0;
1408 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001409
1410 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001411 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001416 /* Do not forget to remove processed frame from the output buffer */
1417 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001418 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001419
1420 SPOE_APPCTX(appctx)->task->expire =
1421 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001422 return 0;
1423 stop:
1424 return 1;
1425 exit:
1426 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1427 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001428}
1429
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001430
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001432spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001434 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1435 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001436 char *frame, *buf;
1437 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001438
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1440 * length. */
1441 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001442
1443 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1444 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1445 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1446 SPOE_APPCTX(appctx)->max_frame_size);
1447 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001448 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001449 *skip = 1;
1450 ret = 1;
1451 goto end;
1452 }
1453 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001454 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001455 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1456 SPOE_APPCTX(appctx)->max_frame_size);
1457
1458 }
1459
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 if (ret > 1)
1461 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001462
Christopher Faulet8ef75252017-02-20 22:56:03 +01001463 switch (ret) {
1464 case -1: /* error */
1465 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1466 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001467
Christopher Faulet8ef75252017-02-20 22:56:03 +01001468 case 0: /* ignore */
1469 if (ctx == NULL)
1470 goto abort_frag_frame;
1471
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001472 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001473 LIST_DEL(&ctx->list);
1474 LIST_INIT(&ctx->list);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001475 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 ctx->state = SPOE_CTX_ST_ERROR;
1477 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1478 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001479 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001480 break;
1481
1482 case 1: /* retry */
1483 *skip = 1;
1484 break;
1485
1486 default:
1487 if (ctx == NULL)
1488 goto abort_frag_frame;
1489
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001490 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 LIST_DEL(&ctx->list);
1492 LIST_INIT(&ctx->list);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001493 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001494 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1495 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1496 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001497 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001498 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001499 }
1500 goto end;
1501
1502 frag_frame_sent:
1503 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001504 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1506 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1507 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1509 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1510 goto end;
1511
1512 no_frag_frame_sent:
1513 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1514 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001515 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001516 }
1517 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1518 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1519 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1520 }
1521 else {
1522 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001523 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001524 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1525 }
1526 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1527 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1528 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001529 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001530
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1532 goto end;
1533
1534 abort_frag_frame:
1535 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1536 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1537 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1538 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1539 goto end;
1540
1541 end:
1542 return ret;
1543}
1544
1545static int
1546spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1547{
1548 struct spoe_context *ctx = NULL;
1549 char *frame;
1550 int ret;
1551
1552 frame = trash.str; trash.len = 0;
1553 ret = spoe_recv_frame(appctx, frame,
1554 SPOE_APPCTX(appctx)->max_frame_size);
1555 if (ret > 1) {
1556 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1557 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001558 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001559 goto end;
1560 }
1561 trash.len = ret + 4;
1562 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1563 }
1564 switch (ret) {
1565 case -1: /* error */
1566 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1567 break;
1568
1569 case 0: /* ignore */
1570 break;
1571
1572 case 1: /* retry */
1573 *skip = 1;
1574 break;
1575
1576 default:
1577 LIST_DEL(&ctx->list);
1578 LIST_INIT(&ctx->list);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001579 if (ctx->spoe_appctx) {
1580 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001581 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001582 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001583 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1584 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1585 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1586 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1587 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1588 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1589 }
1590 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1591 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1592
Christopher Faulet8ef75252017-02-20 22:56:03 +01001593 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1594 break;
1595 }
1596
1597 /* Do not forget to remove processed frame from the output buffer */
1598 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001599 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001600 end:
1601 return ret;
1602}
1603
1604static int
1605spoe_handle_processing_appctx(struct appctx *appctx)
1606{
1607 struct stream_interface *si = appctx->owner;
1608 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001609 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610
1611 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1612 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1613 goto exit;
1614 }
1615
1616 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1617 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1618 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1619 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1620 goto next;
1621 }
1622
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001623 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001624 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001625 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001626 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1627 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001628 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001629
Christopher Faulet8f82b202018-01-24 16:23:03 +01001630 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1631 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001632
Christopher Faulet8f82b202018-01-24 16:23:03 +01001633 /* receiving_frame loop */
1634 while (!skip_receiving) {
1635 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1636 switch (ret) {
1637 case -1: /* error */
1638 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001639
Christopher Faulet8f82b202018-01-24 16:23:03 +01001640 case 0: /* ignore */
1641 active_r = 1;
1642 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001643
Christopher Faulet8f82b202018-01-24 16:23:03 +01001644 case 1: /* retry */
1645 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001646
Christopher Faulet8f82b202018-01-24 16:23:03 +01001647 default:
1648 active_r = 1;
1649 break;
1650 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001651 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001652
Christopher Faulet8f82b202018-01-24 16:23:03 +01001653 /* send_frame loop */
1654 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1655 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1656 switch (ret) {
1657 case -1: /* error */
1658 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001659
Christopher Faulet8f82b202018-01-24 16:23:03 +01001660 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001661 if (SPOE_APPCTX(appctx)->node.key)
1662 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001663 active_s++;
1664 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001665
Christopher Faulet8f82b202018-01-24 16:23:03 +01001666 case 1: /* retry */
1667 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001668
Christopher Faulet8f82b202018-01-24 16:23:03 +01001669 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001670 if (SPOE_APPCTX(appctx)->node.key)
1671 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001672 active_s++;
1673 break;
1674 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001675 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001676
Christopher Faulet8f82b202018-01-24 16:23:03 +01001677 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1679 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1680 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001681
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001683 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001685 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686 }
1687 return 1;
1688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 next:
1690 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1691 return 0;
1692
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693 exit:
1694 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1695 return 0;
1696}
1697
1698static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001700{
1701 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001702 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001703 char *frame, *buf;
1704 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001705
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1707 goto exit;
1708
1709 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1710 goto exit;
1711
Christopher Faulet8ef75252017-02-20 22:56:03 +01001712 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1713 * length. */
1714 buf = trash.str; frame = buf+4;
1715 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1716 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001718 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001719
1720 switch (ret) {
1721 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001722 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723 goto exit;
1724
1725 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001726 goto stop;
1727
1728 default:
1729 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1730 " - disconnected by HAProxy (%d): %s\n",
1731 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001732 __FUNCTION__, appctx,
1733 SPOE_APPCTX(appctx)->status_code,
1734 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001735
1736 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1737 goto next;
1738 }
1739
1740 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001741 SPOE_APPCTX(appctx)->task->expire =
1742 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001743 return 0;
1744 stop:
1745 return 1;
1746 exit:
1747 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1748 return 0;
1749}
1750
1751static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001752spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753{
1754 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 char *frame;
1756 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757
Christopher Fauletb067b062017-01-04 16:39:11 +01001758 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001760 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001761 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762
Christopher Fauletb067b062017-01-04 16:39:11 +01001763 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001766 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 frame = trash.str; trash.len = 0;
1769 ret = spoe_recv_frame(appctx, frame,
1770 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 trash.len = ret + 4;
1773 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 }
1775
1776 switch (ret) {
1777 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1779 " - error on frame (%s)\n",
1780 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001781 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784 goto exit;
1785
1786 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 goto next;
1788
1789 case 1: /* retry */
1790 goto stop;
1791
1792 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001796 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001797 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1798 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799 goto exit;
1800 }
1801
1802 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001803 /* Do not forget to remove processed frame from the output buffer */
1804 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001805 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 return 0;
1808 stop:
1809 return 1;
1810 exit:
1811 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1812 return 0;
1813}
1814
1815/* I/O Handler processing messages exchanged with the agent */
1816static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001817spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001819 struct stream_interface *si = appctx->owner;
1820 struct spoe_agent *agent;
1821
1822 if (SPOE_APPCTX(appctx) == NULL)
1823 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1826 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001827
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 switchstate:
1829 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1830 " - appctx-state=%s\n",
1831 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1832 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1833
1834 switch (appctx->st0) {
1835 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 goto out;
1838 goto switchstate;
1839
1840 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001841 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 goto out;
1843 goto switchstate;
1844
1845 case SPOE_APPCTX_ST_IDLE:
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001846 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1847 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001849 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001850 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001851 SPOE_APPCTX(appctx)->task->expire =
1852 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001854 goto switchstate;
1855 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001856 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1857 /* fall through */
1858
1859 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001860 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1861 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 goto out;
1864 goto switchstate;
1865
1866 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto out;
1869 goto switchstate;
1870
1871 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 goto out;
1874 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001875
1876 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001877 appctx->st0 = SPOE_APPCTX_ST_END;
1878 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1879
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001880 si_shutw(si);
1881 si_shutr(si);
1882 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001883 /* fall through */
1884
1885 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001886 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001887 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001889 if (stopping)
1890 spoe_wakeup_appctx(appctx);
1891
Christopher Faulet42bfa462017-01-04 14:14:19 +01001892 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1893 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001894 si_oc(si)->flags |= CF_READ_DONTWAIT;
1895 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001896}
1897
1898struct applet spoe_applet = {
1899 .obj_type = OBJ_TYPE_APPLET,
1900 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001901 .fct = spoe_handle_appctx,
1902 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001903};
1904
1905/* Create a SPOE applet. On success, the created applet is returned, else
1906 * NULL. */
1907static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001909{
1910 struct appctx *appctx;
1911 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001912 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001913
Emeric Brun1138fd02017-06-19 12:38:55 +02001914 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001915 goto out_error;
1916
Willy Tarreaubafbe012017-11-24 17:34:44 +01001917 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001918 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001919 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001920 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001921
Christopher Faulet42bfa462017-01-04 14:14:19 +01001922 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001923 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001924 goto out_free_spoe_appctx;
1925
1926 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001927 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001928 SPOE_APPCTX(appctx)->task->context = appctx;
1929 SPOE_APPCTX(appctx)->agent = conf->agent;
1930 SPOE_APPCTX(appctx)->version = 0;
1931 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1932 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001933 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001934 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001935 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001936
1937 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1938 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001939 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001940
1941 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1942 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943
Willy Tarreau5820a362016-12-22 15:59:02 +01001944 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945 if (!sess)
1946 goto out_free_spoe;
1947
Willy Tarreau87787ac2017-08-28 16:22:54 +02001948 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001949 goto out_free_sess;
1950
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951 stream_set_backend(strm, conf->agent->b.be);
1952
1953 /* applet is waiting for data */
1954 si_applet_cant_get(&strm->si[0]);
1955 appctx_wakeup(appctx);
1956
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957 strm->do_log = NULL;
1958 strm->res.flags |= CF_READ_DONTWAIT;
1959
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001960 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001961 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001962 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001963 SPOE_DEBUG_STMT(conf->agent->rt[tid].applets_act++);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001964
Emeric Brunc60def82017-09-27 14:59:38 +02001965 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
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:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001975 pool_free(pool_head_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 Fauletf7e4e7e2016-10-27 22:29:49 +02001989
Christopher Fauleta1cda022016-12-21 08:58:06 +01001990 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001991 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01001992 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994
1995 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001996 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
1998 ctx->strm);
1999
Christopher Fauleta1cda022016-12-21 08:58:06 +01002000 /* Do not try to create a new applet if there is no server up for the
2001 * agent's backend. */
2002 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2003 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2004 " - cannot create SPOE appctx: no server up\n",
2005 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2006 __FUNCTION__, ctx->strm);
2007 goto end;
2008 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009
Christopher Fauleta1cda022016-12-21 08:58:06 +01002010 /* Do not try to create a new applet if we have reached the maximum of
2011 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002012 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002013 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002014 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2015 " - cannot create SPOE appctx: max CPS reached\n",
2016 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2017 __FUNCTION__, ctx->strm);
2018 goto end;
2019 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020 }
2021
Christopher Faulet8ef75252017-02-20 22:56:03 +01002022 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002023 if (appctx == NULL) {
2024 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2025 " - failed to create SPOE appctx\n",
2026 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2027 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002028 send_log(ctx->strm->be, LOG_EMERG,
2029 "SPOE: [%s] failed to create SPOE applet\n",
2030 agent->id);
2031
Christopher Fauleta1cda022016-12-21 08:58:06 +01002032 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033 }
2034
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 /* Increase the per-process number of cumulated connections */
2036 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002037 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 end:
2040 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002041 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002042 ctx->status_code = SPOE_CTX_ERR_RES;
2043 return -1;
2044 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002046 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002047 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048
2049 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002050 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002051 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002053 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002054 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002055
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002056 /* Finally try to wakeup an IDLE applet. */
2057 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2058 struct eb32_node *node;
2059
2060 node = eb32_first(&agent->rt[tid].idle_applets);
2061 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2062 if (node && spoe_appctx) {
2063 eb32_delete(&spoe_appctx->node);
2064 spoe_appctx->node.key++;
2065 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2066 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002067 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002070}
2071
2072/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002073 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002075/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2076 * fragmented_content. If the next message can be processed, it returns 0. If
2077 * the message is too big, it returns -1.*/
2078static int
2079spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2080 struct spoe_message *msg, int dir,
2081 char **buf, char *end)
2082{
2083 struct sample *smp;
2084 struct spoe_arg *arg;
2085 int ret;
2086
2087 if (msg->cond) {
2088 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2089 ret = acl_pass(ret);
2090 if (msg->cond->pol == ACL_COND_UNLESS)
2091 ret = !ret;
2092
2093 /* the rule does not match */
2094 if (!ret)
2095 goto next;
2096 }
2097
2098 /* Resume encoding of a SPOE argument */
2099 if (ctx->frag_ctx.curarg != NULL) {
2100 arg = ctx->frag_ctx.curarg;
2101 goto encode_argument;
2102 }
2103
2104 if (ctx->frag_ctx.curoff != UINT_MAX)
2105 goto encode_msg_payload;
2106
2107 /* Check if there is enough space for the message name and the
2108 * number of arguments. It implies <msg->id_len> is encoded on 2
2109 * bytes, at most (< 2288). */
2110 if (*buf + 2 + msg->id_len + 1 > end)
2111 goto too_big;
2112
2113 /* Encode the message name */
2114 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2115 goto too_big;
2116
2117 /* Set the number of arguments for this message */
2118 **buf = msg->nargs;
2119 (*buf)++;
2120
2121 ctx->frag_ctx.curoff = 0;
2122 encode_msg_payload:
2123
2124 /* Loop on arguments */
2125 list_for_each_entry(arg, &msg->args, list) {
2126 ctx->frag_ctx.curarg = arg;
2127 ctx->frag_ctx.curoff = UINT_MAX;
2128
2129 encode_argument:
2130 if (ctx->frag_ctx.curoff != UINT_MAX)
2131 goto encode_arg_value;
2132
2133 /* Encode the arguement name as a string. It can by NULL */
2134 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2135 goto too_big;
2136
2137 ctx->frag_ctx.curoff = 0;
2138 encode_arg_value:
2139
2140 /* Fetch the arguement value */
2141 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2142 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2143 if (ret == -1 || ctx->frag_ctx.curoff)
2144 goto too_big;
2145 }
2146
2147 next:
2148 return 0;
2149
2150 too_big:
2151 return -1;
2152}
2153
Christopher Fauletc718b822017-09-21 16:50:56 +02002154/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2155 * handle fragmented content. On success it returns 1. If an error occurred, -1
2156 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2157 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002158static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002159spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002160 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002161{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002162 struct spoe_config *conf = FLT_CONF(ctx->filter);
2163 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002164 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002165 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002166
Christopher Faulet8ef75252017-02-20 22:56:03 +01002167 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002168 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002169
Christopher Fauletc718b822017-09-21 16:50:56 +02002170 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2171 /* Resume encoding of a SPOE message */
2172 if (ctx->frag_ctx.curmsg != NULL) {
2173 msg = ctx->frag_ctx.curmsg;
2174 goto encode_evt_message;
2175 }
2176
2177 list_for_each_entry(msg, messages, by_evt) {
2178 ctx->frag_ctx.curmsg = msg;
2179 ctx->frag_ctx.curarg = NULL;
2180 ctx->frag_ctx.curoff = UINT_MAX;
2181
2182 encode_evt_message:
2183 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2184 goto too_big;
2185 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002186 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002187 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2188 /* Resume encoding of a SPOE message */
2189 if (ctx->frag_ctx.curmsg != NULL) {
2190 msg = ctx->frag_ctx.curmsg;
2191 goto encode_grp_message;
2192 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002193
Christopher Fauletc718b822017-09-21 16:50:56 +02002194 list_for_each_entry(msg, messages, by_grp) {
2195 ctx->frag_ctx.curmsg = msg;
2196 ctx->frag_ctx.curarg = NULL;
2197 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002198
Christopher Fauletc718b822017-09-21 16:50:56 +02002199 encode_grp_message:
2200 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2201 goto too_big;
2202 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002203 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002204 else
2205 goto skip;
2206
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002207
Christopher Faulet57583e42017-09-04 15:41:09 +02002208 /* nothing has been encoded for an unfragmented payload */
2209 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2210 goto skip;
2211
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002212 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002213 " - encode %s messages - spoe_appctx=%p"
2214 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002215 (int)now.tv_sec, (int)now.tv_usec,
2216 agent->id, __FUNCTION__, s,
2217 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002218 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002219 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002220
Christopher Faulet8ef75252017-02-20 22:56:03 +01002221 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002222 ctx->frag_ctx.curmsg = NULL;
2223 ctx->frag_ctx.curarg = NULL;
2224 ctx->frag_ctx.curoff = 0;
2225 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002226
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002227 return 1;
2228
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002229 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002230 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2231 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2232 return -1;
2233 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234
2235 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002236 " - encode fragmented messages - spoe_appctx=%p"
2237 " - curmsg=%p - curarg=%p - curoff=%u"
2238 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002239 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002240 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002241 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002242 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002243
Christopher Faulet8ef75252017-02-20 22:56:03 +01002244 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2246 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2247 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002248
2249 skip:
2250 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2251 " - skip the frame because nothing has been encoded\n",
2252 (int)now.tv_sec, (int)now.tv_usec,
2253 agent->id, __FUNCTION__, s);
2254 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002255}
2256
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
2258/***************************************************************************
2259 * Functions that handle SPOE actions
2260 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002261/* Helper function to set a variable */
2262static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002263spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264 struct sample *smp)
2265{
2266 struct spoe_config *conf = FLT_CONF(ctx->filter);
2267 struct spoe_agent *agent = conf->agent;
2268 char varname[64];
2269
2270 memset(varname, 0, sizeof(varname));
2271 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2272 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002273 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2274 vars_set_by_name(varname, len, smp);
2275 else
2276 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002277}
2278
2279/* Helper function to unset a variable */
2280static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002281spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002282 struct sample *smp)
2283{
2284 struct spoe_config *conf = FLT_CONF(ctx->filter);
2285 struct spoe_agent *agent = conf->agent;
2286 char varname[64];
2287
2288 memset(varname, 0, sizeof(varname));
2289 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2290 scope, agent->var_pfx, len, name);
2291 vars_unset_by_name_ifexist(varname, len, smp);
2292}
2293
2294
Christopher Faulet8ef75252017-02-20 22:56:03 +01002295static inline int
2296spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2297 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002299 char *str, *scope, *p = *buf;
2300 struct sample smp;
2301 uint64_t sz;
2302 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002303
Christopher Faulet8ef75252017-02-20 22:56:03 +01002304 if (p + 2 >= end)
2305 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002306
Christopher Faulet8ef75252017-02-20 22:56:03 +01002307 /* SET-VAR requires 3 arguments */
2308 if (*p++ != 3)
2309 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002310
Christopher Faulet8ef75252017-02-20 22:56:03 +01002311 switch (*p++) {
2312 case SPOE_SCOPE_PROC: scope = "proc"; break;
2313 case SPOE_SCOPE_SESS: scope = "sess"; break;
2314 case SPOE_SCOPE_TXN : scope = "txn"; break;
2315 case SPOE_SCOPE_REQ : scope = "req"; break;
2316 case SPOE_SCOPE_RES : scope = "res"; break;
2317 default: goto skip;
2318 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002319
Christopher Faulet8ef75252017-02-20 22:56:03 +01002320 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2321 goto skip;
2322 memset(&smp, 0, sizeof(smp));
2323 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002324
Christopher Faulet8ef75252017-02-20 22:56:03 +01002325 if (spoe_decode_data(&p, end, &smp) == -1)
2326 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002327
Christopher Faulet8ef75252017-02-20 22:56:03 +01002328 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2329 " - set-var '%s.%s.%.*s'\n",
2330 (int)now.tv_sec, (int)now.tv_usec,
2331 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2332 __FUNCTION__, s, scope,
2333 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2334 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002335
Christopher Faulet8ef75252017-02-20 22:56:03 +01002336 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002337
Christopher Faulet8ef75252017-02-20 22:56:03 +01002338 ret = (p - *buf);
2339 *buf = p;
2340 return ret;
2341 skip:
2342 return 0;
2343}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345static inline int
2346spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2347 char **buf, char *end, int dir)
2348{
2349 char *str, *scope, *p = *buf;
2350 struct sample smp;
2351 uint64_t sz;
2352 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 if (p + 2 >= end)
2355 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002356
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357 /* UNSET-VAR requires 2 arguments */
2358 if (*p++ != 2)
2359 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002360
Christopher Faulet8ef75252017-02-20 22:56:03 +01002361 switch (*p++) {
2362 case SPOE_SCOPE_PROC: scope = "proc"; break;
2363 case SPOE_SCOPE_SESS: scope = "sess"; break;
2364 case SPOE_SCOPE_TXN : scope = "txn"; break;
2365 case SPOE_SCOPE_REQ : scope = "req"; break;
2366 case SPOE_SCOPE_RES : scope = "res"; break;
2367 default: goto skip;
2368 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369
Christopher Faulet8ef75252017-02-20 22:56:03 +01002370 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2371 goto skip;
2372 memset(&smp, 0, sizeof(smp));
2373 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2376 " - unset-var '%s.%s.%.*s'\n",
2377 (int)now.tv_sec, (int)now.tv_usec,
2378 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2379 __FUNCTION__, s, scope,
2380 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2381 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 spoe_unset_var(ctx, scope, str, sz, &smp);
2384
2385 ret = (p - *buf);
2386 *buf = p;
2387 return ret;
2388 skip:
2389 return 0;
2390}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002391
Christopher Faulet8ef75252017-02-20 22:56:03 +01002392/* Process SPOE actions for a specific event. It returns 1 on success. If an
2393 * error occurred, 0 is returned. */
2394static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002395spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002396{
2397 char *p, *end;
2398 int ret;
2399
2400 p = ctx->buffer->p;
2401 end = p + ctx->buffer->i;
2402
2403 while (p < end) {
2404 enum spoe_action_type type;
2405
2406 type = *p++;
2407 switch (type) {
2408 case SPOE_ACT_T_SET_VAR:
2409 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2410 if (!ret)
2411 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002413
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414 case SPOE_ACT_T_UNSET_VAR:
2415 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2416 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 break;
2419
2420 default:
2421 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422 }
2423 }
2424
2425 return 1;
2426 skip:
2427 return 0;
2428}
2429
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002430/***************************************************************************
2431 * Functions that process SPOE events
2432 **************************************************************************/
2433static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002434spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002435{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002436 /* If a process is already started for this SPOE context, retry
2437 * later. */
2438 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002439 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002440
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002441 agent->rt[tid].processing++;
2442
Christopher Fauleta1cda022016-12-21 08:58:06 +01002443 /* Set the right flag to prevent request and response processing
2444 * in same time. */
2445 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2446 ? SPOE_CTX_FL_REQ_PROCESS
2447 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002448 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002449}
2450
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002451static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002452spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002453{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002454 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002455
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002456 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2457 return;
2458
Christopher Fauletfce747b2018-01-24 15:59:32 +01002459 if (sa && sa->frag_ctx.ctx == ctx) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002460 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002461 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002462 }
2463
Christopher Fauleta1cda022016-12-21 08:58:06 +01002464 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002465 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002466 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002467
Christopher Fauletb067b062017-01-04 16:39:11 +01002468 ctx->status_code = 0;
2469
Christopher Fauleta1cda022016-12-21 08:58:06 +01002470 /* Reset processing timer */
2471 ctx->process_exp = TICK_ETERNITY;
2472
Christopher Faulet8ef75252017-02-20 22:56:03 +01002473 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002474
Christopher Fauletfce747b2018-01-24 15:59:32 +01002475 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002476 ctx->frag_ctx.curmsg = NULL;
2477 ctx->frag_ctx.curarg = NULL;
2478 ctx->frag_ctx.curoff = 0;
2479 ctx->frag_ctx.flags = 0;
2480
Christopher Fauleta1cda022016-12-21 08:58:06 +01002481 if (!LIST_ISEMPTY(&ctx->list)) {
2482 LIST_DEL(&ctx->list);
2483 LIST_INIT(&ctx->list);
2484 }
2485}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002486
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002487static void
2488spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2489 struct spoe_context *ctx, int dir)
2490{
2491 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002492 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002493
2494 if (agent->var_on_error) {
2495 struct sample smp;
2496
2497 memset(&smp, 0, sizeof(smp));
2498 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2499 smp.data.u.sint = ctx->status_code;
2500 smp.data.type = SMP_T_BOOL;
2501
2502 spoe_set_var(ctx, "txn", agent->var_on_error,
2503 strlen(agent->var_on_error), &smp);
2504 }
2505 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2506 " - failed to process messages: code=%u\n",
2507 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2508 __FUNCTION__, s, ctx->status_code);
2509 send_log(ctx->strm->be, LOG_WARNING,
2510 "SPOE: [%s] failed to process messages: code=%u\n",
2511 agent->id, ctx->status_code);
2512
2513 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2514 ? SPOE_CTX_ST_READY
2515 : SPOE_CTX_ST_NONE);
2516}
2517
Christopher Faulet58d03682017-09-21 16:57:24 +02002518/* Process a list of SPOE messages. First, this functions will process messages
2519 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2520 * to process corresponding actions. During all the processing, it returns 0
2521 * and it returns 1 when the processing is finished. If an error occurred, -1
2522 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002523static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002524spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2525 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002526{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002527 struct spoe_config *conf = FLT_CONF(ctx->filter);
2528 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002529 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002530
2531 if (ctx->state == SPOE_CTX_ST_ERROR)
2532 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002533
2534 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2535 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002536 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002537 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002538 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002539 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002540 goto error;
2541 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002542
2543 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002544 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002545 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002546 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002547 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002549 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002550 goto skip;
2551 }
2552 }
2553
Christopher Fauletf7a30922016-11-10 15:04:51 +01002554 if (!tick_isset(ctx->process_exp)) {
2555 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2556 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2557 ctx->process_exp);
2558 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002559 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002560 if (!ret)
2561 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002562
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002563 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002565 }
2566
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002567 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002568 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002570 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002571 if (ret < 0)
2572 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002573 if (!ret)
2574 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002575 if (spoe_queue_context(ctx) < 0)
2576 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002577 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2578 }
2579
2580 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002581 if (ctx->spoe_appctx)
2582 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002583 ret = 0;
2584 goto out;
2585 }
2586
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002587 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2588 ret = 0;
2589 goto out;
2590 }
2591
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002592 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002593 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002594 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002595 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002596 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002597 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002598 }
2599
2600 out:
2601 return ret;
2602
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002603 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002604 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 ret = 1;
2606 goto end;
2607
2608 skip:
2609 ctx->state = SPOE_CTX_ST_READY;
2610 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002611
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 end:
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002613 spoe_stop_processing(agent, ctx);
Christopher Faulet58d03682017-09-21 16:57:24 +02002614 return ret;
2615}
2616
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002617/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2618 * See spoe_process_message for details. */
2619static int
2620spoe_process_group(struct stream *s, struct spoe_context *ctx,
2621 struct spoe_group *group, int dir)
2622{
2623 int ret;
2624
2625 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2626 " - ctx-state=%s - Process messages for group=%s\n",
2627 (int)now.tv_sec, (int)now.tv_usec,
2628 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2629 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2630 group->id);
2631
2632 if (LIST_ISEMPTY(&group->messages))
2633 return 1;
2634
2635 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2636 return ret;
2637}
2638
Christopher Faulet58d03682017-09-21 16:57:24 +02002639/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2640 * See spoe_process_message for details. */
2641static int
2642spoe_process_event(struct stream *s, struct spoe_context *ctx,
2643 enum spoe_event ev)
2644{
2645 int dir, ret;
2646
2647 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002648 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002649 (int)now.tv_sec, (int)now.tv_usec,
2650 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2651 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2652 spoe_event_str[ev]);
2653
2654 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2655
2656 if (LIST_ISEMPTY(&(ctx->events[ev])))
2657 return 1;
2658
2659 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002660 return ret;
2661}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002662
2663/***************************************************************************
2664 * Functions that create/destroy SPOE contexts
2665 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002666static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002667spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002668{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002669 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002670 return 1;
2671
Christopher Faulet4596fb72017-01-11 14:05:19 +01002672 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002673 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002674 LIST_DEL(&buffer_wait->list);
2675 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002676 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002677 }
2678
Christopher Faulet4596fb72017-01-11 14:05:19 +01002679 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002680 return 1;
2681
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002682 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002683 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002684 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002685 return 0;
2686}
2687
2688static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002689spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002690{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002691 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002692 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002693 LIST_DEL(&buffer_wait->list);
2694 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002695 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 }
2697
2698 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002699 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002700 b_free(buf);
2701 offer_buffers(buffer_wait->target,
2702 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 }
2704}
2705
Christopher Faulet4596fb72017-01-11 14:05:19 +01002706static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002707spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002708{
2709 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2710 return 1;
2711}
2712
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002714spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002715{
2716 struct spoe_config *conf = FLT_CONF(filter);
2717 struct spoe_context *ctx;
2718
Willy Tarreaubafbe012017-11-24 17:34:44 +01002719 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002720 if (ctx == NULL) {
2721 return NULL;
2722 }
2723 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002724 ctx->filter = filter;
2725 ctx->state = SPOE_CTX_ST_NONE;
2726 ctx->status_code = SPOE_CTX_ERR_NONE;
2727 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002728 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002729 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002730 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002731 LIST_INIT(&ctx->buffer_wait.list);
2732 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002733 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002734 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002735
Christopher Fauletf7a30922016-11-10 15:04:51 +01002736 ctx->stream_id = 0;
2737 ctx->frame_id = 1;
2738 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002739
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002740 ctx->strm = s;
2741 ctx->state = SPOE_CTX_ST_READY;
2742 filter->ctx = ctx;
2743
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744 return ctx;
2745}
2746
2747static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002748spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002749{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002750 struct spoe_config *conf = FLT_CONF(filter);
2751 struct spoe_context *ctx = filter->ctx;
2752
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002753 if (!ctx)
2754 return;
2755
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002756 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002757 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002758 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002759}
2760
2761static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002762spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002763{
2764 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002765 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766}
2767
2768
2769/***************************************************************************
2770 * Hooks that manage the filter lifecycle (init/check/deinit)
2771 **************************************************************************/
2772/* Signal handler: Do a soft stop, wakeup SPOE applet */
2773static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002774spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002775{
2776 struct proxy *p;
2777
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002778 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002779 while (p) {
2780 struct flt_conf *fconf;
2781
2782 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002783 struct spoe_config *conf;
2784 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002785 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002786 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002787
Christopher Faulet3b386a32017-02-23 10:17:15 +01002788 if (fconf->id != spoe_filter_id)
2789 continue;
2790
2791 conf = fconf->conf;
2792 agent = conf->agent;
2793
Christopher Faulet24289f22017-09-25 14:48:02 +02002794 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002795 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002796 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2797 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002798 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002799 }
2800 }
2801 p = p->next;
2802 }
2803}
2804
2805
2806/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2807static int
2808spoe_init(struct proxy *px, struct flt_conf *fconf)
2809{
2810 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002811
2812 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2813 init_new_proxy(&conf->agent_fe);
2814 conf->agent_fe.parent = conf->agent;
2815 conf->agent_fe.last_change = now.tv_sec;
2816 conf->agent_fe.id = conf->agent->id;
2817 conf->agent_fe.cap = PR_CAP_FE;
2818 conf->agent_fe.mode = PR_MODE_TCP;
2819 conf->agent_fe.maxconn = 0;
2820 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2821 conf->agent_fe.conn_retries = CONN_RETRIES;
2822 conf->agent_fe.accept = frontend_accept;
2823 conf->agent_fe.srv = NULL;
2824 conf->agent_fe.timeout.client = TICK_ETERNITY;
2825 conf->agent_fe.default_target = &spoe_applet.obj_type;
2826 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2827
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002828 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002829 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002830 sighandler_registered = 1;
2831 }
2832
2833 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002834}
2835
2836/* Free ressources allocated by the SPOE filter. */
2837static void
2838spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2839{
2840 struct spoe_config *conf = fconf->conf;
2841
2842 if (conf) {
2843 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002844
Christopher Faulet8ef75252017-02-20 22:56:03 +01002845 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002846 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002847 free(conf);
2848 }
2849 fconf->conf = NULL;
2850}
2851
2852/* Check configuration of a SPOE filter for a specified proxy.
2853 * Return 1 on error, else 0. */
2854static int
2855spoe_check(struct proxy *px, struct flt_conf *fconf)
2856{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002857 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002858 struct spoe_config *conf = fconf->conf;
2859 struct proxy *target;
2860
Christopher Faulet7ee86672017-09-19 11:08:28 +02002861 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2862 * are uniq */
2863 list_for_each_entry(f, &px->filter_configs, list) {
2864 struct spoe_config *c = f->conf;
2865
2866 /* This is not an SPOE filter */
2867 if (f->id != spoe_filter_id)
2868 continue;
2869 /* This is the current SPOE filter */
2870 if (f == fconf)
2871 continue;
2872
2873 /* Check engine Id. It should be uniq */
2874 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002875 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2876 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002877 return 1;
2878 }
2879 }
2880
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881 target = proxy_be_by_name(conf->agent->b.name);
2882 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002883 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2884 " declared at %s:%d.\n",
2885 px->id, conf->agent->b.name, conf->agent->id,
2886 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887 return 1;
2888 }
2889 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002890 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2891 " at %s:%d does not support HTTP mode.\n",
2892 px->id, target->id, conf->agent->id,
2893 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002894 return 1;
2895 }
2896
2897 free(conf->agent->b.name);
2898 conf->agent->b.name = NULL;
2899 conf->agent->b.be = target;
2900 return 0;
2901}
2902
2903/**************************************************************************
2904 * Hooks attached to a stream
2905 *************************************************************************/
2906/* Called when a filter instance is created and attach to a stream. It creates
2907 * the context that will be used to process this stream. */
2908static int
2909spoe_start(struct stream *s, struct filter *filter)
2910{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002911 struct spoe_config *conf = FLT_CONF(filter);
2912 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913 struct spoe_context *ctx;
2914
2915 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002916 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917 __FUNCTION__, s);
2918
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002919 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002920 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2921 " - failed to create SPOE context\n",
2922 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002923 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002924 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002925 "SPOE: [%s] failed to create SPOE context\n",
2926 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002927 return 0;
2928 }
2929
Christopher Faulet11610f32017-09-21 10:23:10 +02002930 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2932
Christopher Faulet11610f32017-09-21 10:23:10 +02002933 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002934 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2935
Christopher Faulet11610f32017-09-21 10:23:10 +02002936 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002937 filter->pre_analyzers |= AN_RES_INSPECT;
2938
Christopher Faulet11610f32017-09-21 10:23:10 +02002939 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2941
Christopher Faulet11610f32017-09-21 10:23:10 +02002942 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002943 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2944
Christopher Faulet11610f32017-09-21 10:23:10 +02002945 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2947
2948 return 1;
2949}
2950
2951/* Called when a filter instance is detached from a stream. It release the
2952 * attached SPOE context. */
2953static void
2954spoe_stop(struct stream *s, struct filter *filter)
2955{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2957 (int)now.tv_sec, (int)now.tv_usec,
2958 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2959 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002960 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961}
2962
Christopher Fauletf7a30922016-11-10 15:04:51 +01002963
2964/*
2965 * Called when the stream is woken up because of expired timer.
2966 */
2967static void
2968spoe_check_timeouts(struct stream *s, struct filter *filter)
2969{
2970 struct spoe_context *ctx = filter->ctx;
2971
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002972 if (tick_is_expired(ctx->process_exp, now_ms)) {
2973 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002974 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002975 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002976}
2977
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978/* Called when we are ready to filter data on a channel */
2979static int
2980spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2981{
2982 struct spoe_context *ctx = filter->ctx;
2983 int ret = 1;
2984
2985 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2986 " - ctx-flags=0x%08x\n",
2987 (int)now.tv_sec, (int)now.tv_usec,
2988 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2989 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2990
Christopher Fauletb067b062017-01-04 16:39:11 +01002991 if (ctx->state == SPOE_CTX_ST_NONE)
2992 goto out;
2993
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002994 if (!(chn->flags & CF_ISRESP)) {
2995 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2996 chn->analysers |= AN_REQ_INSPECT_FE;
2997 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2998 chn->analysers |= AN_REQ_INSPECT_BE;
2999
3000 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3001 goto out;
3002
3003 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003004 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003005 if (!ret)
3006 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3008 }
3009 else {
3010 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3011 chn->analysers |= AN_RES_INSPECT;
3012
3013 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3014 goto out;
3015
Christopher Faulet8ef75252017-02-20 22:56:03 +01003016 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003017 if (!ret) {
3018 channel_dont_read(chn);
3019 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003020 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003021 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003022 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 }
3024
3025 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003026 return ret;
3027}
3028
3029/* Called before a processing happens on a given channel */
3030static int
3031spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3032 struct channel *chn, unsigned an_bit)
3033{
3034 struct spoe_context *ctx = filter->ctx;
3035 int ret = 1;
3036
3037 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3038 " - ctx-flags=0x%08x - ana=0x%08x\n",
3039 (int)now.tv_sec, (int)now.tv_usec,
3040 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3041 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3042 ctx->flags, an_bit);
3043
Christopher Fauletb067b062017-01-04 16:39:11 +01003044 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 goto out;
3046
3047 switch (an_bit) {
3048 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003049 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003050 break;
3051 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003052 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053 break;
3054 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003055 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056 break;
3057 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003058 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059 break;
3060 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003061 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062 break;
3063 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003064 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003065 break;
3066 }
3067
3068 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003069 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003070 channel_dont_read(chn);
3071 channel_dont_close(chn);
3072 }
3073 return ret;
3074}
3075
3076/* Called when the filtering on the channel ends. */
3077static int
3078spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3079{
3080 struct spoe_context *ctx = filter->ctx;
3081
3082 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3083 " - ctx-flags=0x%08x\n",
3084 (int)now.tv_sec, (int)now.tv_usec,
3085 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3086 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3087
3088 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003089 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003090 }
3091
3092 return 1;
3093}
3094
3095/********************************************************************
3096 * Functions that manage the filter initialization
3097 ********************************************************************/
3098struct flt_ops spoe_ops = {
3099 /* Manage SPOE filter, called for each filter declaration */
3100 .init = spoe_init,
3101 .deinit = spoe_deinit,
3102 .check = spoe_check,
3103
3104 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003105 .attach = spoe_start,
3106 .detach = spoe_stop,
3107 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003108
3109 /* Handle channels activity */
3110 .channel_start_analyze = spoe_start_analyze,
3111 .channel_pre_analyze = spoe_chn_pre_analyze,
3112 .channel_end_analyze = spoe_end_analyze,
3113};
3114
3115
3116static int
3117cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3118{
3119 const char *err;
3120 int i, err_code = 0;
3121
3122 if ((cfg_scope == NULL && curengine != NULL) ||
3123 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003124 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003125 goto out;
3126
3127 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3128 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003129 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3130 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003131 err_code |= ERR_ALERT | ERR_ABORT;
3132 goto out;
3133 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003134 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3135 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003136 goto out;
3137 }
3138
3139 err = invalid_char(args[1]);
3140 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003141 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3142 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003143 err_code |= ERR_ALERT | ERR_ABORT;
3144 goto out;
3145 }
3146
3147 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003148 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3149 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003150 err_code |= ERR_ALERT | ERR_ABORT;
3151 goto out;
3152 }
3153 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003154 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155 err_code |= ERR_ALERT | ERR_ABORT;
3156 goto out;
3157 }
3158
3159 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003160
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003161 curagent->conf.file = strdup(file);
3162 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003163
3164 curagent->timeout.hello = TICK_ETERNITY;
3165 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003166 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003167
3168 curagent->engine_id = NULL;
3169 curagent->var_pfx = NULL;
3170 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003171 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3172 if (global.nbthread == 1)
3173 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003174 curagent->cps_max = 0;
3175 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003176 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003177 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003178
3179 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003180 LIST_INIT(&curagent->events[i]);
3181 LIST_INIT(&curagent->groups);
3182 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003183
Christopher Faulet24289f22017-09-25 14:48:02 +02003184 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003185 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003186 err_code |= ERR_ALERT | ERR_ABORT;
3187 goto out;
3188 }
3189 for (i = 0; i < global.nbthread; ++i) {
3190 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003191 SPOE_DEBUG_STMT(curagent->rt[i].applets_act = 0);
3192 SPOE_DEBUG_STMT(curagent->rt[i].applets_idle = 0);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003193 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003194 LIST_INIT(&curagent->rt[i].applets);
3195 LIST_INIT(&curagent->rt[i].sending_queue);
3196 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003197 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003198 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003199 }
3200 else if (!strcmp(args[0], "use-backend")) {
3201 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003202 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3203 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003204 err_code |= ERR_ALERT | ERR_FATAL;
3205 goto out;
3206 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003207 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209 free(curagent->b.name);
3210 curagent->b.name = strdup(args[1]);
3211 }
3212 else if (!strcmp(args[0], "messages")) {
3213 int cur_arg = 1;
3214 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003215 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003216
Christopher Faulet11610f32017-09-21 10:23:10 +02003217 list_for_each_entry(ph, &curmphs, list) {
3218 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003219 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3220 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 err_code |= ERR_ALERT | ERR_FATAL;
3222 goto out;
3223 }
3224 }
3225
Christopher Faulet11610f32017-09-21 10:23:10 +02003226 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003227 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 err_code |= ERR_ALERT | ERR_ABORT;
3229 goto out;
3230 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003231 ph->id = strdup(args[cur_arg]);
3232 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233 cur_arg++;
3234 }
3235 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003236 else if (!strcmp(args[0], "groups")) {
3237 int cur_arg = 1;
3238 while (*args[cur_arg]) {
3239 struct spoe_placeholder *ph = NULL;
3240
3241 list_for_each_entry(ph, &curgphs, list) {
3242 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003243 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3244 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003245 err_code |= ERR_ALERT | ERR_FATAL;
3246 goto out;
3247 }
3248 }
3249
3250 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003251 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003252 err_code |= ERR_ALERT | ERR_ABORT;
3253 goto out;
3254 }
3255 ph->id = strdup(args[cur_arg]);
3256 LIST_ADDQ(&curgphs, &ph->list);
3257 cur_arg++;
3258 }
3259 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 else if (!strcmp(args[0], "timeout")) {
3261 unsigned int *tv = NULL;
3262 const char *res;
3263 unsigned timeout;
3264
3265 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003266 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3267 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003268 err_code |= ERR_ALERT | ERR_FATAL;
3269 goto out;
3270 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003271 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3272 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 if (!strcmp(args[1], "hello"))
3274 tv = &curagent->timeout.hello;
3275 else if (!strcmp(args[1], "idle"))
3276 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003277 else if (!strcmp(args[1], "processing"))
3278 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003280 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3281 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003282 err_code |= ERR_ALERT | ERR_FATAL;
3283 goto out;
3284 }
3285 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003286 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3287 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003288 err_code |= ERR_ALERT | ERR_FATAL;
3289 goto out;
3290 }
3291 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3292 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003293 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3294 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003295 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003296 goto out;
3297 }
3298 *tv = MS_TO_TICKS(timeout);
3299 }
3300 else if (!strcmp(args[0], "option")) {
3301 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003302 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3303 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 err_code |= ERR_ALERT | ERR_FATAL;
3305 goto out;
3306 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003307
Christopher Faulet305c6072017-02-23 16:17:53 +01003308 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003309 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003310 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003311 if (kwm == 1)
3312 curagent->flags &= ~SPOE_FL_PIPELINING;
3313 else
3314 curagent->flags |= SPOE_FL_PIPELINING;
3315 goto out;
3316 }
3317 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003318 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003319 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003320 if (kwm == 1)
3321 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003322 else {
3323 if (global.nbthread == 1)
3324 curagent->flags |= SPOE_FL_ASYNC;
3325 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003326 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3327 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003328 err_code |= ERR_WARN;
3329 }
3330 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003331 goto out;
3332 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003333 else if (!strcmp(args[1], "send-frag-payload")) {
3334 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3335 goto out;
3336 if (kwm == 1)
3337 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3338 else
3339 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3340 goto out;
3341 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003342
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003343 /* Following options does not support negation */
3344 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003345 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3346 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003347 err_code |= ERR_ALERT | ERR_FATAL;
3348 goto out;
3349 }
3350
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003351 if (!strcmp(args[1], "var-prefix")) {
3352 char *tmp;
3353
3354 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003355 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3356 file, linenum, args[0],
3357 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003358 err_code |= ERR_ALERT | ERR_FATAL;
3359 goto out;
3360 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003361 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3362 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003363 tmp = args[2];
3364 while (*tmp) {
3365 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003366 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3367 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003368 err_code |= ERR_ALERT | ERR_FATAL;
3369 goto out;
3370 }
3371 tmp++;
3372 }
3373 curagent->var_pfx = strdup(args[2]);
3374 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003375 else if (!strcmp(args[1], "force-set-var")) {
3376 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3377 goto out;
3378 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3379 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003380 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003381 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003382 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003383 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3384 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003385 else if (!strcmp(args[1], "set-on-error")) {
3386 char *tmp;
3387
3388 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003389 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3390 file, linenum, args[0],
3391 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003392 err_code |= ERR_ALERT | ERR_FATAL;
3393 goto out;
3394 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003395 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3396 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003397 tmp = args[2];
3398 while (*tmp) {
3399 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003400 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3401 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003402 err_code |= ERR_ALERT | ERR_FATAL;
3403 goto out;
3404 }
3405 tmp++;
3406 }
3407 curagent->var_on_error = strdup(args[2]);
3408 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003409 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003410 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3411 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003412 err_code |= ERR_ALERT | ERR_FATAL;
3413 goto out;
3414 }
Christopher Faulet48026722016-11-16 15:01:12 +01003415 }
3416 else if (!strcmp(args[0], "maxconnrate")) {
3417 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003418 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3419 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003420 err_code |= ERR_ALERT | ERR_FATAL;
3421 goto out;
3422 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003423 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003424 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003425 curagent->cps_max = atol(args[1]);
3426 }
3427 else if (!strcmp(args[0], "maxerrrate")) {
3428 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003429 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3430 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003431 err_code |= ERR_ALERT | ERR_FATAL;
3432 goto out;
3433 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003434 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003435 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003436 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003438 else if (!strcmp(args[0], "max-frame-size")) {
3439 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003440 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3441 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003442 err_code |= ERR_ALERT | ERR_FATAL;
3443 goto out;
3444 }
3445 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3446 goto out;
3447 curagent->max_frame_size = atol(args[1]);
3448 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3449 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003450 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3451 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003452 err_code |= ERR_ALERT | ERR_FATAL;
3453 goto out;
3454 }
3455 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003456 else if (!strcmp(args[0], "max-waiting-frames")) {
3457 if (!*args[1]) {
3458 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3459 file, linenum, args[0]);
3460 err_code |= ERR_ALERT | ERR_FATAL;
3461 goto out;
3462 }
3463 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3464 goto out;
3465 curagent->max_fpa = atol(args[1]);
3466 if (curagent->max_fpa < 1) {
3467 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3468 file, linenum, args[0]);
3469 err_code |= ERR_ALERT | ERR_FATAL;
3470 goto out;
3471 }
3472 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003473 else if (!strcmp(args[0], "register-var-names")) {
3474 int cur_arg;
3475
3476 if (!*args[1]) {
3477 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3478 file, linenum, args[0]);
3479 err_code |= ERR_ALERT | ERR_FATAL;
3480 goto out;
3481 }
3482 cur_arg = 1;
3483 while (*args[cur_arg]) {
3484 struct spoe_var_placeholder *vph;
3485
3486 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3487 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3488 err_code |= ERR_ALERT | ERR_ABORT;
3489 goto out;
3490 }
3491 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3492 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3493 err_code |= ERR_ALERT | ERR_ABORT;
3494 goto out;
3495 }
3496 LIST_ADDQ(&curvars, &vph->list);
3497 cur_arg++;
3498 }
3499 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003500 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003501 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3502 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003503 err_code |= ERR_ALERT | ERR_FATAL;
3504 goto out;
3505 }
3506 out:
3507 return err_code;
3508}
Christopher Faulet11610f32017-09-21 10:23:10 +02003509static int
3510cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3511{
3512 struct spoe_group *grp;
3513 const char *err;
3514 int err_code = 0;
3515
3516 if ((cfg_scope == NULL && curengine != NULL) ||
3517 (cfg_scope != NULL && curengine == NULL) ||
3518 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3519 goto out;
3520
3521 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3522 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003523 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3524 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003525 err_code |= ERR_ALERT | ERR_ABORT;
3526 goto out;
3527 }
3528 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3529 err_code |= ERR_ABORT;
3530 goto out;
3531 }
3532
3533 err = invalid_char(args[1]);
3534 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003535 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3536 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003537 err_code |= ERR_ALERT | ERR_ABORT;
3538 goto out;
3539 }
3540
3541 list_for_each_entry(grp, &curgrps, list) {
3542 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003543 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3544 " name as another one declared at %s:%d.\n",
3545 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003546 err_code |= ERR_ALERT | ERR_FATAL;
3547 goto out;
3548 }
3549 }
3550
3551 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003552 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003553 err_code |= ERR_ALERT | ERR_ABORT;
3554 goto out;
3555 }
3556
3557 curgrp->id = strdup(args[1]);
3558 curgrp->conf.file = strdup(file);
3559 curgrp->conf.line = linenum;
3560 LIST_INIT(&curgrp->phs);
3561 LIST_INIT(&curgrp->messages);
3562 LIST_ADDQ(&curgrps, &curgrp->list);
3563 }
3564 else if (!strcmp(args[0], "messages")) {
3565 int cur_arg = 1;
3566 while (*args[cur_arg]) {
3567 struct spoe_placeholder *ph = NULL;
3568
3569 list_for_each_entry(ph, &curgrp->phs, list) {
3570 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003571 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3572 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003573 err_code |= ERR_ALERT | ERR_FATAL;
3574 goto out;
3575 }
3576 }
3577
3578 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003579 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003580 err_code |= ERR_ALERT | ERR_ABORT;
3581 goto out;
3582 }
3583 ph->id = strdup(args[cur_arg]);
3584 LIST_ADDQ(&curgrp->phs, &ph->list);
3585 cur_arg++;
3586 }
3587 }
3588 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003589 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3590 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003591 err_code |= ERR_ALERT | ERR_FATAL;
3592 goto out;
3593 }
3594 out:
3595 return err_code;
3596}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003597
3598static int
3599cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3600{
3601 struct spoe_message *msg;
3602 struct spoe_arg *arg;
3603 const char *err;
3604 char *errmsg = NULL;
3605 int err_code = 0;
3606
3607 if ((cfg_scope == NULL && curengine != NULL) ||
3608 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003609 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003610 goto out;
3611
3612 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3613 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003614 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3615 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003616 err_code |= ERR_ALERT | ERR_ABORT;
3617 goto out;
3618 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003619 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3620 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003621 goto out;
3622 }
3623
3624 err = invalid_char(args[1]);
3625 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003626 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3627 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003628 err_code |= ERR_ALERT | ERR_ABORT;
3629 goto out;
3630 }
3631
3632 list_for_each_entry(msg, &curmsgs, list) {
3633 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003634 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3635 " name as another one declared at %s:%d.\n",
3636 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003637 err_code |= ERR_ALERT | ERR_FATAL;
3638 goto out;
3639 }
3640 }
3641
3642 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003643 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003644 err_code |= ERR_ALERT | ERR_ABORT;
3645 goto out;
3646 }
3647
3648 curmsg->id = strdup(args[1]);
3649 curmsg->id_len = strlen(curmsg->id);
3650 curmsg->event = SPOE_EV_NONE;
3651 curmsg->conf.file = strdup(file);
3652 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003653 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003654 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003655 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003656 LIST_INIT(&curmsg->by_evt);
3657 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003658 LIST_ADDQ(&curmsgs, &curmsg->list);
3659 }
3660 else if (!strcmp(args[0], "args")) {
3661 int cur_arg = 1;
3662
3663 curproxy->conf.args.ctx = ARGC_SPOE;
3664 curproxy->conf.args.file = file;
3665 curproxy->conf.args.line = linenum;
3666 while (*args[cur_arg]) {
3667 char *delim = strchr(args[cur_arg], '=');
3668 int idx = 0;
3669
3670 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003671 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003672 err_code |= ERR_ALERT | ERR_ABORT;
3673 goto out;
3674 }
3675
3676 if (!delim) {
3677 arg->name = NULL;
3678 arg->name_len = 0;
3679 delim = args[cur_arg];
3680 }
3681 else {
3682 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3683 arg->name_len = delim - args[cur_arg];
3684 delim++;
3685 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003686 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3687 &idx, file, linenum, &errmsg,
3688 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003689 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003690 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003691 err_code |= ERR_ALERT | ERR_FATAL;
3692 free(arg->name);
3693 free(arg);
3694 goto out;
3695 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003696 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003697 LIST_ADDQ(&curmsg->args, &arg->list);
3698 cur_arg++;
3699 }
3700 curproxy->conf.args.file = NULL;
3701 curproxy->conf.args.line = 0;
3702 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003703 else if (!strcmp(args[0], "acl")) {
3704 err = invalid_char(args[1]);
3705 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003706 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3707 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003708 err_code |= ERR_ALERT | ERR_FATAL;
3709 goto out;
3710 }
3711 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003712 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3713 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003714 err_code |= ERR_ALERT | ERR_FATAL;
3715 goto out;
3716 }
3717 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003718 else if (!strcmp(args[0], "event")) {
3719 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003720 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003721 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003722 goto out;
3723 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003724 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3725 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003726
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003727 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3728 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3729 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3730 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3731
3732 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3733 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3734 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3735 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3736 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3737 curmsg->event = SPOE_EV_ON_TCP_RSP;
3738
3739 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3740 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3741 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3742 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3743 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3744 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3745 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003746 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3747 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003748 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003749 goto out;
3750 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003751
3752 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3753 struct acl_cond *cond;
3754
3755 cond = build_acl_cond(file, linenum, &curmsg->acls,
3756 curproxy, (const char **)args+2,
3757 &errmsg);
3758 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003759 ha_alert("parsing [%s:%d] : error detected while "
3760 "parsing an 'event %s' condition : %s.\n",
3761 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003762 err_code |= ERR_ALERT | ERR_FATAL;
3763 goto out;
3764 }
3765 curmsg->cond = cond;
3766 }
3767 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003768 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3769 "or 'unless' followed by a condition but found '%s'.\n",
3770 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003771 err_code |= ERR_ALERT | ERR_FATAL;
3772 goto out;
3773 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003774 }
3775 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003776 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3777 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003778 err_code |= ERR_ALERT | ERR_FATAL;
3779 goto out;
3780 }
3781 out:
3782 free(errmsg);
3783 return err_code;
3784}
3785
3786/* Return -1 on error, else 0 */
3787static int
3788parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3789 struct flt_conf *fconf, char **err, void *private)
3790{
3791 struct list backup_sections;
3792 struct spoe_config *conf;
3793 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003794 struct spoe_group *grp, *grpback;
3795 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003796 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003797 char *file = NULL, *engine = NULL;
3798 int ret, pos = *cur_arg + 1;
3799
3800 conf = calloc(1, sizeof(*conf));
3801 if (conf == NULL) {
3802 memprintf(err, "%s: out of memory", args[*cur_arg]);
3803 goto error;
3804 }
3805 conf->proxy = px;
3806
3807 while (*args[pos]) {
3808 if (!strcmp(args[pos], "config")) {
3809 if (!*args[pos+1]) {
3810 memprintf(err, "'%s' : '%s' option without value",
3811 args[*cur_arg], args[pos]);
3812 goto error;
3813 }
3814 file = args[pos+1];
3815 pos += 2;
3816 }
3817 else if (!strcmp(args[pos], "engine")) {
3818 if (!*args[pos+1]) {
3819 memprintf(err, "'%s' : '%s' option without value",
3820 args[*cur_arg], args[pos]);
3821 goto error;
3822 }
3823 engine = args[pos+1];
3824 pos += 2;
3825 }
3826 else {
3827 memprintf(err, "unknown keyword '%s'", args[pos]);
3828 goto error;
3829 }
3830 }
3831 if (file == NULL) {
3832 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3833 goto error;
3834 }
3835
3836 /* backup sections and register SPOE sections */
3837 LIST_INIT(&backup_sections);
3838 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003839 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3840 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003841 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003842
3843 /* Parse SPOE filter configuration file */
3844 curengine = engine;
3845 curproxy = px;
3846 curagent = NULL;
3847 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003848 LIST_INIT(&curmsgs);
3849 LIST_INIT(&curgrps);
3850 LIST_INIT(&curmphs);
3851 LIST_INIT(&curgphs);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003852 LIST_INIT(&curvars);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003853 ret = readcfgfile(file);
3854 curproxy = NULL;
3855
3856 /* unregister SPOE sections and restore previous sections */
3857 cfg_unregister_sections();
3858 cfg_restore_sections(&backup_sections);
3859
3860 if (ret == -1) {
3861 memprintf(err, "Could not open configuration file %s : %s",
3862 file, strerror(errno));
3863 goto error;
3864 }
3865 if (ret & (ERR_ABORT|ERR_FATAL)) {
3866 memprintf(err, "Error(s) found in configuration file %s", file);
3867 goto error;
3868 }
3869
3870 /* Check SPOE agent */
3871 if (curagent == NULL) {
3872 memprintf(err, "No SPOE agent found in file %s", file);
3873 goto error;
3874 }
3875 if (curagent->b.name == NULL) {
3876 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3877 curagent->id, curagent->conf.file, curagent->conf.line);
3878 goto error;
3879 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003880 if (curagent->timeout.hello == TICK_ETERNITY ||
3881 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003882 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003883 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3884 " | While not properly invalid, you will certainly encounter various problems\n"
3885 " | with such a configuration. To fix this, please ensure that all following\n"
3886 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3887 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003888 }
3889 if (curagent->var_pfx == NULL) {
3890 char *tmp = curagent->id;
3891
3892 while (*tmp) {
3893 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3894 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3895 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3896 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3897 goto error;
3898 }
3899 tmp++;
3900 }
3901 curagent->var_pfx = strdup(curagent->id);
3902 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003903 if (curagent->engine_id == NULL)
3904 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003905
Christopher Faulet11610f32017-09-21 10:23:10 +02003906 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003907 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3908 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003909 goto finish;
3910 }
3911
Christopher Faulet11610f32017-09-21 10:23:10 +02003912 /* Replace placeholders by the corresponding messages for the SPOE
3913 * agent */
3914 list_for_each_entry(ph, &curmphs, list) {
3915 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003916 struct spoe_arg *arg;
3917 unsigned int where;
3918
Christopher Faulet11610f32017-09-21 10:23:10 +02003919 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003920 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3921 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3922 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3923 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3924 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3925 }
3926 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3927 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3928 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003929 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3930 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003931 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003932 }
3933 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003934 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3935 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003936 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003937 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003938
3939 where = 0;
3940 switch (msg->event) {
3941 case SPOE_EV_ON_CLIENT_SESS:
3942 where |= SMP_VAL_FE_CON_ACC;
3943 break;
3944
3945 case SPOE_EV_ON_TCP_REQ_FE:
3946 where |= SMP_VAL_FE_REQ_CNT;
3947 break;
3948
3949 case SPOE_EV_ON_HTTP_REQ_FE:
3950 where |= SMP_VAL_FE_HRQ_HDR;
3951 break;
3952
3953 case SPOE_EV_ON_TCP_REQ_BE:
3954 if (px->cap & PR_CAP_FE)
3955 where |= SMP_VAL_FE_REQ_CNT;
3956 if (px->cap & PR_CAP_BE)
3957 where |= SMP_VAL_BE_REQ_CNT;
3958 break;
3959
3960 case SPOE_EV_ON_HTTP_REQ_BE:
3961 if (px->cap & PR_CAP_FE)
3962 where |= SMP_VAL_FE_HRQ_HDR;
3963 if (px->cap & PR_CAP_BE)
3964 where |= SMP_VAL_BE_HRQ_HDR;
3965 break;
3966
3967 case SPOE_EV_ON_SERVER_SESS:
3968 where |= SMP_VAL_BE_SRV_CON;
3969 break;
3970
3971 case SPOE_EV_ON_TCP_RSP:
3972 if (px->cap & PR_CAP_FE)
3973 where |= SMP_VAL_FE_RES_CNT;
3974 if (px->cap & PR_CAP_BE)
3975 where |= SMP_VAL_BE_RES_CNT;
3976 break;
3977
3978 case SPOE_EV_ON_HTTP_RSP:
3979 if (px->cap & PR_CAP_FE)
3980 where |= SMP_VAL_FE_HRS_HDR;
3981 if (px->cap & PR_CAP_BE)
3982 where |= SMP_VAL_BE_HRS_HDR;
3983 break;
3984
3985 default:
3986 break;
3987 }
3988
3989 list_for_each_entry(arg, &msg->args, list) {
3990 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003991 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003992 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003993 "none of which is available here ('%s')",
3994 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003995 sample_ckp_names(arg->expr->fetch->use),
3996 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003997 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003998 }
3999 }
4000
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004001 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004002 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4003 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004004 }
4005 }
4006 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004007 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004008 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004009 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004010 continue;
4011 }
4012
Christopher Faulet11610f32017-09-21 10:23:10 +02004013 /* Replace placeholders by the corresponding groups for the SPOE
4014 * agent */
4015 list_for_each_entry(ph, &curgphs, list) {
4016 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4017 if (!strcmp(grp->id, ph->id)) {
4018 grp->agent = curagent;
4019 LIST_DEL(&grp->list);
4020 LIST_ADDQ(&curagent->groups, &grp->list);
4021 goto next_aph;
4022 }
4023 }
4024 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4025 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4026 goto error;
4027 next_aph:
4028 continue;
4029 }
4030
4031 /* Replace placeholders by the corresponding message for each SPOE
4032 * group of the SPOE agent */
4033 list_for_each_entry(grp, &curagent->groups, list) {
4034 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4035 list_for_each_entry(msg, &curmsgs, list) {
4036 if (!strcmp(msg->id, ph->id)) {
4037 if (msg->group != NULL) {
4038 memprintf(err, "SPOE message '%s' already belongs to "
4039 "the SPOE group '%s' declare at %s:%d",
4040 msg->id, msg->group->id,
4041 msg->group->conf.file,
4042 msg->group->conf.line);
4043 goto error;
4044 }
4045
4046 /* Scope for arguments are not checked for now. We will check
4047 * them only if a rule use the corresponding SPOE group. */
4048 msg->agent = curagent;
4049 msg->group = grp;
4050 LIST_DEL(&ph->list);
4051 LIST_ADDQ(&grp->messages, &msg->by_grp);
4052 goto next_mph_grp;
4053 }
4054 }
4055 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4056 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4057 goto error;
4058 next_mph_grp:
4059 continue;
4060 }
4061 }
4062
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004063 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004064 /* move curmsgs to the agent message list */
4065 curmsgs.n->p = &curagent->messages;
4066 curmsgs.p->n = &curagent->messages;
4067 curagent->messages = curmsgs;
4068 LIST_INIT(&curmsgs);
4069
Christopher Faulet7ee86672017-09-19 11:08:28 +02004070 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004071 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004072 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4073 LIST_DEL(&ph->list);
4074 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004075 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004076 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4077 LIST_DEL(&ph->list);
4078 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004079 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004080 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4081 struct arg arg;
4082
4083 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4084 curagent->var_pfx, vph->name);
4085
4086 arg.type = ARGT_STR;
4087 arg.data.str.str = trash.str;
4088 arg.data.str.len = trash.len;
4089 if (!vars_check_arg(&arg, err)) {
4090 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4091 curagent->id, curagent->var_pfx, vph->name, *err);
4092 goto error;
4093 }
4094
4095 LIST_DEL(&vph->list);
4096 free(vph->name);
4097 free(vph);
4098 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004099 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4100 LIST_DEL(&grp->list);
4101 spoe_release_group(grp);
4102 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004103 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004104 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004105 fconf->ops = &spoe_ops;
4106 fconf->conf = conf;
4107 return 0;
4108
4109 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004110 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004111 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4112 LIST_DEL(&ph->list);
4113 spoe_release_placeholder(ph);
4114 }
4115 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4116 LIST_DEL(&ph->list);
4117 spoe_release_placeholder(ph);
4118 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004119 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4120 LIST_DEL(&vph->list);
4121 free(vph->name);
4122 free(vph);
4123 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004124 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4125 LIST_DEL(&grp->list);
4126 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004127 }
4128 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4129 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004130 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004131 }
4132 free(conf);
4133 return -1;
4134}
4135
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004136/* Send message of a SPOE group. This is the action_ptr callback of a rule
4137 * associated to a "send-spoe-group" action.
4138 *
4139 * It returns ACT_RET_CONT is processing is finished without error, it returns
4140 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4141 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004142static enum act_return
4143spoe_send_group(struct act_rule *rule, struct proxy *px,
4144 struct session *sess, struct stream *s, int flags)
4145{
4146 struct filter *filter;
4147 struct spoe_agent *agent = NULL;
4148 struct spoe_group *group = NULL;
4149 struct spoe_context *ctx = NULL;
4150 int ret, dir;
4151
4152 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4153 if (filter->config == rule->arg.act.p[0]) {
4154 agent = rule->arg.act.p[2];
4155 group = rule->arg.act.p[3];
4156 ctx = filter->ctx;
4157 break;
4158 }
4159 }
4160 if (agent == NULL || group == NULL || ctx == NULL)
4161 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004162 if (ctx->state == SPOE_CTX_ST_NONE)
4163 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004164
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004165 switch (rule->from) {
4166 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4167 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4168 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4169 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4170 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4171 default:
4172 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4173 " - internal error while execute spoe-send-group\n",
4174 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4175 __FUNCTION__, s);
4176 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4177 agent->id);
4178 return ACT_RET_CONT;
4179 }
4180
4181 ret = spoe_process_group(s, ctx, group, dir);
4182 if (ret == 1)
4183 return ACT_RET_CONT;
4184 else if (ret == 0) {
4185 if (flags & ACT_FLAG_FINAL) {
4186 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4187 " - failed to process group '%s': interrupted by caller\n",
4188 (int)now.tv_sec, (int)now.tv_usec,
4189 agent->id, __FUNCTION__, s, group->id);
4190 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4191 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004192 spoe_stop_processing(agent, ctx);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004193 return ACT_RET_CONT;
4194 }
4195 return ACT_RET_YIELD;
4196 }
4197 else
4198 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004199}
4200
4201/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4202 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4203 * action should be:
4204 *
4205 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4206 *
4207 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4208 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4209 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4210 * group.
4211 *
4212 * The function returns 1 in success case, otherwise, it returns 0 and err is
4213 * filled.
4214 */
4215static int
4216check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4217{
4218 struct flt_conf *fconf;
4219 struct spoe_config *conf;
4220 struct spoe_agent *agent = NULL;
4221 struct spoe_group *group;
4222 struct spoe_message *msg;
4223 char *engine_id = rule->arg.act.p[0];
4224 char *group_id = rule->arg.act.p[1];
4225 unsigned int where = 0;
4226
4227 switch (rule->from) {
4228 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4229 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4230 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4231 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4232 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4233 default:
4234 memprintf(err,
4235 "internal error, unexpected rule->from=%d, please report this bug!",
4236 rule->from);
4237 goto error;
4238 }
4239
4240 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4241 * <px> */
4242 list_for_each_entry(fconf, &px->filter_configs, list) {
4243 conf = fconf->conf;
4244
4245 /* This is not an SPOE filter */
4246 if (fconf->id != spoe_filter_id)
4247 continue;
4248
4249 /* This is the good engine */
4250 if (!strcmp(conf->id, engine_id)) {
4251 agent = conf->agent;
4252 break;
4253 }
4254 }
4255 if (agent == NULL) {
4256 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4257 engine_id, group_id);
4258 goto error;
4259 }
4260
4261 /* Try to find the right group */
4262 list_for_each_entry(group, &agent->groups, list) {
4263 /* This is the good group */
4264 if (!strcmp(group->id, group_id))
4265 break;
4266 }
4267 if (&group->list == &agent->groups) {
4268 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4269 group_id, engine_id);
4270 goto error;
4271 }
4272
4273 /* Ok, we found the group, we need to check messages and their
4274 * arguments */
4275 list_for_each_entry(msg, &group->messages, by_grp) {
4276 struct spoe_arg *arg;
4277
4278 list_for_each_entry(arg, &msg->args, list) {
4279 if (!(arg->expr->fetch->val & where)) {
4280 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4281 "some args extract information from '%s',"
4282 "none of which is available here ('%s')",
4283 msg->id, group->id, msg->conf.file, msg->conf.line,
4284 sample_ckp_names(arg->expr->fetch->use),
4285 sample_ckp_names(where));
4286 goto error;
4287 }
4288 }
4289 }
4290
4291 free(engine_id);
4292 free(group_id);
4293 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4294 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4295 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4296 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4297 return 1;
4298
4299 error:
4300 free(engine_id);
4301 free(group_id);
4302 return 0;
4303}
4304
4305/* Parse 'send-spoe-group' action following the format:
4306 *
4307 * ... send-spoe-group <engine-id> <group-id>
4308 *
4309 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4310 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4311 * ids are saved and used later, when the rule will be checked.
4312 */
4313static enum act_parse_ret
4314parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4315 struct act_rule *rule, char **err)
4316{
4317 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4318 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4319 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4320 return ACT_RET_PRS_ERR;
4321 }
4322 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4323 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4324
4325 (*orig_arg) += 2;
4326
4327 rule->action = ACT_CUSTOM;
4328 rule->action_ptr = spoe_send_group;
4329 rule->check_ptr = check_send_spoe_group;
4330 return ACT_RET_PRS_OK;
4331}
4332
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004333
4334/* Declare the filter parser for "spoe" keyword */
4335static struct flt_kw_list flt_kws = { "SPOE", { }, {
4336 { "spoe", parse_spoe_flt, NULL },
4337 { NULL, NULL, NULL },
4338 }
4339};
4340
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004341/* Delcate the action parser for "spoe-action" keyword */
4342static struct action_kw_list tcp_req_action_kws = { { }, {
4343 { "send-spoe-group", parse_send_spoe_group },
4344 { /* END */ },
4345 }
4346};
4347static struct action_kw_list tcp_res_action_kws = { { }, {
4348 { "send-spoe-group", parse_send_spoe_group },
4349 { /* END */ },
4350 }
4351};
4352static struct action_kw_list http_req_action_kws = { { }, {
4353 { "send-spoe-group", parse_send_spoe_group },
4354 { /* END */ },
4355 }
4356};
4357static struct action_kw_list http_res_action_kws = { { }, {
4358 { "send-spoe-group", parse_send_spoe_group },
4359 { /* END */ },
4360 }
4361};
4362
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004363__attribute__((constructor))
4364static void __spoe_init(void)
4365{
4366 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004367 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4368 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4369 http_req_keywords_register(&http_req_action_kws);
4370 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004371
Willy Tarreaubafbe012017-11-24 17:34:44 +01004372 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4373 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004374}
4375
4376__attribute__((destructor))
4377static void
4378__spoe_deinit(void)
4379{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004380 pool_destroy(pool_head_spoe_ctx);
4381 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004382}