blob: e70cf111e31ea074e30b3587706a85bf91c17cb9 [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)
49#else
50#define SPOE_PRINTF(x...)
51#endif
52
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010053/* Reserved 4 bytes to the frame size. So a frame and its size can be written
54 * together in a buffer */
55#define MAX_FRAME_SIZE global.tune.bufsize - 4
56
57/* The minimum size for a frame */
58#define MIN_FRAME_SIZE 256
59
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Reserved for the metadata and the frame type.
61 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010062#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020063
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010065#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
66
Christopher Faulet3b386a32017-02-23 10:17:15 +010067/* SPOE filter id. Used to identify SPOE filters */
68const char *spoe_filter_id = "SPOE filter";
69
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020070/* Set if the handle on SIGUSR1 is registered */
71static int sighandler_registered = 0;
72
73/* proxy used during the parsing */
74struct proxy *curproxy = NULL;
75
76/* The name of the SPOE engine, used during the parsing */
77char *curengine = NULL;
78
79/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020080/* SPOE agent/group/message used during the parsing */
81struct spoe_agent *curagent = NULL;
82struct spoe_group *curgrp = NULL;
83struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020084
85/* list of SPOE messages and placeholders used during the parsing */
86struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020087struct list curgrps;
88struct list curmphs;
89struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010090struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020091
Christopher Faulet42bfa462017-01-04 14:14:19 +010092/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093static struct pool_head *pool_head_spoe_ctx = NULL;
94static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020096struct flt_ops spoe_ops;
97
Christopher Faulet8ef75252017-02-20 22:56:03 +010098static int spoe_queue_context(struct spoe_context *ctx);
99static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
100static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101
102/********************************************************************
103 * helper functions/globals
104 ********************************************************************/
105static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200106spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107{
Christopher Faulet11610f32017-09-21 10:23:10 +0200108 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200110 free(ph->id);
111 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112}
113
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100115spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet57583e42017-09-04 15:41:09 +0200117 struct spoe_arg *arg, *argback;
118 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119
120 if (!msg)
121 return;
122 free(msg->id);
123 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200124 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125 release_sample_expr(arg->expr);
126 free(arg->name);
127 LIST_DEL(&arg->list);
128 free(arg);
129 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200130 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
131 LIST_DEL(&acl->list);
132 prune_acl(acl);
133 free(acl);
134 }
135 if (msg->cond) {
136 prune_acl_cond(msg->cond);
137 free(msg->cond);
138 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200139 free(msg);
140}
141
142static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200143spoe_release_group(struct spoe_group *grp)
144{
145 if (!grp)
146 return;
147 free(grp->id);
148 free(grp->conf.file);
149 free(grp);
150}
151
152static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100153spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200154{
Christopher Faulet11610f32017-09-21 10:23:10 +0200155 struct spoe_message *msg, *msgback;
156 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200157 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200158
159 if (!agent)
160 return;
161 free(agent->id);
162 free(agent->conf.file);
163 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100164 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100165 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
167 LIST_DEL(&msg->list);
168 spoe_release_message(msg);
169 }
170 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
171 LIST_DEL(&grp->list);
172 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200173 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200174 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100175 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200176 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200177 free(agent);
178}
179
180static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100181 [SPOE_FRM_ERR_NONE] = "normal",
182 [SPOE_FRM_ERR_IO] = "I/O error",
183 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
184 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
185 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
186 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
187 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
188 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
189 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
190 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
191 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
192 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100193 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100194 [SPOE_FRM_ERR_RES] = "resource allocation error",
195 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200196};
197
198static const char *spoe_event_str[SPOE_EV_EVENTS] = {
199 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
200 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
201 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
202 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
203 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
204
205 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
206 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
207 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
208};
209
210
211#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
212
213static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100214 [SPOE_CTX_ST_NONE] = "NONE",
215 [SPOE_CTX_ST_READY] = "READY",
216 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
217 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
218 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
219 [SPOE_CTX_ST_DONE] = "DONE",
220 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200221};
222
223static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100224 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
225 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
226 [SPOE_APPCTX_ST_IDLE] = "IDLE",
227 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
228 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
229 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
230 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
231 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
232 [SPOE_APPCTX_ST_EXIT] = "EXIT",
233 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200234};
235
236#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100237
Christopher Faulet8ef75252017-02-20 22:56:03 +0100238/* Used to generates a unique id for an engine. On success, it returns a
239 * allocated string. So it is the caller's reponsibility to release it. If the
240 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100241static char *
242generate_pseudo_uuid()
243{
244 static int init = 0;
245
246 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
247 const char uuid_chr[] = "0123456789ABCDEF-";
248 char *uuid;
249 int i;
250
251 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
252 return NULL;
253
254 if (!init) {
255 srand(now_ms);
256 init = 1;
257 }
258
259 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
260 int r = rand () % 16;
261
262 switch (uuid_fmt[i]) {
263 case 'x' : uuid[i] = uuid_chr[r]; break;
264 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
265 default : uuid[i] = uuid_fmt[i]; break;
266 }
267 }
268 return uuid;
269}
270
Christopher Faulet8ef75252017-02-20 22:56:03 +0100271/* Returns the minimum number of appets alive at a time. This function is used
272 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100273static inline unsigned int
274min_applets_act(struct spoe_agent *agent)
275{
276 unsigned int nbsrv;
277
Christopher Faulet8ef75252017-02-20 22:56:03 +0100278 /* TODO: Add a config parameter to customize this value. Always 0 for
279 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100280 if (agent->min_applets)
281 return agent->min_applets;
282
Christopher Faulet8ef75252017-02-20 22:56:03 +0100283 /* Get the number of active servers for the backend */
284 nbsrv = (agent->b.be->srv_act
285 ? agent->b.be->srv_act
286 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100287 return 2*nbsrv;
288}
289
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200290/********************************************************************
291 * Functions that encode/decode SPOE frames
292 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293/* Helper to get static string length, excluding the terminating null byte */
294#define SLEN(str) (sizeof(str)-1)
295
296/* Predefined key used in HELLO/DISCONNECT frames */
297#define SUPPORTED_VERSIONS_KEY "supported-versions"
298#define VERSION_KEY "version"
299#define MAX_FRAME_SIZE_KEY "max-frame-size"
300#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100301#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100302#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200303#define STATUS_CODE_KEY "status-code"
304#define MSG_KEY "message"
305
306struct spoe_version {
307 char *str;
308 int min;
309 int max;
310};
311
312/* All supported versions */
313static struct spoe_version supported_versions[] = {
314 {"1.0", 1000, 1000},
315 {NULL, 0, 0}
316};
317
318/* Comma-separated list of supported versions */
319#define SUPPORTED_VERSIONS_VAL "1.0"
320
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321/* Convert a string to a SPOE version value. The string must follow the format
322 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
323 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200324static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100325spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200326{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100327 const char *p, *end;
328 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 p = str;
331 end = str+len;
332 maj = min = 0;
333 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 /* skip leading spaces */
336 while (p < end && isspace(*p))
337 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200338
Christopher Faulet8ef75252017-02-20 22:56:03 +0100339 /* parse Major number, until the '.' */
340 while (*p != '.') {
341 if (p >= end || *p < '0' || *p > '9')
342 goto out;
343 maj *= 10;
344 maj += (*p - '0');
345 p++;
346 }
347
348 /* check Major version */
349 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200350 goto out;
351
Christopher Faulet8ef75252017-02-20 22:56:03 +0100352 p++; /* skip the '.' */
353 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
354 goto out;
355
356 /* Parse Minor number */
357 while (p < end) {
358 if (*p < '0' || *p > '9')
359 break;
360 min *= 10;
361 min += (*p - '0');
362 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200363 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364
365 /* check Minor number */
366 if (min > 999)
367 goto out;
368
369 /* skip trailing spaces */
370 while (p < end && isspace(*p))
371 p++;
372 if (p != end)
373 goto out;
374
375 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200376 out:
377 return vsn;
378}
379
Christopher Faulet8ef75252017-02-20 22:56:03 +0100380/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
381 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
382 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200383static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385{
Christopher Faulet305c6072017-02-23 16:17:53 +0100386 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100387 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 char *p, *end;
389 unsigned int flags = SPOE_FRM_FL_FIN;
390 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392 p = frame;
393 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200394
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 /* Set Frame type */
396 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100398 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 memcpy(p, (char *)&flags, 4);
400 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200401
402 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200404
405 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
406 * and "capabilities" */
407
408 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100409 sz = SLEN(SUPPORTED_VERSIONS_KEY);
410 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
411 goto too_big;
412
413 *p++ = SPOE_DATA_T_STR;
414 sz = SLEN(SUPPORTED_VERSIONS_VAL);
415 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
416 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200417
418 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100419 sz = SLEN(MAX_FRAME_SIZE_KEY);
420 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
421 goto too_big;
422
423 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200424 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100425 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200426
427 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 sz = SLEN(CAPABILITIES_KEY);
429 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
430 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200431
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100433 chk = get_trash_chunk();
434 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
435 memcpy(chk->str, "pipelining", 10);
436 chk->len += 10;
437 }
438 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
439 if (chk->len) chk->str[chk->len++] = ',';
440 memcpy(chk->str+chk->len, "async", 5);
441 chk->len += 5;
442 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100443 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
444 if (chk->len) chk->str[chk->len++] = ',';
445 memcpy(chk->str+chk->len, "fragmentation", 13);
446 chk->len += 5;
447 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100448 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100449 goto too_big;
450
451 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100452 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453 sz = SLEN(ENGINE_ID_KEY);
454 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
455 goto too_big;
456
457 *p++ = SPOE_DATA_T_STR;
458 sz = strlen(agent->engine_id);
459 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
460 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100461 }
462
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 return (p - frame);
464
465 too_big:
466 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
467 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468}
469
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
471 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
472 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200475{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 const char *reason;
477 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100478 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100479 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481 p = frame;
482 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200483
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 /* Set Frame type */
485 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100487 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 memcpy(p, (char *)&flags, 4);
489 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 *p++ = 0; *p++ = 0;
493
494 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
495 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496
497 /* There are 2 mandatory items: "status-code" and "message" */
498
499 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100500 sz = SLEN(STATUS_CODE_KEY);
501 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
502 goto too_big;
503
504 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200505 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100506 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200507
508 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509 sz = SLEN(MSG_KEY);
510 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
511 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513 /*Get the message corresponding to the status code */
514 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
515
516 *p++ = SPOE_DATA_T_STR;
517 sz = strlen(reason);
518 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
519 goto too_big;
520
521 return (p - frame);
522
523 too_big:
524 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
525 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200526}
527
Christopher Faulet8ef75252017-02-20 22:56:03 +0100528/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
529 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
530 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200531static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100532spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100533 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200534{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100535 char *p, *end;
536 unsigned int stream_id, frame_id;
537 unsigned int flags = SPOE_FRM_FL_FIN;
538 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200539
Christopher Faulet8ef75252017-02-20 22:56:03 +0100540 p = frame;
541 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200542
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100543 stream_id = ctx->stream_id;
544 frame_id = ctx->frame_id;
545
546 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
547 /* The fragmentation is not supported by the applet */
548 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
549 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
550 return -1;
551 }
552 flags = ctx->frag_ctx.flags;
553 }
554
555 /* Set Frame type */
556 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
557
558 /* Set flags */
559 memcpy(p, (char *)&flags, 4);
560 p += 4;
561
562 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200563 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100564 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200565 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100566 goto too_big;
567
568 /* Copy encoded messages, if possible */
569 sz = ctx->buffer->i;
570 if (p + sz >= end)
571 goto too_big;
572 memcpy(p, ctx->buffer->p, sz);
573 p += sz;
574
575 return (p - frame);
576
577 too_big:
578 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
579 return 0;
580}
581
582/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
583 * returns the number of encoded bytes in the frame on success, 0 if an encoding
584 * error occurred and -1 if a fatal error occurred. */
585static int
586spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
587 char *frame, size_t size)
588{
589 char *p, *end;
590 unsigned int stream_id, frame_id;
591 unsigned int flags;
592 size_t sz;
593
594 p = frame;
595 end = frame+size;
596
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597 /* <ctx> is null when the stream has aborted the processing of a
598 * fragmented frame. In this case, we must notify the corresponding
599 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100600 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100601 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100602 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
603 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
604 }
605 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100606 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100607 stream_id = ctx->stream_id;
608 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100609 }
610
Christopher Faulet8ef75252017-02-20 22:56:03 +0100611 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100614 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 memcpy(p, (char *)&flags, 4);
616 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200617
618 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200619 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200621 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200623
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100624 if (ctx == NULL)
625 goto end;
626
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100628 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 if (p + sz >= end)
630 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100631 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100633
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100634 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 return (p - frame);
636
637 too_big:
638 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
639 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200640}
641
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642/* Decode and process the HELLO frame sent by an agent. It returns the number of
643 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
644 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647{
Christopher Faulet305c6072017-02-23 16:17:53 +0100648 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
649 char *p, *end;
650 int vsn, max_frame_size;
651 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100652
653 p = frame;
654 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655
656 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
658 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 if (size < 7 /* TYPE + METADATA */) {
663 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
664 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200665 }
666
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100667 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668 memcpy((char *)&flags, p, 4);
669 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100671 /* Fragmentation is not supported for HELLO frame */
672 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100674 return -1;
675 }
676
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 if (*p != 0 || *(p+1) != 0) {
679 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
680 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200681 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683
684 /* There are 3 mandatory items: "version", "max-frame-size" and
685 * "capabilities" */
686
687 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100688 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 while (p < end) {
690 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200691 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693
694 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 ret = spoe_decode_buffer(&p, end, &str, &sz);
696 if (ret == -1 || !sz) {
697 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
698 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 /* Check "version" K/V item */
702 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 int i, type = *p++;
704
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
707 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
708 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100710 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
711 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
712 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 }
714
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 return -1;
719 }
720 for (i = 0; supported_versions[i].str != NULL; ++i) {
721 if (vsn >= supported_versions[i].min &&
722 vsn <= supported_versions[i].max)
723 break;
724 }
725 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 return -1;
728 }
729 }
730 /* Check "max-frame-size" K/V item */
731 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200733
734 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
736 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
737 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
738 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
740 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200741 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200742 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
744 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200745 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 if (sz < MIN_FRAME_SIZE ||
747 sz > SPOE_APPCTX(appctx)->max_frame_size) {
748 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200749 return -1;
750 }
751 max_frame_size = sz;
752 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753 /* Check "capabilities" K/V item */
754 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100755 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100756
757 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100758 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
759 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
760 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
763 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
764 return 0;
765 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100768 char *delim;
769
770 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100771 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
774 str += 10; sz -= 10;
775 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 flags |= SPOE_APPCTX_FL_PIPELINING;
777 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 else if (sz >= 5 && !strncmp(str, "async", 5)) {
779 str += 5; sz -= 5;
780 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781 flags |= SPOE_APPCTX_FL_ASYNC;
782 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
784 str += 13; sz -= 13;
785 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100786 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
787 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100788
Christopher Faulet8ef75252017-02-20 22:56:03 +0100789 /* Get the next comma or break */
790 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100791 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100792 delim++;
793 sz -= (delim - str);
794 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100795 }
796 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 else {
798 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100799 if (spoe_skip_data(&p, end) == -1) {
800 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
801 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200803 }
804 }
805
806 /* Final checks */
807 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809 return -1;
810 }
811 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813 return -1;
814 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100815 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
816 flags &= ~SPOE_APPCTX_FL_PIPELINING;
817 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
818 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819
Christopher Faulet42bfa462017-01-04 14:14:19 +0100820 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
821 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
822 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823
824 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825}
826
827/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
828 * bytes on success, 0 if the frame can be ignored and -1 if an error
829 * occurred. */
830static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100834 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100835
836 p = frame;
837 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838
839 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
841 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200844
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845 if (size < 7 /* TYPE + METADATA */) {
846 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
847 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200848 }
849
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100850 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 memcpy((char *)&flags, p, 4);
852 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100854 /* Fragmentation is not supported for DISCONNECT frame */
855 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100857 return -1;
858 }
859
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 if (*p != 0 || *(p+1) != 0) {
862 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
863 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200864 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200866
867 /* There are 2 mandatory items: "status-code" and "message" */
868
869 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 while (p < end) {
871 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200872 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874
875 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 ret = spoe_decode_buffer(&p, end, &str, &sz);
877 if (ret == -1 || !sz) {
878 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
879 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880 }
881
882 /* Check "status-code" K/V item */
883 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200885
886 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
888 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
889 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
890 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
892 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200894 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
896 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 }
900
901 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 else if (!memcmp(str, MSG_KEY, sz)) {
903 int type = *p++;
904
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
907 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
908 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100910 ret = spoe_decode_buffer(&p, end, &str, &sz);
911 if (ret == -1 || sz > 255) {
912 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
913 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100915#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
916 SPOE_APPCTX(appctx)->reason = str;
917 SPOE_APPCTX(appctx)->rlen = sz;
918#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
920 else {
921 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100922 if (spoe_skip_data(&p, end) == -1) {
923 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
924 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926 }
927 }
928
Christopher Faulet8ef75252017-02-20 22:56:03 +0100929 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930}
931
932
Christopher Fauleta1cda022016-12-21 08:58:06 +0100933/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200934 * success, 0 if the frame can be ignored and -1 if an error occurred. */
935static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200938{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100939 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100941 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100943 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944
945 p = frame;
946 end = frame + size;
947 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948
949 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
951 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200954
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 if (size < 7 /* TYPE + METADATA */) {
956 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
957 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958 }
959
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100960 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 memcpy((char *)&flags, p, 4);
962 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100964 /* Fragmentation is not supported for now */
965 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100967 return -1;
968 }
969
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200971 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100973 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200975 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100976 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200977 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100978 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100979
Christopher Faulet8ef75252017-02-20 22:56:03 +0100980 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100981 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200982 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100983 if ((*ctx)->stream_id == (unsigned int)stream_id &&
984 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100985 goto found;
986 }
987 }
988 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100989 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
990 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100991 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100992 goto found;
993 }
994 }
995
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100996 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
997 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
998 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
999
1000 /* ABRT bit is set for an unfinished fragmented frame */
1001 if (flags & SPOE_FRM_FL_ABRT) {
1002 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1003 (*ctx)->frag_ctx.spoe_appctx = NULL;
1004 (*ctx)->state = SPOE_CTX_ST_ERROR;
1005 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1006 /* Ignore the payload */
1007 goto end;
1008 }
1009 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1010 /* For now, we ignore the ack */
1011 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1012 return 0;
1013 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001014
Christopher Fauleta1cda022016-12-21 08:58:06 +01001015 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001016 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1017 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001018 " - stream-id=%u - frame-id=%u\n",
1019 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1020 __FUNCTION__, appctx,
1021 (unsigned int)stream_id, (unsigned int)frame_id);
1022
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001023 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 return 0;
1025
1026 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1028 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001030 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001032
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001033 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001034 len = (end - p);
1035 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1036 SPOE_APPCTX(appctx)->buffer->i = len;
1037 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001038
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001039 /* Transfer the buffer ownership to the SPOE context */
1040 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1041 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001042
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 (*ctx)->state = SPOE_CTX_ST_DONE;
1044
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001045 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 " - ACK frame received"
1048 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001049 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1051 (*ctx)->frame_id, flags);
1052 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001053}
1054
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055/* This function is used in cfgparse.c and declared in proto/checks.h. It
1056 * prepare the request to send to agents during a healthcheck. It returns 0 on
1057 * success and -1 if an error occurred. */
1058int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 struct appctx appctx;
1062 struct spoe_appctx spoe_appctx;
1063 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1064 size_t sz;
1065 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001066
Christopher Faulet42bfa462017-01-04 14:14:19 +01001067 memset(&appctx, 0, sizeof(appctx));
1068 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001070
1071 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001072 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1075 end = frame + MAX_FRAME_SIZE;
1076
1077 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1078 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001079 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 /* Add "healthcheck" K/V item */
1083 sz = SLEN(HEALTHCHECK_KEY);
1084 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1085 return -1;
1086 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087
Christopher Faulet8ef75252017-02-20 22:56:03 +01001088 *len = frame - buf;
1089 sz = htonl(*len - 4);
1090 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001091
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return 0;
1096}
1097
1098/* This function is used in checks.c and declared in proto/checks.h. It decode
1099 * the response received from an agent during a healthcheck. It returns 0 on
1100 * success and -1 if an error occurred. */
1101int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001104 struct appctx appctx;
1105 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106
Christopher Faulet42bfa462017-01-04 14:14:19 +01001107 memset(&appctx, 0, sizeof(appctx));
1108 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109
Christopher Faulet42bfa462017-01-04 14:14:19 +01001110 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001111 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1114 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115 goto error;
1116 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1118 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001119
1120 return 0;
1121
1122 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001123 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1124 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1125 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126 return -1;
1127}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001128
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1130 * the frame can be ignored, 1 to retry later, and the frame legnth on
1131 * success. */
1132static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134{
1135 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 int ret;
1137 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138
1139 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001140 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1143 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001144 netint = htonl(framesz);
1145 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001146 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147
1148 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001149 if (ret == -1) {
1150 retry:
1151 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153 }
1154 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 return -1; /* error */
1156 }
1157 return framesz;
1158}
1159
1160/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1161 * when the frame can be ignored, 1 to retry later and the frame length on
1162 * success. */
1163static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001165{
1166 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001167 int ret;
1168 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169
1170 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172
Willy Tarreau06d80a92017-10-19 14:32:15 +02001173 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 if (ret > 0) {
1175 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001176 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 return -1;
1179 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001180 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001181 }
1182 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001183 if (ret == 0) {
1184 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186 }
1187 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001188 return -1; /* error */
1189 }
1190 return framesz;
1191}
1192
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001193/********************************************************************
1194 * Functions that manage the SPOE applet
1195 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001196static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198{
1199 si_applet_want_get(appctx->owner);
1200 si_applet_want_put(appctx->owner);
1201 appctx_wakeup(appctx);
1202 return 1;
1203}
1204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205/* Callback function that catches applet timeouts. If a timeout occurred, we set
1206 * <appctx->st1> flag and the SPOE applet is woken up. */
1207static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209{
1210 struct appctx *appctx = task->context;
1211
1212 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1213 if (tick_is_expired(task->expire, now_ms)) {
1214 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218 return task;
1219}
1220
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221/* Callback function that releases a SPOE applet. This happens when the
1222 * connection with the agent is closed. */
1223static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 struct stream_interface *si = appctx->owner;
1227 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1228 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230
1231 if (spoe_appctx == NULL)
1232 return;
1233
1234 appctx->ctx.spoe.ptr = NULL;
1235 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236
1237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1238 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1239 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Remove applet from the list of running applets */
Christopher Faulet24289f22017-09-25 14:48:02 +02001242 agent->rt[tid].applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001243 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1244 LIST_DEL(&spoe_appctx->list);
1245 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001246 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001247
Christopher Faulet8ef75252017-02-20 22:56:03 +01001248 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001250 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
Christopher Faulet24289f22017-09-25 14:48:02 +02001251 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001252
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001253 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1255 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001256
1257 si_shutw(si);
1258 si_shutr(si);
1259 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001260 }
1261
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 /* Destroy the task attached to this applet */
1263 if (spoe_appctx->task) {
1264 task_delete(spoe_appctx->task);
1265 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001266 }
1267
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 /* Notify all waiting streams */
1269 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 LIST_DEL(&ctx->list);
1271 LIST_INIT(&ctx->list);
1272 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001274 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 }
1276
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 /* If the applet was processing a fragmented frame, notify the
1278 * corresponding stream. */
1279 if (spoe_appctx->frag_ctx.ctx) {
1280 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001281 ctx->frag_ctx.spoe_appctx = NULL;
1282 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001284 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1285 }
1286
Christopher Faulet8ef75252017-02-20 22:56:03 +01001287 /* Release allocated memory */
1288 spoe_release_buffer(&spoe_appctx->buffer,
1289 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001290 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001291
Christopher Faulet24289f22017-09-25 14:48:02 +02001292 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001293 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294
Christopher Faulet8ef75252017-02-20 22:56:03 +01001295 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001296 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 LIST_DEL(&ctx->list);
1298 LIST_INIT(&ctx->list);
1299 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001301 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001302 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001303 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001304 LIST_DEL(&ctx->list);
1305 LIST_INIT(&ctx->list);
1306 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001307 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001308 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1309 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001310
1311 end:
1312 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001313 agent->rt[tid].frame_size = agent->max_frame_size;
1314 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1315 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316}
1317
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001318static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001320{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001322 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001324 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001325
Christopher Fauleta1cda022016-12-21 08:58:06 +01001326 if (si->state <= SI_ST_CON) {
1327 si_applet_want_put(si);
1328 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1329 goto stop;
1330 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001331 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001332 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001334 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001335
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1338 " - Connection timed out\n",
1339 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1340 __FUNCTION__, appctx);
1341 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001344
Christopher Faulet42bfa462017-01-04 14:14:19 +01001345 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001346 SPOE_APPCTX(appctx)->task->expire =
1347 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001348
Christopher Faulet8ef75252017-02-20 22:56:03 +01001349 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1350 * length. */
1351 buf = trash.str; frame = buf+4;
1352 ret = spoe_prepare_hahello_frame(appctx, frame,
1353 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001356
1357 switch (ret) {
1358 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001359 case 0: /* ignore => an error, cannot be ignored */
1360 goto exit;
1361
1362 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 goto stop;
1364
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 default:
1366 /* HELLO frame successfully sent, now wait for the
1367 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1369 goto next;
1370 }
1371
1372 next:
1373 return 0;
1374 stop:
1375 return 1;
1376 exit:
1377 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1378 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001379}
1380
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001381static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001382spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001383{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001385 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 char *frame;
1387 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389
Christopher Fauletb067b062017-01-04 16:39:11 +01001390 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001393 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394
Christopher Fauleta1cda022016-12-21 08:58:06 +01001395 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1397 " - Connection timed out\n",
1398 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1399 __FUNCTION__, appctx);
1400 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001401 goto exit;
1402 }
1403
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 frame = trash.str; trash.len = 0;
1405 ret = spoe_recv_frame(appctx, frame,
1406 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 if (ret > 1) {
1408 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1409 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1410 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001412 trash.len = ret + 4;
1413 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 switch (ret) {
1417 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001418 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1420 goto next;
1421
1422 case 1: /* retry later */
1423 goto stop;
1424
1425 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 /* HELLO handshake is finished, set the idle timeout and
1427 * add the applet in the list of running applets. */
Christopher Faulet24289f22017-09-25 14:48:02 +02001428 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001430 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001431 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001432 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001433 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001434
1435 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001436 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001438 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001439
Christopher Fauleta1cda022016-12-21 08:58:06 +01001440 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001441 /* Do not forget to remove processed frame from the output buffer */
1442 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001443 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001444
1445 SPOE_APPCTX(appctx)->task->expire =
1446 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447 return 0;
1448 stop:
1449 return 1;
1450 exit:
1451 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1452 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001453}
1454
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001455
Christopher Fauleta1cda022016-12-21 08:58:06 +01001456static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001457spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001458{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001459 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1460 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001461 char *frame, *buf;
1462 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001463
Christopher Faulet8ef75252017-02-20 22:56:03 +01001464 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1465 * length. */
1466 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001467
1468 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1469 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1470 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1471 SPOE_APPCTX(appctx)->max_frame_size);
1472 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001473 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001474 *skip = 1;
1475 ret = 1;
1476 goto end;
1477 }
1478 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001479 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001480 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1481 SPOE_APPCTX(appctx)->max_frame_size);
1482
1483 }
1484
Christopher Faulet8ef75252017-02-20 22:56:03 +01001485 if (ret > 1)
1486 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001487
Christopher Faulet8ef75252017-02-20 22:56:03 +01001488 switch (ret) {
1489 case -1: /* error */
1490 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1491 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001492
Christopher Faulet8ef75252017-02-20 22:56:03 +01001493 case 0: /* ignore */
1494 if (ctx == NULL)
1495 goto abort_frag_frame;
1496
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001497 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001498 LIST_DEL(&ctx->list);
1499 LIST_INIT(&ctx->list);
1500 ctx->state = SPOE_CTX_ST_ERROR;
1501 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1502 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1503 break;
1504
1505 case 1: /* retry */
1506 *skip = 1;
1507 break;
1508
1509 default:
1510 if (ctx == NULL)
1511 goto abort_frag_frame;
1512
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001513 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 LIST_DEL(&ctx->list);
1515 LIST_INIT(&ctx->list);
1516 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1517 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1518 goto no_frag_frame_sent;
1519 else {
1520 *skip = 1;
1521 goto frag_frame_sent;
1522 }
1523 }
1524 goto end;
1525
1526 frag_frame_sent:
1527 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1528 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1529 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1530 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1531
1532 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1533 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1534 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1535 goto end;
1536
1537 no_frag_frame_sent:
1538 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1539 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001540 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 }
1542 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1543 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1544 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1545 }
1546 else {
1547 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1548 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1549 }
1550 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1551 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1552 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1553
1554 ctx->frag_ctx.spoe_appctx = NULL;
1555 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1556 goto end;
1557
1558 abort_frag_frame:
1559 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1560 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1561 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1562 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1563 goto end;
1564
1565 end:
1566 return ret;
1567}
1568
1569static int
1570spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1571{
1572 struct spoe_context *ctx = NULL;
1573 char *frame;
1574 int ret;
1575
1576 frame = trash.str; trash.len = 0;
1577 ret = spoe_recv_frame(appctx, frame,
1578 SPOE_APPCTX(appctx)->max_frame_size);
1579 if (ret > 1) {
1580 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1581 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1582 goto end;
1583 }
1584 trash.len = ret + 4;
1585 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1586 }
1587 switch (ret) {
1588 case -1: /* error */
1589 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1590 break;
1591
1592 case 0: /* ignore */
1593 break;
1594
1595 case 1: /* retry */
1596 *skip = 1;
1597 break;
1598
1599 default:
1600 LIST_DEL(&ctx->list);
1601 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001602
1603 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1604 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1605 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1606 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1607 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1608 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1609 }
1610 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1611 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1612
Christopher Faulet8ef75252017-02-20 22:56:03 +01001613 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1614 break;
1615 }
1616
1617 /* Do not forget to remove processed frame from the output buffer */
1618 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001619 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001620 end:
1621 return ret;
1622}
1623
1624static int
1625spoe_handle_processing_appctx(struct appctx *appctx)
1626{
1627 struct stream_interface *si = appctx->owner;
1628 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001629 unsigned int fpa = 0;
1630 int ret, skip_sending = 0, skip_receiving = 0;
1631
1632 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1633 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1634 goto exit;
1635 }
1636
1637 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1638 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1639 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1640 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1641 goto next;
1642 }
1643
1644 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001645 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1646 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1647 " - appctx-state=%s\n",
1648 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1649 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001650 skip_sending, skip_receiving,
1651 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001652
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001653 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001654 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001655 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001656 if (skip_receiving)
1657 goto stop;
1658 goto recv_frame;
1659 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001660
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001661 /* send_frame */
1662 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001663 switch (ret) {
1664 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001665 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001666
Christopher Fauleta1cda022016-12-21 08:58:06 +01001667 case 0: /* ignore */
Christopher Faulet24289f22017-09-25 14:48:02 +02001668 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001669 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001670 break;
1671
Christopher Fauleta1cda022016-12-21 08:58:06 +01001672 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001673 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001674
Christopher Fauleta1cda022016-12-21 08:58:06 +01001675 default:
Christopher Faulet24289f22017-09-25 14:48:02 +02001676 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001677 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001678 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001680 if (fpa > agent->max_fpa)
1681 goto stop;
1682
1683 recv_frame:
1684 if (skip_receiving)
1685 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001686 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001687 switch (ret) {
1688 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001689 goto next;
1690
1691 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692 fpa++;
1693 break;
1694
1695 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696 break;
1697
1698 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001699 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001700 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701 }
1702 goto process;
1703
1704 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001705 SPOE_APPCTX(appctx)->task->expire =
1706 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707 return 0;
1708 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001709 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet24289f22017-09-25 14:48:02 +02001711 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001712 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001713 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001714 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001715 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001716 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001717 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001718 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001719 SPOE_APPCTX(appctx)->task->expire =
1720 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721 }
1722 return 1;
1723
1724 exit:
1725 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1726 return 0;
1727}
1728
1729static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001730spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731{
1732 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001733 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001734 char *frame, *buf;
1735 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001736
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1738 goto exit;
1739
1740 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1741 goto exit;
1742
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1744 * length. */
1745 buf = trash.str; frame = buf+4;
1746 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1747 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750
1751 switch (ret) {
1752 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001753 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001754 goto exit;
1755
1756 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757 goto stop;
1758
1759 default:
1760 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1761 " - disconnected by HAProxy (%d): %s\n",
1762 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 __FUNCTION__, appctx,
1764 SPOE_APPCTX(appctx)->status_code,
1765 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766
1767 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1768 goto next;
1769 }
1770
1771 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 SPOE_APPCTX(appctx)->task->expire =
1773 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 return 0;
1775 stop:
1776 return 1;
1777 exit:
1778 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1779 return 0;
1780}
1781
1782static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784{
1785 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 char *frame;
1787 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788
Christopher Fauletb067b062017-01-04 16:39:11 +01001789 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001792 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793
Christopher Fauletb067b062017-01-04 16:39:11 +01001794 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001797 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 frame = trash.str; trash.len = 0;
1800 ret = spoe_recv_frame(appctx, frame,
1801 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001803 trash.len = ret + 4;
1804 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 }
1806
1807 switch (ret) {
1808 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1810 " - error on frame (%s)\n",
1811 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001812 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 goto exit;
1816
1817 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 goto next;
1819
1820 case 1: /* retry */
1821 goto stop;
1822
1823 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001827 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1829 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 goto exit;
1831 }
1832
1833 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834 /* Do not forget to remove processed frame from the output buffer */
1835 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001836 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 return 0;
1839 stop:
1840 return 1;
1841 exit:
1842 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1843 return 0;
1844}
1845
1846/* I/O Handler processing messages exchanged with the agent */
1847static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 struct stream_interface *si = appctx->owner;
1851 struct spoe_agent *agent;
1852
1853 if (SPOE_APPCTX(appctx) == NULL)
1854 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1857 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001858
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 switchstate:
1860 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1861 " - appctx-state=%s\n",
1862 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1863 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1864
1865 switch (appctx->st0) {
1866 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto out;
1869 goto switchstate;
1870
1871 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 goto out;
1874 goto switchstate;
1875
1876 case SPOE_APPCTX_ST_IDLE:
1877 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001878 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001879 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 SPOE_APPCTX(appctx)->task->expire =
1881 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001883 goto switchstate;
1884 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001885 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001886 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1887 /* fall through */
1888
1889 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001890 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1891 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001892 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 goto out;
1894 goto switchstate;
1895
1896 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001897 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 goto out;
1899 goto switchstate;
1900
1901 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001902 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001903 goto out;
1904 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001905
1906 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001907 appctx->st0 = SPOE_APPCTX_ST_END;
1908 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1909
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001910 si_shutw(si);
1911 si_shutr(si);
1912 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001913 /* fall through */
1914
1915 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001916 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001917 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001918 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001919 if (stopping)
1920 spoe_wakeup_appctx(appctx);
1921
Christopher Faulet42bfa462017-01-04 14:14:19 +01001922 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1923 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001924 si_oc(si)->flags |= CF_READ_DONTWAIT;
1925 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001926}
1927
1928struct applet spoe_applet = {
1929 .obj_type = OBJ_TYPE_APPLET,
1930 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001931 .fct = spoe_handle_appctx,
1932 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933};
1934
1935/* Create a SPOE applet. On success, the created applet is returned, else
1936 * NULL. */
1937static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001938spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939{
1940 struct appctx *appctx;
1941 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943
Emeric Brun1138fd02017-06-19 12:38:55 +02001944 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945 goto out_error;
1946
Willy Tarreaubafbe012017-11-24 17:34:44 +01001947 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001948 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001949 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001950 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001951
Christopher Faulet42bfa462017-01-04 14:14:19 +01001952 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001953 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001954 goto out_free_spoe_appctx;
1955
1956 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001957 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001958 SPOE_APPCTX(appctx)->task->context = appctx;
1959 SPOE_APPCTX(appctx)->agent = conf->agent;
1960 SPOE_APPCTX(appctx)->version = 0;
1961 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1962 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001963 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001964 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1965
1966 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1967 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001968 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969
1970 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1971 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001972
Willy Tarreau5820a362016-12-22 15:59:02 +01001973 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001974 if (!sess)
1975 goto out_free_spoe;
1976
Willy Tarreau87787ac2017-08-28 16:22:54 +02001977 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001978 goto out_free_sess;
1979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001980 stream_set_backend(strm, conf->agent->b.be);
1981
1982 /* applet is waiting for data */
1983 si_applet_cant_get(&strm->si[0]);
1984 appctx_wakeup(appctx);
1985
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001986 strm->do_log = NULL;
1987 strm->res.flags |= CF_READ_DONTWAIT;
1988
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001989 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001990 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001991 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001992 conf->agent->rt[tid].applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001993
Emeric Brunc60def82017-09-27 14:59:38 +02001994 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001995 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 return appctx;
1997
1998 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 out_free_sess:
2000 session_free(sess);
2001 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002002 task_free(SPOE_APPCTX(appctx)->task);
2003 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002004 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 out_free_appctx:
2006 appctx_free(appctx);
2007 out_error:
2008 return NULL;
2009}
2010
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002012spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013{
2014 struct spoe_config *conf = FLT_CONF(ctx->filter);
2015 struct spoe_agent *agent = conf->agent;
2016 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002017 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002018 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002019
Christopher Fauleta1cda022016-12-21 08:58:06 +01002020 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021
Christopher Fauleta1cda022016-12-21 08:58:06 +01002022 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002023 if (agent->rt[tid].applets_act >= min_applets &&
2024 agent->rt[tid].applets_idle &&
2025 agent->rt[tid].sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002026 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002027
2028 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002029 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2031 ctx->strm);
2032
Christopher Fauleta1cda022016-12-21 08:58:06 +01002033 /* Do not try to create a new applet if there is no server up for the
2034 * agent's backend. */
2035 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2036 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2037 " - cannot create SPOE appctx: no server up\n",
2038 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2039 __FUNCTION__, ctx->strm);
2040 goto end;
2041 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002042
Christopher Fauleta1cda022016-12-21 08:58:06 +01002043 /* Do not try to create a new applet if we have reached the maximum of
2044 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002045 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002046 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002047 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2048 " - cannot create SPOE appctx: max CPS reached\n",
2049 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2050 __FUNCTION__, ctx->strm);
2051 goto end;
2052 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053 }
2054
Christopher Faulet8ef75252017-02-20 22:56:03 +01002055 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002056 if (appctx == NULL) {
2057 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2058 " - failed to create SPOE appctx\n",
2059 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2060 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002061 send_log(ctx->strm->be, LOG_EMERG,
2062 "SPOE: [%s] failed to create SPOE applet\n",
2063 agent->id);
2064
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066 }
Christopher Faulet24289f22017-09-25 14:48:02 +02002067 if (agent->rt[tid].applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002068 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002069
Christopher Fauleta1cda022016-12-21 08:58:06 +01002070 /* Increase the per-process number of cumulated connections */
2071 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002072 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002073
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 end:
2075 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002076 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002077 ctx->status_code = SPOE_CTX_ERR_RES;
2078 return -1;
2079 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 /* Add the SPOE context in the sending queue and update all running
2082 * info */
Christopher Faulet24289f22017-09-25 14:48:02 +02002083 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
2084 if (agent->rt[tid].sending_rate)
2085 agent->rt[tid].sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086
2087 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002088 " - Add stream in sending queue"
2089 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002090 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002091 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
2092 agent->rt[tid].sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002093
Christopher Fauleta1cda022016-12-21 08:58:06 +01002094 /* Finally try to wakeup the first IDLE applet found and move it at the
2095 * end of the list. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002096 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) {
Christopher Faulet42bfa462017-01-04 14:14:19 +01002097 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002098 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002099 spoe_wakeup_appctx(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002100 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002101 LIST_DEL(&spoe_appctx->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02002102 LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002103 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002104 break;
2105 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002107 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002108}
2109
2110/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002111 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002112 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002113/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2114 * fragmented_content. If the next message can be processed, it returns 0. If
2115 * the message is too big, it returns -1.*/
2116static int
2117spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2118 struct spoe_message *msg, int dir,
2119 char **buf, char *end)
2120{
2121 struct sample *smp;
2122 struct spoe_arg *arg;
2123 int ret;
2124
2125 if (msg->cond) {
2126 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2127 ret = acl_pass(ret);
2128 if (msg->cond->pol == ACL_COND_UNLESS)
2129 ret = !ret;
2130
2131 /* the rule does not match */
2132 if (!ret)
2133 goto next;
2134 }
2135
2136 /* Resume encoding of a SPOE argument */
2137 if (ctx->frag_ctx.curarg != NULL) {
2138 arg = ctx->frag_ctx.curarg;
2139 goto encode_argument;
2140 }
2141
2142 if (ctx->frag_ctx.curoff != UINT_MAX)
2143 goto encode_msg_payload;
2144
2145 /* Check if there is enough space for the message name and the
2146 * number of arguments. It implies <msg->id_len> is encoded on 2
2147 * bytes, at most (< 2288). */
2148 if (*buf + 2 + msg->id_len + 1 > end)
2149 goto too_big;
2150
2151 /* Encode the message name */
2152 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2153 goto too_big;
2154
2155 /* Set the number of arguments for this message */
2156 **buf = msg->nargs;
2157 (*buf)++;
2158
2159 ctx->frag_ctx.curoff = 0;
2160 encode_msg_payload:
2161
2162 /* Loop on arguments */
2163 list_for_each_entry(arg, &msg->args, list) {
2164 ctx->frag_ctx.curarg = arg;
2165 ctx->frag_ctx.curoff = UINT_MAX;
2166
2167 encode_argument:
2168 if (ctx->frag_ctx.curoff != UINT_MAX)
2169 goto encode_arg_value;
2170
2171 /* Encode the arguement name as a string. It can by NULL */
2172 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2173 goto too_big;
2174
2175 ctx->frag_ctx.curoff = 0;
2176 encode_arg_value:
2177
2178 /* Fetch the arguement value */
2179 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2180 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2181 if (ret == -1 || ctx->frag_ctx.curoff)
2182 goto too_big;
2183 }
2184
2185 next:
2186 return 0;
2187
2188 too_big:
2189 return -1;
2190}
2191
Christopher Fauletc718b822017-09-21 16:50:56 +02002192/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2193 * handle fragmented content. On success it returns 1. If an error occurred, -1
2194 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2195 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002196static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002197spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002198 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002199{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002200 struct spoe_config *conf = FLT_CONF(ctx->filter);
2201 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002202 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002203 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002204
Christopher Faulet8ef75252017-02-20 22:56:03 +01002205 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002206 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002207
Christopher Fauletc718b822017-09-21 16:50:56 +02002208 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2209 /* Resume encoding of a SPOE message */
2210 if (ctx->frag_ctx.curmsg != NULL) {
2211 msg = ctx->frag_ctx.curmsg;
2212 goto encode_evt_message;
2213 }
2214
2215 list_for_each_entry(msg, messages, by_evt) {
2216 ctx->frag_ctx.curmsg = msg;
2217 ctx->frag_ctx.curarg = NULL;
2218 ctx->frag_ctx.curoff = UINT_MAX;
2219
2220 encode_evt_message:
2221 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2222 goto too_big;
2223 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002224 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002225 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2226 /* Resume encoding of a SPOE message */
2227 if (ctx->frag_ctx.curmsg != NULL) {
2228 msg = ctx->frag_ctx.curmsg;
2229 goto encode_grp_message;
2230 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002231
Christopher Fauletc718b822017-09-21 16:50:56 +02002232 list_for_each_entry(msg, messages, by_grp) {
2233 ctx->frag_ctx.curmsg = msg;
2234 ctx->frag_ctx.curarg = NULL;
2235 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002236
Christopher Fauletc718b822017-09-21 16:50:56 +02002237 encode_grp_message:
2238 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2239 goto too_big;
2240 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002241 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002242 else
2243 goto skip;
2244
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245
Christopher Faulet57583e42017-09-04 15:41:09 +02002246 /* nothing has been encoded for an unfragmented payload */
2247 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2248 goto skip;
2249
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002250 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002251 " - encode %s messages - spoe_appctx=%p"
2252 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002253 (int)now.tv_sec, (int)now.tv_usec,
2254 agent->id, __FUNCTION__, s,
2255 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet24289f22017-09-25 14:48:02 +02002256 ctx->frag_ctx.spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002257 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002258
Christopher Faulet8ef75252017-02-20 22:56:03 +01002259 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002260 ctx->frag_ctx.curmsg = NULL;
2261 ctx->frag_ctx.curarg = NULL;
2262 ctx->frag_ctx.curoff = 0;
2263 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002264
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002265 return 1;
2266
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002268 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2269 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2270 return -1;
2271 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002272
2273 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002274 " - encode fragmented messages - spoe_appctx=%p"
2275 " - curmsg=%p - curarg=%p - curoff=%u"
2276 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002277 (int)now.tv_sec, (int)now.tv_usec,
2278 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2279 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002280 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281
Christopher Faulet8ef75252017-02-20 22:56:03 +01002282 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002283 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2284 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2285 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002286
2287 skip:
2288 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2289 " - skip the frame because nothing has been encoded\n",
2290 (int)now.tv_sec, (int)now.tv_usec,
2291 agent->id, __FUNCTION__, s);
2292 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002293}
2294
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002295
2296/***************************************************************************
2297 * Functions that handle SPOE actions
2298 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002299/* Helper function to set a variable */
2300static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301spoe_set_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);
Etienne Carriereaec89892017-12-14 09:36:40 +00002311 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2312 vars_set_by_name(varname, len, smp);
2313 else
2314 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002315}
2316
2317/* Helper function to unset a variable */
2318static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320 struct sample *smp)
2321{
2322 struct spoe_config *conf = FLT_CONF(ctx->filter);
2323 struct spoe_agent *agent = conf->agent;
2324 char varname[64];
2325
2326 memset(varname, 0, sizeof(varname));
2327 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2328 scope, agent->var_pfx, len, name);
2329 vars_unset_by_name_ifexist(varname, len, smp);
2330}
2331
2332
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333static inline int
2334spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2335 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002336{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002337 char *str, *scope, *p = *buf;
2338 struct sample smp;
2339 uint64_t sz;
2340 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002341
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342 if (p + 2 >= end)
2343 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345 /* SET-VAR requires 3 arguments */
2346 if (*p++ != 3)
2347 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 switch (*p++) {
2350 case SPOE_SCOPE_PROC: scope = "proc"; break;
2351 case SPOE_SCOPE_SESS: scope = "sess"; break;
2352 case SPOE_SCOPE_TXN : scope = "txn"; break;
2353 case SPOE_SCOPE_REQ : scope = "req"; break;
2354 case SPOE_SCOPE_RES : scope = "res"; break;
2355 default: goto skip;
2356 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2359 goto skip;
2360 memset(&smp, 0, sizeof(smp));
2361 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002362
Christopher Faulet8ef75252017-02-20 22:56:03 +01002363 if (spoe_decode_data(&p, end, &smp) == -1)
2364 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002365
Christopher Faulet8ef75252017-02-20 22:56:03 +01002366 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2367 " - set-var '%s.%s.%.*s'\n",
2368 (int)now.tv_sec, (int)now.tv_usec,
2369 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2370 __FUNCTION__, s, scope,
2371 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2372 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 ret = (p - *buf);
2377 *buf = p;
2378 return ret;
2379 skip:
2380 return 0;
2381}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383static inline int
2384spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2385 char **buf, char *end, int dir)
2386{
2387 char *str, *scope, *p = *buf;
2388 struct sample smp;
2389 uint64_t sz;
2390 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002391
Christopher Faulet8ef75252017-02-20 22:56:03 +01002392 if (p + 2 >= end)
2393 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 /* UNSET-VAR requires 2 arguments */
2396 if (*p++ != 2)
2397 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002398
Christopher Faulet8ef75252017-02-20 22:56:03 +01002399 switch (*p++) {
2400 case SPOE_SCOPE_PROC: scope = "proc"; break;
2401 case SPOE_SCOPE_SESS: scope = "sess"; break;
2402 case SPOE_SCOPE_TXN : scope = "txn"; break;
2403 case SPOE_SCOPE_REQ : scope = "req"; break;
2404 case SPOE_SCOPE_RES : scope = "res"; break;
2405 default: goto skip;
2406 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002407
Christopher Faulet8ef75252017-02-20 22:56:03 +01002408 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2409 goto skip;
2410 memset(&smp, 0, sizeof(smp));
2411 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2414 " - unset-var '%s.%s.%.*s'\n",
2415 (int)now.tv_sec, (int)now.tv_usec,
2416 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2417 __FUNCTION__, s, scope,
2418 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2419 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002420
Christopher Faulet8ef75252017-02-20 22:56:03 +01002421 spoe_unset_var(ctx, scope, str, sz, &smp);
2422
2423 ret = (p - *buf);
2424 *buf = p;
2425 return ret;
2426 skip:
2427 return 0;
2428}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002429
Christopher Faulet8ef75252017-02-20 22:56:03 +01002430/* Process SPOE actions for a specific event. It returns 1 on success. If an
2431 * error occurred, 0 is returned. */
2432static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002433spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434{
2435 char *p, *end;
2436 int ret;
2437
2438 p = ctx->buffer->p;
2439 end = p + ctx->buffer->i;
2440
2441 while (p < end) {
2442 enum spoe_action_type type;
2443
2444 type = *p++;
2445 switch (type) {
2446 case SPOE_ACT_T_SET_VAR:
2447 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2448 if (!ret)
2449 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002451
Christopher Faulet8ef75252017-02-20 22:56:03 +01002452 case SPOE_ACT_T_UNSET_VAR:
2453 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2454 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002455 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002456 break;
2457
2458 default:
2459 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460 }
2461 }
2462
2463 return 1;
2464 skip:
2465 return 0;
2466}
2467
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002468/***************************************************************************
2469 * Functions that process SPOE events
2470 **************************************************************************/
2471static inline int
Christopher Faulet58d03682017-09-21 16:57:24 +02002472spoe_start_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002473{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002474 /* If a process is already started for this SPOE context, retry
2475 * later. */
2476 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002477 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002478
2479 /* Set the right flag to prevent request and response processing
2480 * in same time. */
2481 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2482 ? SPOE_CTX_FL_REQ_PROCESS
2483 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002484 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002485}
2486
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002487static inline void
Christopher Faulet58d03682017-09-21 16:57:24 +02002488spoe_stop_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002489{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002490 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2491
2492 if (sa) {
2493 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002494 spoe_wakeup_appctx(sa->owner);
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 Fauletf51f5fa2017-01-19 10:01:12 +01002498 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002499
Christopher Fauletb067b062017-01-04 16:39:11 +01002500 ctx->status_code = 0;
2501
Christopher Fauleta1cda022016-12-21 08:58:06 +01002502 /* Reset processing timer */
2503 ctx->process_exp = TICK_ETERNITY;
2504
Christopher Faulet8ef75252017-02-20 22:56:03 +01002505 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002506
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002507 ctx->frag_ctx.spoe_appctx = NULL;
2508 ctx->frag_ctx.curmsg = NULL;
2509 ctx->frag_ctx.curarg = NULL;
2510 ctx->frag_ctx.curoff = 0;
2511 ctx->frag_ctx.flags = 0;
2512
Christopher Fauleta1cda022016-12-21 08:58:06 +01002513 if (!LIST_ISEMPTY(&ctx->list)) {
2514 LIST_DEL(&ctx->list);
2515 LIST_INIT(&ctx->list);
2516 }
2517}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002518
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002519static void
2520spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2521 struct spoe_context *ctx, int dir)
2522{
2523 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002524 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002525
2526 if (agent->var_on_error) {
2527 struct sample smp;
2528
2529 memset(&smp, 0, sizeof(smp));
2530 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2531 smp.data.u.sint = ctx->status_code;
2532 smp.data.type = SMP_T_BOOL;
2533
2534 spoe_set_var(ctx, "txn", agent->var_on_error,
2535 strlen(agent->var_on_error), &smp);
2536 }
2537 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2538 " - failed to process messages: code=%u\n",
2539 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2540 __FUNCTION__, s, ctx->status_code);
2541 send_log(ctx->strm->be, LOG_WARNING,
2542 "SPOE: [%s] failed to process messages: code=%u\n",
2543 agent->id, ctx->status_code);
2544
2545 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2546 ? SPOE_CTX_ST_READY
2547 : SPOE_CTX_ST_NONE);
2548}
2549
Christopher Faulet58d03682017-09-21 16:57:24 +02002550/* Process a list of SPOE messages. First, this functions will process messages
2551 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2552 * to process corresponding actions. During all the processing, it returns 0
2553 * and it returns 1 when the processing is finished. If an error occurred, -1
2554 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002555static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002556spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2557 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002558{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002559 struct spoe_config *conf = FLT_CONF(ctx->filter);
2560 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002561 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002562
2563 if (ctx->state == SPOE_CTX_ST_ERROR)
2564 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002565
2566 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2567 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002568 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002569 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002570 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002571 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002572 goto error;
2573 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002574
2575 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002577 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002578 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002579 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002580 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002581 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 goto skip;
2583 }
2584 }
2585
Christopher Fauletf7a30922016-11-10 15:04:51 +01002586 if (!tick_isset(ctx->process_exp)) {
2587 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2588 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2589 ctx->process_exp);
2590 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002591 ret = spoe_start_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002592 if (!ret)
2593 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002594
Christopher Faulet8ef75252017-02-20 22:56:03 +01002595 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596 goto error;
2597
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002598 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002600 }
2601
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002602 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002603 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002604 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002605 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002606 if (ret < 0)
2607 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002608 if (!ret)
2609 goto skip;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002610 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2611 }
2612
2613 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2614 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002615 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616 ret = 0;
2617 goto out;
2618 }
2619
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002620 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2621 ret = 0;
2622 goto out;
2623 }
2624
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002625 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002626 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002627 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002628 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002629 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002630 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002631 }
2632
2633 out:
2634 return ret;
2635
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002636 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002637 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 ret = 1;
2639 goto end;
2640
2641 skip:
2642 ctx->state = SPOE_CTX_ST_READY;
2643 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002644
Christopher Fauleta1cda022016-12-21 08:58:06 +01002645 end:
Christopher Faulet58d03682017-09-21 16:57:24 +02002646 spoe_stop_processing(ctx);
2647 return ret;
2648}
2649
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002650/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2651 * See spoe_process_message for details. */
2652static int
2653spoe_process_group(struct stream *s, struct spoe_context *ctx,
2654 struct spoe_group *group, int dir)
2655{
2656 int ret;
2657
2658 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2659 " - ctx-state=%s - Process messages for group=%s\n",
2660 (int)now.tv_sec, (int)now.tv_usec,
2661 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2662 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2663 group->id);
2664
2665 if (LIST_ISEMPTY(&group->messages))
2666 return 1;
2667
2668 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2669 return ret;
2670}
2671
Christopher Faulet58d03682017-09-21 16:57:24 +02002672/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2673 * See spoe_process_message for details. */
2674static int
2675spoe_process_event(struct stream *s, struct spoe_context *ctx,
2676 enum spoe_event ev)
2677{
2678 int dir, ret;
2679
2680 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002681 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002682 (int)now.tv_sec, (int)now.tv_usec,
2683 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2684 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2685 spoe_event_str[ev]);
2686
2687 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2688
2689 if (LIST_ISEMPTY(&(ctx->events[ev])))
2690 return 1;
2691
2692 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002693 return ret;
2694}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002695
2696/***************************************************************************
2697 * Functions that create/destroy SPOE contexts
2698 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002700spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002701{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002702 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 return 1;
2704
Christopher Faulet4596fb72017-01-11 14:05:19 +01002705 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002706 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002707 LIST_DEL(&buffer_wait->list);
2708 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002709 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002710 }
2711
Christopher Faulet4596fb72017-01-11 14:05:19 +01002712 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002713 return 1;
2714
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002715 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002716 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002717 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002718 return 0;
2719}
2720
2721static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002722spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002723{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002724 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002725 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002726 LIST_DEL(&buffer_wait->list);
2727 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002728 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002729 }
2730
2731 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002732 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002733 b_free(buf);
2734 offer_buffers(buffer_wait->target,
2735 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002736 }
2737}
2738
Christopher Faulet4596fb72017-01-11 14:05:19 +01002739static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002740spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002741{
2742 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2743 return 1;
2744}
2745
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002746static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002747spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002748{
2749 struct spoe_config *conf = FLT_CONF(filter);
2750 struct spoe_context *ctx;
2751
Willy Tarreaubafbe012017-11-24 17:34:44 +01002752 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002753 if (ctx == NULL) {
2754 return NULL;
2755 }
2756 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002757 ctx->filter = filter;
2758 ctx->state = SPOE_CTX_ST_NONE;
2759 ctx->status_code = SPOE_CTX_ERR_NONE;
2760 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002761 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002762 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002763 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002764 LIST_INIT(&ctx->buffer_wait.list);
2765 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002766 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002767 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002768
Christopher Fauletf7a30922016-11-10 15:04:51 +01002769 ctx->stream_id = 0;
2770 ctx->frame_id = 1;
2771 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002772
2773 return ctx;
2774}
2775
2776static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002777spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002778{
2779 if (!ctx)
2780 return;
2781
Christopher Faulet58d03682017-09-21 16:57:24 +02002782 spoe_stop_processing(ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002783 pool_free(pool_head_spoe_ctx, ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002784}
2785
2786static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002787spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002788{
2789 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002790 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002791}
2792
2793
2794/***************************************************************************
2795 * Hooks that manage the filter lifecycle (init/check/deinit)
2796 **************************************************************************/
2797/* Signal handler: Do a soft stop, wakeup SPOE applet */
2798static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002799spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002800{
2801 struct proxy *p;
2802
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002803 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002804 while (p) {
2805 struct flt_conf *fconf;
2806
2807 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002808 struct spoe_config *conf;
2809 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002810 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002811 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002812
Christopher Faulet3b386a32017-02-23 10:17:15 +01002813 if (fconf->id != spoe_filter_id)
2814 continue;
2815
2816 conf = fconf->conf;
2817 agent = conf->agent;
2818
Christopher Faulet24289f22017-09-25 14:48:02 +02002819 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002820 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002821 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2822 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002823 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002824 }
2825 }
2826 p = p->next;
2827 }
2828}
2829
2830
2831/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2832static int
2833spoe_init(struct proxy *px, struct flt_conf *fconf)
2834{
2835 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002836
2837 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2838 init_new_proxy(&conf->agent_fe);
2839 conf->agent_fe.parent = conf->agent;
2840 conf->agent_fe.last_change = now.tv_sec;
2841 conf->agent_fe.id = conf->agent->id;
2842 conf->agent_fe.cap = PR_CAP_FE;
2843 conf->agent_fe.mode = PR_MODE_TCP;
2844 conf->agent_fe.maxconn = 0;
2845 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2846 conf->agent_fe.conn_retries = CONN_RETRIES;
2847 conf->agent_fe.accept = frontend_accept;
2848 conf->agent_fe.srv = NULL;
2849 conf->agent_fe.timeout.client = TICK_ETERNITY;
2850 conf->agent_fe.default_target = &spoe_applet.obj_type;
2851 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2852
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002854 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002855 sighandler_registered = 1;
2856 }
2857
2858 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002859}
2860
2861/* Free ressources allocated by the SPOE filter. */
2862static void
2863spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2864{
2865 struct spoe_config *conf = fconf->conf;
2866
2867 if (conf) {
2868 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002869
Christopher Faulet8ef75252017-02-20 22:56:03 +01002870 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002871 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872 free(conf);
2873 }
2874 fconf->conf = NULL;
2875}
2876
2877/* Check configuration of a SPOE filter for a specified proxy.
2878 * Return 1 on error, else 0. */
2879static int
2880spoe_check(struct proxy *px, struct flt_conf *fconf)
2881{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002882 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002883 struct spoe_config *conf = fconf->conf;
2884 struct proxy *target;
2885
Christopher Faulet7ee86672017-09-19 11:08:28 +02002886 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2887 * are uniq */
2888 list_for_each_entry(f, &px->filter_configs, list) {
2889 struct spoe_config *c = f->conf;
2890
2891 /* This is not an SPOE filter */
2892 if (f->id != spoe_filter_id)
2893 continue;
2894 /* This is the current SPOE filter */
2895 if (f == fconf)
2896 continue;
2897
2898 /* Check engine Id. It should be uniq */
2899 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002900 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2901 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002902 return 1;
2903 }
2904 }
2905
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002906 target = proxy_be_by_name(conf->agent->b.name);
2907 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002908 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2909 " declared at %s:%d.\n",
2910 px->id, conf->agent->b.name, conf->agent->id,
2911 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002912 return 1;
2913 }
2914 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002915 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2916 " at %s:%d does not support HTTP mode.\n",
2917 px->id, target->id, conf->agent->id,
2918 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919 return 1;
2920 }
2921
2922 free(conf->agent->b.name);
2923 conf->agent->b.name = NULL;
2924 conf->agent->b.be = target;
2925 return 0;
2926}
2927
2928/**************************************************************************
2929 * Hooks attached to a stream
2930 *************************************************************************/
2931/* Called when a filter instance is created and attach to a stream. It creates
2932 * the context that will be used to process this stream. */
2933static int
2934spoe_start(struct stream *s, struct filter *filter)
2935{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002936 struct spoe_config *conf = FLT_CONF(filter);
2937 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002938 struct spoe_context *ctx;
2939
2940 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002941 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 __FUNCTION__, s);
2943
Christopher Faulet8ef75252017-02-20 22:56:03 +01002944 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002946 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2947 " - failed to create SPOE context\n",
2948 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002949 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002950 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002951 "SPOE: [%s] failed to create SPOE context\n",
2952 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953 return 0;
2954 }
2955
2956 ctx->strm = s;
2957 ctx->state = SPOE_CTX_ST_READY;
2958 filter->ctx = ctx;
2959
Christopher Faulet11610f32017-09-21 10:23:10 +02002960 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2962
Christopher Faulet11610f32017-09-21 10:23:10 +02002963 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2965
Christopher Faulet11610f32017-09-21 10:23:10 +02002966 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967 filter->pre_analyzers |= AN_RES_INSPECT;
2968
Christopher Faulet11610f32017-09-21 10:23:10 +02002969 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002970 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2971
Christopher Faulet11610f32017-09-21 10:23:10 +02002972 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002973 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2974
Christopher Faulet11610f32017-09-21 10:23:10 +02002975 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2977
2978 return 1;
2979}
2980
2981/* Called when a filter instance is detached from a stream. It release the
2982 * attached SPOE context. */
2983static void
2984spoe_stop(struct stream *s, struct filter *filter)
2985{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2987 (int)now.tv_sec, (int)now.tv_usec,
2988 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2989 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002990 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991}
2992
Christopher Fauletf7a30922016-11-10 15:04:51 +01002993
2994/*
2995 * Called when the stream is woken up because of expired timer.
2996 */
2997static void
2998spoe_check_timeouts(struct stream *s, struct filter *filter)
2999{
3000 struct spoe_context *ctx = filter->ctx;
3001
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003002 if (tick_is_expired(ctx->process_exp, now_ms)) {
3003 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003004 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003005 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003006}
3007
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003008/* Called when we are ready to filter data on a channel */
3009static int
3010spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3011{
3012 struct spoe_context *ctx = filter->ctx;
3013 int ret = 1;
3014
3015 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3016 " - ctx-flags=0x%08x\n",
3017 (int)now.tv_sec, (int)now.tv_usec,
3018 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3019 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3020
Christopher Fauletb067b062017-01-04 16:39:11 +01003021 if (ctx->state == SPOE_CTX_ST_NONE)
3022 goto out;
3023
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024 if (!(chn->flags & CF_ISRESP)) {
3025 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3026 chn->analysers |= AN_REQ_INSPECT_FE;
3027 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3028 chn->analysers |= AN_REQ_INSPECT_BE;
3029
3030 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3031 goto out;
3032
3033 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003034 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003035 if (!ret)
3036 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003037 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3038 }
3039 else {
3040 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3041 chn->analysers |= AN_RES_INSPECT;
3042
3043 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3044 goto out;
3045
Christopher Faulet8ef75252017-02-20 22:56:03 +01003046 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003047 if (!ret) {
3048 channel_dont_read(chn);
3049 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003050 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003051 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003052 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053 }
3054
3055 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056 return ret;
3057}
3058
3059/* Called before a processing happens on a given channel */
3060static int
3061spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3062 struct channel *chn, unsigned an_bit)
3063{
3064 struct spoe_context *ctx = filter->ctx;
3065 int ret = 1;
3066
3067 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3068 " - ctx-flags=0x%08x - ana=0x%08x\n",
3069 (int)now.tv_sec, (int)now.tv_usec,
3070 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3071 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3072 ctx->flags, an_bit);
3073
Christopher Fauletb067b062017-01-04 16:39:11 +01003074 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003075 goto out;
3076
3077 switch (an_bit) {
3078 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003079 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003080 break;
3081 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003082 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003083 break;
3084 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003085 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003086 break;
3087 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003088 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003089 break;
3090 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003091 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003092 break;
3093 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003094 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003095 break;
3096 }
3097
3098 out:
3099 if (!ret) {
3100 channel_dont_read(chn);
3101 channel_dont_close(chn);
3102 }
3103 return ret;
3104}
3105
3106/* Called when the filtering on the channel ends. */
3107static int
3108spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3109{
3110 struct spoe_context *ctx = filter->ctx;
3111
3112 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3113 " - ctx-flags=0x%08x\n",
3114 (int)now.tv_sec, (int)now.tv_usec,
3115 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3116 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3117
3118 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003119 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003120 }
3121
3122 return 1;
3123}
3124
3125/********************************************************************
3126 * Functions that manage the filter initialization
3127 ********************************************************************/
3128struct flt_ops spoe_ops = {
3129 /* Manage SPOE filter, called for each filter declaration */
3130 .init = spoe_init,
3131 .deinit = spoe_deinit,
3132 .check = spoe_check,
3133
3134 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003135 .attach = spoe_start,
3136 .detach = spoe_stop,
3137 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003138
3139 /* Handle channels activity */
3140 .channel_start_analyze = spoe_start_analyze,
3141 .channel_pre_analyze = spoe_chn_pre_analyze,
3142 .channel_end_analyze = spoe_end_analyze,
3143};
3144
3145
3146static int
3147cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3148{
3149 const char *err;
3150 int i, err_code = 0;
3151
3152 if ((cfg_scope == NULL && curengine != NULL) ||
3153 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003154 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155 goto out;
3156
3157 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3158 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003159 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3160 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003161 err_code |= ERR_ALERT | ERR_ABORT;
3162 goto out;
3163 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003164 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3165 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003166 goto out;
3167 }
3168
3169 err = invalid_char(args[1]);
3170 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003171 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3172 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003173 err_code |= ERR_ALERT | ERR_ABORT;
3174 goto out;
3175 }
3176
3177 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003178 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3179 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003180 err_code |= ERR_ALERT | ERR_ABORT;
3181 goto out;
3182 }
3183 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003184 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 err_code |= ERR_ALERT | ERR_ABORT;
3186 goto out;
3187 }
3188
3189 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003190
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 curagent->conf.file = strdup(file);
3192 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003193
3194 curagent->timeout.hello = TICK_ETERNITY;
3195 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003196 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003197
3198 curagent->engine_id = NULL;
3199 curagent->var_pfx = NULL;
3200 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003201 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3202 if (global.nbthread == 1)
3203 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003204 curagent->cps_max = 0;
3205 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003206 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003207 curagent->min_applets = 0;
3208 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209
3210 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003211 LIST_INIT(&curagent->events[i]);
3212 LIST_INIT(&curagent->groups);
3213 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003214
Christopher Faulet24289f22017-09-25 14:48:02 +02003215 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003216 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003217 err_code |= ERR_ALERT | ERR_ABORT;
3218 goto out;
3219 }
3220 for (i = 0; i < global.nbthread; ++i) {
3221 curagent->rt[i].frame_size = curagent->max_frame_size;
3222 curagent->rt[i].applets_act = 0;
3223 curagent->rt[i].applets_idle = 0;
3224 curagent->rt[i].sending_rate = 0;
3225 LIST_INIT(&curagent->rt[i].applets);
3226 LIST_INIT(&curagent->rt[i].sending_queue);
3227 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003228 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003229 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 }
3231 else if (!strcmp(args[0], "use-backend")) {
3232 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003233 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3234 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003235 err_code |= ERR_ALERT | ERR_FATAL;
3236 goto out;
3237 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003238 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003239 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003240 free(curagent->b.name);
3241 curagent->b.name = strdup(args[1]);
3242 }
3243 else if (!strcmp(args[0], "messages")) {
3244 int cur_arg = 1;
3245 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003246 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247
Christopher Faulet11610f32017-09-21 10:23:10 +02003248 list_for_each_entry(ph, &curmphs, list) {
3249 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003250 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3251 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003252 err_code |= ERR_ALERT | ERR_FATAL;
3253 goto out;
3254 }
3255 }
3256
Christopher Faulet11610f32017-09-21 10:23:10 +02003257 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003258 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003259 err_code |= ERR_ALERT | ERR_ABORT;
3260 goto out;
3261 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003262 ph->id = strdup(args[cur_arg]);
3263 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003264 cur_arg++;
3265 }
3266 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003267 else if (!strcmp(args[0], "groups")) {
3268 int cur_arg = 1;
3269 while (*args[cur_arg]) {
3270 struct spoe_placeholder *ph = NULL;
3271
3272 list_for_each_entry(ph, &curgphs, list) {
3273 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003274 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3275 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003276 err_code |= ERR_ALERT | ERR_FATAL;
3277 goto out;
3278 }
3279 }
3280
3281 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003282 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003283 err_code |= ERR_ALERT | ERR_ABORT;
3284 goto out;
3285 }
3286 ph->id = strdup(args[cur_arg]);
3287 LIST_ADDQ(&curgphs, &ph->list);
3288 cur_arg++;
3289 }
3290 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003291 else if (!strcmp(args[0], "timeout")) {
3292 unsigned int *tv = NULL;
3293 const char *res;
3294 unsigned timeout;
3295
3296 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003297 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3298 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 err_code |= ERR_ALERT | ERR_FATAL;
3300 goto out;
3301 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003302 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3303 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 if (!strcmp(args[1], "hello"))
3305 tv = &curagent->timeout.hello;
3306 else if (!strcmp(args[1], "idle"))
3307 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003308 else if (!strcmp(args[1], "processing"))
3309 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003310 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003311 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3312 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003313 err_code |= ERR_ALERT | ERR_FATAL;
3314 goto out;
3315 }
3316 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003317 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3318 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003319 err_code |= ERR_ALERT | ERR_FATAL;
3320 goto out;
3321 }
3322 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3323 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003324 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3325 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003326 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003327 goto out;
3328 }
3329 *tv = MS_TO_TICKS(timeout);
3330 }
3331 else if (!strcmp(args[0], "option")) {
3332 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003333 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3334 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003335 err_code |= ERR_ALERT | ERR_FATAL;
3336 goto out;
3337 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003338
Christopher Faulet305c6072017-02-23 16:17:53 +01003339 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003340 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003341 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003342 if (kwm == 1)
3343 curagent->flags &= ~SPOE_FL_PIPELINING;
3344 else
3345 curagent->flags |= SPOE_FL_PIPELINING;
3346 goto out;
3347 }
3348 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003349 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003350 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003351 if (kwm == 1)
3352 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003353 else {
3354 if (global.nbthread == 1)
3355 curagent->flags |= SPOE_FL_ASYNC;
3356 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003357 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3358 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003359 err_code |= ERR_WARN;
3360 }
3361 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003362 goto out;
3363 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003364 else if (!strcmp(args[1], "send-frag-payload")) {
3365 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3366 goto out;
3367 if (kwm == 1)
3368 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3369 else
3370 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3371 goto out;
3372 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003373
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003374 /* Following options does not support negation */
3375 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003376 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3377 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003378 err_code |= ERR_ALERT | ERR_FATAL;
3379 goto out;
3380 }
3381
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003382 if (!strcmp(args[1], "var-prefix")) {
3383 char *tmp;
3384
3385 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003386 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3387 file, linenum, args[0],
3388 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 err_code |= ERR_ALERT | ERR_FATAL;
3390 goto out;
3391 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003392 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3393 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 tmp = args[2];
3395 while (*tmp) {
3396 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003397 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3398 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 err_code |= ERR_ALERT | ERR_FATAL;
3400 goto out;
3401 }
3402 tmp++;
3403 }
3404 curagent->var_pfx = strdup(args[2]);
3405 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003406 else if (!strcmp(args[1], "force-set-var")) {
3407 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3408 goto out;
3409 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3410 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003411 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003412 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003413 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003414 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3415 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003416 else if (!strcmp(args[1], "set-on-error")) {
3417 char *tmp;
3418
3419 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003420 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3421 file, linenum, args[0],
3422 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003423 err_code |= ERR_ALERT | ERR_FATAL;
3424 goto out;
3425 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003426 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3427 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003428 tmp = args[2];
3429 while (*tmp) {
3430 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003431 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3432 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003433 err_code |= ERR_ALERT | ERR_FATAL;
3434 goto out;
3435 }
3436 tmp++;
3437 }
3438 curagent->var_on_error = strdup(args[2]);
3439 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3442 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 err_code |= ERR_ALERT | ERR_FATAL;
3444 goto out;
3445 }
Christopher Faulet48026722016-11-16 15:01:12 +01003446 }
3447 else if (!strcmp(args[0], "maxconnrate")) {
3448 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003449 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3450 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003451 err_code |= ERR_ALERT | ERR_FATAL;
3452 goto out;
3453 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003454 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003455 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003456 curagent->cps_max = atol(args[1]);
3457 }
3458 else if (!strcmp(args[0], "maxerrrate")) {
3459 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003460 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3461 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003462 err_code |= ERR_ALERT | ERR_FATAL;
3463 goto out;
3464 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003465 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003466 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003467 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003468 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003469 else if (!strcmp(args[0], "max-frame-size")) {
3470 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003471 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3472 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003473 err_code |= ERR_ALERT | ERR_FATAL;
3474 goto out;
3475 }
3476 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3477 goto out;
3478 curagent->max_frame_size = atol(args[1]);
3479 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3480 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003481 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3482 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003483 err_code |= ERR_ALERT | ERR_FATAL;
3484 goto out;
3485 }
3486 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003487 else if (!strcmp(args[0], "register-var-names")) {
3488 int cur_arg;
3489
3490 if (!*args[1]) {
3491 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3492 file, linenum, args[0]);
3493 err_code |= ERR_ALERT | ERR_FATAL;
3494 goto out;
3495 }
3496 cur_arg = 1;
3497 while (*args[cur_arg]) {
3498 struct spoe_var_placeholder *vph;
3499
3500 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3501 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3502 err_code |= ERR_ALERT | ERR_ABORT;
3503 goto out;
3504 }
3505 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3506 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3507 err_code |= ERR_ALERT | ERR_ABORT;
3508 goto out;
3509 }
3510 LIST_ADDQ(&curvars, &vph->list);
3511 cur_arg++;
3512 }
3513 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003514 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003515 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3516 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003517 err_code |= ERR_ALERT | ERR_FATAL;
3518 goto out;
3519 }
3520 out:
3521 return err_code;
3522}
Christopher Faulet11610f32017-09-21 10:23:10 +02003523static int
3524cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3525{
3526 struct spoe_group *grp;
3527 const char *err;
3528 int err_code = 0;
3529
3530 if ((cfg_scope == NULL && curengine != NULL) ||
3531 (cfg_scope != NULL && curengine == NULL) ||
3532 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3533 goto out;
3534
3535 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3536 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003537 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3538 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003539 err_code |= ERR_ALERT | ERR_ABORT;
3540 goto out;
3541 }
3542 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3543 err_code |= ERR_ABORT;
3544 goto out;
3545 }
3546
3547 err = invalid_char(args[1]);
3548 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003549 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3550 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003551 err_code |= ERR_ALERT | ERR_ABORT;
3552 goto out;
3553 }
3554
3555 list_for_each_entry(grp, &curgrps, list) {
3556 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003557 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3558 " name as another one declared at %s:%d.\n",
3559 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003560 err_code |= ERR_ALERT | ERR_FATAL;
3561 goto out;
3562 }
3563 }
3564
3565 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003566 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003567 err_code |= ERR_ALERT | ERR_ABORT;
3568 goto out;
3569 }
3570
3571 curgrp->id = strdup(args[1]);
3572 curgrp->conf.file = strdup(file);
3573 curgrp->conf.line = linenum;
3574 LIST_INIT(&curgrp->phs);
3575 LIST_INIT(&curgrp->messages);
3576 LIST_ADDQ(&curgrps, &curgrp->list);
3577 }
3578 else if (!strcmp(args[0], "messages")) {
3579 int cur_arg = 1;
3580 while (*args[cur_arg]) {
3581 struct spoe_placeholder *ph = NULL;
3582
3583 list_for_each_entry(ph, &curgrp->phs, list) {
3584 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003585 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3586 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003587 err_code |= ERR_ALERT | ERR_FATAL;
3588 goto out;
3589 }
3590 }
3591
3592 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003593 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003594 err_code |= ERR_ALERT | ERR_ABORT;
3595 goto out;
3596 }
3597 ph->id = strdup(args[cur_arg]);
3598 LIST_ADDQ(&curgrp->phs, &ph->list);
3599 cur_arg++;
3600 }
3601 }
3602 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003603 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3604 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003605 err_code |= ERR_ALERT | ERR_FATAL;
3606 goto out;
3607 }
3608 out:
3609 return err_code;
3610}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003611
3612static int
3613cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3614{
3615 struct spoe_message *msg;
3616 struct spoe_arg *arg;
3617 const char *err;
3618 char *errmsg = NULL;
3619 int err_code = 0;
3620
3621 if ((cfg_scope == NULL && curengine != NULL) ||
3622 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003623 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003624 goto out;
3625
3626 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3627 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003628 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3629 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003630 err_code |= ERR_ALERT | ERR_ABORT;
3631 goto out;
3632 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003633 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3634 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003635 goto out;
3636 }
3637
3638 err = invalid_char(args[1]);
3639 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003640 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3641 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003642 err_code |= ERR_ALERT | ERR_ABORT;
3643 goto out;
3644 }
3645
3646 list_for_each_entry(msg, &curmsgs, list) {
3647 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003648 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3649 " name as another one declared at %s:%d.\n",
3650 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003651 err_code |= ERR_ALERT | ERR_FATAL;
3652 goto out;
3653 }
3654 }
3655
3656 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003657 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003658 err_code |= ERR_ALERT | ERR_ABORT;
3659 goto out;
3660 }
3661
3662 curmsg->id = strdup(args[1]);
3663 curmsg->id_len = strlen(curmsg->id);
3664 curmsg->event = SPOE_EV_NONE;
3665 curmsg->conf.file = strdup(file);
3666 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003667 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003668 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003669 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003670 LIST_INIT(&curmsg->by_evt);
3671 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003672 LIST_ADDQ(&curmsgs, &curmsg->list);
3673 }
3674 else if (!strcmp(args[0], "args")) {
3675 int cur_arg = 1;
3676
3677 curproxy->conf.args.ctx = ARGC_SPOE;
3678 curproxy->conf.args.file = file;
3679 curproxy->conf.args.line = linenum;
3680 while (*args[cur_arg]) {
3681 char *delim = strchr(args[cur_arg], '=');
3682 int idx = 0;
3683
3684 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003685 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003686 err_code |= ERR_ALERT | ERR_ABORT;
3687 goto out;
3688 }
3689
3690 if (!delim) {
3691 arg->name = NULL;
3692 arg->name_len = 0;
3693 delim = args[cur_arg];
3694 }
3695 else {
3696 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3697 arg->name_len = delim - args[cur_arg];
3698 delim++;
3699 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003700 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3701 &idx, file, linenum, &errmsg,
3702 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003703 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003704 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003705 err_code |= ERR_ALERT | ERR_FATAL;
3706 free(arg->name);
3707 free(arg);
3708 goto out;
3709 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003710 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003711 LIST_ADDQ(&curmsg->args, &arg->list);
3712 cur_arg++;
3713 }
3714 curproxy->conf.args.file = NULL;
3715 curproxy->conf.args.line = 0;
3716 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003717 else if (!strcmp(args[0], "acl")) {
3718 err = invalid_char(args[1]);
3719 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003720 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3721 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003722 err_code |= ERR_ALERT | ERR_FATAL;
3723 goto out;
3724 }
3725 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003726 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3727 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003728 err_code |= ERR_ALERT | ERR_FATAL;
3729 goto out;
3730 }
3731 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003732 else if (!strcmp(args[0], "event")) {
3733 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003734 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003735 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003736 goto out;
3737 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003738 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3739 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003740
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003741 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3742 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3743 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3744 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3745
3746 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3747 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3748 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3749 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3750 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3751 curmsg->event = SPOE_EV_ON_TCP_RSP;
3752
3753 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3754 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3755 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3756 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3757 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3758 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3759 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003760 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3761 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003762 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003763 goto out;
3764 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003765
3766 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3767 struct acl_cond *cond;
3768
3769 cond = build_acl_cond(file, linenum, &curmsg->acls,
3770 curproxy, (const char **)args+2,
3771 &errmsg);
3772 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003773 ha_alert("parsing [%s:%d] : error detected while "
3774 "parsing an 'event %s' condition : %s.\n",
3775 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003776 err_code |= ERR_ALERT | ERR_FATAL;
3777 goto out;
3778 }
3779 curmsg->cond = cond;
3780 }
3781 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003782 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3783 "or 'unless' followed by a condition but found '%s'.\n",
3784 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003785 err_code |= ERR_ALERT | ERR_FATAL;
3786 goto out;
3787 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003788 }
3789 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003790 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3791 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003792 err_code |= ERR_ALERT | ERR_FATAL;
3793 goto out;
3794 }
3795 out:
3796 free(errmsg);
3797 return err_code;
3798}
3799
3800/* Return -1 on error, else 0 */
3801static int
3802parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3803 struct flt_conf *fconf, char **err, void *private)
3804{
3805 struct list backup_sections;
3806 struct spoe_config *conf;
3807 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003808 struct spoe_group *grp, *grpback;
3809 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003810 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003811 char *file = NULL, *engine = NULL;
3812 int ret, pos = *cur_arg + 1;
3813
3814 conf = calloc(1, sizeof(*conf));
3815 if (conf == NULL) {
3816 memprintf(err, "%s: out of memory", args[*cur_arg]);
3817 goto error;
3818 }
3819 conf->proxy = px;
3820
3821 while (*args[pos]) {
3822 if (!strcmp(args[pos], "config")) {
3823 if (!*args[pos+1]) {
3824 memprintf(err, "'%s' : '%s' option without value",
3825 args[*cur_arg], args[pos]);
3826 goto error;
3827 }
3828 file = args[pos+1];
3829 pos += 2;
3830 }
3831 else if (!strcmp(args[pos], "engine")) {
3832 if (!*args[pos+1]) {
3833 memprintf(err, "'%s' : '%s' option without value",
3834 args[*cur_arg], args[pos]);
3835 goto error;
3836 }
3837 engine = args[pos+1];
3838 pos += 2;
3839 }
3840 else {
3841 memprintf(err, "unknown keyword '%s'", args[pos]);
3842 goto error;
3843 }
3844 }
3845 if (file == NULL) {
3846 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3847 goto error;
3848 }
3849
3850 /* backup sections and register SPOE sections */
3851 LIST_INIT(&backup_sections);
3852 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003853 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3854 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003855 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003856
3857 /* Parse SPOE filter configuration file */
3858 curengine = engine;
3859 curproxy = px;
3860 curagent = NULL;
3861 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003862 LIST_INIT(&curmsgs);
3863 LIST_INIT(&curgrps);
3864 LIST_INIT(&curmphs);
3865 LIST_INIT(&curgphs);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003866 LIST_INIT(&curvars);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003867 ret = readcfgfile(file);
3868 curproxy = NULL;
3869
3870 /* unregister SPOE sections and restore previous sections */
3871 cfg_unregister_sections();
3872 cfg_restore_sections(&backup_sections);
3873
3874 if (ret == -1) {
3875 memprintf(err, "Could not open configuration file %s : %s",
3876 file, strerror(errno));
3877 goto error;
3878 }
3879 if (ret & (ERR_ABORT|ERR_FATAL)) {
3880 memprintf(err, "Error(s) found in configuration file %s", file);
3881 goto error;
3882 }
3883
3884 /* Check SPOE agent */
3885 if (curagent == NULL) {
3886 memprintf(err, "No SPOE agent found in file %s", file);
3887 goto error;
3888 }
3889 if (curagent->b.name == NULL) {
3890 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3891 curagent->id, curagent->conf.file, curagent->conf.line);
3892 goto error;
3893 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003894 if (curagent->timeout.hello == TICK_ETERNITY ||
3895 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003896 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003897 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3898 " | While not properly invalid, you will certainly encounter various problems\n"
3899 " | with such a configuration. To fix this, please ensure that all following\n"
3900 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3901 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003902 }
3903 if (curagent->var_pfx == NULL) {
3904 char *tmp = curagent->id;
3905
3906 while (*tmp) {
3907 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3908 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3909 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3910 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3911 goto error;
3912 }
3913 tmp++;
3914 }
3915 curagent->var_pfx = strdup(curagent->id);
3916 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003917 if (curagent->engine_id == NULL)
3918 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919
Christopher Faulet11610f32017-09-21 10:23:10 +02003920 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003921 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3922 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003923 goto finish;
3924 }
3925
Christopher Faulet11610f32017-09-21 10:23:10 +02003926 /* Replace placeholders by the corresponding messages for the SPOE
3927 * agent */
3928 list_for_each_entry(ph, &curmphs, list) {
3929 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003930 struct spoe_arg *arg;
3931 unsigned int where;
3932
Christopher Faulet11610f32017-09-21 10:23:10 +02003933 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3935 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3936 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3937 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3938 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3939 }
3940 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3941 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3942 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003943 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3944 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003945 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 }
3947 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003948 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3949 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003950 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003951 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003952
3953 where = 0;
3954 switch (msg->event) {
3955 case SPOE_EV_ON_CLIENT_SESS:
3956 where |= SMP_VAL_FE_CON_ACC;
3957 break;
3958
3959 case SPOE_EV_ON_TCP_REQ_FE:
3960 where |= SMP_VAL_FE_REQ_CNT;
3961 break;
3962
3963 case SPOE_EV_ON_HTTP_REQ_FE:
3964 where |= SMP_VAL_FE_HRQ_HDR;
3965 break;
3966
3967 case SPOE_EV_ON_TCP_REQ_BE:
3968 if (px->cap & PR_CAP_FE)
3969 where |= SMP_VAL_FE_REQ_CNT;
3970 if (px->cap & PR_CAP_BE)
3971 where |= SMP_VAL_BE_REQ_CNT;
3972 break;
3973
3974 case SPOE_EV_ON_HTTP_REQ_BE:
3975 if (px->cap & PR_CAP_FE)
3976 where |= SMP_VAL_FE_HRQ_HDR;
3977 if (px->cap & PR_CAP_BE)
3978 where |= SMP_VAL_BE_HRQ_HDR;
3979 break;
3980
3981 case SPOE_EV_ON_SERVER_SESS:
3982 where |= SMP_VAL_BE_SRV_CON;
3983 break;
3984
3985 case SPOE_EV_ON_TCP_RSP:
3986 if (px->cap & PR_CAP_FE)
3987 where |= SMP_VAL_FE_RES_CNT;
3988 if (px->cap & PR_CAP_BE)
3989 where |= SMP_VAL_BE_RES_CNT;
3990 break;
3991
3992 case SPOE_EV_ON_HTTP_RSP:
3993 if (px->cap & PR_CAP_FE)
3994 where |= SMP_VAL_FE_HRS_HDR;
3995 if (px->cap & PR_CAP_BE)
3996 where |= SMP_VAL_BE_HRS_HDR;
3997 break;
3998
3999 default:
4000 break;
4001 }
4002
4003 list_for_each_entry(arg, &msg->args, list) {
4004 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004005 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004006 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004007 "none of which is available here ('%s')",
4008 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004009 sample_ckp_names(arg->expr->fetch->use),
4010 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004011 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004012 }
4013 }
4014
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004015 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004016 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4017 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004018 }
4019 }
4020 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004021 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004022 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004023 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004024 continue;
4025 }
4026
Christopher Faulet11610f32017-09-21 10:23:10 +02004027 /* Replace placeholders by the corresponding groups for the SPOE
4028 * agent */
4029 list_for_each_entry(ph, &curgphs, list) {
4030 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4031 if (!strcmp(grp->id, ph->id)) {
4032 grp->agent = curagent;
4033 LIST_DEL(&grp->list);
4034 LIST_ADDQ(&curagent->groups, &grp->list);
4035 goto next_aph;
4036 }
4037 }
4038 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4039 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4040 goto error;
4041 next_aph:
4042 continue;
4043 }
4044
4045 /* Replace placeholders by the corresponding message for each SPOE
4046 * group of the SPOE agent */
4047 list_for_each_entry(grp, &curagent->groups, list) {
4048 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4049 list_for_each_entry(msg, &curmsgs, list) {
4050 if (!strcmp(msg->id, ph->id)) {
4051 if (msg->group != NULL) {
4052 memprintf(err, "SPOE message '%s' already belongs to "
4053 "the SPOE group '%s' declare at %s:%d",
4054 msg->id, msg->group->id,
4055 msg->group->conf.file,
4056 msg->group->conf.line);
4057 goto error;
4058 }
4059
4060 /* Scope for arguments are not checked for now. We will check
4061 * them only if a rule use the corresponding SPOE group. */
4062 msg->agent = curagent;
4063 msg->group = grp;
4064 LIST_DEL(&ph->list);
4065 LIST_ADDQ(&grp->messages, &msg->by_grp);
4066 goto next_mph_grp;
4067 }
4068 }
4069 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4070 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4071 goto error;
4072 next_mph_grp:
4073 continue;
4074 }
4075 }
4076
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004077 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004078 /* move curmsgs to the agent message list */
4079 curmsgs.n->p = &curagent->messages;
4080 curmsgs.p->n = &curagent->messages;
4081 curagent->messages = curmsgs;
4082 LIST_INIT(&curmsgs);
4083
Christopher Faulet7ee86672017-09-19 11:08:28 +02004084 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004085 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004086 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4087 LIST_DEL(&ph->list);
4088 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004089 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004090 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4091 LIST_DEL(&ph->list);
4092 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004093 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004094 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4095 struct arg arg;
4096
4097 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4098 curagent->var_pfx, vph->name);
4099
4100 arg.type = ARGT_STR;
4101 arg.data.str.str = trash.str;
4102 arg.data.str.len = trash.len;
4103 if (!vars_check_arg(&arg, err)) {
4104 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4105 curagent->id, curagent->var_pfx, vph->name, *err);
4106 goto error;
4107 }
4108
4109 LIST_DEL(&vph->list);
4110 free(vph->name);
4111 free(vph);
4112 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004113 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4114 LIST_DEL(&grp->list);
4115 spoe_release_group(grp);
4116 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004117 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004118 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004119 fconf->ops = &spoe_ops;
4120 fconf->conf = conf;
4121 return 0;
4122
4123 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004124 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004125 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4126 LIST_DEL(&ph->list);
4127 spoe_release_placeholder(ph);
4128 }
4129 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4130 LIST_DEL(&ph->list);
4131 spoe_release_placeholder(ph);
4132 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004133 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4134 LIST_DEL(&vph->list);
4135 free(vph->name);
4136 free(vph);
4137 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004138 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4139 LIST_DEL(&grp->list);
4140 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004141 }
4142 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4143 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004144 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004145 }
4146 free(conf);
4147 return -1;
4148}
4149
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004150/* Send message of a SPOE group. This is the action_ptr callback of a rule
4151 * associated to a "send-spoe-group" action.
4152 *
4153 * It returns ACT_RET_CONT is processing is finished without error, it returns
4154 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4155 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004156static enum act_return
4157spoe_send_group(struct act_rule *rule, struct proxy *px,
4158 struct session *sess, struct stream *s, int flags)
4159{
4160 struct filter *filter;
4161 struct spoe_agent *agent = NULL;
4162 struct spoe_group *group = NULL;
4163 struct spoe_context *ctx = NULL;
4164 int ret, dir;
4165
4166 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4167 if (filter->config == rule->arg.act.p[0]) {
4168 agent = rule->arg.act.p[2];
4169 group = rule->arg.act.p[3];
4170 ctx = filter->ctx;
4171 break;
4172 }
4173 }
4174 if (agent == NULL || group == NULL || ctx == NULL)
4175 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004176 if (ctx->state == SPOE_CTX_ST_NONE)
4177 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004178
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004179 switch (rule->from) {
4180 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4181 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4182 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4183 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4184 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4185 default:
4186 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4187 " - internal error while execute spoe-send-group\n",
4188 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4189 __FUNCTION__, s);
4190 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4191 agent->id);
4192 return ACT_RET_CONT;
4193 }
4194
4195 ret = spoe_process_group(s, ctx, group, dir);
4196 if (ret == 1)
4197 return ACT_RET_CONT;
4198 else if (ret == 0) {
4199 if (flags & ACT_FLAG_FINAL) {
4200 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4201 " - failed to process group '%s': interrupted by caller\n",
4202 (int)now.tv_sec, (int)now.tv_usec,
4203 agent->id, __FUNCTION__, s, group->id);
4204 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4205 spoe_handle_processing_error(s, agent, ctx, dir);
4206 spoe_stop_processing(ctx);
4207 return ACT_RET_CONT;
4208 }
4209 return ACT_RET_YIELD;
4210 }
4211 else
4212 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004213}
4214
4215/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4216 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4217 * action should be:
4218 *
4219 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4220 *
4221 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4222 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4223 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4224 * group.
4225 *
4226 * The function returns 1 in success case, otherwise, it returns 0 and err is
4227 * filled.
4228 */
4229static int
4230check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4231{
4232 struct flt_conf *fconf;
4233 struct spoe_config *conf;
4234 struct spoe_agent *agent = NULL;
4235 struct spoe_group *group;
4236 struct spoe_message *msg;
4237 char *engine_id = rule->arg.act.p[0];
4238 char *group_id = rule->arg.act.p[1];
4239 unsigned int where = 0;
4240
4241 switch (rule->from) {
4242 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4243 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4244 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4245 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4246 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4247 default:
4248 memprintf(err,
4249 "internal error, unexpected rule->from=%d, please report this bug!",
4250 rule->from);
4251 goto error;
4252 }
4253
4254 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4255 * <px> */
4256 list_for_each_entry(fconf, &px->filter_configs, list) {
4257 conf = fconf->conf;
4258
4259 /* This is not an SPOE filter */
4260 if (fconf->id != spoe_filter_id)
4261 continue;
4262
4263 /* This is the good engine */
4264 if (!strcmp(conf->id, engine_id)) {
4265 agent = conf->agent;
4266 break;
4267 }
4268 }
4269 if (agent == NULL) {
4270 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4271 engine_id, group_id);
4272 goto error;
4273 }
4274
4275 /* Try to find the right group */
4276 list_for_each_entry(group, &agent->groups, list) {
4277 /* This is the good group */
4278 if (!strcmp(group->id, group_id))
4279 break;
4280 }
4281 if (&group->list == &agent->groups) {
4282 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4283 group_id, engine_id);
4284 goto error;
4285 }
4286
4287 /* Ok, we found the group, we need to check messages and their
4288 * arguments */
4289 list_for_each_entry(msg, &group->messages, by_grp) {
4290 struct spoe_arg *arg;
4291
4292 list_for_each_entry(arg, &msg->args, list) {
4293 if (!(arg->expr->fetch->val & where)) {
4294 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4295 "some args extract information from '%s',"
4296 "none of which is available here ('%s')",
4297 msg->id, group->id, msg->conf.file, msg->conf.line,
4298 sample_ckp_names(arg->expr->fetch->use),
4299 sample_ckp_names(where));
4300 goto error;
4301 }
4302 }
4303 }
4304
4305 free(engine_id);
4306 free(group_id);
4307 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4308 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4309 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4310 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4311 return 1;
4312
4313 error:
4314 free(engine_id);
4315 free(group_id);
4316 return 0;
4317}
4318
4319/* Parse 'send-spoe-group' action following the format:
4320 *
4321 * ... send-spoe-group <engine-id> <group-id>
4322 *
4323 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4324 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4325 * ids are saved and used later, when the rule will be checked.
4326 */
4327static enum act_parse_ret
4328parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4329 struct act_rule *rule, char **err)
4330{
4331 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4332 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4333 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4334 return ACT_RET_PRS_ERR;
4335 }
4336 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4337 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4338
4339 (*orig_arg) += 2;
4340
4341 rule->action = ACT_CUSTOM;
4342 rule->action_ptr = spoe_send_group;
4343 rule->check_ptr = check_send_spoe_group;
4344 return ACT_RET_PRS_OK;
4345}
4346
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004347
4348/* Declare the filter parser for "spoe" keyword */
4349static struct flt_kw_list flt_kws = { "SPOE", { }, {
4350 { "spoe", parse_spoe_flt, NULL },
4351 { NULL, NULL, NULL },
4352 }
4353};
4354
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004355/* Delcate the action parser for "spoe-action" keyword */
4356static struct action_kw_list tcp_req_action_kws = { { }, {
4357 { "send-spoe-group", parse_send_spoe_group },
4358 { /* END */ },
4359 }
4360};
4361static struct action_kw_list tcp_res_action_kws = { { }, {
4362 { "send-spoe-group", parse_send_spoe_group },
4363 { /* END */ },
4364 }
4365};
4366static struct action_kw_list http_req_action_kws = { { }, {
4367 { "send-spoe-group", parse_send_spoe_group },
4368 { /* END */ },
4369 }
4370};
4371static struct action_kw_list http_res_action_kws = { { }, {
4372 { "send-spoe-group", parse_send_spoe_group },
4373 { /* END */ },
4374 }
4375};
4376
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004377__attribute__((constructor))
4378static void __spoe_init(void)
4379{
4380 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004381 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4382 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4383 http_req_keywords_register(&http_req_action_kws);
4384 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004385
Willy Tarreaubafbe012017-11-24 17:34:44 +01004386 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4387 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004388}
4389
4390__attribute__((destructor))
4391static void
4392__spoe_deinit(void)
4393{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004394 pool_destroy(pool_head_spoe_ctx);
4395 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004396}