blob: cb97164677cbaa85a7a43353caa07e12707550fe [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 Fauletf51f5fa2017-01-19 10:01:12 +01002595 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002597 }
2598
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002599 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002600 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002601 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002602 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002603 if (ret < 0)
2604 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002605 if (!ret)
2606 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002607 if (spoe_queue_context(ctx) < 0)
2608 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002609 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2610 }
2611
2612 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2613 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002614 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002615 ret = 0;
2616 goto out;
2617 }
2618
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002619 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2620 ret = 0;
2621 goto out;
2622 }
2623
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002624 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002625 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002626 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002627 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002628 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002629 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002630 }
2631
2632 out:
2633 return ret;
2634
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002635 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002636 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002637 ret = 1;
2638 goto end;
2639
2640 skip:
2641 ctx->state = SPOE_CTX_ST_READY;
2642 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002643
Christopher Fauleta1cda022016-12-21 08:58:06 +01002644 end:
Christopher Faulet58d03682017-09-21 16:57:24 +02002645 spoe_stop_processing(ctx);
2646 return ret;
2647}
2648
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002649/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2650 * See spoe_process_message for details. */
2651static int
2652spoe_process_group(struct stream *s, struct spoe_context *ctx,
2653 struct spoe_group *group, int dir)
2654{
2655 int ret;
2656
2657 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2658 " - ctx-state=%s - Process messages for group=%s\n",
2659 (int)now.tv_sec, (int)now.tv_usec,
2660 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2661 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2662 group->id);
2663
2664 if (LIST_ISEMPTY(&group->messages))
2665 return 1;
2666
2667 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2668 return ret;
2669}
2670
Christopher Faulet58d03682017-09-21 16:57:24 +02002671/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2672 * See spoe_process_message for details. */
2673static int
2674spoe_process_event(struct stream *s, struct spoe_context *ctx,
2675 enum spoe_event ev)
2676{
2677 int dir, ret;
2678
2679 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002680 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002681 (int)now.tv_sec, (int)now.tv_usec,
2682 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2683 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2684 spoe_event_str[ev]);
2685
2686 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2687
2688 if (LIST_ISEMPTY(&(ctx->events[ev])))
2689 return 1;
2690
2691 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002692 return ret;
2693}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002694
2695/***************************************************************************
2696 * Functions that create/destroy SPOE contexts
2697 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002699spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002700{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002701 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 return 1;
2703
Christopher Faulet4596fb72017-01-11 14:05:19 +01002704 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002705 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002706 LIST_DEL(&buffer_wait->list);
2707 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002708 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002709 }
2710
Christopher Faulet4596fb72017-01-11 14:05:19 +01002711 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002712 return 1;
2713
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002714 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002715 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002716 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002717 return 0;
2718}
2719
2720static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002721spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002722{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002723 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002724 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002725 LIST_DEL(&buffer_wait->list);
2726 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002727 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002728 }
2729
2730 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002731 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002732 b_free(buf);
2733 offer_buffers(buffer_wait->target,
2734 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002735 }
2736}
2737
Christopher Faulet4596fb72017-01-11 14:05:19 +01002738static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002739spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002740{
2741 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2742 return 1;
2743}
2744
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002745static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002746spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002747{
2748 struct spoe_config *conf = FLT_CONF(filter);
2749 struct spoe_context *ctx;
2750
Willy Tarreaubafbe012017-11-24 17:34:44 +01002751 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002752 if (ctx == NULL) {
2753 return NULL;
2754 }
2755 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002756 ctx->filter = filter;
2757 ctx->state = SPOE_CTX_ST_NONE;
2758 ctx->status_code = SPOE_CTX_ERR_NONE;
2759 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002760 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002761 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002762 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002763 LIST_INIT(&ctx->buffer_wait.list);
2764 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002765 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002766 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002767
Christopher Fauletf7a30922016-11-10 15:04:51 +01002768 ctx->stream_id = 0;
2769 ctx->frame_id = 1;
2770 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002771
2772 return ctx;
2773}
2774
2775static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002776spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002777{
2778 if (!ctx)
2779 return;
2780
Christopher Faulet58d03682017-09-21 16:57:24 +02002781 spoe_stop_processing(ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002782 pool_free(pool_head_spoe_ctx, ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002783}
2784
2785static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002786spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002787{
2788 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002789 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002790}
2791
2792
2793/***************************************************************************
2794 * Hooks that manage the filter lifecycle (init/check/deinit)
2795 **************************************************************************/
2796/* Signal handler: Do a soft stop, wakeup SPOE applet */
2797static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002798spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002799{
2800 struct proxy *p;
2801
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002802 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002803 while (p) {
2804 struct flt_conf *fconf;
2805
2806 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002807 struct spoe_config *conf;
2808 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002809 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002810 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002811
Christopher Faulet3b386a32017-02-23 10:17:15 +01002812 if (fconf->id != spoe_filter_id)
2813 continue;
2814
2815 conf = fconf->conf;
2816 agent = conf->agent;
2817
Christopher Faulet24289f22017-09-25 14:48:02 +02002818 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002819 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002820 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2821 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002822 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002823 }
2824 }
2825 p = p->next;
2826 }
2827}
2828
2829
2830/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2831static int
2832spoe_init(struct proxy *px, struct flt_conf *fconf)
2833{
2834 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002835
2836 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2837 init_new_proxy(&conf->agent_fe);
2838 conf->agent_fe.parent = conf->agent;
2839 conf->agent_fe.last_change = now.tv_sec;
2840 conf->agent_fe.id = conf->agent->id;
2841 conf->agent_fe.cap = PR_CAP_FE;
2842 conf->agent_fe.mode = PR_MODE_TCP;
2843 conf->agent_fe.maxconn = 0;
2844 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2845 conf->agent_fe.conn_retries = CONN_RETRIES;
2846 conf->agent_fe.accept = frontend_accept;
2847 conf->agent_fe.srv = NULL;
2848 conf->agent_fe.timeout.client = TICK_ETERNITY;
2849 conf->agent_fe.default_target = &spoe_applet.obj_type;
2850 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2851
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002852 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002853 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002854 sighandler_registered = 1;
2855 }
2856
2857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002858}
2859
2860/* Free ressources allocated by the SPOE filter. */
2861static void
2862spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2863{
2864 struct spoe_config *conf = fconf->conf;
2865
2866 if (conf) {
2867 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002868
Christopher Faulet8ef75252017-02-20 22:56:03 +01002869 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002870 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002871 free(conf);
2872 }
2873 fconf->conf = NULL;
2874}
2875
2876/* Check configuration of a SPOE filter for a specified proxy.
2877 * Return 1 on error, else 0. */
2878static int
2879spoe_check(struct proxy *px, struct flt_conf *fconf)
2880{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002881 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882 struct spoe_config *conf = fconf->conf;
2883 struct proxy *target;
2884
Christopher Faulet7ee86672017-09-19 11:08:28 +02002885 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2886 * are uniq */
2887 list_for_each_entry(f, &px->filter_configs, list) {
2888 struct spoe_config *c = f->conf;
2889
2890 /* This is not an SPOE filter */
2891 if (f->id != spoe_filter_id)
2892 continue;
2893 /* This is the current SPOE filter */
2894 if (f == fconf)
2895 continue;
2896
2897 /* Check engine Id. It should be uniq */
2898 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002899 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2900 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002901 return 1;
2902 }
2903 }
2904
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905 target = proxy_be_by_name(conf->agent->b.name);
2906 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002907 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2908 " declared at %s:%d.\n",
2909 px->id, conf->agent->b.name, conf->agent->id,
2910 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002911 return 1;
2912 }
2913 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002914 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2915 " at %s:%d does not support HTTP mode.\n",
2916 px->id, target->id, conf->agent->id,
2917 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002918 return 1;
2919 }
2920
2921 free(conf->agent->b.name);
2922 conf->agent->b.name = NULL;
2923 conf->agent->b.be = target;
2924 return 0;
2925}
2926
2927/**************************************************************************
2928 * Hooks attached to a stream
2929 *************************************************************************/
2930/* Called when a filter instance is created and attach to a stream. It creates
2931 * the context that will be used to process this stream. */
2932static int
2933spoe_start(struct stream *s, struct filter *filter)
2934{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002935 struct spoe_config *conf = FLT_CONF(filter);
2936 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002937 struct spoe_context *ctx;
2938
2939 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002940 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002941 __FUNCTION__, s);
2942
Christopher Faulet8ef75252017-02-20 22:56:03 +01002943 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002945 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2946 " - failed to create SPOE context\n",
2947 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002948 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002949 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002950 "SPOE: [%s] failed to create SPOE context\n",
2951 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952 return 0;
2953 }
2954
2955 ctx->strm = s;
2956 ctx->state = SPOE_CTX_ST_READY;
2957 filter->ctx = ctx;
2958
Christopher Faulet11610f32017-09-21 10:23:10 +02002959 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002960 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2961
Christopher Faulet11610f32017-09-21 10:23:10 +02002962 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2964
Christopher Faulet11610f32017-09-21 10:23:10 +02002965 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002966 filter->pre_analyzers |= AN_RES_INSPECT;
2967
Christopher Faulet11610f32017-09-21 10:23:10 +02002968 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002969 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2970
Christopher Faulet11610f32017-09-21 10:23:10 +02002971 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002972 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2973
Christopher Faulet11610f32017-09-21 10:23:10 +02002974 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002975 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2976
2977 return 1;
2978}
2979
2980/* Called when a filter instance is detached from a stream. It release the
2981 * attached SPOE context. */
2982static void
2983spoe_stop(struct stream *s, struct filter *filter)
2984{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2986 (int)now.tv_sec, (int)now.tv_usec,
2987 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2988 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002989 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002990}
2991
Christopher Fauletf7a30922016-11-10 15:04:51 +01002992
2993/*
2994 * Called when the stream is woken up because of expired timer.
2995 */
2996static void
2997spoe_check_timeouts(struct stream *s, struct filter *filter)
2998{
2999 struct spoe_context *ctx = filter->ctx;
3000
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003001 if (tick_is_expired(ctx->process_exp, now_ms)) {
3002 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003003 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003004 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003005}
3006
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007/* Called when we are ready to filter data on a channel */
3008static int
3009spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3010{
3011 struct spoe_context *ctx = filter->ctx;
3012 int ret = 1;
3013
3014 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3015 " - ctx-flags=0x%08x\n",
3016 (int)now.tv_sec, (int)now.tv_usec,
3017 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3018 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3019
Christopher Fauletb067b062017-01-04 16:39:11 +01003020 if (ctx->state == SPOE_CTX_ST_NONE)
3021 goto out;
3022
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 if (!(chn->flags & CF_ISRESP)) {
3024 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3025 chn->analysers |= AN_REQ_INSPECT_FE;
3026 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3027 chn->analysers |= AN_REQ_INSPECT_BE;
3028
3029 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3030 goto out;
3031
3032 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003033 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003034 if (!ret)
3035 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003036 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3037 }
3038 else {
3039 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3040 chn->analysers |= AN_RES_INSPECT;
3041
3042 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3043 goto out;
3044
Christopher Faulet8ef75252017-02-20 22:56:03 +01003045 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003046 if (!ret) {
3047 channel_dont_read(chn);
3048 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003049 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003050 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003051 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 }
3053
3054 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003055 return ret;
3056}
3057
3058/* Called before a processing happens on a given channel */
3059static int
3060spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3061 struct channel *chn, unsigned an_bit)
3062{
3063 struct spoe_context *ctx = filter->ctx;
3064 int ret = 1;
3065
3066 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3067 " - ctx-flags=0x%08x - ana=0x%08x\n",
3068 (int)now.tv_sec, (int)now.tv_usec,
3069 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3070 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3071 ctx->flags, an_bit);
3072
Christopher Fauletb067b062017-01-04 16:39:11 +01003073 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003074 goto out;
3075
3076 switch (an_bit) {
3077 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003078 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003079 break;
3080 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003081 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082 break;
3083 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003084 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003085 break;
3086 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003087 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003088 break;
3089 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003090 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091 break;
3092 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003093 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 break;
3095 }
3096
3097 out:
3098 if (!ret) {
3099 channel_dont_read(chn);
3100 channel_dont_close(chn);
3101 }
3102 return ret;
3103}
3104
3105/* Called when the filtering on the channel ends. */
3106static int
3107spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3108{
3109 struct spoe_context *ctx = filter->ctx;
3110
3111 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3112 " - ctx-flags=0x%08x\n",
3113 (int)now.tv_sec, (int)now.tv_usec,
3114 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3115 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3116
3117 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003118 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003119 }
3120
3121 return 1;
3122}
3123
3124/********************************************************************
3125 * Functions that manage the filter initialization
3126 ********************************************************************/
3127struct flt_ops spoe_ops = {
3128 /* Manage SPOE filter, called for each filter declaration */
3129 .init = spoe_init,
3130 .deinit = spoe_deinit,
3131 .check = spoe_check,
3132
3133 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003134 .attach = spoe_start,
3135 .detach = spoe_stop,
3136 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003137
3138 /* Handle channels activity */
3139 .channel_start_analyze = spoe_start_analyze,
3140 .channel_pre_analyze = spoe_chn_pre_analyze,
3141 .channel_end_analyze = spoe_end_analyze,
3142};
3143
3144
3145static int
3146cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3147{
3148 const char *err;
3149 int i, err_code = 0;
3150
3151 if ((cfg_scope == NULL && curengine != NULL) ||
3152 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003153 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003154 goto out;
3155
3156 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3157 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003158 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3159 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160 err_code |= ERR_ALERT | ERR_ABORT;
3161 goto out;
3162 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003163 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3164 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003165 goto out;
3166 }
3167
3168 err = invalid_char(args[1]);
3169 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003170 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3171 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003172 err_code |= ERR_ALERT | ERR_ABORT;
3173 goto out;
3174 }
3175
3176 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003177 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3178 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003179 err_code |= ERR_ALERT | ERR_ABORT;
3180 goto out;
3181 }
3182 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003183 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184 err_code |= ERR_ALERT | ERR_ABORT;
3185 goto out;
3186 }
3187
3188 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003189
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003190 curagent->conf.file = strdup(file);
3191 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003192
3193 curagent->timeout.hello = TICK_ETERNITY;
3194 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003195 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003196
3197 curagent->engine_id = NULL;
3198 curagent->var_pfx = NULL;
3199 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003200 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3201 if (global.nbthread == 1)
3202 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003203 curagent->cps_max = 0;
3204 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003205 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003206 curagent->min_applets = 0;
3207 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208
3209 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003210 LIST_INIT(&curagent->events[i]);
3211 LIST_INIT(&curagent->groups);
3212 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003213
Christopher Faulet24289f22017-09-25 14:48:02 +02003214 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003215 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003216 err_code |= ERR_ALERT | ERR_ABORT;
3217 goto out;
3218 }
3219 for (i = 0; i < global.nbthread; ++i) {
3220 curagent->rt[i].frame_size = curagent->max_frame_size;
3221 curagent->rt[i].applets_act = 0;
3222 curagent->rt[i].applets_idle = 0;
3223 curagent->rt[i].sending_rate = 0;
3224 LIST_INIT(&curagent->rt[i].applets);
3225 LIST_INIT(&curagent->rt[i].sending_queue);
3226 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003227 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003228 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 }
3230 else if (!strcmp(args[0], "use-backend")) {
3231 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003232 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3233 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003234 err_code |= ERR_ALERT | ERR_FATAL;
3235 goto out;
3236 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003237 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003238 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003239 free(curagent->b.name);
3240 curagent->b.name = strdup(args[1]);
3241 }
3242 else if (!strcmp(args[0], "messages")) {
3243 int cur_arg = 1;
3244 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003245 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003246
Christopher Faulet11610f32017-09-21 10:23:10 +02003247 list_for_each_entry(ph, &curmphs, list) {
3248 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003249 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3250 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003251 err_code |= ERR_ALERT | ERR_FATAL;
3252 goto out;
3253 }
3254 }
3255
Christopher Faulet11610f32017-09-21 10:23:10 +02003256 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003257 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003258 err_code |= ERR_ALERT | ERR_ABORT;
3259 goto out;
3260 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003261 ph->id = strdup(args[cur_arg]);
3262 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003263 cur_arg++;
3264 }
3265 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003266 else if (!strcmp(args[0], "groups")) {
3267 int cur_arg = 1;
3268 while (*args[cur_arg]) {
3269 struct spoe_placeholder *ph = NULL;
3270
3271 list_for_each_entry(ph, &curgphs, list) {
3272 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003273 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3274 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003275 err_code |= ERR_ALERT | ERR_FATAL;
3276 goto out;
3277 }
3278 }
3279
3280 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003281 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003282 err_code |= ERR_ALERT | ERR_ABORT;
3283 goto out;
3284 }
3285 ph->id = strdup(args[cur_arg]);
3286 LIST_ADDQ(&curgphs, &ph->list);
3287 cur_arg++;
3288 }
3289 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003290 else if (!strcmp(args[0], "timeout")) {
3291 unsigned int *tv = NULL;
3292 const char *res;
3293 unsigned timeout;
3294
3295 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003296 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3297 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003298 err_code |= ERR_ALERT | ERR_FATAL;
3299 goto out;
3300 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003301 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3302 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003303 if (!strcmp(args[1], "hello"))
3304 tv = &curagent->timeout.hello;
3305 else if (!strcmp(args[1], "idle"))
3306 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003307 else if (!strcmp(args[1], "processing"))
3308 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003309 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003310 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3311 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003312 err_code |= ERR_ALERT | ERR_FATAL;
3313 goto out;
3314 }
3315 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003316 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3317 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003318 err_code |= ERR_ALERT | ERR_FATAL;
3319 goto out;
3320 }
3321 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3322 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003323 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3324 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003325 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003326 goto out;
3327 }
3328 *tv = MS_TO_TICKS(timeout);
3329 }
3330 else if (!strcmp(args[0], "option")) {
3331 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003332 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3333 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003334 err_code |= ERR_ALERT | ERR_FATAL;
3335 goto out;
3336 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003337
Christopher Faulet305c6072017-02-23 16:17:53 +01003338 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003339 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003340 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003341 if (kwm == 1)
3342 curagent->flags &= ~SPOE_FL_PIPELINING;
3343 else
3344 curagent->flags |= SPOE_FL_PIPELINING;
3345 goto out;
3346 }
3347 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003348 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003349 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003350 if (kwm == 1)
3351 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003352 else {
3353 if (global.nbthread == 1)
3354 curagent->flags |= SPOE_FL_ASYNC;
3355 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003356 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3357 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003358 err_code |= ERR_WARN;
3359 }
3360 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003361 goto out;
3362 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003363 else if (!strcmp(args[1], "send-frag-payload")) {
3364 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3365 goto out;
3366 if (kwm == 1)
3367 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3368 else
3369 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3370 goto out;
3371 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003372
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003373 /* Following options does not support negation */
3374 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003375 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3376 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003377 err_code |= ERR_ALERT | ERR_FATAL;
3378 goto out;
3379 }
3380
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 if (!strcmp(args[1], "var-prefix")) {
3382 char *tmp;
3383
3384 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003385 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3386 file, linenum, args[0],
3387 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003388 err_code |= ERR_ALERT | ERR_FATAL;
3389 goto out;
3390 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003391 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3392 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003393 tmp = args[2];
3394 while (*tmp) {
3395 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3397 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 err_code |= ERR_ALERT | ERR_FATAL;
3399 goto out;
3400 }
3401 tmp++;
3402 }
3403 curagent->var_pfx = strdup(args[2]);
3404 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003405 else if (!strcmp(args[1], "force-set-var")) {
3406 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3407 goto out;
3408 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3409 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003410 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003411 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003412 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003413 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3414 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003415 else if (!strcmp(args[1], "set-on-error")) {
3416 char *tmp;
3417
3418 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003419 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3420 file, linenum, args[0],
3421 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003422 err_code |= ERR_ALERT | ERR_FATAL;
3423 goto out;
3424 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003425 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3426 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003427 tmp = args[2];
3428 while (*tmp) {
3429 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003430 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3431 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003432 err_code |= ERR_ALERT | ERR_FATAL;
3433 goto out;
3434 }
3435 tmp++;
3436 }
3437 curagent->var_on_error = strdup(args[2]);
3438 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003439 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003440 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3441 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003442 err_code |= ERR_ALERT | ERR_FATAL;
3443 goto out;
3444 }
Christopher Faulet48026722016-11-16 15:01:12 +01003445 }
3446 else if (!strcmp(args[0], "maxconnrate")) {
3447 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3449 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003450 err_code |= ERR_ALERT | ERR_FATAL;
3451 goto out;
3452 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003453 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003454 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003455 curagent->cps_max = atol(args[1]);
3456 }
3457 else if (!strcmp(args[0], "maxerrrate")) {
3458 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003459 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3460 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003461 err_code |= ERR_ALERT | ERR_FATAL;
3462 goto out;
3463 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003464 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003465 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003466 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003467 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003468 else if (!strcmp(args[0], "max-frame-size")) {
3469 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003470 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3471 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003472 err_code |= ERR_ALERT | ERR_FATAL;
3473 goto out;
3474 }
3475 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3476 goto out;
3477 curagent->max_frame_size = atol(args[1]);
3478 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3479 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003480 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3481 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003482 err_code |= ERR_ALERT | ERR_FATAL;
3483 goto out;
3484 }
3485 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003486 else if (!strcmp(args[0], "register-var-names")) {
3487 int cur_arg;
3488
3489 if (!*args[1]) {
3490 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3491 file, linenum, args[0]);
3492 err_code |= ERR_ALERT | ERR_FATAL;
3493 goto out;
3494 }
3495 cur_arg = 1;
3496 while (*args[cur_arg]) {
3497 struct spoe_var_placeholder *vph;
3498
3499 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3500 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3501 err_code |= ERR_ALERT | ERR_ABORT;
3502 goto out;
3503 }
3504 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3505 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3506 err_code |= ERR_ALERT | ERR_ABORT;
3507 goto out;
3508 }
3509 LIST_ADDQ(&curvars, &vph->list);
3510 cur_arg++;
3511 }
3512 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003513 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003514 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3515 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003516 err_code |= ERR_ALERT | ERR_FATAL;
3517 goto out;
3518 }
3519 out:
3520 return err_code;
3521}
Christopher Faulet11610f32017-09-21 10:23:10 +02003522static int
3523cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3524{
3525 struct spoe_group *grp;
3526 const char *err;
3527 int err_code = 0;
3528
3529 if ((cfg_scope == NULL && curengine != NULL) ||
3530 (cfg_scope != NULL && curengine == NULL) ||
3531 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3532 goto out;
3533
3534 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3535 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003536 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3537 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003538 err_code |= ERR_ALERT | ERR_ABORT;
3539 goto out;
3540 }
3541 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3542 err_code |= ERR_ABORT;
3543 goto out;
3544 }
3545
3546 err = invalid_char(args[1]);
3547 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003548 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3549 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003550 err_code |= ERR_ALERT | ERR_ABORT;
3551 goto out;
3552 }
3553
3554 list_for_each_entry(grp, &curgrps, list) {
3555 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003556 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3557 " name as another one declared at %s:%d.\n",
3558 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003559 err_code |= ERR_ALERT | ERR_FATAL;
3560 goto out;
3561 }
3562 }
3563
3564 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003565 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003566 err_code |= ERR_ALERT | ERR_ABORT;
3567 goto out;
3568 }
3569
3570 curgrp->id = strdup(args[1]);
3571 curgrp->conf.file = strdup(file);
3572 curgrp->conf.line = linenum;
3573 LIST_INIT(&curgrp->phs);
3574 LIST_INIT(&curgrp->messages);
3575 LIST_ADDQ(&curgrps, &curgrp->list);
3576 }
3577 else if (!strcmp(args[0], "messages")) {
3578 int cur_arg = 1;
3579 while (*args[cur_arg]) {
3580 struct spoe_placeholder *ph = NULL;
3581
3582 list_for_each_entry(ph, &curgrp->phs, list) {
3583 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003584 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3585 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003586 err_code |= ERR_ALERT | ERR_FATAL;
3587 goto out;
3588 }
3589 }
3590
3591 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003592 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003593 err_code |= ERR_ALERT | ERR_ABORT;
3594 goto out;
3595 }
3596 ph->id = strdup(args[cur_arg]);
3597 LIST_ADDQ(&curgrp->phs, &ph->list);
3598 cur_arg++;
3599 }
3600 }
3601 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003602 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3603 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003604 err_code |= ERR_ALERT | ERR_FATAL;
3605 goto out;
3606 }
3607 out:
3608 return err_code;
3609}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003610
3611static int
3612cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3613{
3614 struct spoe_message *msg;
3615 struct spoe_arg *arg;
3616 const char *err;
3617 char *errmsg = NULL;
3618 int err_code = 0;
3619
3620 if ((cfg_scope == NULL && curengine != NULL) ||
3621 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003622 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003623 goto out;
3624
3625 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3626 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003627 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3628 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003629 err_code |= ERR_ALERT | ERR_ABORT;
3630 goto out;
3631 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003632 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3633 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003634 goto out;
3635 }
3636
3637 err = invalid_char(args[1]);
3638 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003639 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3640 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003641 err_code |= ERR_ALERT | ERR_ABORT;
3642 goto out;
3643 }
3644
3645 list_for_each_entry(msg, &curmsgs, list) {
3646 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003647 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3648 " name as another one declared at %s:%d.\n",
3649 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003650 err_code |= ERR_ALERT | ERR_FATAL;
3651 goto out;
3652 }
3653 }
3654
3655 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003656 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003657 err_code |= ERR_ALERT | ERR_ABORT;
3658 goto out;
3659 }
3660
3661 curmsg->id = strdup(args[1]);
3662 curmsg->id_len = strlen(curmsg->id);
3663 curmsg->event = SPOE_EV_NONE;
3664 curmsg->conf.file = strdup(file);
3665 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003666 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003667 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003668 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003669 LIST_INIT(&curmsg->by_evt);
3670 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003671 LIST_ADDQ(&curmsgs, &curmsg->list);
3672 }
3673 else if (!strcmp(args[0], "args")) {
3674 int cur_arg = 1;
3675
3676 curproxy->conf.args.ctx = ARGC_SPOE;
3677 curproxy->conf.args.file = file;
3678 curproxy->conf.args.line = linenum;
3679 while (*args[cur_arg]) {
3680 char *delim = strchr(args[cur_arg], '=');
3681 int idx = 0;
3682
3683 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003684 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003685 err_code |= ERR_ALERT | ERR_ABORT;
3686 goto out;
3687 }
3688
3689 if (!delim) {
3690 arg->name = NULL;
3691 arg->name_len = 0;
3692 delim = args[cur_arg];
3693 }
3694 else {
3695 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3696 arg->name_len = delim - args[cur_arg];
3697 delim++;
3698 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003699 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3700 &idx, file, linenum, &errmsg,
3701 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003702 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003703 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003704 err_code |= ERR_ALERT | ERR_FATAL;
3705 free(arg->name);
3706 free(arg);
3707 goto out;
3708 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003709 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003710 LIST_ADDQ(&curmsg->args, &arg->list);
3711 cur_arg++;
3712 }
3713 curproxy->conf.args.file = NULL;
3714 curproxy->conf.args.line = 0;
3715 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003716 else if (!strcmp(args[0], "acl")) {
3717 err = invalid_char(args[1]);
3718 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003719 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3720 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003721 err_code |= ERR_ALERT | ERR_FATAL;
3722 goto out;
3723 }
3724 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003725 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3726 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003727 err_code |= ERR_ALERT | ERR_FATAL;
3728 goto out;
3729 }
3730 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003731 else if (!strcmp(args[0], "event")) {
3732 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003733 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003734 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003735 goto out;
3736 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003737 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3738 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003739
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003740 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3741 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3742 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3743 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3744
3745 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3746 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3747 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3748 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3749 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3750 curmsg->event = SPOE_EV_ON_TCP_RSP;
3751
3752 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3753 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3754 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3755 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3756 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3757 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3758 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003759 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3760 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003761 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003762 goto out;
3763 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003764
3765 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3766 struct acl_cond *cond;
3767
3768 cond = build_acl_cond(file, linenum, &curmsg->acls,
3769 curproxy, (const char **)args+2,
3770 &errmsg);
3771 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003772 ha_alert("parsing [%s:%d] : error detected while "
3773 "parsing an 'event %s' condition : %s.\n",
3774 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003775 err_code |= ERR_ALERT | ERR_FATAL;
3776 goto out;
3777 }
3778 curmsg->cond = cond;
3779 }
3780 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003781 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3782 "or 'unless' followed by a condition but found '%s'.\n",
3783 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003784 err_code |= ERR_ALERT | ERR_FATAL;
3785 goto out;
3786 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003787 }
3788 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003789 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3790 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003791 err_code |= ERR_ALERT | ERR_FATAL;
3792 goto out;
3793 }
3794 out:
3795 free(errmsg);
3796 return err_code;
3797}
3798
3799/* Return -1 on error, else 0 */
3800static int
3801parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3802 struct flt_conf *fconf, char **err, void *private)
3803{
3804 struct list backup_sections;
3805 struct spoe_config *conf;
3806 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003807 struct spoe_group *grp, *grpback;
3808 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003809 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003810 char *file = NULL, *engine = NULL;
3811 int ret, pos = *cur_arg + 1;
3812
3813 conf = calloc(1, sizeof(*conf));
3814 if (conf == NULL) {
3815 memprintf(err, "%s: out of memory", args[*cur_arg]);
3816 goto error;
3817 }
3818 conf->proxy = px;
3819
3820 while (*args[pos]) {
3821 if (!strcmp(args[pos], "config")) {
3822 if (!*args[pos+1]) {
3823 memprintf(err, "'%s' : '%s' option without value",
3824 args[*cur_arg], args[pos]);
3825 goto error;
3826 }
3827 file = args[pos+1];
3828 pos += 2;
3829 }
3830 else if (!strcmp(args[pos], "engine")) {
3831 if (!*args[pos+1]) {
3832 memprintf(err, "'%s' : '%s' option without value",
3833 args[*cur_arg], args[pos]);
3834 goto error;
3835 }
3836 engine = args[pos+1];
3837 pos += 2;
3838 }
3839 else {
3840 memprintf(err, "unknown keyword '%s'", args[pos]);
3841 goto error;
3842 }
3843 }
3844 if (file == NULL) {
3845 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3846 goto error;
3847 }
3848
3849 /* backup sections and register SPOE sections */
3850 LIST_INIT(&backup_sections);
3851 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003852 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3853 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003854 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003855
3856 /* Parse SPOE filter configuration file */
3857 curengine = engine;
3858 curproxy = px;
3859 curagent = NULL;
3860 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003861 LIST_INIT(&curmsgs);
3862 LIST_INIT(&curgrps);
3863 LIST_INIT(&curmphs);
3864 LIST_INIT(&curgphs);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003865 LIST_INIT(&curvars);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003866 ret = readcfgfile(file);
3867 curproxy = NULL;
3868
3869 /* unregister SPOE sections and restore previous sections */
3870 cfg_unregister_sections();
3871 cfg_restore_sections(&backup_sections);
3872
3873 if (ret == -1) {
3874 memprintf(err, "Could not open configuration file %s : %s",
3875 file, strerror(errno));
3876 goto error;
3877 }
3878 if (ret & (ERR_ABORT|ERR_FATAL)) {
3879 memprintf(err, "Error(s) found in configuration file %s", file);
3880 goto error;
3881 }
3882
3883 /* Check SPOE agent */
3884 if (curagent == NULL) {
3885 memprintf(err, "No SPOE agent found in file %s", file);
3886 goto error;
3887 }
3888 if (curagent->b.name == NULL) {
3889 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3890 curagent->id, curagent->conf.file, curagent->conf.line);
3891 goto error;
3892 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003893 if (curagent->timeout.hello == TICK_ETERNITY ||
3894 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003895 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003896 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3897 " | While not properly invalid, you will certainly encounter various problems\n"
3898 " | with such a configuration. To fix this, please ensure that all following\n"
3899 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3900 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003901 }
3902 if (curagent->var_pfx == NULL) {
3903 char *tmp = curagent->id;
3904
3905 while (*tmp) {
3906 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3907 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3908 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3909 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3910 goto error;
3911 }
3912 tmp++;
3913 }
3914 curagent->var_pfx = strdup(curagent->id);
3915 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003916 if (curagent->engine_id == NULL)
3917 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918
Christopher Faulet11610f32017-09-21 10:23:10 +02003919 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003920 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3921 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003922 goto finish;
3923 }
3924
Christopher Faulet11610f32017-09-21 10:23:10 +02003925 /* Replace placeholders by the corresponding messages for the SPOE
3926 * agent */
3927 list_for_each_entry(ph, &curmphs, list) {
3928 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003929 struct spoe_arg *arg;
3930 unsigned int where;
3931
Christopher Faulet11610f32017-09-21 10:23:10 +02003932 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003933 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3934 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3935 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3936 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3937 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3938 }
3939 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3940 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3941 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003942 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3943 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003944 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003945 }
3946 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3948 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003949 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003951
3952 where = 0;
3953 switch (msg->event) {
3954 case SPOE_EV_ON_CLIENT_SESS:
3955 where |= SMP_VAL_FE_CON_ACC;
3956 break;
3957
3958 case SPOE_EV_ON_TCP_REQ_FE:
3959 where |= SMP_VAL_FE_REQ_CNT;
3960 break;
3961
3962 case SPOE_EV_ON_HTTP_REQ_FE:
3963 where |= SMP_VAL_FE_HRQ_HDR;
3964 break;
3965
3966 case SPOE_EV_ON_TCP_REQ_BE:
3967 if (px->cap & PR_CAP_FE)
3968 where |= SMP_VAL_FE_REQ_CNT;
3969 if (px->cap & PR_CAP_BE)
3970 where |= SMP_VAL_BE_REQ_CNT;
3971 break;
3972
3973 case SPOE_EV_ON_HTTP_REQ_BE:
3974 if (px->cap & PR_CAP_FE)
3975 where |= SMP_VAL_FE_HRQ_HDR;
3976 if (px->cap & PR_CAP_BE)
3977 where |= SMP_VAL_BE_HRQ_HDR;
3978 break;
3979
3980 case SPOE_EV_ON_SERVER_SESS:
3981 where |= SMP_VAL_BE_SRV_CON;
3982 break;
3983
3984 case SPOE_EV_ON_TCP_RSP:
3985 if (px->cap & PR_CAP_FE)
3986 where |= SMP_VAL_FE_RES_CNT;
3987 if (px->cap & PR_CAP_BE)
3988 where |= SMP_VAL_BE_RES_CNT;
3989 break;
3990
3991 case SPOE_EV_ON_HTTP_RSP:
3992 if (px->cap & PR_CAP_FE)
3993 where |= SMP_VAL_FE_HRS_HDR;
3994 if (px->cap & PR_CAP_BE)
3995 where |= SMP_VAL_BE_HRS_HDR;
3996 break;
3997
3998 default:
3999 break;
4000 }
4001
4002 list_for_each_entry(arg, &msg->args, list) {
4003 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004004 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004005 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004006 "none of which is available here ('%s')",
4007 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004008 sample_ckp_names(arg->expr->fetch->use),
4009 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004010 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004011 }
4012 }
4013
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004014 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004015 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4016 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004017 }
4018 }
4019 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004020 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004021 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004022 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004023 continue;
4024 }
4025
Christopher Faulet11610f32017-09-21 10:23:10 +02004026 /* Replace placeholders by the corresponding groups for the SPOE
4027 * agent */
4028 list_for_each_entry(ph, &curgphs, list) {
4029 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4030 if (!strcmp(grp->id, ph->id)) {
4031 grp->agent = curagent;
4032 LIST_DEL(&grp->list);
4033 LIST_ADDQ(&curagent->groups, &grp->list);
4034 goto next_aph;
4035 }
4036 }
4037 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4038 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4039 goto error;
4040 next_aph:
4041 continue;
4042 }
4043
4044 /* Replace placeholders by the corresponding message for each SPOE
4045 * group of the SPOE agent */
4046 list_for_each_entry(grp, &curagent->groups, list) {
4047 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4048 list_for_each_entry(msg, &curmsgs, list) {
4049 if (!strcmp(msg->id, ph->id)) {
4050 if (msg->group != NULL) {
4051 memprintf(err, "SPOE message '%s' already belongs to "
4052 "the SPOE group '%s' declare at %s:%d",
4053 msg->id, msg->group->id,
4054 msg->group->conf.file,
4055 msg->group->conf.line);
4056 goto error;
4057 }
4058
4059 /* Scope for arguments are not checked for now. We will check
4060 * them only if a rule use the corresponding SPOE group. */
4061 msg->agent = curagent;
4062 msg->group = grp;
4063 LIST_DEL(&ph->list);
4064 LIST_ADDQ(&grp->messages, &msg->by_grp);
4065 goto next_mph_grp;
4066 }
4067 }
4068 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4069 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4070 goto error;
4071 next_mph_grp:
4072 continue;
4073 }
4074 }
4075
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004076 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004077 /* move curmsgs to the agent message list */
4078 curmsgs.n->p = &curagent->messages;
4079 curmsgs.p->n = &curagent->messages;
4080 curagent->messages = curmsgs;
4081 LIST_INIT(&curmsgs);
4082
Christopher Faulet7ee86672017-09-19 11:08:28 +02004083 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004084 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004085 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4086 LIST_DEL(&ph->list);
4087 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004088 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004089 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4090 LIST_DEL(&ph->list);
4091 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004092 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004093 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4094 struct arg arg;
4095
4096 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4097 curagent->var_pfx, vph->name);
4098
4099 arg.type = ARGT_STR;
4100 arg.data.str.str = trash.str;
4101 arg.data.str.len = trash.len;
4102 if (!vars_check_arg(&arg, err)) {
4103 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4104 curagent->id, curagent->var_pfx, vph->name, *err);
4105 goto error;
4106 }
4107
4108 LIST_DEL(&vph->list);
4109 free(vph->name);
4110 free(vph);
4111 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004112 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4113 LIST_DEL(&grp->list);
4114 spoe_release_group(grp);
4115 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004116 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004117 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004118 fconf->ops = &spoe_ops;
4119 fconf->conf = conf;
4120 return 0;
4121
4122 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004123 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004124 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4125 LIST_DEL(&ph->list);
4126 spoe_release_placeholder(ph);
4127 }
4128 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4129 LIST_DEL(&ph->list);
4130 spoe_release_placeholder(ph);
4131 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004132 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4133 LIST_DEL(&vph->list);
4134 free(vph->name);
4135 free(vph);
4136 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004137 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4138 LIST_DEL(&grp->list);
4139 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004140 }
4141 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4142 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004143 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004144 }
4145 free(conf);
4146 return -1;
4147}
4148
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004149/* Send message of a SPOE group. This is the action_ptr callback of a rule
4150 * associated to a "send-spoe-group" action.
4151 *
4152 * It returns ACT_RET_CONT is processing is finished without error, it returns
4153 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4154 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004155static enum act_return
4156spoe_send_group(struct act_rule *rule, struct proxy *px,
4157 struct session *sess, struct stream *s, int flags)
4158{
4159 struct filter *filter;
4160 struct spoe_agent *agent = NULL;
4161 struct spoe_group *group = NULL;
4162 struct spoe_context *ctx = NULL;
4163 int ret, dir;
4164
4165 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4166 if (filter->config == rule->arg.act.p[0]) {
4167 agent = rule->arg.act.p[2];
4168 group = rule->arg.act.p[3];
4169 ctx = filter->ctx;
4170 break;
4171 }
4172 }
4173 if (agent == NULL || group == NULL || ctx == NULL)
4174 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004175 if (ctx->state == SPOE_CTX_ST_NONE)
4176 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004177
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004178 switch (rule->from) {
4179 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4180 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4181 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4182 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4183 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4184 default:
4185 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4186 " - internal error while execute spoe-send-group\n",
4187 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4188 __FUNCTION__, s);
4189 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4190 agent->id);
4191 return ACT_RET_CONT;
4192 }
4193
4194 ret = spoe_process_group(s, ctx, group, dir);
4195 if (ret == 1)
4196 return ACT_RET_CONT;
4197 else if (ret == 0) {
4198 if (flags & ACT_FLAG_FINAL) {
4199 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4200 " - failed to process group '%s': interrupted by caller\n",
4201 (int)now.tv_sec, (int)now.tv_usec,
4202 agent->id, __FUNCTION__, s, group->id);
4203 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4204 spoe_handle_processing_error(s, agent, ctx, dir);
4205 spoe_stop_processing(ctx);
4206 return ACT_RET_CONT;
4207 }
4208 return ACT_RET_YIELD;
4209 }
4210 else
4211 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004212}
4213
4214/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4215 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4216 * action should be:
4217 *
4218 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4219 *
4220 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4221 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4222 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4223 * group.
4224 *
4225 * The function returns 1 in success case, otherwise, it returns 0 and err is
4226 * filled.
4227 */
4228static int
4229check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4230{
4231 struct flt_conf *fconf;
4232 struct spoe_config *conf;
4233 struct spoe_agent *agent = NULL;
4234 struct spoe_group *group;
4235 struct spoe_message *msg;
4236 char *engine_id = rule->arg.act.p[0];
4237 char *group_id = rule->arg.act.p[1];
4238 unsigned int where = 0;
4239
4240 switch (rule->from) {
4241 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4242 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4243 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4244 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4245 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4246 default:
4247 memprintf(err,
4248 "internal error, unexpected rule->from=%d, please report this bug!",
4249 rule->from);
4250 goto error;
4251 }
4252
4253 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4254 * <px> */
4255 list_for_each_entry(fconf, &px->filter_configs, list) {
4256 conf = fconf->conf;
4257
4258 /* This is not an SPOE filter */
4259 if (fconf->id != spoe_filter_id)
4260 continue;
4261
4262 /* This is the good engine */
4263 if (!strcmp(conf->id, engine_id)) {
4264 agent = conf->agent;
4265 break;
4266 }
4267 }
4268 if (agent == NULL) {
4269 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4270 engine_id, group_id);
4271 goto error;
4272 }
4273
4274 /* Try to find the right group */
4275 list_for_each_entry(group, &agent->groups, list) {
4276 /* This is the good group */
4277 if (!strcmp(group->id, group_id))
4278 break;
4279 }
4280 if (&group->list == &agent->groups) {
4281 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4282 group_id, engine_id);
4283 goto error;
4284 }
4285
4286 /* Ok, we found the group, we need to check messages and their
4287 * arguments */
4288 list_for_each_entry(msg, &group->messages, by_grp) {
4289 struct spoe_arg *arg;
4290
4291 list_for_each_entry(arg, &msg->args, list) {
4292 if (!(arg->expr->fetch->val & where)) {
4293 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4294 "some args extract information from '%s',"
4295 "none of which is available here ('%s')",
4296 msg->id, group->id, msg->conf.file, msg->conf.line,
4297 sample_ckp_names(arg->expr->fetch->use),
4298 sample_ckp_names(where));
4299 goto error;
4300 }
4301 }
4302 }
4303
4304 free(engine_id);
4305 free(group_id);
4306 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4307 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4308 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4309 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4310 return 1;
4311
4312 error:
4313 free(engine_id);
4314 free(group_id);
4315 return 0;
4316}
4317
4318/* Parse 'send-spoe-group' action following the format:
4319 *
4320 * ... send-spoe-group <engine-id> <group-id>
4321 *
4322 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4323 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4324 * ids are saved and used later, when the rule will be checked.
4325 */
4326static enum act_parse_ret
4327parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4328 struct act_rule *rule, char **err)
4329{
4330 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4331 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4332 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4333 return ACT_RET_PRS_ERR;
4334 }
4335 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4336 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4337
4338 (*orig_arg) += 2;
4339
4340 rule->action = ACT_CUSTOM;
4341 rule->action_ptr = spoe_send_group;
4342 rule->check_ptr = check_send_spoe_group;
4343 return ACT_RET_PRS_OK;
4344}
4345
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004346
4347/* Declare the filter parser for "spoe" keyword */
4348static struct flt_kw_list flt_kws = { "SPOE", { }, {
4349 { "spoe", parse_spoe_flt, NULL },
4350 { NULL, NULL, NULL },
4351 }
4352};
4353
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004354/* Delcate the action parser for "spoe-action" keyword */
4355static struct action_kw_list tcp_req_action_kws = { { }, {
4356 { "send-spoe-group", parse_send_spoe_group },
4357 { /* END */ },
4358 }
4359};
4360static struct action_kw_list tcp_res_action_kws = { { }, {
4361 { "send-spoe-group", parse_send_spoe_group },
4362 { /* END */ },
4363 }
4364};
4365static struct action_kw_list http_req_action_kws = { { }, {
4366 { "send-spoe-group", parse_send_spoe_group },
4367 { /* END */ },
4368 }
4369};
4370static struct action_kw_list http_res_action_kws = { { }, {
4371 { "send-spoe-group", parse_send_spoe_group },
4372 { /* END */ },
4373 }
4374};
4375
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004376__attribute__((constructor))
4377static void __spoe_init(void)
4378{
4379 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004380 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4381 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4382 http_req_keywords_register(&http_req_action_kws);
4383 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004384
Willy Tarreaubafbe012017-11-24 17:34:44 +01004385 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4386 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004387}
4388
4389__attribute__((destructor))
4390static void
4391__spoe_deinit(void)
4392{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004393 pool_destroy(pool_head_spoe_ctx);
4394 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004395}