blob: 1b69ee2589d785a6d0e7589389bc6f2ca87c14b3 [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 Fauletf7e4e7e2016-10-27 22:29:49 +020090
Christopher Faulet42bfa462017-01-04 14:14:19 +010091/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +010092static struct pool_head *pool_head_spoe_ctx = NULL;
93static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020094
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095struct flt_ops spoe_ops;
96
Christopher Faulet8ef75252017-02-20 22:56:03 +010097static int spoe_queue_context(struct spoe_context *ctx);
98static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
99static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100
101/********************************************************************
102 * helper functions/globals
103 ********************************************************************/
104static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200105spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106{
Christopher Faulet11610f32017-09-21 10:23:10 +0200107 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200108 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200109 free(ph->id);
110 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111}
112
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100114spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200115{
Christopher Faulet57583e42017-09-04 15:41:09 +0200116 struct spoe_arg *arg, *argback;
117 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118
119 if (!msg)
120 return;
121 free(msg->id);
122 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200123 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124 release_sample_expr(arg->expr);
125 free(arg->name);
126 LIST_DEL(&arg->list);
127 free(arg);
128 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200129 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
130 LIST_DEL(&acl->list);
131 prune_acl(acl);
132 free(acl);
133 }
134 if (msg->cond) {
135 prune_acl_cond(msg->cond);
136 free(msg->cond);
137 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200138 free(msg);
139}
140
141static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200142spoe_release_group(struct spoe_group *grp)
143{
144 if (!grp)
145 return;
146 free(grp->id);
147 free(grp->conf.file);
148 free(grp);
149}
150
151static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100152spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200153{
Christopher Faulet11610f32017-09-21 10:23:10 +0200154 struct spoe_message *msg, *msgback;
155 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200156 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200157
158 if (!agent)
159 return;
160 free(agent->id);
161 free(agent->conf.file);
162 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100163 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100164 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200165 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
166 LIST_DEL(&msg->list);
167 spoe_release_message(msg);
168 }
169 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
170 LIST_DEL(&grp->list);
171 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200172 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200173 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100174 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200175 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200176 free(agent);
177}
178
179static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100180 [SPOE_FRM_ERR_NONE] = "normal",
181 [SPOE_FRM_ERR_IO] = "I/O error",
182 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
183 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
184 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
185 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
186 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
187 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
188 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
189 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
190 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
191 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100192 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100193 [SPOE_FRM_ERR_RES] = "resource allocation error",
194 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200195};
196
197static const char *spoe_event_str[SPOE_EV_EVENTS] = {
198 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
199 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
200 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
201 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
202 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
203
204 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
205 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
206 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
207};
208
209
210#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
211
212static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100213 [SPOE_CTX_ST_NONE] = "NONE",
214 [SPOE_CTX_ST_READY] = "READY",
215 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
216 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
217 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
218 [SPOE_CTX_ST_DONE] = "DONE",
219 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200220};
221
222static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100223 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
224 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
225 [SPOE_APPCTX_ST_IDLE] = "IDLE",
226 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
227 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
228 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
229 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
230 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
231 [SPOE_APPCTX_ST_EXIT] = "EXIT",
232 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200233};
234
235#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100236
Christopher Faulet8ef75252017-02-20 22:56:03 +0100237/* Used to generates a unique id for an engine. On success, it returns a
238 * allocated string. So it is the caller's reponsibility to release it. If the
239 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100240static char *
241generate_pseudo_uuid()
242{
243 static int init = 0;
244
245 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
246 const char uuid_chr[] = "0123456789ABCDEF-";
247 char *uuid;
248 int i;
249
250 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
251 return NULL;
252
253 if (!init) {
254 srand(now_ms);
255 init = 1;
256 }
257
258 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
259 int r = rand () % 16;
260
261 switch (uuid_fmt[i]) {
262 case 'x' : uuid[i] = uuid_chr[r]; break;
263 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
264 default : uuid[i] = uuid_fmt[i]; break;
265 }
266 }
267 return uuid;
268}
269
Christopher Faulet8ef75252017-02-20 22:56:03 +0100270/* Returns the minimum number of appets alive at a time. This function is used
271 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100272static inline unsigned int
273min_applets_act(struct spoe_agent *agent)
274{
275 unsigned int nbsrv;
276
Christopher Faulet8ef75252017-02-20 22:56:03 +0100277 /* TODO: Add a config parameter to customize this value. Always 0 for
278 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279 if (agent->min_applets)
280 return agent->min_applets;
281
Christopher Faulet8ef75252017-02-20 22:56:03 +0100282 /* Get the number of active servers for the backend */
283 nbsrv = (agent->b.be->srv_act
284 ? agent->b.be->srv_act
285 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100286 return 2*nbsrv;
287}
288
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200289/********************************************************************
290 * Functions that encode/decode SPOE frames
291 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200292/* Helper to get static string length, excluding the terminating null byte */
293#define SLEN(str) (sizeof(str)-1)
294
295/* Predefined key used in HELLO/DISCONNECT frames */
296#define SUPPORTED_VERSIONS_KEY "supported-versions"
297#define VERSION_KEY "version"
298#define MAX_FRAME_SIZE_KEY "max-frame-size"
299#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100300#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100301#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200302#define STATUS_CODE_KEY "status-code"
303#define MSG_KEY "message"
304
305struct spoe_version {
306 char *str;
307 int min;
308 int max;
309};
310
311/* All supported versions */
312static struct spoe_version supported_versions[] = {
313 {"1.0", 1000, 1000},
314 {NULL, 0, 0}
315};
316
317/* Comma-separated list of supported versions */
318#define SUPPORTED_VERSIONS_VAL "1.0"
319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320/* Convert a string to a SPOE version value. The string must follow the format
321 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
322 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200323static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200325{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100326 const char *p, *end;
327 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329 p = str;
330 end = str+len;
331 maj = min = 0;
332 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200333
Christopher Faulet8ef75252017-02-20 22:56:03 +0100334 /* skip leading spaces */
335 while (p < end && isspace(*p))
336 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 /* parse Major number, until the '.' */
339 while (*p != '.') {
340 if (p >= end || *p < '0' || *p > '9')
341 goto out;
342 maj *= 10;
343 maj += (*p - '0');
344 p++;
345 }
346
347 /* check Major version */
348 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200349 goto out;
350
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351 p++; /* skip the '.' */
352 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
353 goto out;
354
355 /* Parse Minor number */
356 while (p < end) {
357 if (*p < '0' || *p > '9')
358 break;
359 min *= 10;
360 min += (*p - '0');
361 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363
364 /* check Minor number */
365 if (min > 999)
366 goto out;
367
368 /* skip trailing spaces */
369 while (p < end && isspace(*p))
370 p++;
371 if (p != end)
372 goto out;
373
374 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375 out:
376 return vsn;
377}
378
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
380 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
381 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384{
Christopher Faulet305c6072017-02-23 16:17:53 +0100385 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100386 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387 char *p, *end;
388 unsigned int flags = SPOE_FRM_FL_FIN;
389 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200390
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 p = frame;
392 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 /* Set Frame type */
395 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200396
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100397 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 memcpy(p, (char *)&flags, 4);
399 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
401 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200403
404 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
405 * and "capabilities" */
406
407 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 sz = SLEN(SUPPORTED_VERSIONS_KEY);
409 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
410 goto too_big;
411
412 *p++ = SPOE_DATA_T_STR;
413 sz = SLEN(SUPPORTED_VERSIONS_VAL);
414 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
415 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200416
417 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100418 sz = SLEN(MAX_FRAME_SIZE_KEY);
419 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
420 goto too_big;
421
422 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200423 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100424 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200425
426 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100427 sz = SLEN(CAPABILITIES_KEY);
428 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100432 chk = get_trash_chunk();
433 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
434 memcpy(chk->str, "pipelining", 10);
435 chk->len += 10;
436 }
437 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
438 if (chk->len) chk->str[chk->len++] = ',';
439 memcpy(chk->str+chk->len, "async", 5);
440 chk->len += 5;
441 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100442 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
443 if (chk->len) chk->str[chk->len++] = ',';
444 memcpy(chk->str+chk->len, "fragmentation", 13);
445 chk->len += 5;
446 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100447 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448 goto too_big;
449
450 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100451 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452 sz = SLEN(ENGINE_ID_KEY);
453 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
454 goto too_big;
455
456 *p++ = SPOE_DATA_T_STR;
457 sz = strlen(agent->engine_id);
458 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
459 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100460 }
461
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 return (p - frame);
463
464 too_big:
465 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
466 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467}
468
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
470 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
471 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200472static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 const char *reason;
476 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100477 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 p = frame;
481 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 /* Set Frame type */
484 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200485
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100486 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 memcpy(p, (char *)&flags, 4);
488 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
490 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 *p++ = 0; *p++ = 0;
492
493 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
494 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
496 /* There are 2 mandatory items: "status-code" and "message" */
497
498 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499 sz = SLEN(STATUS_CODE_KEY);
500 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
501 goto too_big;
502
503 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200504 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
507 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100508 sz = SLEN(MSG_KEY);
509 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
510 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200511
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512 /*Get the message corresponding to the status code */
513 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
514
515 *p++ = SPOE_DATA_T_STR;
516 sz = strlen(reason);
517 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
518 goto too_big;
519
520 return (p - frame);
521
522 too_big:
523 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
524 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525}
526
Christopher Faulet8ef75252017-02-20 22:56:03 +0100527/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
528 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
529 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200530static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100531spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100532 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200533{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100534 char *p, *end;
535 unsigned int stream_id, frame_id;
536 unsigned int flags = SPOE_FRM_FL_FIN;
537 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200538
Christopher Faulet8ef75252017-02-20 22:56:03 +0100539 p = frame;
540 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200541
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100542 stream_id = ctx->stream_id;
543 frame_id = ctx->frame_id;
544
545 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
546 /* The fragmentation is not supported by the applet */
547 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
548 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
549 return -1;
550 }
551 flags = ctx->frag_ctx.flags;
552 }
553
554 /* Set Frame type */
555 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
556
557 /* Set flags */
558 memcpy(p, (char *)&flags, 4);
559 p += 4;
560
561 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200562 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100563 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200564 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100565 goto too_big;
566
567 /* Copy encoded messages, if possible */
568 sz = ctx->buffer->i;
569 if (p + sz >= end)
570 goto too_big;
571 memcpy(p, ctx->buffer->p, sz);
572 p += sz;
573
574 return (p - frame);
575
576 too_big:
577 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
578 return 0;
579}
580
581/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
582 * returns the number of encoded bytes in the frame on success, 0 if an encoding
583 * error occurred and -1 if a fatal error occurred. */
584static int
585spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
586 char *frame, size_t size)
587{
588 char *p, *end;
589 unsigned int stream_id, frame_id;
590 unsigned int flags;
591 size_t sz;
592
593 p = frame;
594 end = frame+size;
595
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 /* <ctx> is null when the stream has aborted the processing of a
597 * fragmented frame. In this case, we must notify the corresponding
598 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100599 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100600 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
602 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
603 }
604 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100605 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100606 stream_id = ctx->stream_id;
607 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 }
609
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100611 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 memcpy(p, (char *)&flags, 4);
615 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200616
617 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200618 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200620 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200622
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100623 if (ctx == NULL)
624 goto end;
625
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628 if (p + sz >= end)
629 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100630 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100632
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100633 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 return (p - frame);
635
636 too_big:
637 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
638 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639}
640
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641/* Decode and process the HELLO frame sent by an agent. It returns the number of
642 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
643 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646{
Christopher Faulet305c6072017-02-23 16:17:53 +0100647 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
648 char *p, *end;
649 int vsn, max_frame_size;
650 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651
652 p = frame;
653 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654
655 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200660
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661 if (size < 7 /* TYPE + METADATA */) {
662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
663 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664 }
665
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100666 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 memcpy((char *)&flags, p, 4);
668 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100670 /* Fragmentation is not supported for HELLO frame */
671 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100673 return -1;
674 }
675
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200676 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 if (*p != 0 || *(p+1) != 0) {
678 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
679 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682
683 /* There are 3 mandatory items: "version", "max-frame-size" and
684 * "capabilities" */
685
686 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100687 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 while (p < end) {
689 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200690 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692
693 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 ret = spoe_decode_buffer(&p, end, &str, &sz);
695 if (ret == -1 || !sz) {
696 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
697 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200698 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 /* Check "version" K/V item */
701 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 int i, type = *p++;
703
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
707 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
713
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200715 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 return -1;
718 }
719 for (i = 0; supported_versions[i].str != NULL; ++i) {
720 if (vsn >= supported_versions[i].min &&
721 vsn <= supported_versions[i].max)
722 break;
723 }
724 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 return -1;
727 }
728 }
729 /* Check "max-frame-size" K/V item */
730 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100731 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732
733 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
735 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
736 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
737 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100738 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
739 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200740 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200741 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
743 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if (sz < MIN_FRAME_SIZE ||
746 sz > SPOE_APPCTX(appctx)->max_frame_size) {
747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 return -1;
749 }
750 max_frame_size = sz;
751 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752 /* Check "capabilities" K/V item */
753 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
756 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100757 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
758 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
759 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
763 return 0;
764 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767 char *delim;
768
769 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
773 str += 10; sz -= 10;
774 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775 flags |= SPOE_APPCTX_FL_PIPELINING;
776 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 else if (sz >= 5 && !strncmp(str, "async", 5)) {
778 str += 5; sz -= 5;
779 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780 flags |= SPOE_APPCTX_FL_ASYNC;
781 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100782 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
783 str += 13; sz -= 13;
784 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100785 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
786 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100787
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 /* Get the next comma or break */
789 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100790 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 delim++;
792 sz -= (delim - str);
793 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100794 }
795 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 else {
797 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 if (spoe_skip_data(&p, end) == -1) {
799 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
800 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200801 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802 }
803 }
804
805 /* Final checks */
806 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200808 return -1;
809 }
810 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812 return -1;
813 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100814 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
815 flags &= ~SPOE_APPCTX_FL_PIPELINING;
816 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
817 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818
Christopher Faulet42bfa462017-01-04 14:14:19 +0100819 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
820 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
821 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822
823 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200824}
825
826/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
827 * bytes on success, 0 if the frame can be ignored and -1 if an error
828 * occurred. */
829static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100833 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834
835 p = frame;
836 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837
838 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 if (size < 7 /* TYPE + METADATA */) {
845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
846 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847 }
848
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100849 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 memcpy((char *)&flags, p, 4);
851 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100853 /* Fragmentation is not supported for DISCONNECT frame */
854 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100856 return -1;
857 }
858
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 if (*p != 0 || *(p+1) != 0) {
861 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
862 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
866 /* There are 2 mandatory items: "status-code" and "message" */
867
868 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 while (p < end) {
870 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200871 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873
874 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 ret = spoe_decode_buffer(&p, end, &str, &sz);
876 if (ret == -1 || !sz) {
877 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
878 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879 }
880
881 /* Check "status-code" K/V item */
882 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884
885 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
887 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
888 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
889 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100890 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
891 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200893 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
895 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 }
899
900 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 else if (!memcmp(str, MSG_KEY, sz)) {
902 int type = *p++;
903
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200904 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 ret = spoe_decode_buffer(&p, end, &str, &sz);
910 if (ret == -1 || sz > 255) {
911 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
912 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100914#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
915 SPOE_APPCTX(appctx)->reason = str;
916 SPOE_APPCTX(appctx)->rlen = sz;
917#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
919 else {
920 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 if (spoe_skip_data(&p, end) == -1) {
922 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
923 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 }
926 }
927
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929}
930
931
Christopher Fauleta1cda022016-12-21 08:58:06 +0100932/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933 * success, 0 if the frame can be ignored and -1 if an error occurred. */
934static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100935spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100936 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100938 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100940 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100942 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943
944 p = frame;
945 end = frame + size;
946 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947
948 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200953
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 if (size < 7 /* TYPE + METADATA */) {
955 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
956 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957 }
958
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 memcpy((char *)&flags, p, 4);
961 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 /* Fragmentation is not supported for now */
964 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100966 return -1;
967 }
968
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200970 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100972 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200974 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100978
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100980 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200981 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 if ((*ctx)->stream_id == (unsigned int)stream_id &&
983 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100984 goto found;
985 }
986 }
987 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100988 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
989 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100990 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100991 goto found;
992 }
993 }
994
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100995 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
996 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
997 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
998
999 /* ABRT bit is set for an unfinished fragmented frame */
1000 if (flags & SPOE_FRM_FL_ABRT) {
1001 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1002 (*ctx)->frag_ctx.spoe_appctx = NULL;
1003 (*ctx)->state = SPOE_CTX_ST_ERROR;
1004 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1005 /* Ignore the payload */
1006 goto end;
1007 }
1008 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1009 /* For now, we ignore the ack */
1010 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1011 return 0;
1012 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001013
Christopher Fauleta1cda022016-12-21 08:58:06 +01001014 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001015 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1016 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001017 " - stream-id=%u - frame-id=%u\n",
1018 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1019 __FUNCTION__, appctx,
1020 (unsigned int)stream_id, (unsigned int)frame_id);
1021
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001022 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001023 return 0;
1024
1025 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001026 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1027 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001028 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001030 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001031
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001032 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001033 len = (end - p);
1034 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1035 SPOE_APPCTX(appctx)->buffer->i = len;
1036 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001037
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001038 /* Transfer the buffer ownership to the SPOE context */
1039 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1040 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001041
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 (*ctx)->state = SPOE_CTX_ST_DONE;
1043
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001044 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001045 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001046 " - ACK frame received"
1047 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1050 (*ctx)->frame_id, flags);
1051 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001052}
1053
Christopher Fauletba7bc162016-11-07 21:07:38 +01001054/* This function is used in cfgparse.c and declared in proto/checks.h. It
1055 * prepare the request to send to agents during a healthcheck. It returns 0 on
1056 * success and -1 if an error occurred. */
1057int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001058spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 struct appctx appctx;
1061 struct spoe_appctx spoe_appctx;
1062 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1063 size_t sz;
1064 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065
Christopher Faulet42bfa462017-01-04 14:14:19 +01001066 memset(&appctx, 0, sizeof(appctx));
1067 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069
1070 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001071 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001072
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1074 end = frame + MAX_FRAME_SIZE;
1075
1076 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1077 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080
Christopher Faulet8ef75252017-02-20 22:56:03 +01001081 /* Add "healthcheck" K/V item */
1082 sz = SLEN(HEALTHCHECK_KEY);
1083 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1084 return -1;
1085 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 *len = frame - buf;
1088 sz = htonl(*len - 4);
1089 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001094 return 0;
1095}
1096
1097/* This function is used in checks.c and declared in proto/checks.h. It decode
1098 * the response received from an agent during a healthcheck. It returns 0 on
1099 * success and -1 if an error occurred. */
1100int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001103 struct appctx appctx;
1104 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 memset(&appctx, 0, sizeof(appctx));
1107 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001110 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001111
Christopher Faulet8ef75252017-02-20 22:56:03 +01001112 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1113 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114 goto error;
1115 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1117 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118
1119 return 0;
1120
1121 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1123 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1124 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001125 return -1;
1126}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001127
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1129 * the frame can be ignored, 1 to retry later, and the frame legnth on
1130 * success. */
1131static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001132spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133{
1134 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135 int ret;
1136 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001137
1138 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1142 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 netint = htonl(framesz);
1144 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001145 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146
1147 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 if (ret == -1) {
1149 retry:
1150 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001151 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001152 }
1153 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154 return -1; /* error */
1155 }
1156 return framesz;
1157}
1158
1159/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1160 * when the frame can be ignored, 1 to retry later and the frame length on
1161 * success. */
1162static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001163spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164{
1165 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001166 int ret;
1167 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168
1169 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171
Willy Tarreau06d80a92017-10-19 14:32:15 +02001172 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 if (ret > 0) {
1174 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001175 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 return -1;
1178 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001179 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180 }
1181 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 if (ret == 0) {
1183 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001185 }
1186 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001187 return -1; /* error */
1188 }
1189 return framesz;
1190}
1191
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001192/********************************************************************
1193 * Functions that manage the SPOE applet
1194 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001195static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001196spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001197{
1198 si_applet_want_get(appctx->owner);
1199 si_applet_want_put(appctx->owner);
1200 appctx_wakeup(appctx);
1201 return 1;
1202}
1203
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204/* Callback function that catches applet timeouts. If a timeout occurred, we set
1205 * <appctx->st1> flag and the SPOE applet is woken up. */
1206static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001207spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208{
1209 struct appctx *appctx = task->context;
1210
1211 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1212 if (tick_is_expired(task->expire, now_ms)) {
1213 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001217 return task;
1218}
1219
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220/* Callback function that releases a SPOE applet. This happens when the
1221 * connection with the agent is closed. */
1222static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001224{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001225 struct stream_interface *si = appctx->owner;
1226 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1227 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001228 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229
1230 if (spoe_appctx == NULL)
1231 return;
1232
1233 appctx->ctx.spoe.ptr = NULL;
1234 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001235
1236 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1237 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1238 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001239
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 /* Remove applet from the list of running applets */
Christopher Faulet24289f22017-09-25 14:48:02 +02001241 agent->rt[tid].applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1243 LIST_DEL(&spoe_appctx->list);
1244 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001245 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001246
Christopher Faulet8ef75252017-02-20 22:56:03 +01001247 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001249 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
Christopher Faulet24289f22017-09-25 14:48:02 +02001250 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001251
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001253 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1254 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001255
1256 si_shutw(si);
1257 si_shutr(si);
1258 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001259 }
1260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Destroy the task attached to this applet */
1262 if (spoe_appctx->task) {
1263 task_delete(spoe_appctx->task);
1264 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001265 }
1266
Christopher Faulet8ef75252017-02-20 22:56:03 +01001267 /* Notify all waiting streams */
1268 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001269 LIST_DEL(&ctx->list);
1270 LIST_INIT(&ctx->list);
1271 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001273 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001274 }
1275
Christopher Faulet8ef75252017-02-20 22:56:03 +01001276 /* If the applet was processing a fragmented frame, notify the
1277 * corresponding stream. */
1278 if (spoe_appctx->frag_ctx.ctx) {
1279 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001280 ctx->frag_ctx.spoe_appctx = NULL;
1281 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001283 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1284 }
1285
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 /* Release allocated memory */
1287 spoe_release_buffer(&spoe_appctx->buffer,
1288 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001289 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001290
Christopher Faulet24289f22017-09-25 14:48:02 +02001291 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001292 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001295 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001296 LIST_DEL(&ctx->list);
1297 LIST_INIT(&ctx->list);
1298 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001299 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001301 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001302 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001303 LIST_DEL(&ctx->list);
1304 LIST_INIT(&ctx->list);
1305 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001306 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1308 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001309
1310 end:
1311 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001312 agent->rt[tid].frame_size = agent->max_frame_size;
1313 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1314 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001315}
1316
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001317static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001318spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001319{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001321 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001322 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001323 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001324
Christopher Fauleta1cda022016-12-21 08:58:06 +01001325 if (si->state <= SI_ST_CON) {
1326 si_applet_want_put(si);
1327 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1328 goto stop;
1329 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001330 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001331 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001332 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001333 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001334
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001336 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1337 " - Connection timed out\n",
1338 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1339 __FUNCTION__, appctx);
1340 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001342 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343
Christopher Faulet42bfa462017-01-04 14:14:19 +01001344 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345 SPOE_APPCTX(appctx)->task->expire =
1346 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1349 * length. */
1350 buf = trash.str; frame = buf+4;
1351 ret = spoe_prepare_hahello_frame(appctx, frame,
1352 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355
1356 switch (ret) {
1357 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 case 0: /* ignore => an error, cannot be ignored */
1359 goto exit;
1360
1361 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001362 goto stop;
1363
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 default:
1365 /* HELLO frame successfully sent, now wait for the
1366 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001367 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1368 goto next;
1369 }
1370
1371 next:
1372 return 0;
1373 stop:
1374 return 1;
1375 exit:
1376 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1377 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378}
1379
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001380static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001382{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001384 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 char *frame;
1386 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001387
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauletb067b062017-01-04 16:39:11 +01001389 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001390 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001392 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1396 " - Connection timed out\n",
1397 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1398 __FUNCTION__, appctx);
1399 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 goto exit;
1401 }
1402
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 frame = trash.str; trash.len = 0;
1404 ret = spoe_recv_frame(appctx, frame,
1405 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (ret > 1) {
1407 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1408 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1409 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 trash.len = ret + 4;
1412 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 switch (ret) {
1416 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1419 goto next;
1420
1421 case 1: /* retry later */
1422 goto stop;
1423
1424 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 /* HELLO handshake is finished, set the idle timeout and
1426 * add the applet in the list of running applets. */
Christopher Faulet24289f22017-09-25 14:48:02 +02001427 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001429 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001430 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001431 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001432 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001433
1434 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001435 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001437 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001438
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001440 /* Do not forget to remove processed frame from the output buffer */
1441 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001442 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001443
1444 SPOE_APPCTX(appctx)->task->expire =
1445 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001446 return 0;
1447 stop:
1448 return 1;
1449 exit:
1450 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1451 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452}
1453
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001454
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001456spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001458 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1459 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 char *frame, *buf;
1461 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001462
Christopher Faulet8ef75252017-02-20 22:56:03 +01001463 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1464 * length. */
1465 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001466
1467 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1468 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1469 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1470 SPOE_APPCTX(appctx)->max_frame_size);
1471 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001472 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473 *skip = 1;
1474 ret = 1;
1475 goto end;
1476 }
1477 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001478 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001479 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1480 SPOE_APPCTX(appctx)->max_frame_size);
1481
1482 }
1483
Christopher Faulet8ef75252017-02-20 22:56:03 +01001484 if (ret > 1)
1485 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486
Christopher Faulet8ef75252017-02-20 22:56:03 +01001487 switch (ret) {
1488 case -1: /* error */
1489 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1490 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001491
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 case 0: /* ignore */
1493 if (ctx == NULL)
1494 goto abort_frag_frame;
1495
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001496 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001497 LIST_DEL(&ctx->list);
1498 LIST_INIT(&ctx->list);
1499 ctx->state = SPOE_CTX_ST_ERROR;
1500 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1501 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1502 break;
1503
1504 case 1: /* retry */
1505 *skip = 1;
1506 break;
1507
1508 default:
1509 if (ctx == NULL)
1510 goto abort_frag_frame;
1511
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001512 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001513 LIST_DEL(&ctx->list);
1514 LIST_INIT(&ctx->list);
1515 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1516 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1517 goto no_frag_frame_sent;
1518 else {
1519 *skip = 1;
1520 goto frag_frame_sent;
1521 }
1522 }
1523 goto end;
1524
1525 frag_frame_sent:
1526 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1527 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1528 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1529 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1530
1531 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1532 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1533 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1534 goto end;
1535
1536 no_frag_frame_sent:
1537 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1538 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001539 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 }
1541 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1542 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1543 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1544 }
1545 else {
1546 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1547 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1548 }
1549 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1550 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1551 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1552
1553 ctx->frag_ctx.spoe_appctx = NULL;
1554 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1555 goto end;
1556
1557 abort_frag_frame:
1558 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1559 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1560 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1561 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1562 goto end;
1563
1564 end:
1565 return ret;
1566}
1567
1568static int
1569spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1570{
1571 struct spoe_context *ctx = NULL;
1572 char *frame;
1573 int ret;
1574
1575 frame = trash.str; trash.len = 0;
1576 ret = spoe_recv_frame(appctx, frame,
1577 SPOE_APPCTX(appctx)->max_frame_size);
1578 if (ret > 1) {
1579 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1580 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1581 goto end;
1582 }
1583 trash.len = ret + 4;
1584 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1585 }
1586 switch (ret) {
1587 case -1: /* error */
1588 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1589 break;
1590
1591 case 0: /* ignore */
1592 break;
1593
1594 case 1: /* retry */
1595 *skip = 1;
1596 break;
1597
1598 default:
1599 LIST_DEL(&ctx->list);
1600 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001601
1602 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1603 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1604 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1605 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1606 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1607 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1608 }
1609 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1610 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1611
Christopher Faulet8ef75252017-02-20 22:56:03 +01001612 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1613 break;
1614 }
1615
1616 /* Do not forget to remove processed frame from the output buffer */
1617 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001618 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001619 end:
1620 return ret;
1621}
1622
1623static int
1624spoe_handle_processing_appctx(struct appctx *appctx)
1625{
1626 struct stream_interface *si = appctx->owner;
1627 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 unsigned int fpa = 0;
1629 int ret, skip_sending = 0, skip_receiving = 0;
1630
1631 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1632 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1633 goto exit;
1634 }
1635
1636 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1637 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1638 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1639 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1640 goto next;
1641 }
1642
1643 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001644 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1645 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1646 " - appctx-state=%s\n",
1647 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1648 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001649 skip_sending, skip_receiving,
1650 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001651
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001652 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001653 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001654 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001655 if (skip_receiving)
1656 goto stop;
1657 goto recv_frame;
1658 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001659
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001660 /* send_frame */
1661 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001662 switch (ret) {
1663 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001664 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001665
Christopher Fauleta1cda022016-12-21 08:58:06 +01001666 case 0: /* ignore */
Christopher Faulet24289f22017-09-25 14:48:02 +02001667 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001668 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001669 break;
1670
Christopher Fauleta1cda022016-12-21 08:58:06 +01001671 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001672 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001673
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 default:
Christopher Faulet24289f22017-09-25 14:48:02 +02001675 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001676 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001677 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001678 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679 if (fpa > agent->max_fpa)
1680 goto stop;
1681
1682 recv_frame:
1683 if (skip_receiving)
1684 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001685 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686 switch (ret) {
1687 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001688 goto next;
1689
1690 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001691 fpa++;
1692 break;
1693
1694 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 break;
1696
1697 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001698 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001700 }
1701 goto process;
1702
1703 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001704 SPOE_APPCTX(appctx)->task->expire =
1705 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 return 0;
1707 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001708 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001709 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet24289f22017-09-25 14:48:02 +02001710 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001711 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001712 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001713 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001714 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001715 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001716 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001718 SPOE_APPCTX(appctx)->task->expire =
1719 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720 }
1721 return 1;
1722
1723 exit:
1724 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1725 return 0;
1726}
1727
1728static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001729spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001730{
1731 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001732 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001733 char *frame, *buf;
1734 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001735
Christopher Fauleta1cda022016-12-21 08:58:06 +01001736 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1737 goto exit;
1738
1739 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1740 goto exit;
1741
Christopher Faulet8ef75252017-02-20 22:56:03 +01001742 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1743 * length. */
1744 buf = trash.str; frame = buf+4;
1745 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1746 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749
1750 switch (ret) {
1751 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001752 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753 goto exit;
1754
1755 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 goto stop;
1757
1758 default:
1759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1760 " - disconnected by HAProxy (%d): %s\n",
1761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 __FUNCTION__, appctx,
1763 SPOE_APPCTX(appctx)->status_code,
1764 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765
1766 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1767 goto next;
1768 }
1769
1770 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 SPOE_APPCTX(appctx)->task->expire =
1772 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 return 0;
1774 stop:
1775 return 1;
1776 exit:
1777 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1778 return 0;
1779}
1780
1781static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783{
1784 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001785 char *frame;
1786 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787
Christopher Fauletb067b062017-01-04 16:39:11 +01001788 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001791 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792
Christopher Fauletb067b062017-01-04 16:39:11 +01001793 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001796 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 frame = trash.str; trash.len = 0;
1799 ret = spoe_recv_frame(appctx, frame,
1800 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802 trash.len = ret + 4;
1803 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 }
1805
1806 switch (ret) {
1807 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001808 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1809 " - error on frame (%s)\n",
1810 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001811 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 goto exit;
1815
1816 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 goto next;
1818
1819 case 1: /* retry */
1820 goto stop;
1821
1822 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001826 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1828 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 goto exit;
1830 }
1831
1832 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 /* Do not forget to remove processed frame from the output buffer */
1834 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001835 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 return 0;
1838 stop:
1839 return 1;
1840 exit:
1841 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1842 return 0;
1843}
1844
1845/* I/O Handler processing messages exchanged with the agent */
1846static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001849 struct stream_interface *si = appctx->owner;
1850 struct spoe_agent *agent;
1851
1852 if (SPOE_APPCTX(appctx) == NULL)
1853 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001854
Christopher Faulet8ef75252017-02-20 22:56:03 +01001855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1856 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001857
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 switchstate:
1859 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1860 " - appctx-state=%s\n",
1861 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1862 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1863
1864 switch (appctx->st0) {
1865 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 goto out;
1868 goto switchstate;
1869
1870 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001872 goto out;
1873 goto switchstate;
1874
1875 case SPOE_APPCTX_ST_IDLE:
1876 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001877 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001878 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001879 SPOE_APPCTX(appctx)->task->expire =
1880 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001882 goto switchstate;
1883 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001884 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1886 /* fall through */
1887
1888 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001889 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1890 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001891 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 goto out;
1893 goto switchstate;
1894
1895 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001896 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897 goto out;
1898 goto switchstate;
1899
1900 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001901 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001902 goto out;
1903 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001904
1905 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001906 appctx->st0 = SPOE_APPCTX_ST_END;
1907 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1908
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001909 si_shutw(si);
1910 si_shutr(si);
1911 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001912 /* fall through */
1913
1914 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001915 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001918 if (stopping)
1919 spoe_wakeup_appctx(appctx);
1920
Christopher Faulet42bfa462017-01-04 14:14:19 +01001921 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1922 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001923 si_oc(si)->flags |= CF_READ_DONTWAIT;
1924 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925}
1926
1927struct applet spoe_applet = {
1928 .obj_type = OBJ_TYPE_APPLET,
1929 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 .fct = spoe_handle_appctx,
1931 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932};
1933
1934/* Create a SPOE applet. On success, the created applet is returned, else
1935 * NULL. */
1936static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001937spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938{
1939 struct appctx *appctx;
1940 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942
Emeric Brun1138fd02017-06-19 12:38:55 +02001943 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 goto out_error;
1945
Willy Tarreaubafbe012017-11-24 17:34:44 +01001946 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001947 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001949 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001950
Christopher Faulet42bfa462017-01-04 14:14:19 +01001951 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001952 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001953 goto out_free_spoe_appctx;
1954
1955 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001956 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001957 SPOE_APPCTX(appctx)->task->context = appctx;
1958 SPOE_APPCTX(appctx)->agent = conf->agent;
1959 SPOE_APPCTX(appctx)->version = 0;
1960 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1961 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001963 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1964
1965 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1966 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001967 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001968
1969 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1970 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971
Willy Tarreau5820a362016-12-22 15:59:02 +01001972 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973 if (!sess)
1974 goto out_free_spoe;
1975
Willy Tarreau87787ac2017-08-28 16:22:54 +02001976 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977 goto out_free_sess;
1978
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979 stream_set_backend(strm, conf->agent->b.be);
1980
1981 /* applet is waiting for data */
1982 si_applet_cant_get(&strm->si[0]);
1983 appctx_wakeup(appctx);
1984
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985 strm->do_log = NULL;
1986 strm->res.flags |= CF_READ_DONTWAIT;
1987
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001988 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001989 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001990 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001991 conf->agent->rt[tid].applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001992
Emeric Brunc60def82017-09-27 14:59:38 +02001993 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001994 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995 return appctx;
1996
1997 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998 out_free_sess:
1999 session_free(sess);
2000 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002001 task_free(SPOE_APPCTX(appctx)->task);
2002 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002003 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004 out_free_appctx:
2005 appctx_free(appctx);
2006 out_error:
2007 return NULL;
2008}
2009
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002010static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002011spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012{
2013 struct spoe_config *conf = FLT_CONF(ctx->filter);
2014 struct spoe_agent *agent = conf->agent;
2015 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002016 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002017 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018
Christopher Fauleta1cda022016-12-21 08:58:06 +01002019 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020
Christopher Fauleta1cda022016-12-21 08:58:06 +01002021 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002022 if (agent->rt[tid].applets_act >= min_applets &&
2023 agent->rt[tid].applets_idle &&
2024 agent->rt[tid].sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026
2027 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002028 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2030 ctx->strm);
2031
Christopher Fauleta1cda022016-12-21 08:58:06 +01002032 /* Do not try to create a new applet if there is no server up for the
2033 * agent's backend. */
2034 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2035 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2036 " - cannot create SPOE appctx: no server up\n",
2037 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2038 __FUNCTION__, ctx->strm);
2039 goto end;
2040 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 /* Do not try to create a new applet if we have reached the maximum of
2043 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002044 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002045 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2047 " - cannot create SPOE appctx: max CPS reached\n",
2048 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2049 __FUNCTION__, ctx->strm);
2050 goto end;
2051 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002052 }
2053
Christopher Faulet8ef75252017-02-20 22:56:03 +01002054 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 if (appctx == NULL) {
2056 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2057 " - failed to create SPOE appctx\n",
2058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2059 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002060 send_log(ctx->strm->be, LOG_EMERG,
2061 "SPOE: [%s] failed to create SPOE applet\n",
2062 agent->id);
2063
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065 }
Christopher Faulet24289f22017-09-25 14:48:02 +02002066 if (agent->rt[tid].applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002067 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 /* Increase the per-process number of cumulated connections */
2070 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002071 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072
Christopher Fauleta1cda022016-12-21 08:58:06 +01002073 end:
2074 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002075 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002076 ctx->status_code = SPOE_CTX_ERR_RES;
2077 return -1;
2078 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 /* Add the SPOE context in the sending queue and update all running
2081 * info */
Christopher Faulet24289f22017-09-25 14:48:02 +02002082 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
2083 if (agent->rt[tid].sending_rate)
2084 agent->rt[tid].sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085
2086 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002087 " - Add stream in sending queue"
2088 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002090 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
2091 agent->rt[tid].sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002092
Christopher Fauleta1cda022016-12-21 08:58:06 +01002093 /* Finally try to wakeup the first IDLE applet found and move it at the
2094 * end of the list. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002095 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) {
Christopher Faulet42bfa462017-01-04 14:14:19 +01002096 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002097 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002098 spoe_wakeup_appctx(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002099 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002100 LIST_DEL(&spoe_appctx->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02002101 LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002102 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002103 break;
2104 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002105 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002106 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107}
2108
2109/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002110 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002111 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002112/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2113 * fragmented_content. If the next message can be processed, it returns 0. If
2114 * the message is too big, it returns -1.*/
2115static int
2116spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2117 struct spoe_message *msg, int dir,
2118 char **buf, char *end)
2119{
2120 struct sample *smp;
2121 struct spoe_arg *arg;
2122 int ret;
2123
2124 if (msg->cond) {
2125 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2126 ret = acl_pass(ret);
2127 if (msg->cond->pol == ACL_COND_UNLESS)
2128 ret = !ret;
2129
2130 /* the rule does not match */
2131 if (!ret)
2132 goto next;
2133 }
2134
2135 /* Resume encoding of a SPOE argument */
2136 if (ctx->frag_ctx.curarg != NULL) {
2137 arg = ctx->frag_ctx.curarg;
2138 goto encode_argument;
2139 }
2140
2141 if (ctx->frag_ctx.curoff != UINT_MAX)
2142 goto encode_msg_payload;
2143
2144 /* Check if there is enough space for the message name and the
2145 * number of arguments. It implies <msg->id_len> is encoded on 2
2146 * bytes, at most (< 2288). */
2147 if (*buf + 2 + msg->id_len + 1 > end)
2148 goto too_big;
2149
2150 /* Encode the message name */
2151 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2152 goto too_big;
2153
2154 /* Set the number of arguments for this message */
2155 **buf = msg->nargs;
2156 (*buf)++;
2157
2158 ctx->frag_ctx.curoff = 0;
2159 encode_msg_payload:
2160
2161 /* Loop on arguments */
2162 list_for_each_entry(arg, &msg->args, list) {
2163 ctx->frag_ctx.curarg = arg;
2164 ctx->frag_ctx.curoff = UINT_MAX;
2165
2166 encode_argument:
2167 if (ctx->frag_ctx.curoff != UINT_MAX)
2168 goto encode_arg_value;
2169
2170 /* Encode the arguement name as a string. It can by NULL */
2171 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2172 goto too_big;
2173
2174 ctx->frag_ctx.curoff = 0;
2175 encode_arg_value:
2176
2177 /* Fetch the arguement value */
2178 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2179 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2180 if (ret == -1 || ctx->frag_ctx.curoff)
2181 goto too_big;
2182 }
2183
2184 next:
2185 return 0;
2186
2187 too_big:
2188 return -1;
2189}
2190
Christopher Fauletc718b822017-09-21 16:50:56 +02002191/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2192 * handle fragmented content. On success it returns 1. If an error occurred, -1
2193 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2194 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002195static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002196spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002197 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002198{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002199 struct spoe_config *conf = FLT_CONF(ctx->filter);
2200 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002201 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002202 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002203
Christopher Faulet8ef75252017-02-20 22:56:03 +01002204 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002205 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206
Christopher Fauletc718b822017-09-21 16:50:56 +02002207 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2208 /* Resume encoding of a SPOE message */
2209 if (ctx->frag_ctx.curmsg != NULL) {
2210 msg = ctx->frag_ctx.curmsg;
2211 goto encode_evt_message;
2212 }
2213
2214 list_for_each_entry(msg, messages, by_evt) {
2215 ctx->frag_ctx.curmsg = msg;
2216 ctx->frag_ctx.curarg = NULL;
2217 ctx->frag_ctx.curoff = UINT_MAX;
2218
2219 encode_evt_message:
2220 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2221 goto too_big;
2222 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002223 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002224 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2225 /* Resume encoding of a SPOE message */
2226 if (ctx->frag_ctx.curmsg != NULL) {
2227 msg = ctx->frag_ctx.curmsg;
2228 goto encode_grp_message;
2229 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002230
Christopher Fauletc718b822017-09-21 16:50:56 +02002231 list_for_each_entry(msg, messages, by_grp) {
2232 ctx->frag_ctx.curmsg = msg;
2233 ctx->frag_ctx.curarg = NULL;
2234 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235
Christopher Fauletc718b822017-09-21 16:50:56 +02002236 encode_grp_message:
2237 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2238 goto too_big;
2239 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002240 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002241 else
2242 goto skip;
2243
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244
Christopher Faulet57583e42017-09-04 15:41:09 +02002245 /* nothing has been encoded for an unfragmented payload */
2246 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2247 goto skip;
2248
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002250 " - encode %s messages - spoe_appctx=%p"
2251 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252 (int)now.tv_sec, (int)now.tv_usec,
2253 agent->id, __FUNCTION__, s,
2254 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet24289f22017-09-25 14:48:02 +02002255 ctx->frag_ctx.spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002256 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259 ctx->frag_ctx.curmsg = NULL;
2260 ctx->frag_ctx.curarg = NULL;
2261 ctx->frag_ctx.curoff = 0;
2262 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002263
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264 return 1;
2265
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002267 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2268 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2269 return -1;
2270 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002271
2272 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002273 " - encode fragmented messages - spoe_appctx=%p"
2274 " - curmsg=%p - curarg=%p - curoff=%u"
2275 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002276 (int)now.tv_sec, (int)now.tv_usec,
2277 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2278 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002279 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280
Christopher Faulet8ef75252017-02-20 22:56:03 +01002281 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2283 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2284 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002285
2286 skip:
2287 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2288 " - skip the frame because nothing has been encoded\n",
2289 (int)now.tv_sec, (int)now.tv_usec,
2290 agent->id, __FUNCTION__, s);
2291 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002292}
2293
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294
2295/***************************************************************************
2296 * Functions that handle SPOE actions
2297 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298/* Helper function to set a variable */
2299static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002300spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301 struct sample *smp)
2302{
2303 struct spoe_config *conf = FLT_CONF(ctx->filter);
2304 struct spoe_agent *agent = conf->agent;
2305 char varname[64];
2306
2307 memset(varname, 0, sizeof(varname));
2308 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2309 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002310 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2311 vars_set_by_name(varname, len, smp);
2312 else
2313 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002314}
2315
2316/* Helper function to unset a variable */
2317static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002318spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002319 struct sample *smp)
2320{
2321 struct spoe_config *conf = FLT_CONF(ctx->filter);
2322 struct spoe_agent *agent = conf->agent;
2323 char varname[64];
2324
2325 memset(varname, 0, sizeof(varname));
2326 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2327 scope, agent->var_pfx, len, name);
2328 vars_unset_by_name_ifexist(varname, len, smp);
2329}
2330
2331
Christopher Faulet8ef75252017-02-20 22:56:03 +01002332static inline int
2333spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2334 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002335{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002336 char *str, *scope, *p = *buf;
2337 struct sample smp;
2338 uint64_t sz;
2339 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002340
Christopher Faulet8ef75252017-02-20 22:56:03 +01002341 if (p + 2 >= end)
2342 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 /* SET-VAR requires 3 arguments */
2345 if (*p++ != 3)
2346 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 switch (*p++) {
2349 case SPOE_SCOPE_PROC: scope = "proc"; break;
2350 case SPOE_SCOPE_SESS: scope = "sess"; break;
2351 case SPOE_SCOPE_TXN : scope = "txn"; break;
2352 case SPOE_SCOPE_REQ : scope = "req"; break;
2353 case SPOE_SCOPE_RES : scope = "res"; break;
2354 default: goto skip;
2355 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002356
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2358 goto skip;
2359 memset(&smp, 0, sizeof(smp));
2360 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002361
Christopher Faulet8ef75252017-02-20 22:56:03 +01002362 if (spoe_decode_data(&p, end, &smp) == -1)
2363 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2366 " - set-var '%s.%s.%.*s'\n",
2367 (int)now.tv_sec, (int)now.tv_usec,
2368 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2369 __FUNCTION__, s, scope,
2370 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2371 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372
Christopher Faulet8ef75252017-02-20 22:56:03 +01002373 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 ret = (p - *buf);
2376 *buf = p;
2377 return ret;
2378 skip:
2379 return 0;
2380}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002381
Christopher Faulet8ef75252017-02-20 22:56:03 +01002382static inline int
2383spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2384 char **buf, char *end, int dir)
2385{
2386 char *str, *scope, *p = *buf;
2387 struct sample smp;
2388 uint64_t sz;
2389 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002390
Christopher Faulet8ef75252017-02-20 22:56:03 +01002391 if (p + 2 >= end)
2392 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002393
Christopher Faulet8ef75252017-02-20 22:56:03 +01002394 /* UNSET-VAR requires 2 arguments */
2395 if (*p++ != 2)
2396 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002397
Christopher Faulet8ef75252017-02-20 22:56:03 +01002398 switch (*p++) {
2399 case SPOE_SCOPE_PROC: scope = "proc"; break;
2400 case SPOE_SCOPE_SESS: scope = "sess"; break;
2401 case SPOE_SCOPE_TXN : scope = "txn"; break;
2402 case SPOE_SCOPE_REQ : scope = "req"; break;
2403 case SPOE_SCOPE_RES : scope = "res"; break;
2404 default: goto skip;
2405 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002406
Christopher Faulet8ef75252017-02-20 22:56:03 +01002407 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2408 goto skip;
2409 memset(&smp, 0, sizeof(smp));
2410 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2413 " - unset-var '%s.%s.%.*s'\n",
2414 (int)now.tv_sec, (int)now.tv_usec,
2415 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2416 __FUNCTION__, s, scope,
2417 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2418 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002419
Christopher Faulet8ef75252017-02-20 22:56:03 +01002420 spoe_unset_var(ctx, scope, str, sz, &smp);
2421
2422 ret = (p - *buf);
2423 *buf = p;
2424 return ret;
2425 skip:
2426 return 0;
2427}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet8ef75252017-02-20 22:56:03 +01002429/* Process SPOE actions for a specific event. It returns 1 on success. If an
2430 * error occurred, 0 is returned. */
2431static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002432spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002433{
2434 char *p, *end;
2435 int ret;
2436
2437 p = ctx->buffer->p;
2438 end = p + ctx->buffer->i;
2439
2440 while (p < end) {
2441 enum spoe_action_type type;
2442
2443 type = *p++;
2444 switch (type) {
2445 case SPOE_ACT_T_SET_VAR:
2446 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2447 if (!ret)
2448 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002449 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451 case SPOE_ACT_T_UNSET_VAR:
2452 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2453 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002454 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002455 break;
2456
2457 default:
2458 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459 }
2460 }
2461
2462 return 1;
2463 skip:
2464 return 0;
2465}
2466
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002467/***************************************************************************
2468 * Functions that process SPOE events
2469 **************************************************************************/
2470static inline int
Christopher Faulet58d03682017-09-21 16:57:24 +02002471spoe_start_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002472{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002473 /* If a process is already started for this SPOE context, retry
2474 * later. */
2475 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002476 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002477
2478 /* Set the right flag to prevent request and response processing
2479 * in same time. */
2480 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2481 ? SPOE_CTX_FL_REQ_PROCESS
2482 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002483 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002484}
2485
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002486static inline void
Christopher Faulet58d03682017-09-21 16:57:24 +02002487spoe_stop_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002488{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002489 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2490
2491 if (sa) {
2492 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002493 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002494 }
2495
Christopher Fauleta1cda022016-12-21 08:58:06 +01002496 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002497 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002498
Christopher Fauletb067b062017-01-04 16:39:11 +01002499 ctx->status_code = 0;
2500
Christopher Fauleta1cda022016-12-21 08:58:06 +01002501 /* Reset processing timer */
2502 ctx->process_exp = TICK_ETERNITY;
2503
Christopher Faulet8ef75252017-02-20 22:56:03 +01002504 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002505
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002506 ctx->frag_ctx.spoe_appctx = NULL;
2507 ctx->frag_ctx.curmsg = NULL;
2508 ctx->frag_ctx.curarg = NULL;
2509 ctx->frag_ctx.curoff = 0;
2510 ctx->frag_ctx.flags = 0;
2511
Christopher Fauleta1cda022016-12-21 08:58:06 +01002512 if (!LIST_ISEMPTY(&ctx->list)) {
2513 LIST_DEL(&ctx->list);
2514 LIST_INIT(&ctx->list);
2515 }
2516}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002517
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002518static void
2519spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2520 struct spoe_context *ctx, int dir)
2521{
2522 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002523 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002524
2525 if (agent->var_on_error) {
2526 struct sample smp;
2527
2528 memset(&smp, 0, sizeof(smp));
2529 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2530 smp.data.u.sint = ctx->status_code;
2531 smp.data.type = SMP_T_BOOL;
2532
2533 spoe_set_var(ctx, "txn", agent->var_on_error,
2534 strlen(agent->var_on_error), &smp);
2535 }
2536 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2537 " - failed to process messages: code=%u\n",
2538 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2539 __FUNCTION__, s, ctx->status_code);
2540 send_log(ctx->strm->be, LOG_WARNING,
2541 "SPOE: [%s] failed to process messages: code=%u\n",
2542 agent->id, ctx->status_code);
2543
2544 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2545 ? SPOE_CTX_ST_READY
2546 : SPOE_CTX_ST_NONE);
2547}
2548
Christopher Faulet58d03682017-09-21 16:57:24 +02002549/* Process a list of SPOE messages. First, this functions will process messages
2550 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2551 * to process corresponding actions. During all the processing, it returns 0
2552 * and it returns 1 when the processing is finished. If an error occurred, -1
2553 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002554static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002555spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2556 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002557{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002558 struct spoe_config *conf = FLT_CONF(ctx->filter);
2559 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002560 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002561
2562 if (ctx->state == SPOE_CTX_ST_ERROR)
2563 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002564
2565 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2566 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002567 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002568 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002569 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002570 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002571 goto error;
2572 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002573
2574 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002575 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002576 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002577 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002578 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002579 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002580 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581 goto skip;
2582 }
2583 }
2584
Christopher Fauletf7a30922016-11-10 15:04:51 +01002585 if (!tick_isset(ctx->process_exp)) {
2586 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2587 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2588 ctx->process_exp);
2589 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002590 ret = spoe_start_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002591 if (!ret)
2592 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593
Christopher Faulet8ef75252017-02-20 22:56:03 +01002594 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002595 goto error;
2596
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002597 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002599 }
2600
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002601 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002602 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002603 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002604 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605 if (ret < 0)
2606 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002607 if (!ret)
2608 goto skip;
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 Fauletf7e4e7e2016-10-27 22:29:49 +02003486 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003487 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3488 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003489 err_code |= ERR_ALERT | ERR_FATAL;
3490 goto out;
3491 }
3492 out:
3493 return err_code;
3494}
Christopher Faulet11610f32017-09-21 10:23:10 +02003495static int
3496cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3497{
3498 struct spoe_group *grp;
3499 const char *err;
3500 int err_code = 0;
3501
3502 if ((cfg_scope == NULL && curengine != NULL) ||
3503 (cfg_scope != NULL && curengine == NULL) ||
3504 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3505 goto out;
3506
3507 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3508 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003509 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3510 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003511 err_code |= ERR_ALERT | ERR_ABORT;
3512 goto out;
3513 }
3514 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3515 err_code |= ERR_ABORT;
3516 goto out;
3517 }
3518
3519 err = invalid_char(args[1]);
3520 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003521 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3522 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003523 err_code |= ERR_ALERT | ERR_ABORT;
3524 goto out;
3525 }
3526
3527 list_for_each_entry(grp, &curgrps, list) {
3528 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003529 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3530 " name as another one declared at %s:%d.\n",
3531 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003532 err_code |= ERR_ALERT | ERR_FATAL;
3533 goto out;
3534 }
3535 }
3536
3537 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003538 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003539 err_code |= ERR_ALERT | ERR_ABORT;
3540 goto out;
3541 }
3542
3543 curgrp->id = strdup(args[1]);
3544 curgrp->conf.file = strdup(file);
3545 curgrp->conf.line = linenum;
3546 LIST_INIT(&curgrp->phs);
3547 LIST_INIT(&curgrp->messages);
3548 LIST_ADDQ(&curgrps, &curgrp->list);
3549 }
3550 else if (!strcmp(args[0], "messages")) {
3551 int cur_arg = 1;
3552 while (*args[cur_arg]) {
3553 struct spoe_placeholder *ph = NULL;
3554
3555 list_for_each_entry(ph, &curgrp->phs, list) {
3556 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003557 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3558 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003559 err_code |= ERR_ALERT | ERR_FATAL;
3560 goto out;
3561 }
3562 }
3563
3564 if ((ph = calloc(1, sizeof(*ph))) == 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 ph->id = strdup(args[cur_arg]);
3570 LIST_ADDQ(&curgrp->phs, &ph->list);
3571 cur_arg++;
3572 }
3573 }
3574 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003575 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3576 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003577 err_code |= ERR_ALERT | ERR_FATAL;
3578 goto out;
3579 }
3580 out:
3581 return err_code;
3582}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003583
3584static int
3585cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3586{
3587 struct spoe_message *msg;
3588 struct spoe_arg *arg;
3589 const char *err;
3590 char *errmsg = NULL;
3591 int err_code = 0;
3592
3593 if ((cfg_scope == NULL && curengine != NULL) ||
3594 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003595 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003596 goto out;
3597
3598 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3599 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003600 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3601 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003602 err_code |= ERR_ALERT | ERR_ABORT;
3603 goto out;
3604 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003605 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3606 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003607 goto out;
3608 }
3609
3610 err = invalid_char(args[1]);
3611 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003612 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3613 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003614 err_code |= ERR_ALERT | ERR_ABORT;
3615 goto out;
3616 }
3617
3618 list_for_each_entry(msg, &curmsgs, list) {
3619 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003620 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3621 " name as another one declared at %s:%d.\n",
3622 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003623 err_code |= ERR_ALERT | ERR_FATAL;
3624 goto out;
3625 }
3626 }
3627
3628 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003629 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003630 err_code |= ERR_ALERT | ERR_ABORT;
3631 goto out;
3632 }
3633
3634 curmsg->id = strdup(args[1]);
3635 curmsg->id_len = strlen(curmsg->id);
3636 curmsg->event = SPOE_EV_NONE;
3637 curmsg->conf.file = strdup(file);
3638 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003639 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003640 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003641 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003642 LIST_INIT(&curmsg->by_evt);
3643 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003644 LIST_ADDQ(&curmsgs, &curmsg->list);
3645 }
3646 else if (!strcmp(args[0], "args")) {
3647 int cur_arg = 1;
3648
3649 curproxy->conf.args.ctx = ARGC_SPOE;
3650 curproxy->conf.args.file = file;
3651 curproxy->conf.args.line = linenum;
3652 while (*args[cur_arg]) {
3653 char *delim = strchr(args[cur_arg], '=');
3654 int idx = 0;
3655
3656 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003657 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003658 err_code |= ERR_ALERT | ERR_ABORT;
3659 goto out;
3660 }
3661
3662 if (!delim) {
3663 arg->name = NULL;
3664 arg->name_len = 0;
3665 delim = args[cur_arg];
3666 }
3667 else {
3668 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3669 arg->name_len = delim - args[cur_arg];
3670 delim++;
3671 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003672 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3673 &idx, file, linenum, &errmsg,
3674 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003675 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003676 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003677 err_code |= ERR_ALERT | ERR_FATAL;
3678 free(arg->name);
3679 free(arg);
3680 goto out;
3681 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003682 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003683 LIST_ADDQ(&curmsg->args, &arg->list);
3684 cur_arg++;
3685 }
3686 curproxy->conf.args.file = NULL;
3687 curproxy->conf.args.line = 0;
3688 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003689 else if (!strcmp(args[0], "acl")) {
3690 err = invalid_char(args[1]);
3691 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003692 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3693 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
3697 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003698 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3699 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
3703 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003704 else if (!strcmp(args[0], "event")) {
3705 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003706 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003707 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003708 goto out;
3709 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003710 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3711 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003712
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003713 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3714 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3715 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3716 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3717
3718 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3719 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3720 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3721 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3722 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3723 curmsg->event = SPOE_EV_ON_TCP_RSP;
3724
3725 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3726 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3727 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3728 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3729 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3730 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3731 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003732 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3733 file, linenum, args[1]);
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
3738 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3739 struct acl_cond *cond;
3740
3741 cond = build_acl_cond(file, linenum, &curmsg->acls,
3742 curproxy, (const char **)args+2,
3743 &errmsg);
3744 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003745 ha_alert("parsing [%s:%d] : error detected while "
3746 "parsing an 'event %s' condition : %s.\n",
3747 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003748 err_code |= ERR_ALERT | ERR_FATAL;
3749 goto out;
3750 }
3751 curmsg->cond = cond;
3752 }
3753 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003754 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3755 "or 'unless' followed by a condition but found '%s'.\n",
3756 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003757 err_code |= ERR_ALERT | ERR_FATAL;
3758 goto out;
3759 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003760 }
3761 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003762 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3763 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003764 err_code |= ERR_ALERT | ERR_FATAL;
3765 goto out;
3766 }
3767 out:
3768 free(errmsg);
3769 return err_code;
3770}
3771
3772/* Return -1 on error, else 0 */
3773static int
3774parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3775 struct flt_conf *fconf, char **err, void *private)
3776{
3777 struct list backup_sections;
3778 struct spoe_config *conf;
3779 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003780 struct spoe_group *grp, *grpback;
3781 struct spoe_placeholder *ph, *phback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003782 char *file = NULL, *engine = NULL;
3783 int ret, pos = *cur_arg + 1;
3784
3785 conf = calloc(1, sizeof(*conf));
3786 if (conf == NULL) {
3787 memprintf(err, "%s: out of memory", args[*cur_arg]);
3788 goto error;
3789 }
3790 conf->proxy = px;
3791
3792 while (*args[pos]) {
3793 if (!strcmp(args[pos], "config")) {
3794 if (!*args[pos+1]) {
3795 memprintf(err, "'%s' : '%s' option without value",
3796 args[*cur_arg], args[pos]);
3797 goto error;
3798 }
3799 file = args[pos+1];
3800 pos += 2;
3801 }
3802 else if (!strcmp(args[pos], "engine")) {
3803 if (!*args[pos+1]) {
3804 memprintf(err, "'%s' : '%s' option without value",
3805 args[*cur_arg], args[pos]);
3806 goto error;
3807 }
3808 engine = args[pos+1];
3809 pos += 2;
3810 }
3811 else {
3812 memprintf(err, "unknown keyword '%s'", args[pos]);
3813 goto error;
3814 }
3815 }
3816 if (file == NULL) {
3817 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3818 goto error;
3819 }
3820
3821 /* backup sections and register SPOE sections */
3822 LIST_INIT(&backup_sections);
3823 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003824 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3825 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003826 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003827
3828 /* Parse SPOE filter configuration file */
3829 curengine = engine;
3830 curproxy = px;
3831 curagent = NULL;
3832 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003833 LIST_INIT(&curmsgs);
3834 LIST_INIT(&curgrps);
3835 LIST_INIT(&curmphs);
3836 LIST_INIT(&curgphs);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003837 ret = readcfgfile(file);
3838 curproxy = NULL;
3839
3840 /* unregister SPOE sections and restore previous sections */
3841 cfg_unregister_sections();
3842 cfg_restore_sections(&backup_sections);
3843
3844 if (ret == -1) {
3845 memprintf(err, "Could not open configuration file %s : %s",
3846 file, strerror(errno));
3847 goto error;
3848 }
3849 if (ret & (ERR_ABORT|ERR_FATAL)) {
3850 memprintf(err, "Error(s) found in configuration file %s", file);
3851 goto error;
3852 }
3853
3854 /* Check SPOE agent */
3855 if (curagent == NULL) {
3856 memprintf(err, "No SPOE agent found in file %s", file);
3857 goto error;
3858 }
3859 if (curagent->b.name == NULL) {
3860 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3861 curagent->id, curagent->conf.file, curagent->conf.line);
3862 goto error;
3863 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003864 if (curagent->timeout.hello == TICK_ETERNITY ||
3865 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003866 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003867 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3868 " | While not properly invalid, you will certainly encounter various problems\n"
3869 " | with such a configuration. To fix this, please ensure that all following\n"
3870 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3871 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003872 }
3873 if (curagent->var_pfx == NULL) {
3874 char *tmp = curagent->id;
3875
3876 while (*tmp) {
3877 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3878 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3879 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3880 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3881 goto error;
3882 }
3883 tmp++;
3884 }
3885 curagent->var_pfx = strdup(curagent->id);
3886 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003887 if (curagent->engine_id == NULL)
3888 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003889
Christopher Faulet11610f32017-09-21 10:23:10 +02003890 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003891 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3892 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003893 goto finish;
3894 }
3895
Christopher Faulet11610f32017-09-21 10:23:10 +02003896 /* Replace placeholders by the corresponding messages for the SPOE
3897 * agent */
3898 list_for_each_entry(ph, &curmphs, list) {
3899 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003900 struct spoe_arg *arg;
3901 unsigned int where;
3902
Christopher Faulet11610f32017-09-21 10:23:10 +02003903 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003904 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3905 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3906 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3907 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3908 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3909 }
3910 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3911 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3912 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003913 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3914 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003915 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003916 }
3917 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003918 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3919 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003920 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003921 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003922
3923 where = 0;
3924 switch (msg->event) {
3925 case SPOE_EV_ON_CLIENT_SESS:
3926 where |= SMP_VAL_FE_CON_ACC;
3927 break;
3928
3929 case SPOE_EV_ON_TCP_REQ_FE:
3930 where |= SMP_VAL_FE_REQ_CNT;
3931 break;
3932
3933 case SPOE_EV_ON_HTTP_REQ_FE:
3934 where |= SMP_VAL_FE_HRQ_HDR;
3935 break;
3936
3937 case SPOE_EV_ON_TCP_REQ_BE:
3938 if (px->cap & PR_CAP_FE)
3939 where |= SMP_VAL_FE_REQ_CNT;
3940 if (px->cap & PR_CAP_BE)
3941 where |= SMP_VAL_BE_REQ_CNT;
3942 break;
3943
3944 case SPOE_EV_ON_HTTP_REQ_BE:
3945 if (px->cap & PR_CAP_FE)
3946 where |= SMP_VAL_FE_HRQ_HDR;
3947 if (px->cap & PR_CAP_BE)
3948 where |= SMP_VAL_BE_HRQ_HDR;
3949 break;
3950
3951 case SPOE_EV_ON_SERVER_SESS:
3952 where |= SMP_VAL_BE_SRV_CON;
3953 break;
3954
3955 case SPOE_EV_ON_TCP_RSP:
3956 if (px->cap & PR_CAP_FE)
3957 where |= SMP_VAL_FE_RES_CNT;
3958 if (px->cap & PR_CAP_BE)
3959 where |= SMP_VAL_BE_RES_CNT;
3960 break;
3961
3962 case SPOE_EV_ON_HTTP_RSP:
3963 if (px->cap & PR_CAP_FE)
3964 where |= SMP_VAL_FE_HRS_HDR;
3965 if (px->cap & PR_CAP_BE)
3966 where |= SMP_VAL_BE_HRS_HDR;
3967 break;
3968
3969 default:
3970 break;
3971 }
3972
3973 list_for_each_entry(arg, &msg->args, list) {
3974 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003975 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003976 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003977 "none of which is available here ('%s')",
3978 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003979 sample_ckp_names(arg->expr->fetch->use),
3980 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003981 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003982 }
3983 }
3984
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003985 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003986 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3987 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003988 }
3989 }
3990 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003991 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003992 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003993 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003994 continue;
3995 }
3996
Christopher Faulet11610f32017-09-21 10:23:10 +02003997 /* Replace placeholders by the corresponding groups for the SPOE
3998 * agent */
3999 list_for_each_entry(ph, &curgphs, list) {
4000 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4001 if (!strcmp(grp->id, ph->id)) {
4002 grp->agent = curagent;
4003 LIST_DEL(&grp->list);
4004 LIST_ADDQ(&curagent->groups, &grp->list);
4005 goto next_aph;
4006 }
4007 }
4008 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4009 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4010 goto error;
4011 next_aph:
4012 continue;
4013 }
4014
4015 /* Replace placeholders by the corresponding message for each SPOE
4016 * group of the SPOE agent */
4017 list_for_each_entry(grp, &curagent->groups, list) {
4018 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4019 list_for_each_entry(msg, &curmsgs, list) {
4020 if (!strcmp(msg->id, ph->id)) {
4021 if (msg->group != NULL) {
4022 memprintf(err, "SPOE message '%s' already belongs to "
4023 "the SPOE group '%s' declare at %s:%d",
4024 msg->id, msg->group->id,
4025 msg->group->conf.file,
4026 msg->group->conf.line);
4027 goto error;
4028 }
4029
4030 /* Scope for arguments are not checked for now. We will check
4031 * them only if a rule use the corresponding SPOE group. */
4032 msg->agent = curagent;
4033 msg->group = grp;
4034 LIST_DEL(&ph->list);
4035 LIST_ADDQ(&grp->messages, &msg->by_grp);
4036 goto next_mph_grp;
4037 }
4038 }
4039 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4040 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4041 goto error;
4042 next_mph_grp:
4043 continue;
4044 }
4045 }
4046
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004047 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004048 /* move curmsgs to the agent message list */
4049 curmsgs.n->p = &curagent->messages;
4050 curmsgs.p->n = &curagent->messages;
4051 curagent->messages = curmsgs;
4052 LIST_INIT(&curmsgs);
4053
Christopher Faulet7ee86672017-09-19 11:08:28 +02004054 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004056 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4057 LIST_DEL(&ph->list);
4058 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004059 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004060 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4061 LIST_DEL(&ph->list);
4062 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004063 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004064 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4065 LIST_DEL(&grp->list);
4066 spoe_release_group(grp);
4067 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004068 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004069 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004070 fconf->ops = &spoe_ops;
4071 fconf->conf = conf;
4072 return 0;
4073
4074 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004075 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004076 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4077 LIST_DEL(&ph->list);
4078 spoe_release_placeholder(ph);
4079 }
4080 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4081 LIST_DEL(&ph->list);
4082 spoe_release_placeholder(ph);
4083 }
4084 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4085 LIST_DEL(&grp->list);
4086 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004087 }
4088 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4089 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004090 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004091 }
4092 free(conf);
4093 return -1;
4094}
4095
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004096/* Send message of a SPOE group. This is the action_ptr callback of a rule
4097 * associated to a "send-spoe-group" action.
4098 *
4099 * It returns ACT_RET_CONT is processing is finished without error, it returns
4100 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4101 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004102static enum act_return
4103spoe_send_group(struct act_rule *rule, struct proxy *px,
4104 struct session *sess, struct stream *s, int flags)
4105{
4106 struct filter *filter;
4107 struct spoe_agent *agent = NULL;
4108 struct spoe_group *group = NULL;
4109 struct spoe_context *ctx = NULL;
4110 int ret, dir;
4111
4112 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4113 if (filter->config == rule->arg.act.p[0]) {
4114 agent = rule->arg.act.p[2];
4115 group = rule->arg.act.p[3];
4116 ctx = filter->ctx;
4117 break;
4118 }
4119 }
4120 if (agent == NULL || group == NULL || ctx == NULL)
4121 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004122 if (ctx->state == SPOE_CTX_ST_NONE)
4123 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004124
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004125 switch (rule->from) {
4126 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4127 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4128 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4129 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4130 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4131 default:
4132 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4133 " - internal error while execute spoe-send-group\n",
4134 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4135 __FUNCTION__, s);
4136 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4137 agent->id);
4138 return ACT_RET_CONT;
4139 }
4140
4141 ret = spoe_process_group(s, ctx, group, dir);
4142 if (ret == 1)
4143 return ACT_RET_CONT;
4144 else if (ret == 0) {
4145 if (flags & ACT_FLAG_FINAL) {
4146 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4147 " - failed to process group '%s': interrupted by caller\n",
4148 (int)now.tv_sec, (int)now.tv_usec,
4149 agent->id, __FUNCTION__, s, group->id);
4150 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4151 spoe_handle_processing_error(s, agent, ctx, dir);
4152 spoe_stop_processing(ctx);
4153 return ACT_RET_CONT;
4154 }
4155 return ACT_RET_YIELD;
4156 }
4157 else
4158 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004159}
4160
4161/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4162 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4163 * action should be:
4164 *
4165 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4166 *
4167 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4168 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4169 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4170 * group.
4171 *
4172 * The function returns 1 in success case, otherwise, it returns 0 and err is
4173 * filled.
4174 */
4175static int
4176check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4177{
4178 struct flt_conf *fconf;
4179 struct spoe_config *conf;
4180 struct spoe_agent *agent = NULL;
4181 struct spoe_group *group;
4182 struct spoe_message *msg;
4183 char *engine_id = rule->arg.act.p[0];
4184 char *group_id = rule->arg.act.p[1];
4185 unsigned int where = 0;
4186
4187 switch (rule->from) {
4188 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4189 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4190 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4191 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4192 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4193 default:
4194 memprintf(err,
4195 "internal error, unexpected rule->from=%d, please report this bug!",
4196 rule->from);
4197 goto error;
4198 }
4199
4200 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4201 * <px> */
4202 list_for_each_entry(fconf, &px->filter_configs, list) {
4203 conf = fconf->conf;
4204
4205 /* This is not an SPOE filter */
4206 if (fconf->id != spoe_filter_id)
4207 continue;
4208
4209 /* This is the good engine */
4210 if (!strcmp(conf->id, engine_id)) {
4211 agent = conf->agent;
4212 break;
4213 }
4214 }
4215 if (agent == NULL) {
4216 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4217 engine_id, group_id);
4218 goto error;
4219 }
4220
4221 /* Try to find the right group */
4222 list_for_each_entry(group, &agent->groups, list) {
4223 /* This is the good group */
4224 if (!strcmp(group->id, group_id))
4225 break;
4226 }
4227 if (&group->list == &agent->groups) {
4228 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4229 group_id, engine_id);
4230 goto error;
4231 }
4232
4233 /* Ok, we found the group, we need to check messages and their
4234 * arguments */
4235 list_for_each_entry(msg, &group->messages, by_grp) {
4236 struct spoe_arg *arg;
4237
4238 list_for_each_entry(arg, &msg->args, list) {
4239 if (!(arg->expr->fetch->val & where)) {
4240 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4241 "some args extract information from '%s',"
4242 "none of which is available here ('%s')",
4243 msg->id, group->id, msg->conf.file, msg->conf.line,
4244 sample_ckp_names(arg->expr->fetch->use),
4245 sample_ckp_names(where));
4246 goto error;
4247 }
4248 }
4249 }
4250
4251 free(engine_id);
4252 free(group_id);
4253 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4254 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4255 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4256 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4257 return 1;
4258
4259 error:
4260 free(engine_id);
4261 free(group_id);
4262 return 0;
4263}
4264
4265/* Parse 'send-spoe-group' action following the format:
4266 *
4267 * ... send-spoe-group <engine-id> <group-id>
4268 *
4269 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4270 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4271 * ids are saved and used later, when the rule will be checked.
4272 */
4273static enum act_parse_ret
4274parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4275 struct act_rule *rule, char **err)
4276{
4277 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4278 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4279 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4280 return ACT_RET_PRS_ERR;
4281 }
4282 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4283 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4284
4285 (*orig_arg) += 2;
4286
4287 rule->action = ACT_CUSTOM;
4288 rule->action_ptr = spoe_send_group;
4289 rule->check_ptr = check_send_spoe_group;
4290 return ACT_RET_PRS_OK;
4291}
4292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004293
4294/* Declare the filter parser for "spoe" keyword */
4295static struct flt_kw_list flt_kws = { "SPOE", { }, {
4296 { "spoe", parse_spoe_flt, NULL },
4297 { NULL, NULL, NULL },
4298 }
4299};
4300
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004301/* Delcate the action parser for "spoe-action" keyword */
4302static struct action_kw_list tcp_req_action_kws = { { }, {
4303 { "send-spoe-group", parse_send_spoe_group },
4304 { /* END */ },
4305 }
4306};
4307static struct action_kw_list tcp_res_action_kws = { { }, {
4308 { "send-spoe-group", parse_send_spoe_group },
4309 { /* END */ },
4310 }
4311};
4312static struct action_kw_list http_req_action_kws = { { }, {
4313 { "send-spoe-group", parse_send_spoe_group },
4314 { /* END */ },
4315 }
4316};
4317static struct action_kw_list http_res_action_kws = { { }, {
4318 { "send-spoe-group", parse_send_spoe_group },
4319 { /* END */ },
4320 }
4321};
4322
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004323__attribute__((constructor))
4324static void __spoe_init(void)
4325{
4326 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004327 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4328 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4329 http_req_keywords_register(&http_req_action_kws);
4330 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004331
Willy Tarreaubafbe012017-11-24 17:34:44 +01004332 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4333 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004334}
4335
4336__attribute__((destructor))
4337static void
4338__spoe_deinit(void)
4339{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004340 pool_destroy(pool_head_spoe_ctx);
4341 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004342}