blob: 7d5eb3dba135190491d6e2be3dee450ef95d248c [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 Fauletb2dd1e02018-03-22 09:07:41 +0100273
274static inline void
275spoe_update_stat_time(struct timeval *tv, long *t)
276{
277 if (*t == -1)
278 *t = tv_ms_elapsed(tv, &now);
279 else
280 *t += tv_ms_elapsed(tv, &now);
281 tv_zero(tv);
282}
283
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200284/********************************************************************
285 * Functions that encode/decode SPOE frames
286 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200287/* Helper to get static string length, excluding the terminating null byte */
288#define SLEN(str) (sizeof(str)-1)
289
290/* Predefined key used in HELLO/DISCONNECT frames */
291#define SUPPORTED_VERSIONS_KEY "supported-versions"
292#define VERSION_KEY "version"
293#define MAX_FRAME_SIZE_KEY "max-frame-size"
294#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100295#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100296#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297#define STATUS_CODE_KEY "status-code"
298#define MSG_KEY "message"
299
300struct spoe_version {
301 char *str;
302 int min;
303 int max;
304};
305
306/* All supported versions */
307static struct spoe_version supported_versions[] = {
308 {"1.0", 1000, 1000},
309 {NULL, 0, 0}
310};
311
312/* Comma-separated list of supported versions */
313#define SUPPORTED_VERSIONS_VAL "1.0"
314
Christopher Faulet8ef75252017-02-20 22:56:03 +0100315/* Convert a string to a SPOE version value. The string must follow the format
316 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
317 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200318static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 const char *p, *end;
322 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200323
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324 p = str;
325 end = str+len;
326 maj = min = 0;
327 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329 /* skip leading spaces */
330 while (p < end && isspace(*p))
331 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 /* parse Major number, until the '.' */
334 while (*p != '.') {
335 if (p >= end || *p < '0' || *p > '9')
336 goto out;
337 maj *= 10;
338 maj += (*p - '0');
339 p++;
340 }
341
342 /* check Major version */
343 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 goto out;
345
Christopher Faulet8ef75252017-02-20 22:56:03 +0100346 p++; /* skip the '.' */
347 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
348 goto out;
349
350 /* Parse Minor number */
351 while (p < end) {
352 if (*p < '0' || *p > '9')
353 break;
354 min *= 10;
355 min += (*p - '0');
356 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100358
359 /* check Minor number */
360 if (min > 999)
361 goto out;
362
363 /* skip trailing spaces */
364 while (p < end && isspace(*p))
365 p++;
366 if (p != end)
367 goto out;
368
369 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200370 out:
371 return vsn;
372}
373
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
375 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
376 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200377static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379{
Christopher Faulet305c6072017-02-23 16:17:53 +0100380 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100381 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100382 char *p, *end;
383 unsigned int flags = SPOE_FRM_FL_FIN;
384 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 p = frame;
387 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388
Christopher Faulet8ef75252017-02-20 22:56:03 +0100389 /* Set Frame type */
390 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100392 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100393 memcpy(p, (char *)&flags, 4);
394 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395
396 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100397 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
399 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
400 * and "capabilities" */
401
402 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 sz = SLEN(SUPPORTED_VERSIONS_KEY);
404 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
405 goto too_big;
406
407 *p++ = SPOE_DATA_T_STR;
408 sz = SLEN(SUPPORTED_VERSIONS_VAL);
409 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
412 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(MAX_FRAME_SIZE_KEY);
414 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
415 goto too_big;
416
417 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200418 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100419 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200420
421 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100422 sz = SLEN(CAPABILITIES_KEY);
423 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
424 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200425
Christopher Faulet8ef75252017-02-20 22:56:03 +0100426 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100427 chk = get_trash_chunk();
428 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
429 memcpy(chk->str, "pipelining", 10);
430 chk->len += 10;
431 }
432 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
433 if (chk->len) chk->str[chk->len++] = ',';
434 memcpy(chk->str+chk->len, "async", 5);
435 chk->len += 5;
436 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100437 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
438 if (chk->len) chk->str[chk->len++] = ',';
439 memcpy(chk->str+chk->len, "fragmentation", 13);
440 chk->len += 5;
441 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100442 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 goto too_big;
444
445 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100446 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100447 sz = SLEN(ENGINE_ID_KEY);
448 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
449 goto too_big;
450
451 *p++ = SPOE_DATA_T_STR;
452 sz = strlen(agent->engine_id);
453 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
454 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100455 }
456
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 return (p - frame);
458
459 too_big:
460 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
461 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462}
463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
465 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
466 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470 const char *reason;
471 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100472 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 p = frame;
476 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 /* Set Frame type */
479 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100481 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482 memcpy(p, (char *)&flags, 4);
483 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484
485 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 *p++ = 0; *p++ = 0;
487
488 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
489 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* There are 2 mandatory items: "status-code" and "message" */
492
493 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 sz = SLEN(STATUS_CODE_KEY);
495 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
496 goto too_big;
497
498 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200499 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100500 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501
502 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100503 sz = SLEN(MSG_KEY);
504 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
505 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
Christopher Faulet8ef75252017-02-20 22:56:03 +0100507 /*Get the message corresponding to the status code */
508 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
509
510 *p++ = SPOE_DATA_T_STR;
511 sz = strlen(reason);
512 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
513 goto too_big;
514
515 return (p - frame);
516
517 too_big:
518 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
519 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520}
521
Christopher Faulet8ef75252017-02-20 22:56:03 +0100522/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
523 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
524 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100526spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100527 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200528{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100529 char *p, *end;
530 unsigned int stream_id, frame_id;
531 unsigned int flags = SPOE_FRM_FL_FIN;
532 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200533
Christopher Faulet8ef75252017-02-20 22:56:03 +0100534 p = frame;
535 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100537 stream_id = ctx->stream_id;
538 frame_id = ctx->frame_id;
539
540 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
541 /* The fragmentation is not supported by the applet */
542 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
543 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
544 return -1;
545 }
546 flags = ctx->frag_ctx.flags;
547 }
548
549 /* Set Frame type */
550 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
551
552 /* Set flags */
553 memcpy(p, (char *)&flags, 4);
554 p += 4;
555
556 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200557 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100558 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200559 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100560 goto too_big;
561
562 /* Copy encoded messages, if possible */
563 sz = ctx->buffer->i;
564 if (p + sz >= end)
565 goto too_big;
566 memcpy(p, ctx->buffer->p, sz);
567 p += sz;
568
569 return (p - frame);
570
571 too_big:
572 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
573 return 0;
574}
575
576/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
577 * returns the number of encoded bytes in the frame on success, 0 if an encoding
578 * error occurred and -1 if a fatal error occurred. */
579static int
580spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
581 char *frame, size_t size)
582{
583 char *p, *end;
584 unsigned int stream_id, frame_id;
585 unsigned int flags;
586 size_t sz;
587
588 p = frame;
589 end = frame+size;
590
Christopher Faulet8ef75252017-02-20 22:56:03 +0100591 /* <ctx> is null when the stream has aborted the processing of a
592 * fragmented frame. In this case, we must notify the corresponding
593 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100595 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
597 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
598 }
599 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100600 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 stream_id = ctx->stream_id;
602 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100603 }
604
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100606 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100609 memcpy(p, (char *)&flags, 4);
610 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200611
612 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200613 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200615 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200617
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100618 if (ctx == NULL)
619 goto end;
620
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100622 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 if (p + sz >= end)
624 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100625 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100627
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100628 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 return (p - frame);
630
631 too_big:
632 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
633 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200634}
635
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636/* Decode and process the HELLO frame sent by an agent. It returns the number of
637 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
638 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200641{
Christopher Faulet305c6072017-02-23 16:17:53 +0100642 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
643 char *p, *end;
644 int vsn, max_frame_size;
645 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646
647 p = frame;
648 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649
650 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
652 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 if (size < 7 /* TYPE + METADATA */) {
657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
658 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659 }
660
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100661 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 memcpy((char *)&flags, p, 4);
663 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100665 /* Fragmentation is not supported for HELLO frame */
666 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100668 return -1;
669 }
670
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 if (*p != 0 || *(p+1) != 0) {
673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
674 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200675 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677
678 /* There are 3 mandatory items: "version", "max-frame-size" and
679 * "capabilities" */
680
681 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100682 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683 while (p < end) {
684 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200685 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687
688 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 ret = spoe_decode_buffer(&p, end, &str, &sz);
690 if (ret == -1 || !sz) {
691 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
692 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 /* Check "version" K/V item */
696 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 int i, type = *p++;
698
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
701 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
702 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
706 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
708
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 return -1;
713 }
714 for (i = 0; supported_versions[i].str != NULL; ++i) {
715 if (vsn >= supported_versions[i].min &&
716 vsn <= supported_versions[i].max)
717 break;
718 }
719 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 return -1;
722 }
723 }
724 /* Check "max-frame-size" K/V item */
725 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727
728 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
730 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
731 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
732 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
734 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200736 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
738 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200739 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 if (sz < MIN_FRAME_SIZE ||
741 sz > SPOE_APPCTX(appctx)->max_frame_size) {
742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743 return -1;
744 }
745 max_frame_size = sz;
746 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747 /* Check "capabilities" K/V item */
748 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750
751 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
753 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
754 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
757 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
758 return 0;
759 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 char *delim;
763
764 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
768 str += 10; sz -= 10;
769 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 flags |= SPOE_APPCTX_FL_PIPELINING;
771 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 else if (sz >= 5 && !strncmp(str, "async", 5)) {
773 str += 5; sz -= 5;
774 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775 flags |= SPOE_APPCTX_FL_ASYNC;
776 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
778 str += 13; sz -= 13;
779 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100780 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
781 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 /* Get the next comma or break */
784 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 delim++;
787 sz -= (delim - str);
788 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 }
790 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 else {
792 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 if (spoe_skip_data(&p, end) == -1) {
794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
795 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 }
798 }
799
800 /* Final checks */
801 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200803 return -1;
804 }
805 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200807 return -1;
808 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100809 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
810 flags &= ~SPOE_APPCTX_FL_PIPELINING;
811 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
812 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813
Christopher Faulet42bfa462017-01-04 14:14:19 +0100814 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
815 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
816 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100817
818 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819}
820
821/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
822 * bytes on success, 0 if the frame can be ignored and -1 if an error
823 * occurred. */
824static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100827 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100828 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829
830 p = frame;
831 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832
833 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
835 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100837 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 if (size < 7 /* TYPE + METADATA */) {
840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
841 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842 }
843
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100844 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845 memcpy((char *)&flags, p, 4);
846 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 /* Fragmentation is not supported for DISCONNECT frame */
849 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 return -1;
852 }
853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (*p != 0 || *(p+1) != 0) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860
861 /* There are 2 mandatory items: "status-code" and "message" */
862
863 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 while (p < end) {
865 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200866 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868
869 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 ret = spoe_decode_buffer(&p, end, &str, &sz);
871 if (ret == -1 || !sz) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
875
876 /* Check "status-code" K/V item */
877 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879
880 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
883 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200888 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
890 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
894
895 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896 else if (!memcmp(str, MSG_KEY, sz)) {
897 int type = *p++;
898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 ret = spoe_decode_buffer(&p, end, &str, &sz);
905 if (ret == -1 || sz > 255) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
910 SPOE_APPCTX(appctx)->reason = str;
911 SPOE_APPCTX(appctx)->rlen = sz;
912#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
914 else {
915 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 if (spoe_skip_data(&p, end) == -1) {
917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921 }
922
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924}
925
926
Christopher Fauleta1cda022016-12-21 08:58:06 +0100927/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 * success, 0 if the frame can be ignored and -1 if an error occurred. */
929static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938
939 p = frame;
940 end = frame + size;
941 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942
943 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (size < 7 /* TYPE + METADATA */) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
951 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 }
953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 memcpy((char *)&flags, p, 4);
956 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100958 /* Fragmentation is not supported for now */
959 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 return -1;
962 }
963
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200964 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200965 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100967 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200969 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200971 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100973
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100975 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200976 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100977 if ((*ctx)->stream_id == (unsigned int)stream_id &&
978 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100979 goto found;
980 }
981 }
982 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100983 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
984 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100986 goto found;
987 }
988 }
989
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100990 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
991 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
992 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
993
994 /* ABRT bit is set for an unfinished fragmented frame */
995 if (flags & SPOE_FRM_FL_ABRT) {
996 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100997 (*ctx)->state = SPOE_CTX_ST_ERROR;
998 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
999 /* Ignore the payload */
1000 goto end;
1001 }
1002 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1003 /* For now, we ignore the ack */
1004 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1005 return 0;
1006 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001007
Christopher Fauleta1cda022016-12-21 08:58:06 +01001008 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001009 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1010 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 " - stream-id=%u - frame-id=%u\n",
1012 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1013 __FUNCTION__, appctx,
1014 (unsigned int)stream_id, (unsigned int)frame_id);
1015
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001016 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001017 return 0;
1018
1019 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001020 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1021 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001022 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001023 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001024 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001025
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001026 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 len = (end - p);
1028 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1029 SPOE_APPCTX(appctx)->buffer->i = len;
1030 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001031
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001032 /* Transfer the buffer ownership to the SPOE context */
1033 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1034 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001035
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 (*ctx)->state = SPOE_CTX_ST_DONE;
1037
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001038 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001039 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001040 " - ACK frame received"
1041 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001042 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1044 (*ctx)->frame_id, flags);
1045 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001046}
1047
Christopher Fauletba7bc162016-11-07 21:07:38 +01001048/* This function is used in cfgparse.c and declared in proto/checks.h. It
1049 * prepare the request to send to agents during a healthcheck. It returns 0 on
1050 * success and -1 if an error occurred. */
1051int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001053{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054 struct appctx appctx;
1055 struct spoe_appctx spoe_appctx;
1056 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1057 size_t sz;
1058 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059
Christopher Faulet42bfa462017-01-04 14:14:19 +01001060 memset(&appctx, 0, sizeof(appctx));
1061 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001063
1064 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001065 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001066
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1068 end = frame + MAX_FRAME_SIZE;
1069
1070 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1071 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001072 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 /* Add "healthcheck" K/V item */
1076 sz = SLEN(HEALTHCHECK_KEY);
1077 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1078 return -1;
1079 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080
Christopher Faulet8ef75252017-02-20 22:56:03 +01001081 *len = frame - buf;
1082 sz = htonl(*len - 4);
1083 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return 0;
1089}
1090
1091/* This function is used in checks.c and declared in proto/checks.h. It decode
1092 * the response received from an agent during a healthcheck. It returns 0 on
1093 * success and -1 if an error occurred. */
1094int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001097 struct appctx appctx;
1098 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099
Christopher Faulet42bfa462017-01-04 14:14:19 +01001100 memset(&appctx, 0, sizeof(appctx));
1101 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102
Christopher Faulet42bfa462017-01-04 14:14:19 +01001103 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001104 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001105
Christopher Faulet8ef75252017-02-20 22:56:03 +01001106 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1107 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108 goto error;
1109 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001110 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1111 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112
1113 return 0;
1114
1115 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1117 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1118 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001119 return -1;
1120}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001121
Christopher Fauleta1cda022016-12-21 08:58:06 +01001122/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1123 * the frame can be ignored, 1 to retry later, and the frame legnth on
1124 * success. */
1125static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001127{
1128 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001129 int ret;
1130 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131
Christopher Faulet8ef75252017-02-20 22:56:03 +01001132 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1133 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134 netint = htonl(framesz);
1135 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001136 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001137 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001138 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 }
1142 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 return -1; /* error */
1144 }
1145 return framesz;
1146}
1147
1148/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1149 * when the frame can be ignored, 1 to retry later and the frame length on
1150 * success. */
1151static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001152spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153{
1154 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001155 int ret;
1156 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001157
Willy Tarreau06d80a92017-10-19 14:32:15 +02001158 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 if (ret > 0) {
1160 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001161 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001162 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163 return -1;
1164 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001165 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166 }
1167 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170 }
1171 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 return -1; /* error */
1173 }
1174 return framesz;
1175}
1176
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001177/********************************************************************
1178 * Functions that manage the SPOE applet
1179 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001180static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001182{
1183 si_applet_want_get(appctx->owner);
1184 si_applet_want_put(appctx->owner);
1185 appctx_wakeup(appctx);
1186 return 1;
1187}
1188
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001189/* Callback function that catches applet timeouts. If a timeout occurred, we set
1190 * <appctx->st1> flag and the SPOE applet is woken up. */
1191static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001192spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001193{
1194 struct appctx *appctx = task->context;
1195
1196 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1197 if (tick_is_expired(task->expire, now_ms)) {
1198 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001199 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001200 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001201 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202 return task;
1203}
1204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205/* Callback function that releases a SPOE applet. This happens when the
1206 * connection with the agent is closed. */
1207static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 struct stream_interface *si = appctx->owner;
1211 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1212 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001213 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214
1215 if (spoe_appctx == NULL)
1216 return;
1217
1218 appctx->ctx.spoe.ptr = NULL;
1219 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220
1221 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1222 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1223 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001224
Christopher Faulet8ef75252017-02-20 22:56:03 +01001225 /* Remove applet from the list of running applets */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001226 SPOE_DEBUG_STMT(agent->rt[tid].applets_act--);
1227 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001228 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1229 LIST_DEL(&spoe_appctx->list);
1230 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001231 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001232 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233
Christopher Faulet8ef75252017-02-20 22:56:03 +01001234 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001235 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001236 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1237 eb32_delete(&spoe_appctx->node);
1238 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1239 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001241 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1243 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001244
1245 si_shutw(si);
1246 si_shutr(si);
1247 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 }
1249
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 /* Destroy the task attached to this applet */
1251 if (spoe_appctx->task) {
1252 task_delete(spoe_appctx->task);
1253 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001254 }
1255
Christopher Faulet8ef75252017-02-20 22:56:03 +01001256 /* Notify all waiting streams */
1257 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258 LIST_DEL(&ctx->list);
1259 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001260 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001261 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001263 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264 }
1265
Christopher Faulet8ef75252017-02-20 22:56:03 +01001266 /* If the applet was processing a fragmented frame, notify the
1267 * corresponding stream. */
1268 if (spoe_appctx->frag_ctx.ctx) {
1269 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001270 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001271 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001273 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1274 }
1275
Christopher Faulet8ef75252017-02-20 22:56:03 +01001276 /* Release allocated memory */
1277 spoe_release_buffer(&spoe_appctx->buffer,
1278 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001279 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001280
Christopher Faulet24289f22017-09-25 14:48:02 +02001281 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001282 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283
Christopher Faulet8ef75252017-02-20 22:56:03 +01001284 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001285 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001286 LIST_DEL(&ctx->list);
1287 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001288 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001289 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001291 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001293 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001294 LIST_DEL(&ctx->list);
1295 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001296 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1300 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001301
1302 end:
1303 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001304 agent->rt[tid].frame_size = agent->max_frame_size;
1305 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1306 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001307}
1308
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001309static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001310spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001313 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001314 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 if (si->state <= SI_ST_CON) {
1318 si_applet_want_put(si);
1319 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1320 goto stop;
1321 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001322 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001324 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001325 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001326
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001328 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1329 " - Connection timed out\n",
1330 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1331 __FUNCTION__, appctx);
1332 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001334 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001335
Christopher Faulet42bfa462017-01-04 14:14:19 +01001336 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 SPOE_APPCTX(appctx)->task->expire =
1338 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001339
Christopher Faulet8ef75252017-02-20 22:56:03 +01001340 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1341 * length. */
1342 buf = trash.str; frame = buf+4;
1343 ret = spoe_prepare_hahello_frame(appctx, frame,
1344 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001346 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347
1348 switch (ret) {
1349 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001350 case 0: /* ignore => an error, cannot be ignored */
1351 goto exit;
1352
1353 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 goto stop;
1355
Christopher Faulet8ef75252017-02-20 22:56:03 +01001356 default:
1357 /* HELLO frame successfully sent, now wait for the
1358 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001359 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1360 goto next;
1361 }
1362
1363 next:
1364 return 0;
1365 stop:
1366 return 1;
1367 exit:
1368 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1369 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001370}
1371
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001372static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001373spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001375 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001376 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377 char *frame;
1378 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001379
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001380
Christopher Fauletb067b062017-01-04 16:39:11 +01001381 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001382 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001384 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001385
Christopher Fauleta1cda022016-12-21 08:58:06 +01001386 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001387 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1388 " - Connection timed out\n",
1389 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1390 __FUNCTION__, appctx);
1391 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 goto exit;
1393 }
1394
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 frame = trash.str; trash.len = 0;
1396 ret = spoe_recv_frame(appctx, frame,
1397 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001398 if (ret > 1) {
1399 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1400 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1401 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001402 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 trash.len = ret + 4;
1404 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 switch (ret) {
1408 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001409 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1411 goto next;
1412
1413 case 1: /* retry later */
1414 goto stop;
1415
1416 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 /* HELLO handshake is finished, set the idle timeout and
1418 * add the applet in the list of running applets. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001419 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001420 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001421 SPOE_APPCTX(appctx)->node.key = 0;
1422 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001423
1424 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001425 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001426 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001427 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001428
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001430 /* Do not forget to remove processed frame from the output buffer */
1431 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001432 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001433
1434 SPOE_APPCTX(appctx)->task->expire =
1435 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 return 0;
1437 stop:
1438 return 1;
1439 exit:
1440 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1441 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001442}
1443
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001444
Christopher Fauleta1cda022016-12-21 08:58:06 +01001445static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001446spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001447{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001448 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1449 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001450 char *frame, *buf;
1451 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452
Christopher Faulet8ef75252017-02-20 22:56:03 +01001453 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1454 * length. */
1455 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001456
1457 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1458 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1459 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1460 SPOE_APPCTX(appctx)->max_frame_size);
1461 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001462 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463 *skip = 1;
1464 ret = 1;
1465 goto end;
1466 }
1467 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001468 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001469 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1470 SPOE_APPCTX(appctx)->max_frame_size);
1471
1472 }
1473
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474 if (ret > 1)
1475 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001476
Christopher Faulet8ef75252017-02-20 22:56:03 +01001477 switch (ret) {
1478 case -1: /* error */
1479 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1480 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 case 0: /* ignore */
1483 if (ctx == NULL)
1484 goto abort_frag_frame;
1485
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001486 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001487 LIST_DEL(&ctx->list);
1488 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001489 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001490 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 ctx->state = SPOE_CTX_ST_ERROR;
1492 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1493 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001494 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001495 break;
1496
1497 case 1: /* retry */
1498 *skip = 1;
1499 break;
1500
1501 default:
1502 if (ctx == NULL)
1503 goto abort_frag_frame;
1504
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001505 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 LIST_DEL(&ctx->list);
1507 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001508 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001509 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001510 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1511 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1512 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001513 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 }
1516 goto end;
1517
1518 frag_frame_sent:
1519 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001520 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001521 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1522 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1523 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001524 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1525 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1526 goto end;
1527
1528 no_frag_frame_sent:
1529 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1530 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001531 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001532 }
1533 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1534 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1535 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1536 }
1537 else {
1538 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001539 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1541 }
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001542 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001543 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1544 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1545 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001546 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001547
Christopher Faulet8ef75252017-02-20 22:56:03 +01001548 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1549 goto end;
1550
1551 abort_frag_frame:
1552 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1553 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1554 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1555 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1556 goto end;
1557
1558 end:
1559 return ret;
1560}
1561
1562static int
1563spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1564{
1565 struct spoe_context *ctx = NULL;
1566 char *frame;
1567 int ret;
1568
1569 frame = trash.str; trash.len = 0;
1570 ret = spoe_recv_frame(appctx, frame,
1571 SPOE_APPCTX(appctx)->max_frame_size);
1572 if (ret > 1) {
1573 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1574 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001575 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001576 goto end;
1577 }
1578 trash.len = ret + 4;
1579 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1580 }
1581 switch (ret) {
1582 case -1: /* error */
1583 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1584 break;
1585
1586 case 0: /* ignore */
1587 break;
1588
1589 case 1: /* retry */
1590 *skip = 1;
1591 break;
1592
1593 default:
1594 LIST_DEL(&ctx->list);
1595 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001596 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1597 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001598 if (ctx->spoe_appctx) {
1599 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001600 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001601 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001602 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1603 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1604 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1605 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1606 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1607 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1608 }
1609 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1610 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001611 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1612 break;
1613 }
1614
1615 /* Do not forget to remove processed frame from the output buffer */
1616 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001617 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001618 end:
1619 return ret;
1620}
1621
1622static int
1623spoe_handle_processing_appctx(struct appctx *appctx)
1624{
1625 struct stream_interface *si = appctx->owner;
1626 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001627 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628
1629 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1630 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1631 goto exit;
1632 }
1633
1634 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1635 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1636 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1637 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1638 goto next;
1639 }
1640
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001641 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001642 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001643 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001644 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1645 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001646 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001647
Christopher Faulet8f82b202018-01-24 16:23:03 +01001648 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1649 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001650
Christopher Faulet8f82b202018-01-24 16:23:03 +01001651 /* receiving_frame loop */
1652 while (!skip_receiving) {
1653 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1654 switch (ret) {
1655 case -1: /* error */
1656 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001657
Christopher Faulet8f82b202018-01-24 16:23:03 +01001658 case 0: /* ignore */
1659 active_r = 1;
1660 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001661
Christopher Faulet8f82b202018-01-24 16:23:03 +01001662 case 1: /* retry */
1663 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001664
Christopher Faulet8f82b202018-01-24 16:23:03 +01001665 default:
1666 active_r = 1;
1667 break;
1668 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001669 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 /* send_frame loop */
1672 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1673 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1674 switch (ret) {
1675 case -1: /* error */
1676 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001677
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001679 if (SPOE_APPCTX(appctx)->node.key)
1680 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001681 active_s++;
1682 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 case 1: /* retry */
1685 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686
Christopher Faulet8f82b202018-01-24 16:23:03 +01001687 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001688 if (SPOE_APPCTX(appctx)->node.key)
1689 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001690 active_s++;
1691 break;
1692 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001694
Christopher Faulet8f82b202018-01-24 16:23:03 +01001695 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1697 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1698 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001699
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001701 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001702 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001703 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704 }
1705 return 1;
1706
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 next:
1708 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1709 return 0;
1710
Christopher Fauleta1cda022016-12-21 08:58:06 +01001711 exit:
1712 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1713 return 0;
1714}
1715
1716static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001717spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001718{
1719 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001720 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001721 char *frame, *buf;
1722 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001723
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1725 goto exit;
1726
1727 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1728 goto exit;
1729
Christopher Faulet8ef75252017-02-20 22:56:03 +01001730 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1731 * length. */
1732 buf = trash.str; frame = buf+4;
1733 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1734 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001735 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001736 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737
1738 switch (ret) {
1739 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001740 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001741 goto exit;
1742
1743 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744 goto stop;
1745
1746 default:
1747 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1748 " - disconnected by HAProxy (%d): %s\n",
1749 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001750 __FUNCTION__, appctx,
1751 SPOE_APPCTX(appctx)->status_code,
1752 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753
1754 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1755 goto next;
1756 }
1757
1758 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 SPOE_APPCTX(appctx)->task->expire =
1760 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761 return 0;
1762 stop:
1763 return 1;
1764 exit:
1765 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1766 return 0;
1767}
1768
1769static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001770spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771{
1772 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001773 char *frame;
1774 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001775
Christopher Fauletb067b062017-01-04 16:39:11 +01001776 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001777 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001779 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780
Christopher Fauletb067b062017-01-04 16:39:11 +01001781 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001784 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 frame = trash.str; trash.len = 0;
1787 ret = spoe_recv_frame(appctx, frame,
1788 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 trash.len = ret + 4;
1791 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 }
1793
1794 switch (ret) {
1795 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1797 " - error on frame (%s)\n",
1798 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001799 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001800 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 goto exit;
1803
1804 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 goto next;
1806
1807 case 1: /* retry */
1808 goto stop;
1809
1810 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001812 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001814 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1816 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 goto exit;
1818 }
1819
1820 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 /* Do not forget to remove processed frame from the output buffer */
1822 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001823 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 return 0;
1826 stop:
1827 return 1;
1828 exit:
1829 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1830 return 0;
1831}
1832
1833/* I/O Handler processing messages exchanged with the agent */
1834static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001835spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837 struct stream_interface *si = appctx->owner;
1838 struct spoe_agent *agent;
1839
1840 if (SPOE_APPCTX(appctx) == NULL)
1841 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842
Christopher Faulet8ef75252017-02-20 22:56:03 +01001843 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1844 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001845
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 switchstate:
1847 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1848 " - appctx-state=%s\n",
1849 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1850 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1851
1852 switch (appctx->st0) {
1853 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001854 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 goto out;
1856 goto switchstate;
1857
1858 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001859 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860 goto out;
1861 goto switchstate;
1862
1863 case SPOE_APPCTX_ST_IDLE:
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001864 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1865 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001866 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001867 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001868 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001869 SPOE_APPCTX(appctx)->task->expire =
1870 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001872 goto switchstate;
1873 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1875 /* fall through */
1876
1877 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001878 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1879 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 goto out;
1882 goto switchstate;
1883
1884 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001885 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001886 goto out;
1887 goto switchstate;
1888
1889 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 goto out;
1892 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001893
1894 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001895 appctx->st0 = SPOE_APPCTX_ST_END;
1896 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1897
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001898 si_shutw(si);
1899 si_shutr(si);
1900 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001901 /* fall through */
1902
1903 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001904 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001905 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001906 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001907 if (stopping)
1908 spoe_wakeup_appctx(appctx);
1909
Christopher Faulet42bfa462017-01-04 14:14:19 +01001910 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1911 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001912 si_oc(si)->flags |= CF_READ_DONTWAIT;
1913 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914}
1915
1916struct applet spoe_applet = {
1917 .obj_type = OBJ_TYPE_APPLET,
1918 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 .fct = spoe_handle_appctx,
1920 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001921};
1922
1923/* Create a SPOE applet. On success, the created applet is returned, else
1924 * NULL. */
1925static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001926spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001927{
1928 struct appctx *appctx;
1929 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001931
Emeric Brun1138fd02017-06-19 12:38:55 +02001932 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933 goto out_error;
1934
Willy Tarreaubafbe012017-11-24 17:34:44 +01001935 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001936 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001937 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001938 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001939
Christopher Faulet42bfa462017-01-04 14:14:19 +01001940 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001941 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001942 goto out_free_spoe_appctx;
1943
1944 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001945 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001946 SPOE_APPCTX(appctx)->task->context = appctx;
1947 SPOE_APPCTX(appctx)->agent = conf->agent;
1948 SPOE_APPCTX(appctx)->version = 0;
1949 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1950 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001951 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001952 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001953 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001954
1955 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1956 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001957 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001958
1959 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1960 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961
Willy Tarreau5820a362016-12-22 15:59:02 +01001962 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963 if (!sess)
1964 goto out_free_spoe;
1965
Willy Tarreau87787ac2017-08-28 16:22:54 +02001966 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 goto out_free_sess;
1968
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 stream_set_backend(strm, conf->agent->b.be);
1970
1971 /* applet is waiting for data */
1972 si_applet_cant_get(&strm->si[0]);
1973 appctx_wakeup(appctx);
1974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001975 strm->do_log = NULL;
1976 strm->res.flags |= CF_READ_DONTWAIT;
1977
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001978 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001979 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001980 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001981 SPOE_DEBUG_STMT(conf->agent->rt[tid].applets_act++);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001982
Emeric Brunc60def82017-09-27 14:59:38 +02001983 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001984 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985 return appctx;
1986
1987 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001988 out_free_sess:
1989 session_free(sess);
1990 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001991 task_free(SPOE_APPCTX(appctx)->task);
1992 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001993 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 out_free_appctx:
1995 appctx_free(appctx);
1996 out_error:
1997 return NULL;
1998}
1999
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002001spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002{
2003 struct spoe_config *conf = FLT_CONF(ctx->filter);
2004 struct spoe_agent *agent = conf->agent;
2005 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002006 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002007
Christopher Fauleta1cda022016-12-21 08:58:06 +01002008 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002009 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002010 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002011 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012
2013 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002014 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2016 ctx->strm);
2017
Christopher Fauleta1cda022016-12-21 08:58:06 +01002018 /* Do not try to create a new applet if there is no server up for the
2019 * agent's backend. */
2020 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2021 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2022 " - cannot create SPOE appctx: no server up\n",
2023 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2024 __FUNCTION__, ctx->strm);
2025 goto end;
2026 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002027
Christopher Fauleta1cda022016-12-21 08:58:06 +01002028 /* Do not try to create a new applet if we have reached the maximum of
2029 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002030 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002031 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002032 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2033 " - cannot create SPOE appctx: max CPS reached\n",
2034 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2035 __FUNCTION__, ctx->strm);
2036 goto end;
2037 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038 }
2039
Christopher Faulet8ef75252017-02-20 22:56:03 +01002040 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 if (appctx == NULL) {
2042 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2043 " - failed to create SPOE appctx\n",
2044 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2045 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002046 send_log(ctx->strm->be, LOG_EMERG,
2047 "SPOE: [%s] failed to create SPOE applet\n",
2048 agent->id);
2049
Christopher Fauleta1cda022016-12-21 08:58:06 +01002050 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051 }
2052
Christopher Fauleta1cda022016-12-21 08:58:06 +01002053 /* Increase the per-process number of cumulated connections */
2054 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002055 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002056
Christopher Fauleta1cda022016-12-21 08:58:06 +01002057 end:
2058 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002059 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002060 ctx->status_code = SPOE_CTX_ERR_RES;
2061 return -1;
2062 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002064 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002065 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002066 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2067 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068
2069 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002070 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002071 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002072 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002073 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002074 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002075
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002076 /* Finally try to wakeup an IDLE applet. */
2077 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2078 struct eb32_node *node;
2079
2080 node = eb32_first(&agent->rt[tid].idle_applets);
2081 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2082 if (node && spoe_appctx) {
2083 eb32_delete(&spoe_appctx->node);
2084 spoe_appctx->node.key++;
2085 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2086 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002087 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002088 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002090}
2091
2092/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002093 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002094 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002095/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2096 * fragmented_content. If the next message can be processed, it returns 0. If
2097 * the message is too big, it returns -1.*/
2098static int
2099spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2100 struct spoe_message *msg, int dir,
2101 char **buf, char *end)
2102{
2103 struct sample *smp;
2104 struct spoe_arg *arg;
2105 int ret;
2106
2107 if (msg->cond) {
2108 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2109 ret = acl_pass(ret);
2110 if (msg->cond->pol == ACL_COND_UNLESS)
2111 ret = !ret;
2112
2113 /* the rule does not match */
2114 if (!ret)
2115 goto next;
2116 }
2117
2118 /* Resume encoding of a SPOE argument */
2119 if (ctx->frag_ctx.curarg != NULL) {
2120 arg = ctx->frag_ctx.curarg;
2121 goto encode_argument;
2122 }
2123
2124 if (ctx->frag_ctx.curoff != UINT_MAX)
2125 goto encode_msg_payload;
2126
2127 /* Check if there is enough space for the message name and the
2128 * number of arguments. It implies <msg->id_len> is encoded on 2
2129 * bytes, at most (< 2288). */
2130 if (*buf + 2 + msg->id_len + 1 > end)
2131 goto too_big;
2132
2133 /* Encode the message name */
2134 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2135 goto too_big;
2136
2137 /* Set the number of arguments for this message */
2138 **buf = msg->nargs;
2139 (*buf)++;
2140
2141 ctx->frag_ctx.curoff = 0;
2142 encode_msg_payload:
2143
2144 /* Loop on arguments */
2145 list_for_each_entry(arg, &msg->args, list) {
2146 ctx->frag_ctx.curarg = arg;
2147 ctx->frag_ctx.curoff = UINT_MAX;
2148
2149 encode_argument:
2150 if (ctx->frag_ctx.curoff != UINT_MAX)
2151 goto encode_arg_value;
2152
2153 /* Encode the arguement name as a string. It can by NULL */
2154 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2155 goto too_big;
2156
2157 ctx->frag_ctx.curoff = 0;
2158 encode_arg_value:
2159
2160 /* Fetch the arguement value */
2161 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2162 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2163 if (ret == -1 || ctx->frag_ctx.curoff)
2164 goto too_big;
2165 }
2166
2167 next:
2168 return 0;
2169
2170 too_big:
2171 return -1;
2172}
2173
Christopher Fauletc718b822017-09-21 16:50:56 +02002174/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2175 * handle fragmented content. On success it returns 1. If an error occurred, -1
2176 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2177 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002178static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002179spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002180 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002181{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002182 struct spoe_config *conf = FLT_CONF(ctx->filter);
2183 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002184 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002185 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002186
Christopher Faulet8ef75252017-02-20 22:56:03 +01002187 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002188 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002189
Christopher Fauletc718b822017-09-21 16:50:56 +02002190 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2191 /* Resume encoding of a SPOE message */
2192 if (ctx->frag_ctx.curmsg != NULL) {
2193 msg = ctx->frag_ctx.curmsg;
2194 goto encode_evt_message;
2195 }
2196
2197 list_for_each_entry(msg, messages, by_evt) {
2198 ctx->frag_ctx.curmsg = msg;
2199 ctx->frag_ctx.curarg = NULL;
2200 ctx->frag_ctx.curoff = UINT_MAX;
2201
2202 encode_evt_message:
2203 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2204 goto too_big;
2205 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002206 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002207 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2208 /* Resume encoding of a SPOE message */
2209 if (ctx->frag_ctx.curmsg != NULL) {
2210 msg = ctx->frag_ctx.curmsg;
2211 goto encode_grp_message;
2212 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002213
Christopher Fauletc718b822017-09-21 16:50:56 +02002214 list_for_each_entry(msg, messages, by_grp) {
2215 ctx->frag_ctx.curmsg = msg;
2216 ctx->frag_ctx.curarg = NULL;
2217 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002218
Christopher Fauletc718b822017-09-21 16:50:56 +02002219 encode_grp_message:
2220 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2221 goto too_big;
2222 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002223 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002224 else
2225 goto skip;
2226
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002227
Christopher Faulet57583e42017-09-04 15:41:09 +02002228 /* nothing has been encoded for an unfragmented payload */
2229 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2230 goto skip;
2231
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002232 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002233 " - encode %s messages - spoe_appctx=%p"
2234 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235 (int)now.tv_sec, (int)now.tv_usec,
2236 agent->id, __FUNCTION__, s,
2237 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002238 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002239 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002240
Christopher Faulet8ef75252017-02-20 22:56:03 +01002241 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002242 ctx->frag_ctx.curmsg = NULL;
2243 ctx->frag_ctx.curarg = NULL;
2244 ctx->frag_ctx.curoff = 0;
2245 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002246
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002247 return 1;
2248
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002250 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2251 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2252 return -1;
2253 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002254
2255 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002256 " - encode fragmented messages - spoe_appctx=%p"
2257 " - curmsg=%p - curarg=%p - curoff=%u"
2258 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002260 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002262 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263
Christopher Faulet8ef75252017-02-20 22:56:03 +01002264 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002265 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2266 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2267 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002268
2269 skip:
2270 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2271 " - skip the frame because nothing has been encoded\n",
2272 (int)now.tv_sec, (int)now.tv_usec,
2273 agent->id, __FUNCTION__, s);
2274 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002275}
2276
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002277
2278/***************************************************************************
2279 * Functions that handle SPOE actions
2280 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002281/* Helper function to set a variable */
2282static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002283spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002284 struct sample *smp)
2285{
2286 struct spoe_config *conf = FLT_CONF(ctx->filter);
2287 struct spoe_agent *agent = conf->agent;
2288 char varname[64];
2289
2290 memset(varname, 0, sizeof(varname));
2291 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2292 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002293 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2294 vars_set_by_name(varname, len, smp);
2295 else
2296 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002297}
2298
2299/* Helper function to unset a variable */
2300static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002302 struct sample *smp)
2303{
2304 struct spoe_config *conf = FLT_CONF(ctx->filter);
2305 struct spoe_agent *agent = conf->agent;
2306 char varname[64];
2307
2308 memset(varname, 0, sizeof(varname));
2309 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2310 scope, agent->var_pfx, len, name);
2311 vars_unset_by_name_ifexist(varname, len, smp);
2312}
2313
2314
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315static inline int
2316spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2317 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002318{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319 char *str, *scope, *p = *buf;
2320 struct sample smp;
2321 uint64_t sz;
2322 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324 if (p + 2 >= end)
2325 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326
Christopher Faulet8ef75252017-02-20 22:56:03 +01002327 /* SET-VAR requires 3 arguments */
2328 if (*p++ != 3)
2329 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331 switch (*p++) {
2332 case SPOE_SCOPE_PROC: scope = "proc"; break;
2333 case SPOE_SCOPE_SESS: scope = "sess"; break;
2334 case SPOE_SCOPE_TXN : scope = "txn"; break;
2335 case SPOE_SCOPE_REQ : scope = "req"; break;
2336 case SPOE_SCOPE_RES : scope = "res"; break;
2337 default: goto skip;
2338 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2341 goto skip;
2342 memset(&smp, 0, sizeof(smp));
2343 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345 if (spoe_decode_data(&p, end, &smp) == -1)
2346 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2349 " - set-var '%s.%s.%.*s'\n",
2350 (int)now.tv_sec, (int)now.tv_usec,
2351 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2352 __FUNCTION__, s, scope,
2353 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2354 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 ret = (p - *buf);
2359 *buf = p;
2360 return ret;
2361 skip:
2362 return 0;
2363}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365static inline int
2366spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2367 char **buf, char *end, int dir)
2368{
2369 char *str, *scope, *p = *buf;
2370 struct sample smp;
2371 uint64_t sz;
2372 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374 if (p + 2 >= end)
2375 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002376
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 /* UNSET-VAR requires 2 arguments */
2378 if (*p++ != 2)
2379 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 switch (*p++) {
2382 case SPOE_SCOPE_PROC: scope = "proc"; break;
2383 case SPOE_SCOPE_SESS: scope = "sess"; break;
2384 case SPOE_SCOPE_TXN : scope = "txn"; break;
2385 case SPOE_SCOPE_REQ : scope = "req"; break;
2386 case SPOE_SCOPE_RES : scope = "res"; break;
2387 default: goto skip;
2388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2391 goto skip;
2392 memset(&smp, 0, sizeof(smp));
2393 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2396 " - unset-var '%s.%s.%.*s'\n",
2397 (int)now.tv_sec, (int)now.tv_usec,
2398 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2399 __FUNCTION__, s, scope,
2400 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2401 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 spoe_unset_var(ctx, scope, str, sz, &smp);
2404
2405 ret = (p - *buf);
2406 *buf = p;
2407 return ret;
2408 skip:
2409 return 0;
2410}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412/* Process SPOE actions for a specific event. It returns 1 on success. If an
2413 * error occurred, 0 is returned. */
2414static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002415spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002416{
2417 char *p, *end;
2418 int ret;
2419
2420 p = ctx->buffer->p;
2421 end = p + ctx->buffer->i;
2422
2423 while (p < end) {
2424 enum spoe_action_type type;
2425
2426 type = *p++;
2427 switch (type) {
2428 case SPOE_ACT_T_SET_VAR:
2429 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2430 if (!ret)
2431 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002432 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002433
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434 case SPOE_ACT_T_UNSET_VAR:
2435 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2436 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438 break;
2439
2440 default:
2441 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442 }
2443 }
2444
2445 return 1;
2446 skip:
2447 return 0;
2448}
2449
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002450/***************************************************************************
2451 * Functions that process SPOE events
2452 **************************************************************************/
2453static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002454spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002455{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002456 /* If a process is already started for this SPOE context, retry
2457 * later. */
2458 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002459 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002460
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002461 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002462 ctx->stats.tv_start = now;
2463 ctx->stats.tv_request = now;
2464 ctx->stats.t_request = -1;
2465 ctx->stats.t_queue = -1;
2466 ctx->stats.t_waiting = -1;
2467 ctx->stats.t_response = -1;
2468 ctx->stats.t_process = -1;
2469
2470 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002471
Christopher Fauleta1cda022016-12-21 08:58:06 +01002472 /* Set the right flag to prevent request and response processing
2473 * in same time. */
2474 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2475 ? SPOE_CTX_FL_REQ_PROCESS
2476 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002477 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002478}
2479
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002480static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002481spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002482{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002483 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002484
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002485 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2486 return;
2487
Christopher Faulet879dca92018-03-23 11:53:24 +01002488 if (sa) {
2489 if (sa->frag_ctx.ctx == ctx) {
2490 sa->frag_ctx.ctx = NULL;
2491 spoe_wakeup_appctx(sa->owner);
2492 }
2493 else
2494 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002495 }
2496
Christopher Fauleta1cda022016-12-21 08:58:06 +01002497 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002498 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002499 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002500
2501 /* Reset processing timer */
2502 ctx->process_exp = TICK_ETERNITY;
2503
Christopher Faulet8ef75252017-02-20 22:56:03 +01002504 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002505
Christopher Fauletfce747b2018-01-24 15:59:32 +01002506 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002507 ctx->frag_ctx.curmsg = NULL;
2508 ctx->frag_ctx.curarg = NULL;
2509 ctx->frag_ctx.curoff = 0;
2510 ctx->frag_ctx.flags = 0;
2511
Christopher Fauleta1cda022016-12-21 08:58:06 +01002512 if (!LIST_ISEMPTY(&ctx->list)) {
2513 LIST_DEL(&ctx->list);
2514 LIST_INIT(&ctx->list);
2515 }
2516}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002517
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002518static void
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002519spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2520 struct spoe_context *ctx, int dir)
2521{
2522 if (!tv_iszero(&ctx->stats.tv_start)) {
2523 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2524 ctx->stats.t_total += ctx->stats.t_process;
2525 tv_zero(&ctx->stats.tv_request);
2526 tv_zero(&ctx->stats.tv_queue);
2527 tv_zero(&ctx->stats.tv_wait);
2528 tv_zero(&ctx->stats.tv_response);
2529 }
2530}
2531
2532static void
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002533spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2534 struct spoe_context *ctx, int dir)
2535{
2536 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002537 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002538
2539 if (agent->var_on_error) {
2540 struct sample smp;
2541
2542 memset(&smp, 0, sizeof(smp));
2543 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2544 smp.data.u.sint = ctx->status_code;
2545 smp.data.type = SMP_T_BOOL;
2546
2547 spoe_set_var(ctx, "txn", agent->var_on_error,
2548 strlen(agent->var_on_error), &smp);
2549 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002550
2551 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2552 ? SPOE_CTX_ST_READY
2553 : SPOE_CTX_ST_NONE);
2554}
2555
Christopher Faulet58d03682017-09-21 16:57:24 +02002556/* Process a list of SPOE messages. First, this functions will process messages
2557 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2558 * to process corresponding actions. During all the processing, it returns 0
2559 * and it returns 1 when the processing is finished. If an error occurred, -1
2560 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002561static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002562spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2563 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002564{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002565 struct spoe_config *conf = FLT_CONF(ctx->filter);
2566 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002567 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002568
2569 if (ctx->state == SPOE_CTX_ST_ERROR)
2570 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002571
2572 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2573 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002574 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002575 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002576 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002577 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002578 goto error;
2579 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002580
2581 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002583 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002584 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002585 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002586 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002587 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588 goto skip;
2589 }
2590 }
2591
Christopher Fauletf7a30922016-11-10 15:04:51 +01002592 if (!tick_isset(ctx->process_exp)) {
2593 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2594 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2595 ctx->process_exp);
2596 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002597 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002598 if (!ret)
2599 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002601 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002602 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002603 }
2604
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002606 if (!tv_iszero(&ctx->stats.tv_request))
2607 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002608 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002609 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002610 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002611 if (ret < 0)
2612 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002613 if (!ret)
2614 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002615 if (spoe_queue_context(ctx) < 0)
2616 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002617 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2618 }
2619
2620 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002621 if (ctx->spoe_appctx)
2622 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002623 ret = 0;
2624 goto out;
2625 }
2626
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002627 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2628 ret = 0;
2629 goto out;
2630 }
2631
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002632 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002633 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002634 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002635 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002636 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002637 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002639 }
2640
2641 out:
2642 return ret;
2643
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002644 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002645 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646 ret = 1;
2647 goto end;
2648
2649 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002650 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002651 ctx->state = SPOE_CTX_ST_READY;
2652 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002653
Christopher Fauleta1cda022016-12-21 08:58:06 +01002654 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002655 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002656 spoe_stop_processing(agent, ctx);
Christopher Faulet58d03682017-09-21 16:57:24 +02002657 return ret;
2658}
2659
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002660/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2661 * See spoe_process_message for details. */
2662static int
2663spoe_process_group(struct stream *s, struct spoe_context *ctx,
2664 struct spoe_group *group, int dir)
2665{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002666 struct spoe_config *conf = FLT_CONF(ctx->filter);
2667 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002668 int ret;
2669
2670 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2671 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002672 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002673 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2674 group->id);
2675
2676 if (LIST_ISEMPTY(&group->messages))
2677 return 1;
2678
2679 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002680 if (ret && ctx->stats.t_process != -1) {
2681 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2682 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2683 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2684 __FUNCTION__, s, s->uniq_id, ctx->status_code,
2685 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2686 ctx->stats.t_response, ctx->stats.t_process);
2687 send_log(s->be, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2688 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2689 agent->id, group->id, s->uniq_id, ctx->status_code,
2690 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2691 ctx->stats.t_response, ctx->stats.t_process);
2692 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002693 return ret;
2694}
2695
Christopher Faulet58d03682017-09-21 16:57:24 +02002696/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2697 * See spoe_process_message for details. */
2698static int
2699spoe_process_event(struct stream *s, struct spoe_context *ctx,
2700 enum spoe_event ev)
2701{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002702 struct spoe_config *conf = FLT_CONF(ctx->filter);
2703 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002704 int dir, ret;
2705
2706 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002707 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002708 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002709 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2710 spoe_event_str[ev]);
2711
2712 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2713
2714 if (LIST_ISEMPTY(&(ctx->events[ev])))
2715 return 1;
2716
2717 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002718 if (ret && ctx->stats.t_process != -1) {
2719 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2720 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2721 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2722 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2723 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2724 ctx->stats.t_response, ctx->stats.t_process);
2725 send_log(s->be, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2726 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2727 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2728 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2729 ctx->stats.t_response, ctx->stats.t_process);
2730 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002731 return ret;
2732}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002733
2734/***************************************************************************
2735 * Functions that create/destroy SPOE contexts
2736 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002737static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002738spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002739{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002740 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002741 return 1;
2742
Christopher Faulet4596fb72017-01-11 14:05:19 +01002743 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002744 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002745 LIST_DEL(&buffer_wait->list);
2746 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002747 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002748 }
2749
Christopher Faulet4596fb72017-01-11 14:05:19 +01002750 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002751 return 1;
2752
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002753 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002754 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002755 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002756 return 0;
2757}
2758
2759static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002760spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002761{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002762 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002763 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002764 LIST_DEL(&buffer_wait->list);
2765 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002766 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002767 }
2768
2769 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002770 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002771 b_free(buf);
2772 offer_buffers(buffer_wait->target,
2773 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002774 }
2775}
2776
Christopher Faulet4596fb72017-01-11 14:05:19 +01002777static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002778spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002779{
2780 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2781 return 1;
2782}
2783
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002784static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002785spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002786{
2787 struct spoe_config *conf = FLT_CONF(filter);
2788 struct spoe_context *ctx;
2789
Willy Tarreaubafbe012017-11-24 17:34:44 +01002790 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002791 if (ctx == NULL) {
2792 return NULL;
2793 }
2794 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002795 ctx->filter = filter;
2796 ctx->state = SPOE_CTX_ST_NONE;
2797 ctx->status_code = SPOE_CTX_ERR_NONE;
2798 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002799 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002800 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002801 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002802 LIST_INIT(&ctx->buffer_wait.list);
2803 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002804 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002805 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002806
Christopher Fauletf7a30922016-11-10 15:04:51 +01002807 ctx->stream_id = 0;
2808 ctx->frame_id = 1;
2809 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002810
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002811 tv_zero(&ctx->stats.tv_start);
2812 tv_zero(&ctx->stats.tv_request);
2813 tv_zero(&ctx->stats.tv_queue);
2814 tv_zero(&ctx->stats.tv_wait);
2815 tv_zero(&ctx->stats.tv_response);
2816 ctx->stats.t_request = -1;
2817 ctx->stats.t_queue = -1;
2818 ctx->stats.t_waiting = -1;
2819 ctx->stats.t_response = -1;
2820 ctx->stats.t_process = -1;
2821 ctx->stats.t_total = 0;
2822
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002823 ctx->strm = s;
2824 ctx->state = SPOE_CTX_ST_READY;
2825 filter->ctx = ctx;
2826
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002827 return ctx;
2828}
2829
2830static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002831spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002832{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002833 struct spoe_config *conf = FLT_CONF(filter);
2834 struct spoe_context *ctx = filter->ctx;
2835
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002836 if (!ctx)
2837 return;
2838
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002839 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002840 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002841 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002842}
2843
2844static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002845spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002846{
2847 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002848 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002849
2850 tv_zero(&ctx->stats.tv_start);
2851 tv_zero(&ctx->stats.tv_request);
2852 tv_zero(&ctx->stats.tv_queue);
2853 tv_zero(&ctx->stats.tv_wait);
2854 tv_zero(&ctx->stats.tv_response);
2855 ctx->stats.t_request = -1;
2856 ctx->stats.t_queue = -1;
2857 ctx->stats.t_waiting = -1;
2858 ctx->stats.t_response = -1;
2859 ctx->stats.t_process = -1;
2860 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861}
2862
2863
2864/***************************************************************************
2865 * Hooks that manage the filter lifecycle (init/check/deinit)
2866 **************************************************************************/
2867/* Signal handler: Do a soft stop, wakeup SPOE applet */
2868static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002869spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002870{
2871 struct proxy *p;
2872
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002873 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002874 while (p) {
2875 struct flt_conf *fconf;
2876
2877 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002878 struct spoe_config *conf;
2879 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002880 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002881 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882
Christopher Faulet3b386a32017-02-23 10:17:15 +01002883 if (fconf->id != spoe_filter_id)
2884 continue;
2885
2886 conf = fconf->conf;
2887 agent = conf->agent;
2888
Christopher Faulet24289f22017-09-25 14:48:02 +02002889 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002890 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002891 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2892 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002893 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002894 }
2895 }
2896 p = p->next;
2897 }
2898}
2899
2900
2901/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2902static int
2903spoe_init(struct proxy *px, struct flt_conf *fconf)
2904{
2905 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002906
2907 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2908 init_new_proxy(&conf->agent_fe);
2909 conf->agent_fe.parent = conf->agent;
2910 conf->agent_fe.last_change = now.tv_sec;
2911 conf->agent_fe.id = conf->agent->id;
2912 conf->agent_fe.cap = PR_CAP_FE;
2913 conf->agent_fe.mode = PR_MODE_TCP;
2914 conf->agent_fe.maxconn = 0;
2915 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2916 conf->agent_fe.conn_retries = CONN_RETRIES;
2917 conf->agent_fe.accept = frontend_accept;
2918 conf->agent_fe.srv = NULL;
2919 conf->agent_fe.timeout.client = TICK_ETERNITY;
2920 conf->agent_fe.default_target = &spoe_applet.obj_type;
2921 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2922
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002924 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002925 sighandler_registered = 1;
2926 }
2927
2928 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002929}
2930
2931/* Free ressources allocated by the SPOE filter. */
2932static void
2933spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2934{
2935 struct spoe_config *conf = fconf->conf;
2936
2937 if (conf) {
2938 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002939
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002941 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 free(conf);
2943 }
2944 fconf->conf = NULL;
2945}
2946
2947/* Check configuration of a SPOE filter for a specified proxy.
2948 * Return 1 on error, else 0. */
2949static int
2950spoe_check(struct proxy *px, struct flt_conf *fconf)
2951{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002952 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953 struct spoe_config *conf = fconf->conf;
2954 struct proxy *target;
2955
Christopher Faulet7ee86672017-09-19 11:08:28 +02002956 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2957 * are uniq */
2958 list_for_each_entry(f, &px->filter_configs, list) {
2959 struct spoe_config *c = f->conf;
2960
2961 /* This is not an SPOE filter */
2962 if (f->id != spoe_filter_id)
2963 continue;
2964 /* This is the current SPOE filter */
2965 if (f == fconf)
2966 continue;
2967
2968 /* Check engine Id. It should be uniq */
2969 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002970 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2971 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002972 return 1;
2973 }
2974 }
2975
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976 target = proxy_be_by_name(conf->agent->b.name);
2977 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002978 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2979 " declared at %s:%d.\n",
2980 px->id, conf->agent->b.name, conf->agent->id,
2981 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 return 1;
2983 }
2984 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002985 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2986 " at %s:%d does not support HTTP mode.\n",
2987 px->id, target->id, conf->agent->id,
2988 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989 return 1;
2990 }
2991
2992 free(conf->agent->b.name);
2993 conf->agent->b.name = NULL;
2994 conf->agent->b.be = target;
2995 return 0;
2996}
2997
2998/**************************************************************************
2999 * Hooks attached to a stream
3000 *************************************************************************/
3001/* Called when a filter instance is created and attach to a stream. It creates
3002 * the context that will be used to process this stream. */
3003static int
3004spoe_start(struct stream *s, struct filter *filter)
3005{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003006 struct spoe_config *conf = FLT_CONF(filter);
3007 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003008 struct spoe_context *ctx;
3009
3010 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003011 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012 __FUNCTION__, s);
3013
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003014 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003015 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3016 " - failed to create SPOE context\n",
3017 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003018 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003019 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003020 "SPOE: [%s] failed to create SPOE context\n",
3021 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003022 return 0;
3023 }
3024
Christopher Faulet11610f32017-09-21 10:23:10 +02003025 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003026 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3027
Christopher Faulet11610f32017-09-21 10:23:10 +02003028 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003029 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3030
Christopher Faulet11610f32017-09-21 10:23:10 +02003031 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003032 filter->pre_analyzers |= AN_RES_INSPECT;
3033
Christopher Faulet11610f32017-09-21 10:23:10 +02003034 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003035 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3036
Christopher Faulet11610f32017-09-21 10:23:10 +02003037 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3039
Christopher Faulet11610f32017-09-21 10:23:10 +02003040 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003041 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3042
3043 return 1;
3044}
3045
3046/* Called when a filter instance is detached from a stream. It release the
3047 * attached SPOE context. */
3048static void
3049spoe_stop(struct stream *s, struct filter *filter)
3050{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3052 (int)now.tv_sec, (int)now.tv_usec,
3053 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3054 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003055 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056}
3057
Christopher Fauletf7a30922016-11-10 15:04:51 +01003058
3059/*
3060 * Called when the stream is woken up because of expired timer.
3061 */
3062static void
3063spoe_check_timeouts(struct stream *s, struct filter *filter)
3064{
3065 struct spoe_context *ctx = filter->ctx;
3066
Christopher Fauletac580602018-03-20 16:09:20 +01003067 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003068 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003069}
3070
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003071/* Called when we are ready to filter data on a channel */
3072static int
3073spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3074{
3075 struct spoe_context *ctx = filter->ctx;
3076 int ret = 1;
3077
3078 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3079 " - ctx-flags=0x%08x\n",
3080 (int)now.tv_sec, (int)now.tv_usec,
3081 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3082 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3083
Christopher Fauletb067b062017-01-04 16:39:11 +01003084 if (ctx->state == SPOE_CTX_ST_NONE)
3085 goto out;
3086
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003087 if (!(chn->flags & CF_ISRESP)) {
3088 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3089 chn->analysers |= AN_REQ_INSPECT_FE;
3090 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3091 chn->analysers |= AN_REQ_INSPECT_BE;
3092
3093 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3094 goto out;
3095
3096 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003097 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003098 if (!ret)
3099 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003100 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3101 }
3102 else {
3103 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3104 chn->analysers |= AN_RES_INSPECT;
3105
3106 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3107 goto out;
3108
Christopher Faulet8ef75252017-02-20 22:56:03 +01003109 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003110 if (!ret) {
3111 channel_dont_read(chn);
3112 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003113 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003114 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003115 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003116 }
3117
3118 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003119 return ret;
3120}
3121
3122/* Called before a processing happens on a given channel */
3123static int
3124spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3125 struct channel *chn, unsigned an_bit)
3126{
3127 struct spoe_context *ctx = filter->ctx;
3128 int ret = 1;
3129
3130 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3131 " - ctx-flags=0x%08x - ana=0x%08x\n",
3132 (int)now.tv_sec, (int)now.tv_usec,
3133 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3134 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3135 ctx->flags, an_bit);
3136
Christopher Fauletb067b062017-01-04 16:39:11 +01003137 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003138 goto out;
3139
3140 switch (an_bit) {
3141 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003142 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003143 break;
3144 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003145 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146 break;
3147 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003148 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003149 break;
3150 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003151 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003152 break;
3153 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003154 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155 break;
3156 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003157 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003158 break;
3159 }
3160
3161 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003162 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 channel_dont_read(chn);
3164 channel_dont_close(chn);
3165 }
3166 return ret;
3167}
3168
3169/* Called when the filtering on the channel ends. */
3170static int
3171spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3172{
3173 struct spoe_context *ctx = filter->ctx;
3174
3175 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3176 " - ctx-flags=0x%08x\n",
3177 (int)now.tv_sec, (int)now.tv_usec,
3178 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3179 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3180
3181 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003182 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003183 }
3184
3185 return 1;
3186}
3187
3188/********************************************************************
3189 * Functions that manage the filter initialization
3190 ********************************************************************/
3191struct flt_ops spoe_ops = {
3192 /* Manage SPOE filter, called for each filter declaration */
3193 .init = spoe_init,
3194 .deinit = spoe_deinit,
3195 .check = spoe_check,
3196
3197 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003198 .attach = spoe_start,
3199 .detach = spoe_stop,
3200 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003201
3202 /* Handle channels activity */
3203 .channel_start_analyze = spoe_start_analyze,
3204 .channel_pre_analyze = spoe_chn_pre_analyze,
3205 .channel_end_analyze = spoe_end_analyze,
3206};
3207
3208
3209static int
3210cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3211{
3212 const char *err;
3213 int i, err_code = 0;
3214
3215 if ((cfg_scope == NULL && curengine != NULL) ||
3216 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003217 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003218 goto out;
3219
3220 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3221 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003222 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3223 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 err_code |= ERR_ALERT | ERR_ABORT;
3225 goto out;
3226 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003227 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3228 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 goto out;
3230 }
3231
3232 err = invalid_char(args[1]);
3233 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003234 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3235 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003236 err_code |= ERR_ALERT | ERR_ABORT;
3237 goto out;
3238 }
3239
3240 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003241 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3242 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003243 err_code |= ERR_ALERT | ERR_ABORT;
3244 goto out;
3245 }
3246 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003247 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003248 err_code |= ERR_ALERT | ERR_ABORT;
3249 goto out;
3250 }
3251
3252 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003253
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003254 curagent->conf.file = strdup(file);
3255 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003256
3257 curagent->timeout.hello = TICK_ETERNITY;
3258 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003259 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003260
3261 curagent->engine_id = NULL;
3262 curagent->var_pfx = NULL;
3263 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003264 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3265 if (global.nbthread == 1)
3266 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003267 curagent->cps_max = 0;
3268 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003269 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003270 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003271
3272 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003273 LIST_INIT(&curagent->events[i]);
3274 LIST_INIT(&curagent->groups);
3275 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003276
Christopher Faulet24289f22017-09-25 14:48:02 +02003277 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003278 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003279 err_code |= ERR_ALERT | ERR_ABORT;
3280 goto out;
3281 }
3282 for (i = 0; i < global.nbthread; ++i) {
3283 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003284 SPOE_DEBUG_STMT(curagent->rt[i].applets_act = 0);
3285 SPOE_DEBUG_STMT(curagent->rt[i].applets_idle = 0);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003286 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003287 LIST_INIT(&curagent->rt[i].applets);
3288 LIST_INIT(&curagent->rt[i].sending_queue);
3289 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003290 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003291 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 }
3293 else if (!strcmp(args[0], "use-backend")) {
3294 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003295 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3296 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 err_code |= ERR_ALERT | ERR_FATAL;
3298 goto out;
3299 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003300 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003301 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003302 free(curagent->b.name);
3303 curagent->b.name = strdup(args[1]);
3304 }
3305 else if (!strcmp(args[0], "messages")) {
3306 int cur_arg = 1;
3307 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003308 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003309
Christopher Faulet11610f32017-09-21 10:23:10 +02003310 list_for_each_entry(ph, &curmphs, list) {
3311 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003312 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3313 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314 err_code |= ERR_ALERT | ERR_FATAL;
3315 goto out;
3316 }
3317 }
3318
Christopher Faulet11610f32017-09-21 10:23:10 +02003319 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003320 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003321 err_code |= ERR_ALERT | ERR_ABORT;
3322 goto out;
3323 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003324 ph->id = strdup(args[cur_arg]);
3325 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003326 cur_arg++;
3327 }
3328 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003329 else if (!strcmp(args[0], "groups")) {
3330 int cur_arg = 1;
3331 while (*args[cur_arg]) {
3332 struct spoe_placeholder *ph = NULL;
3333
3334 list_for_each_entry(ph, &curgphs, list) {
3335 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003336 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3337 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003338 err_code |= ERR_ALERT | ERR_FATAL;
3339 goto out;
3340 }
3341 }
3342
3343 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003344 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003345 err_code |= ERR_ALERT | ERR_ABORT;
3346 goto out;
3347 }
3348 ph->id = strdup(args[cur_arg]);
3349 LIST_ADDQ(&curgphs, &ph->list);
3350 cur_arg++;
3351 }
3352 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353 else if (!strcmp(args[0], "timeout")) {
3354 unsigned int *tv = NULL;
3355 const char *res;
3356 unsigned timeout;
3357
3358 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003359 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3360 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361 err_code |= ERR_ALERT | ERR_FATAL;
3362 goto out;
3363 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003364 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3365 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366 if (!strcmp(args[1], "hello"))
3367 tv = &curagent->timeout.hello;
3368 else if (!strcmp(args[1], "idle"))
3369 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003370 else if (!strcmp(args[1], "processing"))
3371 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003372 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003373 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3374 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003375 err_code |= ERR_ALERT | ERR_FATAL;
3376 goto out;
3377 }
3378 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003379 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3380 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 err_code |= ERR_ALERT | ERR_FATAL;
3382 goto out;
3383 }
3384 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3385 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003386 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3387 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003388 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 goto out;
3390 }
3391 *tv = MS_TO_TICKS(timeout);
3392 }
3393 else if (!strcmp(args[0], "option")) {
3394 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003395 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3396 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003397 err_code |= ERR_ALERT | ERR_FATAL;
3398 goto out;
3399 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003400
Christopher Faulet305c6072017-02-23 16:17:53 +01003401 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003402 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003403 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003404 if (kwm == 1)
3405 curagent->flags &= ~SPOE_FL_PIPELINING;
3406 else
3407 curagent->flags |= SPOE_FL_PIPELINING;
3408 goto out;
3409 }
3410 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003411 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003412 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003413 if (kwm == 1)
3414 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003415 else {
3416 if (global.nbthread == 1)
3417 curagent->flags |= SPOE_FL_ASYNC;
3418 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003419 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3420 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003421 err_code |= ERR_WARN;
3422 }
3423 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003424 goto out;
3425 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003426 else if (!strcmp(args[1], "send-frag-payload")) {
3427 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3428 goto out;
3429 if (kwm == 1)
3430 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3431 else
3432 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3433 goto out;
3434 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003435
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003436 /* Following options does not support negation */
3437 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003438 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3439 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003440 err_code |= ERR_ALERT | ERR_FATAL;
3441 goto out;
3442 }
3443
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444 if (!strcmp(args[1], "var-prefix")) {
3445 char *tmp;
3446
3447 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3449 file, linenum, args[0],
3450 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 err_code |= ERR_ALERT | ERR_FATAL;
3452 goto out;
3453 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003454 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3455 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003456 tmp = args[2];
3457 while (*tmp) {
3458 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003459 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3460 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003461 err_code |= ERR_ALERT | ERR_FATAL;
3462 goto out;
3463 }
3464 tmp++;
3465 }
3466 curagent->var_pfx = strdup(args[2]);
3467 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003468 else if (!strcmp(args[1], "force-set-var")) {
3469 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3470 goto out;
3471 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3472 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003473 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003474 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003475 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003476 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3477 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003478 else if (!strcmp(args[1], "set-on-error")) {
3479 char *tmp;
3480
3481 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003482 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3483 file, linenum, args[0],
3484 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003485 err_code |= ERR_ALERT | ERR_FATAL;
3486 goto out;
3487 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003488 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3489 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003490 tmp = args[2];
3491 while (*tmp) {
3492 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003493 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3494 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003495 err_code |= ERR_ALERT | ERR_FATAL;
3496 goto out;
3497 }
3498 tmp++;
3499 }
3500 curagent->var_on_error = strdup(args[2]);
3501 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003502 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003503 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3504 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003505 err_code |= ERR_ALERT | ERR_FATAL;
3506 goto out;
3507 }
Christopher Faulet48026722016-11-16 15:01:12 +01003508 }
3509 else if (!strcmp(args[0], "maxconnrate")) {
3510 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003511 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3512 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003513 err_code |= ERR_ALERT | ERR_FATAL;
3514 goto out;
3515 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003516 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003517 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003518 curagent->cps_max = atol(args[1]);
3519 }
3520 else if (!strcmp(args[0], "maxerrrate")) {
3521 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003522 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3523 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003524 err_code |= ERR_ALERT | ERR_FATAL;
3525 goto out;
3526 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003527 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003528 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003529 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003530 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003531 else if (!strcmp(args[0], "max-frame-size")) {
3532 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003533 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3534 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003535 err_code |= ERR_ALERT | ERR_FATAL;
3536 goto out;
3537 }
3538 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3539 goto out;
3540 curagent->max_frame_size = atol(args[1]);
3541 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3542 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003543 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3544 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003545 err_code |= ERR_ALERT | ERR_FATAL;
3546 goto out;
3547 }
3548 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003549 else if (!strcmp(args[0], "max-waiting-frames")) {
3550 if (!*args[1]) {
3551 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3552 file, linenum, args[0]);
3553 err_code |= ERR_ALERT | ERR_FATAL;
3554 goto out;
3555 }
3556 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3557 goto out;
3558 curagent->max_fpa = atol(args[1]);
3559 if (curagent->max_fpa < 1) {
3560 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3561 file, linenum, args[0]);
3562 err_code |= ERR_ALERT | ERR_FATAL;
3563 goto out;
3564 }
3565 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003566 else if (!strcmp(args[0], "register-var-names")) {
3567 int cur_arg;
3568
3569 if (!*args[1]) {
3570 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3571 file, linenum, args[0]);
3572 err_code |= ERR_ALERT | ERR_FATAL;
3573 goto out;
3574 }
3575 cur_arg = 1;
3576 while (*args[cur_arg]) {
3577 struct spoe_var_placeholder *vph;
3578
3579 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3580 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3581 err_code |= ERR_ALERT | ERR_ABORT;
3582 goto out;
3583 }
3584 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3585 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3586 err_code |= ERR_ALERT | ERR_ABORT;
3587 goto out;
3588 }
3589 LIST_ADDQ(&curvars, &vph->list);
3590 cur_arg++;
3591 }
3592 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003593 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003594 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3595 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003596 err_code |= ERR_ALERT | ERR_FATAL;
3597 goto out;
3598 }
3599 out:
3600 return err_code;
3601}
Christopher Faulet11610f32017-09-21 10:23:10 +02003602static int
3603cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3604{
3605 struct spoe_group *grp;
3606 const char *err;
3607 int err_code = 0;
3608
3609 if ((cfg_scope == NULL && curengine != NULL) ||
3610 (cfg_scope != NULL && curengine == NULL) ||
3611 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3612 goto out;
3613
3614 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3615 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003616 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3617 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003618 err_code |= ERR_ALERT | ERR_ABORT;
3619 goto out;
3620 }
3621 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3622 err_code |= ERR_ABORT;
3623 goto out;
3624 }
3625
3626 err = invalid_char(args[1]);
3627 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003628 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3629 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003630 err_code |= ERR_ALERT | ERR_ABORT;
3631 goto out;
3632 }
3633
3634 list_for_each_entry(grp, &curgrps, list) {
3635 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003636 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3637 " name as another one declared at %s:%d.\n",
3638 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
3642 }
3643
3644 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003645 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003646 err_code |= ERR_ALERT | ERR_ABORT;
3647 goto out;
3648 }
3649
3650 curgrp->id = strdup(args[1]);
3651 curgrp->conf.file = strdup(file);
3652 curgrp->conf.line = linenum;
3653 LIST_INIT(&curgrp->phs);
3654 LIST_INIT(&curgrp->messages);
3655 LIST_ADDQ(&curgrps, &curgrp->list);
3656 }
3657 else if (!strcmp(args[0], "messages")) {
3658 int cur_arg = 1;
3659 while (*args[cur_arg]) {
3660 struct spoe_placeholder *ph = NULL;
3661
3662 list_for_each_entry(ph, &curgrp->phs, list) {
3663 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003664 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3665 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003666 err_code |= ERR_ALERT | ERR_FATAL;
3667 goto out;
3668 }
3669 }
3670
3671 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003672 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003673 err_code |= ERR_ALERT | ERR_ABORT;
3674 goto out;
3675 }
3676 ph->id = strdup(args[cur_arg]);
3677 LIST_ADDQ(&curgrp->phs, &ph->list);
3678 cur_arg++;
3679 }
3680 }
3681 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003682 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3683 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003684 err_code |= ERR_ALERT | ERR_FATAL;
3685 goto out;
3686 }
3687 out:
3688 return err_code;
3689}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003690
3691static int
3692cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3693{
3694 struct spoe_message *msg;
3695 struct spoe_arg *arg;
3696 const char *err;
3697 char *errmsg = NULL;
3698 int err_code = 0;
3699
3700 if ((cfg_scope == NULL && curengine != NULL) ||
3701 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003702 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003703 goto out;
3704
3705 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3706 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003707 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3708 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003709 err_code |= ERR_ALERT | ERR_ABORT;
3710 goto out;
3711 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003712 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3713 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003714 goto out;
3715 }
3716
3717 err = invalid_char(args[1]);
3718 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003719 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3720 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003721 err_code |= ERR_ALERT | ERR_ABORT;
3722 goto out;
3723 }
3724
3725 list_for_each_entry(msg, &curmsgs, list) {
3726 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003727 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3728 " name as another one declared at %s:%d.\n",
3729 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003730 err_code |= ERR_ALERT | ERR_FATAL;
3731 goto out;
3732 }
3733 }
3734
3735 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003736 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003737 err_code |= ERR_ALERT | ERR_ABORT;
3738 goto out;
3739 }
3740
3741 curmsg->id = strdup(args[1]);
3742 curmsg->id_len = strlen(curmsg->id);
3743 curmsg->event = SPOE_EV_NONE;
3744 curmsg->conf.file = strdup(file);
3745 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003746 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003747 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003748 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003749 LIST_INIT(&curmsg->by_evt);
3750 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003751 LIST_ADDQ(&curmsgs, &curmsg->list);
3752 }
3753 else if (!strcmp(args[0], "args")) {
3754 int cur_arg = 1;
3755
3756 curproxy->conf.args.ctx = ARGC_SPOE;
3757 curproxy->conf.args.file = file;
3758 curproxy->conf.args.line = linenum;
3759 while (*args[cur_arg]) {
3760 char *delim = strchr(args[cur_arg], '=');
3761 int idx = 0;
3762
3763 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003764 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003765 err_code |= ERR_ALERT | ERR_ABORT;
3766 goto out;
3767 }
3768
3769 if (!delim) {
3770 arg->name = NULL;
3771 arg->name_len = 0;
3772 delim = args[cur_arg];
3773 }
3774 else {
3775 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3776 arg->name_len = delim - args[cur_arg];
3777 delim++;
3778 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003779 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3780 &idx, file, linenum, &errmsg,
3781 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003782 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003783 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003784 err_code |= ERR_ALERT | ERR_FATAL;
3785 free(arg->name);
3786 free(arg);
3787 goto out;
3788 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003789 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003790 LIST_ADDQ(&curmsg->args, &arg->list);
3791 cur_arg++;
3792 }
3793 curproxy->conf.args.file = NULL;
3794 curproxy->conf.args.line = 0;
3795 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003796 else if (!strcmp(args[0], "acl")) {
3797 err = invalid_char(args[1]);
3798 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003799 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3800 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003801 err_code |= ERR_ALERT | ERR_FATAL;
3802 goto out;
3803 }
3804 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003805 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3806 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003807 err_code |= ERR_ALERT | ERR_FATAL;
3808 goto out;
3809 }
3810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003811 else if (!strcmp(args[0], "event")) {
3812 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003813 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003814 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003815 goto out;
3816 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003817 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3818 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003819
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003820 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3821 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3822 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3823 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3824
3825 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3826 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3827 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3828 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3829 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3830 curmsg->event = SPOE_EV_ON_TCP_RSP;
3831
3832 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3833 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3834 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3835 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3836 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3837 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3838 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003839 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3840 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003841 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003842 goto out;
3843 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003844
3845 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3846 struct acl_cond *cond;
3847
3848 cond = build_acl_cond(file, linenum, &curmsg->acls,
3849 curproxy, (const char **)args+2,
3850 &errmsg);
3851 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003852 ha_alert("parsing [%s:%d] : error detected while "
3853 "parsing an 'event %s' condition : %s.\n",
3854 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003855 err_code |= ERR_ALERT | ERR_FATAL;
3856 goto out;
3857 }
3858 curmsg->cond = cond;
3859 }
3860 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3862 "or 'unless' followed by a condition but found '%s'.\n",
3863 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003864 err_code |= ERR_ALERT | ERR_FATAL;
3865 goto out;
3866 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003867 }
3868 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003869 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3870 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003871 err_code |= ERR_ALERT | ERR_FATAL;
3872 goto out;
3873 }
3874 out:
3875 free(errmsg);
3876 return err_code;
3877}
3878
3879/* Return -1 on error, else 0 */
3880static int
3881parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3882 struct flt_conf *fconf, char **err, void *private)
3883{
3884 struct list backup_sections;
3885 struct spoe_config *conf;
3886 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003887 struct spoe_group *grp, *grpback;
3888 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003889 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003890 char *file = NULL, *engine = NULL;
3891 int ret, pos = *cur_arg + 1;
3892
Christopher Faulet84c844e2018-03-23 14:37:14 +01003893 LIST_INIT(&curmsgs);
3894 LIST_INIT(&curgrps);
3895 LIST_INIT(&curmphs);
3896 LIST_INIT(&curgphs);
3897 LIST_INIT(&curvars);
3898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 conf = calloc(1, sizeof(*conf));
3900 if (conf == NULL) {
3901 memprintf(err, "%s: out of memory", args[*cur_arg]);
3902 goto error;
3903 }
3904 conf->proxy = px;
3905
3906 while (*args[pos]) {
3907 if (!strcmp(args[pos], "config")) {
3908 if (!*args[pos+1]) {
3909 memprintf(err, "'%s' : '%s' option without value",
3910 args[*cur_arg], args[pos]);
3911 goto error;
3912 }
3913 file = args[pos+1];
3914 pos += 2;
3915 }
3916 else if (!strcmp(args[pos], "engine")) {
3917 if (!*args[pos+1]) {
3918 memprintf(err, "'%s' : '%s' option without value",
3919 args[*cur_arg], args[pos]);
3920 goto error;
3921 }
3922 engine = args[pos+1];
3923 pos += 2;
3924 }
3925 else {
3926 memprintf(err, "unknown keyword '%s'", args[pos]);
3927 goto error;
3928 }
3929 }
3930 if (file == NULL) {
3931 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3932 goto error;
3933 }
3934
3935 /* backup sections and register SPOE sections */
3936 LIST_INIT(&backup_sections);
3937 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003938 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3939 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003940 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003941
3942 /* Parse SPOE filter configuration file */
3943 curengine = engine;
3944 curproxy = px;
3945 curagent = NULL;
3946 curmsg = NULL;
3947 ret = readcfgfile(file);
3948 curproxy = NULL;
3949
3950 /* unregister SPOE sections and restore previous sections */
3951 cfg_unregister_sections();
3952 cfg_restore_sections(&backup_sections);
3953
3954 if (ret == -1) {
3955 memprintf(err, "Could not open configuration file %s : %s",
3956 file, strerror(errno));
3957 goto error;
3958 }
3959 if (ret & (ERR_ABORT|ERR_FATAL)) {
3960 memprintf(err, "Error(s) found in configuration file %s", file);
3961 goto error;
3962 }
3963
3964 /* Check SPOE agent */
3965 if (curagent == NULL) {
3966 memprintf(err, "No SPOE agent found in file %s", file);
3967 goto error;
3968 }
3969 if (curagent->b.name == NULL) {
3970 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3971 curagent->id, curagent->conf.file, curagent->conf.line);
3972 goto error;
3973 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003974 if (curagent->timeout.hello == TICK_ETERNITY ||
3975 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003976 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003977 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3978 " | While not properly invalid, you will certainly encounter various problems\n"
3979 " | with such a configuration. To fix this, please ensure that all following\n"
3980 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3981 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 }
3983 if (curagent->var_pfx == NULL) {
3984 char *tmp = curagent->id;
3985
3986 while (*tmp) {
3987 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3988 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3989 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3990 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3991 goto error;
3992 }
3993 tmp++;
3994 }
3995 curagent->var_pfx = strdup(curagent->id);
3996 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003997 if (curagent->engine_id == NULL)
3998 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003999
Christopher Fauletb7426d12018-03-21 14:12:17 +01004000 if (curagent->var_on_error) {
4001 struct arg arg;
4002
4003 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4004 curagent->var_pfx, curagent->var_on_error);
4005
4006 arg.type = ARGT_STR;
4007 arg.data.str.str = trash.str;
4008 arg.data.str.len = trash.len;
4009 if (!vars_check_arg(&arg, err)) {
4010 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4011 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4012 goto error;
4013 }
4014 }
4015
Christopher Faulet11610f32017-09-21 10:23:10 +02004016 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004017 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4018 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004019 goto finish;
4020 }
4021
Christopher Faulet11610f32017-09-21 10:23:10 +02004022 /* Replace placeholders by the corresponding messages for the SPOE
4023 * agent */
4024 list_for_each_entry(ph, &curmphs, list) {
4025 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004026 struct spoe_arg *arg;
4027 unsigned int where;
4028
Christopher Faulet11610f32017-09-21 10:23:10 +02004029 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004030 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4031 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4032 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4033 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4034 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4035 }
4036 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4037 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4038 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004039 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4040 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004041 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004042 }
4043 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004044 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4045 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004046 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004047 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004048
4049 where = 0;
4050 switch (msg->event) {
4051 case SPOE_EV_ON_CLIENT_SESS:
4052 where |= SMP_VAL_FE_CON_ACC;
4053 break;
4054
4055 case SPOE_EV_ON_TCP_REQ_FE:
4056 where |= SMP_VAL_FE_REQ_CNT;
4057 break;
4058
4059 case SPOE_EV_ON_HTTP_REQ_FE:
4060 where |= SMP_VAL_FE_HRQ_HDR;
4061 break;
4062
4063 case SPOE_EV_ON_TCP_REQ_BE:
4064 if (px->cap & PR_CAP_FE)
4065 where |= SMP_VAL_FE_REQ_CNT;
4066 if (px->cap & PR_CAP_BE)
4067 where |= SMP_VAL_BE_REQ_CNT;
4068 break;
4069
4070 case SPOE_EV_ON_HTTP_REQ_BE:
4071 if (px->cap & PR_CAP_FE)
4072 where |= SMP_VAL_FE_HRQ_HDR;
4073 if (px->cap & PR_CAP_BE)
4074 where |= SMP_VAL_BE_HRQ_HDR;
4075 break;
4076
4077 case SPOE_EV_ON_SERVER_SESS:
4078 where |= SMP_VAL_BE_SRV_CON;
4079 break;
4080
4081 case SPOE_EV_ON_TCP_RSP:
4082 if (px->cap & PR_CAP_FE)
4083 where |= SMP_VAL_FE_RES_CNT;
4084 if (px->cap & PR_CAP_BE)
4085 where |= SMP_VAL_BE_RES_CNT;
4086 break;
4087
4088 case SPOE_EV_ON_HTTP_RSP:
4089 if (px->cap & PR_CAP_FE)
4090 where |= SMP_VAL_FE_HRS_HDR;
4091 if (px->cap & PR_CAP_BE)
4092 where |= SMP_VAL_BE_HRS_HDR;
4093 break;
4094
4095 default:
4096 break;
4097 }
4098
4099 list_for_each_entry(arg, &msg->args, list) {
4100 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004101 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004102 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004103 "none of which is available here ('%s')",
4104 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004105 sample_ckp_names(arg->expr->fetch->use),
4106 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004107 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004108 }
4109 }
4110
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004111 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004112 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4113 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004114 }
4115 }
4116 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004117 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004118 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004119 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004120 continue;
4121 }
4122
Christopher Faulet11610f32017-09-21 10:23:10 +02004123 /* Replace placeholders by the corresponding groups for the SPOE
4124 * agent */
4125 list_for_each_entry(ph, &curgphs, list) {
4126 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4127 if (!strcmp(grp->id, ph->id)) {
4128 grp->agent = curagent;
4129 LIST_DEL(&grp->list);
4130 LIST_ADDQ(&curagent->groups, &grp->list);
4131 goto next_aph;
4132 }
4133 }
4134 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4135 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4136 goto error;
4137 next_aph:
4138 continue;
4139 }
4140
4141 /* Replace placeholders by the corresponding message for each SPOE
4142 * group of the SPOE agent */
4143 list_for_each_entry(grp, &curagent->groups, list) {
4144 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4145 list_for_each_entry(msg, &curmsgs, list) {
4146 if (!strcmp(msg->id, ph->id)) {
4147 if (msg->group != NULL) {
4148 memprintf(err, "SPOE message '%s' already belongs to "
4149 "the SPOE group '%s' declare at %s:%d",
4150 msg->id, msg->group->id,
4151 msg->group->conf.file,
4152 msg->group->conf.line);
4153 goto error;
4154 }
4155
4156 /* Scope for arguments are not checked for now. We will check
4157 * them only if a rule use the corresponding SPOE group. */
4158 msg->agent = curagent;
4159 msg->group = grp;
4160 LIST_DEL(&ph->list);
4161 LIST_ADDQ(&grp->messages, &msg->by_grp);
4162 goto next_mph_grp;
4163 }
4164 }
4165 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4166 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4167 goto error;
4168 next_mph_grp:
4169 continue;
4170 }
4171 }
4172
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004173 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004174 /* move curmsgs to the agent message list */
4175 curmsgs.n->p = &curagent->messages;
4176 curmsgs.p->n = &curagent->messages;
4177 curagent->messages = curmsgs;
4178 LIST_INIT(&curmsgs);
4179
Christopher Faulet7ee86672017-09-19 11:08:28 +02004180 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004181 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004182 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4183 LIST_DEL(&ph->list);
4184 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004185 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004186 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4187 LIST_DEL(&ph->list);
4188 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004189 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004190 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4191 struct arg arg;
4192
4193 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4194 curagent->var_pfx, vph->name);
4195
4196 arg.type = ARGT_STR;
4197 arg.data.str.str = trash.str;
4198 arg.data.str.len = trash.len;
4199 if (!vars_check_arg(&arg, err)) {
4200 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4201 curagent->id, curagent->var_pfx, vph->name, *err);
4202 goto error;
4203 }
4204
4205 LIST_DEL(&vph->list);
4206 free(vph->name);
4207 free(vph);
4208 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004209 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4210 LIST_DEL(&grp->list);
4211 spoe_release_group(grp);
4212 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004213 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004214 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004215 fconf->ops = &spoe_ops;
4216 fconf->conf = conf;
4217 return 0;
4218
4219 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004220 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004221 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4222 LIST_DEL(&ph->list);
4223 spoe_release_placeholder(ph);
4224 }
4225 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4226 LIST_DEL(&ph->list);
4227 spoe_release_placeholder(ph);
4228 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004229 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4230 LIST_DEL(&vph->list);
4231 free(vph->name);
4232 free(vph);
4233 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004234 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4235 LIST_DEL(&grp->list);
4236 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004237 }
4238 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4239 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004240 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004241 }
4242 free(conf);
4243 return -1;
4244}
4245
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004246/* Send message of a SPOE group. This is the action_ptr callback of a rule
4247 * associated to a "send-spoe-group" action.
4248 *
4249 * It returns ACT_RET_CONT is processing is finished without error, it returns
4250 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4251 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004252static enum act_return
4253spoe_send_group(struct act_rule *rule, struct proxy *px,
4254 struct session *sess, struct stream *s, int flags)
4255{
4256 struct filter *filter;
4257 struct spoe_agent *agent = NULL;
4258 struct spoe_group *group = NULL;
4259 struct spoe_context *ctx = NULL;
4260 int ret, dir;
4261
4262 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4263 if (filter->config == rule->arg.act.p[0]) {
4264 agent = rule->arg.act.p[2];
4265 group = rule->arg.act.p[3];
4266 ctx = filter->ctx;
4267 break;
4268 }
4269 }
4270 if (agent == NULL || group == NULL || ctx == NULL)
4271 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004272 if (ctx->state == SPOE_CTX_ST_NONE)
4273 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004274
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004275 switch (rule->from) {
4276 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4277 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4278 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4279 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4280 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4281 default:
4282 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4283 " - internal error while execute spoe-send-group\n",
4284 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4285 __FUNCTION__, s);
4286 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4287 agent->id);
4288 return ACT_RET_CONT;
4289 }
4290
4291 ret = spoe_process_group(s, ctx, group, dir);
4292 if (ret == 1)
4293 return ACT_RET_CONT;
4294 else if (ret == 0) {
4295 if (flags & ACT_FLAG_FINAL) {
4296 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4297 " - failed to process group '%s': interrupted by caller\n",
4298 (int)now.tv_sec, (int)now.tv_usec,
4299 agent->id, __FUNCTION__, s, group->id);
4300 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4301 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004302 spoe_stop_processing(agent, ctx);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004303 return ACT_RET_CONT;
4304 }
4305 return ACT_RET_YIELD;
4306 }
4307 else
4308 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004309}
4310
4311/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4312 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4313 * action should be:
4314 *
4315 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4316 *
4317 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4318 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4319 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4320 * group.
4321 *
4322 * The function returns 1 in success case, otherwise, it returns 0 and err is
4323 * filled.
4324 */
4325static int
4326check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4327{
4328 struct flt_conf *fconf;
4329 struct spoe_config *conf;
4330 struct spoe_agent *agent = NULL;
4331 struct spoe_group *group;
4332 struct spoe_message *msg;
4333 char *engine_id = rule->arg.act.p[0];
4334 char *group_id = rule->arg.act.p[1];
4335 unsigned int where = 0;
4336
4337 switch (rule->from) {
4338 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4339 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4340 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4341 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4342 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4343 default:
4344 memprintf(err,
4345 "internal error, unexpected rule->from=%d, please report this bug!",
4346 rule->from);
4347 goto error;
4348 }
4349
4350 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4351 * <px> */
4352 list_for_each_entry(fconf, &px->filter_configs, list) {
4353 conf = fconf->conf;
4354
4355 /* This is not an SPOE filter */
4356 if (fconf->id != spoe_filter_id)
4357 continue;
4358
4359 /* This is the good engine */
4360 if (!strcmp(conf->id, engine_id)) {
4361 agent = conf->agent;
4362 break;
4363 }
4364 }
4365 if (agent == NULL) {
4366 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4367 engine_id, group_id);
4368 goto error;
4369 }
4370
4371 /* Try to find the right group */
4372 list_for_each_entry(group, &agent->groups, list) {
4373 /* This is the good group */
4374 if (!strcmp(group->id, group_id))
4375 break;
4376 }
4377 if (&group->list == &agent->groups) {
4378 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4379 group_id, engine_id);
4380 goto error;
4381 }
4382
4383 /* Ok, we found the group, we need to check messages and their
4384 * arguments */
4385 list_for_each_entry(msg, &group->messages, by_grp) {
4386 struct spoe_arg *arg;
4387
4388 list_for_each_entry(arg, &msg->args, list) {
4389 if (!(arg->expr->fetch->val & where)) {
4390 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4391 "some args extract information from '%s',"
4392 "none of which is available here ('%s')",
4393 msg->id, group->id, msg->conf.file, msg->conf.line,
4394 sample_ckp_names(arg->expr->fetch->use),
4395 sample_ckp_names(where));
4396 goto error;
4397 }
4398 }
4399 }
4400
4401 free(engine_id);
4402 free(group_id);
4403 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4404 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4405 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4406 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4407 return 1;
4408
4409 error:
4410 free(engine_id);
4411 free(group_id);
4412 return 0;
4413}
4414
4415/* Parse 'send-spoe-group' action following the format:
4416 *
4417 * ... send-spoe-group <engine-id> <group-id>
4418 *
4419 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4420 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4421 * ids are saved and used later, when the rule will be checked.
4422 */
4423static enum act_parse_ret
4424parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4425 struct act_rule *rule, char **err)
4426{
4427 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4428 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4429 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4430 return ACT_RET_PRS_ERR;
4431 }
4432 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4433 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4434
4435 (*orig_arg) += 2;
4436
4437 rule->action = ACT_CUSTOM;
4438 rule->action_ptr = spoe_send_group;
4439 rule->check_ptr = check_send_spoe_group;
4440 return ACT_RET_PRS_OK;
4441}
4442
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004443
4444/* Declare the filter parser for "spoe" keyword */
4445static struct flt_kw_list flt_kws = { "SPOE", { }, {
4446 { "spoe", parse_spoe_flt, NULL },
4447 { NULL, NULL, NULL },
4448 }
4449};
4450
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004451/* Delcate the action parser for "spoe-action" keyword */
4452static struct action_kw_list tcp_req_action_kws = { { }, {
4453 { "send-spoe-group", parse_send_spoe_group },
4454 { /* END */ },
4455 }
4456};
4457static struct action_kw_list tcp_res_action_kws = { { }, {
4458 { "send-spoe-group", parse_send_spoe_group },
4459 { /* END */ },
4460 }
4461};
4462static struct action_kw_list http_req_action_kws = { { }, {
4463 { "send-spoe-group", parse_send_spoe_group },
4464 { /* END */ },
4465 }
4466};
4467static struct action_kw_list http_res_action_kws = { { }, {
4468 { "send-spoe-group", parse_send_spoe_group },
4469 { /* END */ },
4470 }
4471};
4472
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004473__attribute__((constructor))
4474static void __spoe_init(void)
4475{
4476 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004477 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4478 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4479 http_req_keywords_register(&http_req_action_kws);
4480 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004481
Willy Tarreaubafbe012017-11-24 17:34:44 +01004482 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4483 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004484}
4485
4486__attribute__((destructor))
4487static void
4488__spoe_deinit(void)
4489{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004490 pool_destroy(pool_head_spoe_ctx);
4491 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004492}