blob: ae34c3b9006b2a10994172df0e97aa41657b67e9 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
Emeric Bruna1dd2432017-06-21 15:42:52 +020021#include <common/hathreads.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020022
23#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020024#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010025#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020026
Christopher Faulet57583e42017-09-04 15:41:09 +020027#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020028#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020029#include <proto/arg.h>
30#include <proto/backend.h>
31#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010032#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033#include <proto/frontend.h>
34#include <proto/log.h>
35#include <proto/proto_http.h>
36#include <proto/proxy.h>
37#include <proto/sample.h>
38#include <proto/session.h>
39#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010040#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/stream.h>
42#include <proto/stream_interface.h>
43#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020044#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#include <proto/vars.h>
46
47#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
48#define SPOE_PRINTF(x...) fprintf(x)
49#else
50#define SPOE_PRINTF(x...)
51#endif
52
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010053/* Reserved 4 bytes to the frame size. So a frame and its size can be written
54 * together in a buffer */
55#define MAX_FRAME_SIZE global.tune.bufsize - 4
56
57/* The minimum size for a frame */
58#define MIN_FRAME_SIZE 256
59
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Reserved for the metadata and the frame type.
61 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010062#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020063
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010065#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
66
Christopher Faulet3b386a32017-02-23 10:17:15 +010067/* SPOE filter id. Used to identify SPOE filters */
68const char *spoe_filter_id = "SPOE filter";
69
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020070/* Set if the handle on SIGUSR1 is registered */
71static int sighandler_registered = 0;
72
73/* proxy used during the parsing */
74struct proxy *curproxy = NULL;
75
76/* The name of the SPOE engine, used during the parsing */
77char *curengine = NULL;
78
79/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020080/* SPOE agent/group/message used during the parsing */
81struct spoe_agent *curagent = NULL;
82struct spoe_group *curgrp = NULL;
83struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020084
85/* list of SPOE messages and placeholders used during the parsing */
86struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020087struct list curgrps;
88struct list curmphs;
89struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010090struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020091
Christopher Faulet42bfa462017-01-04 14:14:19 +010092/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093static struct pool_head *pool_head_spoe_ctx = NULL;
94static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020096struct flt_ops spoe_ops;
97
Christopher Faulet8ef75252017-02-20 22:56:03 +010098static int spoe_queue_context(struct spoe_context *ctx);
99static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
100static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101
102/********************************************************************
103 * helper functions/globals
104 ********************************************************************/
105static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200106spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107{
Christopher Faulet11610f32017-09-21 10:23:10 +0200108 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200110 free(ph->id);
111 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112}
113
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100115spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet57583e42017-09-04 15:41:09 +0200117 struct spoe_arg *arg, *argback;
118 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119
120 if (!msg)
121 return;
122 free(msg->id);
123 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200124 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125 release_sample_expr(arg->expr);
126 free(arg->name);
127 LIST_DEL(&arg->list);
128 free(arg);
129 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200130 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
131 LIST_DEL(&acl->list);
132 prune_acl(acl);
133 free(acl);
134 }
135 if (msg->cond) {
136 prune_acl_cond(msg->cond);
137 free(msg->cond);
138 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200139 free(msg);
140}
141
142static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200143spoe_release_group(struct spoe_group *grp)
144{
145 if (!grp)
146 return;
147 free(grp->id);
148 free(grp->conf.file);
149 free(grp);
150}
151
152static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100153spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200154{
Christopher Faulet11610f32017-09-21 10:23:10 +0200155 struct spoe_message *msg, *msgback;
156 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200157 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200158
159 if (!agent)
160 return;
161 free(agent->id);
162 free(agent->conf.file);
163 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100164 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100165 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
167 LIST_DEL(&msg->list);
168 spoe_release_message(msg);
169 }
170 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
171 LIST_DEL(&grp->list);
172 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200173 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200174 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100175 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200176 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200177 free(agent);
178}
179
180static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100181 [SPOE_FRM_ERR_NONE] = "normal",
182 [SPOE_FRM_ERR_IO] = "I/O error",
183 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
184 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
185 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
186 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
187 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
188 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
189 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
190 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
191 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
192 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100193 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100194 [SPOE_FRM_ERR_RES] = "resource allocation error",
195 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200196};
197
198static const char *spoe_event_str[SPOE_EV_EVENTS] = {
199 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
200 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
201 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
202 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
203 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
204
205 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
206 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
207 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
208};
209
210
211#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
212
213static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100214 [SPOE_CTX_ST_NONE] = "NONE",
215 [SPOE_CTX_ST_READY] = "READY",
216 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
217 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
218 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
219 [SPOE_CTX_ST_DONE] = "DONE",
220 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200221};
222
223static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100224 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
225 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
226 [SPOE_APPCTX_ST_IDLE] = "IDLE",
227 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
228 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
229 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
230 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
231 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
232 [SPOE_APPCTX_ST_EXIT] = "EXIT",
233 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200234};
235
236#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100237
Christopher Faulet8ef75252017-02-20 22:56:03 +0100238/* Used to generates a unique id for an engine. On success, it returns a
239 * allocated string. So it is the caller's reponsibility to release it. If the
240 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100241static char *
242generate_pseudo_uuid()
243{
244 static int init = 0;
245
246 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
247 const char uuid_chr[] = "0123456789ABCDEF-";
248 char *uuid;
249 int i;
250
251 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
252 return NULL;
253
254 if (!init) {
255 srand(now_ms);
256 init = 1;
257 }
258
259 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
260 int r = rand () % 16;
261
262 switch (uuid_fmt[i]) {
263 case 'x' : uuid[i] = uuid_chr[r]; break;
264 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
265 default : uuid[i] = uuid_fmt[i]; break;
266 }
267 }
268 return uuid;
269}
270
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/********************************************************************
272 * Functions that encode/decode SPOE frames
273 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200274/* Helper to get static string length, excluding the terminating null byte */
275#define SLEN(str) (sizeof(str)-1)
276
277/* Predefined key used in HELLO/DISCONNECT frames */
278#define SUPPORTED_VERSIONS_KEY "supported-versions"
279#define VERSION_KEY "version"
280#define MAX_FRAME_SIZE_KEY "max-frame-size"
281#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100282#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100283#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200284#define STATUS_CODE_KEY "status-code"
285#define MSG_KEY "message"
286
287struct spoe_version {
288 char *str;
289 int min;
290 int max;
291};
292
293/* All supported versions */
294static struct spoe_version supported_versions[] = {
295 {"1.0", 1000, 1000},
296 {NULL, 0, 0}
297};
298
299/* Comma-separated list of supported versions */
300#define SUPPORTED_VERSIONS_VAL "1.0"
301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302/* Convert a string to a SPOE version value. The string must follow the format
303 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
304 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 const char *p, *end;
309 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 p = str;
312 end = str+len;
313 maj = min = 0;
314 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200315
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 /* skip leading spaces */
317 while (p < end && isspace(*p))
318 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320 /* parse Major number, until the '.' */
321 while (*p != '.') {
322 if (p >= end || *p < '0' || *p > '9')
323 goto out;
324 maj *= 10;
325 maj += (*p - '0');
326 p++;
327 }
328
329 /* check Major version */
330 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331 goto out;
332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p++; /* skip the '.' */
334 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
335 goto out;
336
337 /* Parse Minor number */
338 while (p < end) {
339 if (*p < '0' || *p > '9')
340 break;
341 min *= 10;
342 min += (*p - '0');
343 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100345
346 /* check Minor number */
347 if (min > 999)
348 goto out;
349
350 /* skip trailing spaces */
351 while (p < end && isspace(*p))
352 p++;
353 if (p != end)
354 goto out;
355
356 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 out:
358 return vsn;
359}
360
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
362 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
363 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100365spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366{
Christopher Faulet305c6072017-02-23 16:17:53 +0100367 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100368 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100369 char *p, *end;
370 unsigned int flags = SPOE_FRM_FL_FIN;
371 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 p = frame;
374 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 /* Set Frame type */
377 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100379 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100380 memcpy(p, (char *)&flags, 4);
381 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382
383 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385
386 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
387 * and "capabilities" */
388
389 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390 sz = SLEN(SUPPORTED_VERSIONS_KEY);
391 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
392 goto too_big;
393
394 *p++ = SPOE_DATA_T_STR;
395 sz = SLEN(SUPPORTED_VERSIONS_VAL);
396 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
397 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
399 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 sz = SLEN(MAX_FRAME_SIZE_KEY);
401 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
402 goto too_big;
403
404 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200405 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200407
408 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100409 sz = SLEN(CAPABILITIES_KEY);
410 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
411 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200412
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100414 chk = get_trash_chunk();
415 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
416 memcpy(chk->str, "pipelining", 10);
417 chk->len += 10;
418 }
419 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
420 if (chk->len) chk->str[chk->len++] = ',';
421 memcpy(chk->str+chk->len, "async", 5);
422 chk->len += 5;
423 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100424 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
425 if (chk->len) chk->str[chk->len++] = ',';
426 memcpy(chk->str+chk->len, "fragmentation", 13);
427 chk->len += 5;
428 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100429 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100430 goto too_big;
431
432 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100433 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 sz = SLEN(ENGINE_ID_KEY);
435 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
436 goto too_big;
437
438 *p++ = SPOE_DATA_T_STR;
439 sz = strlen(agent->engine_id);
440 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
441 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100442 }
443
Christopher Faulet8ef75252017-02-20 22:56:03 +0100444 return (p - frame);
445
446 too_big:
447 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
448 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200449}
450
Christopher Faulet8ef75252017-02-20 22:56:03 +0100451/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
452 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
453 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200454static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200456{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 const char *reason;
458 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100459 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100460 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200461
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 p = frame;
463 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200464
Christopher Faulet8ef75252017-02-20 22:56:03 +0100465 /* Set Frame type */
466 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100468 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 memcpy(p, (char *)&flags, 4);
470 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
472 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 *p++ = 0; *p++ = 0;
474
475 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
476 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477
478 /* There are 2 mandatory items: "status-code" and "message" */
479
480 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481 sz = SLEN(STATUS_CODE_KEY);
482 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
483 goto too_big;
484
485 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200486 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488
489 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 sz = SLEN(MSG_KEY);
491 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 /*Get the message corresponding to the status code */
495 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
496
497 *p++ = SPOE_DATA_T_STR;
498 sz = strlen(reason);
499 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
500 goto too_big;
501
502 return (p - frame);
503
504 too_big:
505 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
506 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200507}
508
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
510 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
511 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100514 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 char *p, *end;
517 unsigned int stream_id, frame_id;
518 unsigned int flags = SPOE_FRM_FL_FIN;
519 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 p = frame;
522 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200523
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100524 stream_id = ctx->stream_id;
525 frame_id = ctx->frame_id;
526
527 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
528 /* The fragmentation is not supported by the applet */
529 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
530 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
531 return -1;
532 }
533 flags = ctx->frag_ctx.flags;
534 }
535
536 /* Set Frame type */
537 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
538
539 /* Set flags */
540 memcpy(p, (char *)&flags, 4);
541 p += 4;
542
543 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200544 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100545 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200546 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100547 goto too_big;
548
549 /* Copy encoded messages, if possible */
550 sz = ctx->buffer->i;
551 if (p + sz >= end)
552 goto too_big;
553 memcpy(p, ctx->buffer->p, sz);
554 p += sz;
555
556 return (p - frame);
557
558 too_big:
559 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
560 return 0;
561}
562
563/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
564 * returns the number of encoded bytes in the frame on success, 0 if an encoding
565 * error occurred and -1 if a fatal error occurred. */
566static int
567spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
568 char *frame, size_t size)
569{
570 char *p, *end;
571 unsigned int stream_id, frame_id;
572 unsigned int flags;
573 size_t sz;
574
575 p = frame;
576 end = frame+size;
577
Christopher Faulet8ef75252017-02-20 22:56:03 +0100578 /* <ctx> is null when the stream has aborted the processing of a
579 * fragmented frame. In this case, we must notify the corresponding
580 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100581 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100582 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100583 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
584 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
585 }
586 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100587 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100588 stream_id = ctx->stream_id;
589 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100590 }
591
Christopher Faulet8ef75252017-02-20 22:56:03 +0100592 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100595 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 memcpy(p, (char *)&flags, 4);
597 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200598
599 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200600 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100601 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200602 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200604
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100605 if (ctx == NULL)
606 goto end;
607
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 if (p + sz >= end)
611 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100614
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100615 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 return (p - frame);
617
618 too_big:
619 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
620 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200621}
622
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623/* Decode and process the HELLO frame sent by an agent. It returns the number of
624 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
625 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628{
Christopher Faulet305c6072017-02-23 16:17:53 +0100629 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
630 char *p, *end;
631 int vsn, max_frame_size;
632 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100633
634 p = frame;
635 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200636
637 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
639 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200640 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200642
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 if (size < 7 /* TYPE + METADATA */) {
644 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
645 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646 }
647
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100648 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649 memcpy((char *)&flags, p, 4);
650 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200651
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100652 /* Fragmentation is not supported for HELLO frame */
653 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 return -1;
656 }
657
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 if (*p != 0 || *(p+1) != 0) {
660 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
661 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100663 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
665 /* There are 3 mandatory items: "version", "max-frame-size" and
666 * "capabilities" */
667
668 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100669 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 while (p < end) {
671 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200672 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200674
675 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 ret = spoe_decode_buffer(&p, end, &str, &sz);
677 if (ret == -1 || !sz) {
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
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682 /* Check "version" K/V item */
683 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 int i, type = *p++;
685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
688 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
689 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
692 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
693 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 }
695
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 return -1;
700 }
701 for (i = 0; supported_versions[i].str != NULL; ++i) {
702 if (vsn >= supported_versions[i].min &&
703 vsn <= supported_versions[i].max)
704 break;
705 }
706 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100707 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 return -1;
709 }
710 }
711 /* Check "max-frame-size" K/V item */
712 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714
715 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
717 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
718 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
719 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
721 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200723 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 if (sz < MIN_FRAME_SIZE ||
728 sz > SPOE_APPCTX(appctx)->max_frame_size) {
729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 return -1;
731 }
732 max_frame_size = sz;
733 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100734 /* Check "capabilities" K/V item */
735 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100736 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100737
738 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
740 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
741 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
744 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
745 return 0;
746 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749 char *delim;
750
751 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
755 str += 10; sz -= 10;
756 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 flags |= SPOE_APPCTX_FL_PIPELINING;
758 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 else if (sz >= 5 && !strncmp(str, "async", 5)) {
760 str += 5; sz -= 5;
761 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 flags |= SPOE_APPCTX_FL_ASYNC;
763 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
765 str += 13; sz -= 13;
766 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100767 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
768 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 /* Get the next comma or break */
771 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773 delim++;
774 sz -= (delim - str);
775 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 }
777 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200778 else {
779 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 if (spoe_skip_data(&p, end) == -1) {
781 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
782 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200783 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200784 }
785 }
786
787 /* Final checks */
788 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200790 return -1;
791 }
792 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200794 return -1;
795 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100796 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
797 flags &= ~SPOE_APPCTX_FL_PIPELINING;
798 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
799 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800
Christopher Faulet42bfa462017-01-04 14:14:19 +0100801 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
802 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
803 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100804
805 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806}
807
808/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
809 * bytes on success, 0 if the frame can be ignored and -1 if an error
810 * occurred. */
811static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100814 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100815 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816
817 p = frame;
818 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819
820 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
822 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200823 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 if (size < 7 /* TYPE + METADATA */) {
827 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
828 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829 }
830
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100831 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 memcpy((char *)&flags, p, 4);
833 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100835 /* Fragmentation is not supported for DISCONNECT frame */
836 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100837 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100838 return -1;
839 }
840
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 if (*p != 0 || *(p+1) != 0) {
843 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
844 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
848 /* There are 2 mandatory items: "status-code" and "message" */
849
850 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 while (p < end) {
852 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200853 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
856 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 ret = spoe_decode_buffer(&p, end, &str, &sz);
858 if (ret == -1 || !sz) {
859 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
860 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200861 }
862
863 /* Check "status-code" K/V item */
864 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200866
867 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
869 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
870 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
871 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200875 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
877 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880 }
881
882 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 else if (!memcmp(str, MSG_KEY, sz)) {
884 int type = *p++;
885
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
888 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
889 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 ret = spoe_decode_buffer(&p, end, &str, &sz);
892 if (ret == -1 || sz > 255) {
893 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
894 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
897 SPOE_APPCTX(appctx)->reason = str;
898 SPOE_APPCTX(appctx)->rlen = sz;
899#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
901 else {
902 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903 if (spoe_skip_data(&p, end) == -1) {
904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907 }
908 }
909
Christopher Faulet8ef75252017-02-20 22:56:03 +0100910 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911}
912
913
Christopher Fauleta1cda022016-12-21 08:58:06 +0100914/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 * success, 0 if the frame can be ignored and -1 if an error occurred. */
916static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100918 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100920 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100922 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100924 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925
926 p = frame;
927 end = frame + size;
928 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929
930 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
932 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 if (size < 7 /* TYPE + METADATA */) {
937 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
938 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939 }
940
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100941 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 memcpy((char *)&flags, p, 4);
943 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100945 /* Fragmentation is not supported for now */
946 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 return -1;
949 }
950
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200952 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100954 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200956 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100960
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100962 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200963 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100964 if ((*ctx)->stream_id == (unsigned int)stream_id &&
965 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100966 goto found;
967 }
968 }
969 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100970 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
971 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100973 goto found;
974 }
975 }
976
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100977 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
978 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
979 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
980
981 /* ABRT bit is set for an unfinished fragmented frame */
982 if (flags & SPOE_FRM_FL_ABRT) {
983 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100984 (*ctx)->state = SPOE_CTX_ST_ERROR;
985 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
986 /* Ignore the payload */
987 goto end;
988 }
989 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
990 /* For now, we ignore the ack */
991 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
992 return 0;
993 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100994
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100996 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
997 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100998 " - stream-id=%u - frame-id=%u\n",
999 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1000 __FUNCTION__, appctx,
1001 (unsigned int)stream_id, (unsigned int)frame_id);
1002
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001003 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004 return 0;
1005
1006 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001007 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1008 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001009 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001010 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001012
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001013 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001014 len = (end - p);
1015 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1016 SPOE_APPCTX(appctx)->buffer->i = len;
1017 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001018
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001019 /* Transfer the buffer ownership to the SPOE context */
1020 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1021 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001022
Christopher Faulet8ef75252017-02-20 22:56:03 +01001023 (*ctx)->state = SPOE_CTX_ST_DONE;
1024
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001025 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001026 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 " - ACK frame received"
1028 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001030 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1031 (*ctx)->frame_id, flags);
1032 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001033}
1034
Christopher Fauletba7bc162016-11-07 21:07:38 +01001035/* This function is used in cfgparse.c and declared in proto/checks.h. It
1036 * prepare the request to send to agents during a healthcheck. It returns 0 on
1037 * success and -1 if an error occurred. */
1038int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001040{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041 struct appctx appctx;
1042 struct spoe_appctx spoe_appctx;
1043 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1044 size_t sz;
1045 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001046
Christopher Faulet42bfa462017-01-04 14:14:19 +01001047 memset(&appctx, 0, sizeof(appctx));
1048 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001049 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001050
1051 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001052 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001053
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1055 end = frame + MAX_FRAME_SIZE;
1056
1057 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1058 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001061
Christopher Faulet8ef75252017-02-20 22:56:03 +01001062 /* Add "healthcheck" K/V item */
1063 sz = SLEN(HEALTHCHECK_KEY);
1064 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1065 return -1;
1066 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001067
Christopher Faulet8ef75252017-02-20 22:56:03 +01001068 *len = frame - buf;
1069 sz = htonl(*len - 4);
1070 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet8ef75252017-02-20 22:56:03 +01001072 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075 return 0;
1076}
1077
1078/* This function is used in checks.c and declared in proto/checks.h. It decode
1079 * the response received from an agent during a healthcheck. It returns 0 on
1080 * success and -1 if an error occurred. */
1081int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001084 struct appctx appctx;
1085 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet42bfa462017-01-04 14:14:19 +01001087 memset(&appctx, 0, sizeof(appctx));
1088 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet42bfa462017-01-04 14:14:19 +01001090 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001091 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001092
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1094 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 goto error;
1096 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1098 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099
1100 return 0;
1101
1102 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1104 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1105 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106 return -1;
1107}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001108
Christopher Fauleta1cda022016-12-21 08:58:06 +01001109/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1110 * the frame can be ignored, 1 to retry later, and the frame legnth on
1111 * success. */
1112static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001114{
1115 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116 int ret;
1117 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001118
Christopher Faulet8ef75252017-02-20 22:56:03 +01001119 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1120 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001121 netint = htonl(framesz);
1122 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001123 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001124 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001125 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 retry:
1127 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001129 }
1130 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131 return -1; /* error */
1132 }
1133 return framesz;
1134}
1135
1136/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1137 * when the frame can be ignored, 1 to retry later and the frame length on
1138 * success. */
1139static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001140spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141{
1142 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 int ret;
1144 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145
Willy Tarreau06d80a92017-10-19 14:32:15 +02001146 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147 if (ret > 0) {
1148 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001149 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001151 return -1;
1152 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001153 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154 }
1155 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156 if (ret == 0) {
1157 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 }
1160 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 return -1; /* error */
1162 }
1163 return framesz;
1164}
1165
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001166/********************************************************************
1167 * Functions that manage the SPOE applet
1168 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001169static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001171{
1172 si_applet_want_get(appctx->owner);
1173 si_applet_want_put(appctx->owner);
1174 appctx_wakeup(appctx);
1175 return 1;
1176}
1177
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001178/* Callback function that catches applet timeouts. If a timeout occurred, we set
1179 * <appctx->st1> flag and the SPOE applet is woken up. */
1180static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001182{
1183 struct appctx *appctx = task->context;
1184
1185 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1186 if (tick_is_expired(task->expire, now_ms)) {
1187 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001189 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001190 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001191 return task;
1192}
1193
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001194/* Callback function that releases a SPOE applet. This happens when the
1195 * connection with the agent is closed. */
1196static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001199 struct stream_interface *si = appctx->owner;
1200 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1201 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001202 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001203
1204 if (spoe_appctx == NULL)
1205 return;
1206
1207 appctx->ctx.spoe.ptr = NULL;
1208 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001209
1210 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1211 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1212 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 /* Remove applet from the list of running applets */
Christopher Faulet24289f22017-09-25 14:48:02 +02001215 agent->rt[tid].applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1217 LIST_DEL(&spoe_appctx->list);
1218 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001219 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001222 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001223 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
Christopher Faulet24289f22017-09-25 14:48:02 +02001224 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001225
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001226 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001227 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1228 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001229
1230 si_shutw(si);
1231 si_shutr(si);
1232 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233 }
1234
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 /* Destroy the task attached to this applet */
1236 if (spoe_appctx->task) {
1237 task_delete(spoe_appctx->task);
1238 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001239 }
1240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Notify all waiting streams */
1242 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001243 LIST_DEL(&ctx->list);
1244 LIST_INIT(&ctx->list);
1245 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001246 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001247 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 }
1249
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 /* If the applet was processing a fragmented frame, notify the
1251 * corresponding stream. */
1252 if (spoe_appctx->frag_ctx.ctx) {
1253 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001254 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001255 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001256 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001257 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1258 }
1259
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 /* Release allocated memory */
1261 spoe_release_buffer(&spoe_appctx->buffer,
1262 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001263 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001264
Christopher Faulet24289f22017-09-25 14:48:02 +02001265 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001266 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001267
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001269 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 LIST_DEL(&ctx->list);
1271 LIST_INIT(&ctx->list);
1272 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001276 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001277 LIST_DEL(&ctx->list);
1278 LIST_INIT(&ctx->list);
1279 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001280 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001281 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1282 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001283
1284 end:
1285 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001286 agent->rt[tid].frame_size = agent->max_frame_size;
1287 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1288 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001289}
1290
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001291static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001292spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001294 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001295 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001296 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001298
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 if (si->state <= SI_ST_CON) {
1300 si_applet_want_put(si);
1301 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1302 goto stop;
1303 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001304 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001305 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001306 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001307 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001308
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001310 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1311 " - Connection timed out\n",
1312 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1313 __FUNCTION__, appctx);
1314 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001317
Christopher Faulet42bfa462017-01-04 14:14:19 +01001318 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 SPOE_APPCTX(appctx)->task->expire =
1320 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321
Christopher Faulet8ef75252017-02-20 22:56:03 +01001322 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1323 * length. */
1324 buf = trash.str; frame = buf+4;
1325 ret = spoe_prepare_hahello_frame(appctx, frame,
1326 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001328 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001329
1330 switch (ret) {
1331 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001332 case 0: /* ignore => an error, cannot be ignored */
1333 goto exit;
1334
1335 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 goto stop;
1337
Christopher Faulet8ef75252017-02-20 22:56:03 +01001338 default:
1339 /* HELLO frame successfully sent, now wait for the
1340 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1342 goto next;
1343 }
1344
1345 next:
1346 return 0;
1347 stop:
1348 return 1;
1349 exit:
1350 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1351 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001352}
1353
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001354static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001357 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001358 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 char *frame;
1360 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001361
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001362
Christopher Fauletb067b062017-01-04 16:39:11 +01001363 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001365 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001366 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001367
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001369 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1370 " - Connection timed out\n",
1371 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1372 __FUNCTION__, appctx);
1373 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374 goto exit;
1375 }
1376
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377 frame = trash.str; trash.len = 0;
1378 ret = spoe_recv_frame(appctx, frame,
1379 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 if (ret > 1) {
1381 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1382 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1383 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001384 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 trash.len = ret + 4;
1386 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001387 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauleta1cda022016-12-21 08:58:06 +01001389 switch (ret) {
1390 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1393 goto next;
1394
1395 case 1: /* retry later */
1396 goto stop;
1397
1398 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001399 /* HELLO handshake is finished, set the idle timeout and
1400 * add the applet in the list of running applets. */
Christopher Faulet24289f22017-09-25 14:48:02 +02001401 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001403 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001404 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001405 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001406 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001407
1408 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001409 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001412
Christopher Fauleta1cda022016-12-21 08:58:06 +01001413 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 /* Do not forget to remove processed frame from the output buffer */
1415 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001416 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417
1418 SPOE_APPCTX(appctx)->task->expire =
1419 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001420 return 0;
1421 stop:
1422 return 1;
1423 exit:
1424 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1425 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001426}
1427
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001428
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001430spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001432 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1433 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001434 char *frame, *buf;
1435 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436
Christopher Faulet8ef75252017-02-20 22:56:03 +01001437 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1438 * length. */
1439 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001440
1441 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1442 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1443 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1444 SPOE_APPCTX(appctx)->max_frame_size);
1445 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001446 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001447 *skip = 1;
1448 ret = 1;
1449 goto end;
1450 }
1451 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001452 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001453 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1454 SPOE_APPCTX(appctx)->max_frame_size);
1455
1456 }
1457
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 if (ret > 1)
1459 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001460
Christopher Faulet8ef75252017-02-20 22:56:03 +01001461 switch (ret) {
1462 case -1: /* error */
1463 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1464 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001465
Christopher Faulet8ef75252017-02-20 22:56:03 +01001466 case 0: /* ignore */
1467 if (ctx == NULL)
1468 goto abort_frag_frame;
1469
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001470 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001471 LIST_DEL(&ctx->list);
1472 LIST_INIT(&ctx->list);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001473 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474 ctx->state = SPOE_CTX_ST_ERROR;
1475 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1476 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1477 break;
1478
1479 case 1: /* retry */
1480 *skip = 1;
1481 break;
1482
1483 default:
1484 if (ctx == NULL)
1485 goto abort_frag_frame;
1486
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001488 LIST_DEL(&ctx->list);
1489 LIST_INIT(&ctx->list);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001490 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1492 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1493 goto no_frag_frame_sent;
1494 else {
1495 *skip = 1;
1496 goto frag_frame_sent;
1497 }
1498 }
1499 goto end;
1500
1501 frag_frame_sent:
1502 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1503 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1504 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1505 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1507 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1508 goto end;
1509
1510 no_frag_frame_sent:
1511 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1512 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001513 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 }
1515 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1516 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1517 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1518 }
1519 else {
1520 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1521 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1522 }
1523 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1524 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1525 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001526 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001527
Christopher Faulet8ef75252017-02-20 22:56:03 +01001528 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1529 goto end;
1530
1531 abort_frag_frame:
1532 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1533 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1534 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1535 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1536 goto end;
1537
1538 end:
1539 return ret;
1540}
1541
1542static int
1543spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1544{
1545 struct spoe_context *ctx = NULL;
1546 char *frame;
1547 int ret;
1548
1549 frame = trash.str; trash.len = 0;
1550 ret = spoe_recv_frame(appctx, frame,
1551 SPOE_APPCTX(appctx)->max_frame_size);
1552 if (ret > 1) {
1553 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1554 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1555 goto end;
1556 }
1557 trash.len = ret + 4;
1558 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1559 }
1560 switch (ret) {
1561 case -1: /* error */
1562 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1563 break;
1564
1565 case 0: /* ignore */
1566 break;
1567
1568 case 1: /* retry */
1569 *skip = 1;
1570 break;
1571
1572 default:
1573 LIST_DEL(&ctx->list);
1574 LIST_INIT(&ctx->list);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001575 if (ctx->spoe_appctx) {
1576 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001577 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001578 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001579 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1580 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1581 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1582 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1583 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1584 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1585 }
1586 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1587 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1588
Christopher Faulet8ef75252017-02-20 22:56:03 +01001589 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1590 break;
1591 }
1592
1593 /* Do not forget to remove processed frame from the output buffer */
1594 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001595 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001596 end:
1597 return ret;
1598}
1599
1600static int
1601spoe_handle_processing_appctx(struct appctx *appctx)
1602{
1603 struct stream_interface *si = appctx->owner;
1604 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001605 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001606
1607 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1608 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1609 goto exit;
1610 }
1611
1612 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1613 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1614 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1615 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1616 goto next;
1617 }
1618
Christopher Faulet8f82b202018-01-24 16:23:03 +01001619
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001620 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8f82b202018-01-24 16:23:03 +01001621 " - process: fpa=%u/%u - appctx-state=%s - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001622 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001623 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1624 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
1625 SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001626
Christopher Faulet8f82b202018-01-24 16:23:03 +01001627 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1628 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001629
Christopher Faulet8f82b202018-01-24 16:23:03 +01001630 /* receiving_frame loop */
1631 while (!skip_receiving) {
1632 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1633 switch (ret) {
1634 case -1: /* error */
1635 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001636
Christopher Faulet8f82b202018-01-24 16:23:03 +01001637 case 0: /* ignore */
1638 active_r = 1;
1639 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001640
Christopher Faulet8f82b202018-01-24 16:23:03 +01001641 case 1: /* retry */
1642 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001643
Christopher Faulet8f82b202018-01-24 16:23:03 +01001644 default:
1645 active_r = 1;
1646 break;
1647 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001648 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001649
Christopher Faulet8f82b202018-01-24 16:23:03 +01001650 /* send_frame loop */
1651 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1652 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1653 switch (ret) {
1654 case -1: /* error */
1655 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001656
Christopher Faulet8f82b202018-01-24 16:23:03 +01001657 case 0: /* ignore */
1658 active_s++;
1659 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001660
Christopher Faulet8f82b202018-01-24 16:23:03 +01001661 case 1: /* retry */
1662 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001663
Christopher Faulet8f82b202018-01-24 16:23:03 +01001664 default:
1665 active_s++;
1666 break;
1667 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001668 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001669
Christopher Faulet8f82b202018-01-24 16:23:03 +01001670 if (active_s || active_r) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001671 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001672 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001673 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001674 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675
1676 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1677 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1678 }
1679 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1680 appctx->st0 = SPOE_APPCTX_ST_IDLE;
1681 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001682 }
1683 return 1;
1684
Christopher Faulet8f82b202018-01-24 16:23:03 +01001685 next:
1686 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1687 return 0;
1688
Christopher Fauleta1cda022016-12-21 08:58:06 +01001689 exit:
1690 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1691 return 0;
1692}
1693
1694static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001695spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696{
1697 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001698 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699 char *frame, *buf;
1700 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001701
Christopher Fauleta1cda022016-12-21 08:58:06 +01001702 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1703 goto exit;
1704
1705 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1706 goto exit;
1707
Christopher Faulet8ef75252017-02-20 22:56:03 +01001708 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1709 * length. */
1710 buf = trash.str; frame = buf+4;
1711 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1712 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001714 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
1716 switch (ret) {
1717 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001718 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001719 goto exit;
1720
1721 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722 goto stop;
1723
1724 default:
1725 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1726 " - disconnected by HAProxy (%d): %s\n",
1727 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001728 __FUNCTION__, appctx,
1729 SPOE_APPCTX(appctx)->status_code,
1730 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731
1732 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1733 goto next;
1734 }
1735
1736 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001737 SPOE_APPCTX(appctx)->task->expire =
1738 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 return 0;
1740 stop:
1741 return 1;
1742 exit:
1743 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1744 return 0;
1745}
1746
1747static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749{
1750 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001751 char *frame;
1752 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753
Christopher Fauletb067b062017-01-04 16:39:11 +01001754 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001757 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758
Christopher Fauletb067b062017-01-04 16:39:11 +01001759 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001762 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 frame = trash.str; trash.len = 0;
1765 ret = spoe_recv_frame(appctx, frame,
1766 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 trash.len = ret + 4;
1769 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 }
1771
1772 switch (ret) {
1773 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1775 " - error on frame (%s)\n",
1776 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001777 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780 goto exit;
1781
1782 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783 goto next;
1784
1785 case 1: /* retry */
1786 goto stop;
1787
1788 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001792 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001793 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1794 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 goto exit;
1796 }
1797
1798 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 /* Do not forget to remove processed frame from the output buffer */
1800 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001801 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803 return 0;
1804 stop:
1805 return 1;
1806 exit:
1807 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1808 return 0;
1809}
1810
1811/* I/O Handler processing messages exchanged with the agent */
1812static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 struct stream_interface *si = appctx->owner;
1816 struct spoe_agent *agent;
1817
1818 if (SPOE_APPCTX(appctx) == NULL)
1819 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1822 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001823
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 switchstate:
1825 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1826 " - appctx-state=%s\n",
1827 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1828 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1829
1830 switch (appctx->st0) {
1831 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001832 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 goto out;
1834 goto switchstate;
1835
1836 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 goto out;
1839 goto switchstate;
1840
1841 case SPOE_APPCTX_ST_IDLE:
1842 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001843 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001844 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 SPOE_APPCTX(appctx)->task->expire =
1846 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001848 goto switchstate;
1849 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001850 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1852 /* fall through */
1853
1854 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001855 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1856 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 goto out;
1859 goto switchstate;
1860
1861 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 goto out;
1864 goto switchstate;
1865
1866 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto out;
1869 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001870
1871 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 appctx->st0 = SPOE_APPCTX_ST_END;
1873 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1874
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001875 si_shutw(si);
1876 si_shutr(si);
1877 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001878 /* fall through */
1879
1880 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001881 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001882 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001884 if (stopping)
1885 spoe_wakeup_appctx(appctx);
1886
Christopher Faulet42bfa462017-01-04 14:14:19 +01001887 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1888 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001889 si_oc(si)->flags |= CF_READ_DONTWAIT;
1890 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001891}
1892
1893struct applet spoe_applet = {
1894 .obj_type = OBJ_TYPE_APPLET,
1895 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001896 .fct = spoe_handle_appctx,
1897 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001898};
1899
1900/* Create a SPOE applet. On success, the created applet is returned, else
1901 * NULL. */
1902static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001904{
1905 struct appctx *appctx;
1906 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001908
Emeric Brun1138fd02017-06-19 12:38:55 +02001909 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001910 goto out_error;
1911
Willy Tarreaubafbe012017-11-24 17:34:44 +01001912 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001913 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001915 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001916
Christopher Faulet42bfa462017-01-04 14:14:19 +01001917 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001918 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001919 goto out_free_spoe_appctx;
1920
1921 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001922 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001923 SPOE_APPCTX(appctx)->task->context = appctx;
1924 SPOE_APPCTX(appctx)->agent = conf->agent;
1925 SPOE_APPCTX(appctx)->version = 0;
1926 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1927 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001928 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001929 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001930 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001931
1932 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1933 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001934 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001935
1936 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1937 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938
Willy Tarreau5820a362016-12-22 15:59:02 +01001939 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940 if (!sess)
1941 goto out_free_spoe;
1942
Willy Tarreau87787ac2017-08-28 16:22:54 +02001943 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 goto out_free_sess;
1945
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001946 stream_set_backend(strm, conf->agent->b.be);
1947
1948 /* applet is waiting for data */
1949 si_applet_cant_get(&strm->si[0]);
1950 appctx_wakeup(appctx);
1951
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001952 strm->do_log = NULL;
1953 strm->res.flags |= CF_READ_DONTWAIT;
1954
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001955 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001956 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001957 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001958 conf->agent->rt[tid].applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001959
Emeric Brunc60def82017-09-27 14:59:38 +02001960 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001961 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001962 return appctx;
1963
1964 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965 out_free_sess:
1966 session_free(sess);
1967 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001968 task_free(SPOE_APPCTX(appctx)->task);
1969 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001970 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971 out_free_appctx:
1972 appctx_free(appctx);
1973 out_error:
1974 return NULL;
1975}
1976
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001978spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979{
1980 struct spoe_config *conf = FLT_CONF(ctx->filter);
1981 struct spoe_agent *agent = conf->agent;
1982 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001983 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984
Christopher Fauleta1cda022016-12-21 08:58:06 +01001985 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet42097792018-01-24 15:49:45 +01001986 if (agent->rt[tid].applets_idle &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01001987 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001988 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001989
1990 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001991 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
1993 ctx->strm);
1994
Christopher Fauleta1cda022016-12-21 08:58:06 +01001995 /* Do not try to create a new applet if there is no server up for the
1996 * agent's backend. */
1997 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
1998 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
1999 " - cannot create SPOE appctx: no server up\n",
2000 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2001 __FUNCTION__, ctx->strm);
2002 goto end;
2003 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004
Christopher Fauleta1cda022016-12-21 08:58:06 +01002005 /* Do not try to create a new applet if we have reached the maximum of
2006 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002007 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002008 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002009 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2010 " - cannot create SPOE appctx: max CPS reached\n",
2011 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2012 __FUNCTION__, ctx->strm);
2013 goto end;
2014 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015 }
2016
Christopher Faulet8ef75252017-02-20 22:56:03 +01002017 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002018 if (appctx == NULL) {
2019 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2020 " - failed to create SPOE appctx\n",
2021 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2022 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002023 send_log(ctx->strm->be, LOG_EMERG,
2024 "SPOE: [%s] failed to create SPOE applet\n",
2025 agent->id);
2026
Christopher Fauleta1cda022016-12-21 08:58:06 +01002027 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028 }
2029
Christopher Fauleta1cda022016-12-21 08:58:06 +01002030 /* Increase the per-process number of cumulated connections */
2031 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002032 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 end:
2035 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002036 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002037 ctx->status_code = SPOE_CTX_ERR_RES;
2038 return -1;
2039 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002041 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002042 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002043
2044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002045 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002046 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002047 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002048 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002049 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002050
Christopher Fauleta1cda022016-12-21 08:58:06 +01002051 /* Finally try to wakeup the first IDLE applet found and move it at the
2052 * end of the list. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002053 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) {
Christopher Faulet42bfa462017-01-04 14:14:19 +01002054 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002056 spoe_wakeup_appctx(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002057 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002058 LIST_DEL(&spoe_appctx->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02002059 LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002060 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 break;
2062 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065}
2066
2067/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002068 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002069 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002070/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2071 * fragmented_content. If the next message can be processed, it returns 0. If
2072 * the message is too big, it returns -1.*/
2073static int
2074spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2075 struct spoe_message *msg, int dir,
2076 char **buf, char *end)
2077{
2078 struct sample *smp;
2079 struct spoe_arg *arg;
2080 int ret;
2081
2082 if (msg->cond) {
2083 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2084 ret = acl_pass(ret);
2085 if (msg->cond->pol == ACL_COND_UNLESS)
2086 ret = !ret;
2087
2088 /* the rule does not match */
2089 if (!ret)
2090 goto next;
2091 }
2092
2093 /* Resume encoding of a SPOE argument */
2094 if (ctx->frag_ctx.curarg != NULL) {
2095 arg = ctx->frag_ctx.curarg;
2096 goto encode_argument;
2097 }
2098
2099 if (ctx->frag_ctx.curoff != UINT_MAX)
2100 goto encode_msg_payload;
2101
2102 /* Check if there is enough space for the message name and the
2103 * number of arguments. It implies <msg->id_len> is encoded on 2
2104 * bytes, at most (< 2288). */
2105 if (*buf + 2 + msg->id_len + 1 > end)
2106 goto too_big;
2107
2108 /* Encode the message name */
2109 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2110 goto too_big;
2111
2112 /* Set the number of arguments for this message */
2113 **buf = msg->nargs;
2114 (*buf)++;
2115
2116 ctx->frag_ctx.curoff = 0;
2117 encode_msg_payload:
2118
2119 /* Loop on arguments */
2120 list_for_each_entry(arg, &msg->args, list) {
2121 ctx->frag_ctx.curarg = arg;
2122 ctx->frag_ctx.curoff = UINT_MAX;
2123
2124 encode_argument:
2125 if (ctx->frag_ctx.curoff != UINT_MAX)
2126 goto encode_arg_value;
2127
2128 /* Encode the arguement name as a string. It can by NULL */
2129 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2130 goto too_big;
2131
2132 ctx->frag_ctx.curoff = 0;
2133 encode_arg_value:
2134
2135 /* Fetch the arguement value */
2136 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2137 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2138 if (ret == -1 || ctx->frag_ctx.curoff)
2139 goto too_big;
2140 }
2141
2142 next:
2143 return 0;
2144
2145 too_big:
2146 return -1;
2147}
2148
Christopher Fauletc718b822017-09-21 16:50:56 +02002149/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2150 * handle fragmented content. On success it returns 1. If an error occurred, -1
2151 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2152 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002153static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002154spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002155 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002156{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002157 struct spoe_config *conf = FLT_CONF(ctx->filter);
2158 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002159 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002160 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002161
Christopher Faulet8ef75252017-02-20 22:56:03 +01002162 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002163 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002164
Christopher Fauletc718b822017-09-21 16:50:56 +02002165 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2166 /* Resume encoding of a SPOE message */
2167 if (ctx->frag_ctx.curmsg != NULL) {
2168 msg = ctx->frag_ctx.curmsg;
2169 goto encode_evt_message;
2170 }
2171
2172 list_for_each_entry(msg, messages, by_evt) {
2173 ctx->frag_ctx.curmsg = msg;
2174 ctx->frag_ctx.curarg = NULL;
2175 ctx->frag_ctx.curoff = UINT_MAX;
2176
2177 encode_evt_message:
2178 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2179 goto too_big;
2180 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002181 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002182 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2183 /* Resume encoding of a SPOE message */
2184 if (ctx->frag_ctx.curmsg != NULL) {
2185 msg = ctx->frag_ctx.curmsg;
2186 goto encode_grp_message;
2187 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002188
Christopher Fauletc718b822017-09-21 16:50:56 +02002189 list_for_each_entry(msg, messages, by_grp) {
2190 ctx->frag_ctx.curmsg = msg;
2191 ctx->frag_ctx.curarg = NULL;
2192 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002193
Christopher Fauletc718b822017-09-21 16:50:56 +02002194 encode_grp_message:
2195 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2196 goto too_big;
2197 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002198 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002199 else
2200 goto skip;
2201
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002202
Christopher Faulet57583e42017-09-04 15:41:09 +02002203 /* nothing has been encoded for an unfragmented payload */
2204 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2205 goto skip;
2206
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002207 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002208 " - encode %s messages - spoe_appctx=%p"
2209 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002210 (int)now.tv_sec, (int)now.tv_usec,
2211 agent->id, __FUNCTION__, s,
2212 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002213 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002214 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002215
Christopher Faulet8ef75252017-02-20 22:56:03 +01002216 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002217 ctx->frag_ctx.curmsg = NULL;
2218 ctx->frag_ctx.curarg = NULL;
2219 ctx->frag_ctx.curoff = 0;
2220 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002221
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002222 return 1;
2223
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002224 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002225 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2226 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2227 return -1;
2228 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002229
2230 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002231 " - encode fragmented messages - spoe_appctx=%p"
2232 " - curmsg=%p - curarg=%p - curoff=%u"
2233 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002235 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002236 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002237 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002238
Christopher Faulet8ef75252017-02-20 22:56:03 +01002239 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002240 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2241 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2242 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002243
2244 skip:
2245 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2246 " - skip the frame because nothing has been encoded\n",
2247 (int)now.tv_sec, (int)now.tv_usec,
2248 agent->id, __FUNCTION__, s);
2249 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002250}
2251
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252
2253/***************************************************************************
2254 * Functions that handle SPOE actions
2255 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002256/* Helper function to set a variable */
2257static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002259 struct sample *smp)
2260{
2261 struct spoe_config *conf = FLT_CONF(ctx->filter);
2262 struct spoe_agent *agent = conf->agent;
2263 char varname[64];
2264
2265 memset(varname, 0, sizeof(varname));
2266 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2267 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002268 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2269 vars_set_by_name(varname, len, smp);
2270 else
2271 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002272}
2273
2274/* Helper function to unset a variable */
2275static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002276spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002277 struct sample *smp)
2278{
2279 struct spoe_config *conf = FLT_CONF(ctx->filter);
2280 struct spoe_agent *agent = conf->agent;
2281 char varname[64];
2282
2283 memset(varname, 0, sizeof(varname));
2284 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2285 scope, agent->var_pfx, len, name);
2286 vars_unset_by_name_ifexist(varname, len, smp);
2287}
2288
2289
Christopher Faulet8ef75252017-02-20 22:56:03 +01002290static inline int
2291spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2292 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002293{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002294 char *str, *scope, *p = *buf;
2295 struct sample smp;
2296 uint64_t sz;
2297 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298
Christopher Faulet8ef75252017-02-20 22:56:03 +01002299 if (p + 2 >= end)
2300 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301
Christopher Faulet8ef75252017-02-20 22:56:03 +01002302 /* SET-VAR requires 3 arguments */
2303 if (*p++ != 3)
2304 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002305
Christopher Faulet8ef75252017-02-20 22:56:03 +01002306 switch (*p++) {
2307 case SPOE_SCOPE_PROC: scope = "proc"; break;
2308 case SPOE_SCOPE_SESS: scope = "sess"; break;
2309 case SPOE_SCOPE_TXN : scope = "txn"; break;
2310 case SPOE_SCOPE_REQ : scope = "req"; break;
2311 case SPOE_SCOPE_RES : scope = "res"; break;
2312 default: goto skip;
2313 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002314
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2316 goto skip;
2317 memset(&smp, 0, sizeof(smp));
2318 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002319
Christopher Faulet8ef75252017-02-20 22:56:03 +01002320 if (spoe_decode_data(&p, end, &smp) == -1)
2321 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002322
Christopher Faulet8ef75252017-02-20 22:56:03 +01002323 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2324 " - set-var '%s.%s.%.*s'\n",
2325 (int)now.tv_sec, (int)now.tv_usec,
2326 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2327 __FUNCTION__, s, scope,
2328 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2329 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002332
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333 ret = (p - *buf);
2334 *buf = p;
2335 return ret;
2336 skip:
2337 return 0;
2338}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340static inline int
2341spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2342 char **buf, char *end, int dir)
2343{
2344 char *str, *scope, *p = *buf;
2345 struct sample smp;
2346 uint64_t sz;
2347 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 if (p + 2 >= end)
2350 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002351
Christopher Faulet8ef75252017-02-20 22:56:03 +01002352 /* UNSET-VAR requires 2 arguments */
2353 if (*p++ != 2)
2354 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 switch (*p++) {
2357 case SPOE_SCOPE_PROC: scope = "proc"; break;
2358 case SPOE_SCOPE_SESS: scope = "sess"; break;
2359 case SPOE_SCOPE_TXN : scope = "txn"; break;
2360 case SPOE_SCOPE_REQ : scope = "req"; break;
2361 case SPOE_SCOPE_RES : scope = "res"; break;
2362 default: goto skip;
2363 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2366 goto skip;
2367 memset(&smp, 0, sizeof(smp));
2368 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369
Christopher Faulet8ef75252017-02-20 22:56:03 +01002370 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2371 " - unset-var '%s.%s.%.*s'\n",
2372 (int)now.tv_sec, (int)now.tv_usec,
2373 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2374 __FUNCTION__, s, scope,
2375 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2376 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002377
Christopher Faulet8ef75252017-02-20 22:56:03 +01002378 spoe_unset_var(ctx, scope, str, sz, &smp);
2379
2380 ret = (p - *buf);
2381 *buf = p;
2382 return ret;
2383 skip:
2384 return 0;
2385}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002386
Christopher Faulet8ef75252017-02-20 22:56:03 +01002387/* Process SPOE actions for a specific event. It returns 1 on success. If an
2388 * error occurred, 0 is returned. */
2389static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002390spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002391{
2392 char *p, *end;
2393 int ret;
2394
2395 p = ctx->buffer->p;
2396 end = p + ctx->buffer->i;
2397
2398 while (p < end) {
2399 enum spoe_action_type type;
2400
2401 type = *p++;
2402 switch (type) {
2403 case SPOE_ACT_T_SET_VAR:
2404 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2405 if (!ret)
2406 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002407 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 case SPOE_ACT_T_UNSET_VAR:
2410 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2411 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413 break;
2414
2415 default:
2416 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417 }
2418 }
2419
2420 return 1;
2421 skip:
2422 return 0;
2423}
2424
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002425/***************************************************************************
2426 * Functions that process SPOE events
2427 **************************************************************************/
2428static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002429spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002430{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002431 /* If a process is already started for this SPOE context, retry
2432 * later. */
2433 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002434 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002435
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002436 agent->rt[tid].processing++;
2437
Christopher Fauleta1cda022016-12-21 08:58:06 +01002438 /* Set the right flag to prevent request and response processing
2439 * in same time. */
2440 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2441 ? SPOE_CTX_FL_REQ_PROCESS
2442 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002443 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002444}
2445
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002446static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002447spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002448{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002449 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002450
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002451 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2452 return;
2453
Christopher Fauletfce747b2018-01-24 15:59:32 +01002454 if (sa && sa->frag_ctx.ctx == ctx) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002455 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002456 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002457 }
2458
Christopher Fauleta1cda022016-12-21 08:58:06 +01002459 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002460 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002461 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002462
Christopher Fauletb067b062017-01-04 16:39:11 +01002463 ctx->status_code = 0;
2464
Christopher Fauleta1cda022016-12-21 08:58:06 +01002465 /* Reset processing timer */
2466 ctx->process_exp = TICK_ETERNITY;
2467
Christopher Faulet8ef75252017-02-20 22:56:03 +01002468 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002469
Christopher Fauletfce747b2018-01-24 15:59:32 +01002470 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002471 ctx->frag_ctx.curmsg = NULL;
2472 ctx->frag_ctx.curarg = NULL;
2473 ctx->frag_ctx.curoff = 0;
2474 ctx->frag_ctx.flags = 0;
2475
Christopher Fauleta1cda022016-12-21 08:58:06 +01002476 if (!LIST_ISEMPTY(&ctx->list)) {
2477 LIST_DEL(&ctx->list);
2478 LIST_INIT(&ctx->list);
2479 }
2480}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002482static void
2483spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2484 struct spoe_context *ctx, int dir)
2485{
2486 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002487 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002488
2489 if (agent->var_on_error) {
2490 struct sample smp;
2491
2492 memset(&smp, 0, sizeof(smp));
2493 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2494 smp.data.u.sint = ctx->status_code;
2495 smp.data.type = SMP_T_BOOL;
2496
2497 spoe_set_var(ctx, "txn", agent->var_on_error,
2498 strlen(agent->var_on_error), &smp);
2499 }
2500 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2501 " - failed to process messages: code=%u\n",
2502 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2503 __FUNCTION__, s, ctx->status_code);
2504 send_log(ctx->strm->be, LOG_WARNING,
2505 "SPOE: [%s] failed to process messages: code=%u\n",
2506 agent->id, ctx->status_code);
2507
2508 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2509 ? SPOE_CTX_ST_READY
2510 : SPOE_CTX_ST_NONE);
2511}
2512
Christopher Faulet58d03682017-09-21 16:57:24 +02002513/* Process a list of SPOE messages. First, this functions will process messages
2514 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2515 * to process corresponding actions. During all the processing, it returns 0
2516 * and it returns 1 when the processing is finished. If an error occurred, -1
2517 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002518static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002519spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2520 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002521{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002522 struct spoe_config *conf = FLT_CONF(ctx->filter);
2523 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002524 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002525
2526 if (ctx->state == SPOE_CTX_ST_ERROR)
2527 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002528
2529 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2530 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002531 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002532 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002533 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002534 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002535 goto error;
2536 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002537
2538 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002539 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002540 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002541 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002542 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002544 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002545 goto skip;
2546 }
2547 }
2548
Christopher Fauletf7a30922016-11-10 15:04:51 +01002549 if (!tick_isset(ctx->process_exp)) {
2550 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2551 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2552 ctx->process_exp);
2553 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002554 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002555 if (!ret)
2556 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002557
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002558 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002560 }
2561
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002562 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002563 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002564 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002565 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002566 if (ret < 0)
2567 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002568 if (!ret)
2569 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002570 if (spoe_queue_context(ctx) < 0)
2571 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002572 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2573 }
2574
2575 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002576 if (ctx->spoe_appctx)
2577 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002578 ret = 0;
2579 goto out;
2580 }
2581
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002582 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2583 ret = 0;
2584 goto out;
2585 }
2586
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002587 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002588 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002589 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002590 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002591 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002593 }
2594
2595 out:
2596 return ret;
2597
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002598 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002599 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600 ret = 1;
2601 goto end;
2602
2603 skip:
2604 ctx->state = SPOE_CTX_ST_READY;
2605 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002606
Christopher Fauleta1cda022016-12-21 08:58:06 +01002607 end:
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002608 spoe_stop_processing(agent, ctx);
Christopher Faulet58d03682017-09-21 16:57:24 +02002609 return ret;
2610}
2611
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002612/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2613 * See spoe_process_message for details. */
2614static int
2615spoe_process_group(struct stream *s, struct spoe_context *ctx,
2616 struct spoe_group *group, int dir)
2617{
2618 int ret;
2619
2620 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2621 " - ctx-state=%s - Process messages for group=%s\n",
2622 (int)now.tv_sec, (int)now.tv_usec,
2623 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2624 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2625 group->id);
2626
2627 if (LIST_ISEMPTY(&group->messages))
2628 return 1;
2629
2630 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2631 return ret;
2632}
2633
Christopher Faulet58d03682017-09-21 16:57:24 +02002634/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2635 * See spoe_process_message for details. */
2636static int
2637spoe_process_event(struct stream *s, struct spoe_context *ctx,
2638 enum spoe_event ev)
2639{
2640 int dir, ret;
2641
2642 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002643 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 (int)now.tv_sec, (int)now.tv_usec,
2645 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2646 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2647 spoe_event_str[ev]);
2648
2649 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2650
2651 if (LIST_ISEMPTY(&(ctx->events[ev])))
2652 return 1;
2653
2654 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002655 return ret;
2656}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002657
2658/***************************************************************************
2659 * Functions that create/destroy SPOE contexts
2660 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002662spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002663{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002664 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 return 1;
2666
Christopher Faulet4596fb72017-01-11 14:05:19 +01002667 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002668 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002669 LIST_DEL(&buffer_wait->list);
2670 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002671 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002672 }
2673
Christopher Faulet4596fb72017-01-11 14:05:19 +01002674 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002675 return 1;
2676
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002677 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002678 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002679 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002680 return 0;
2681}
2682
2683static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002684spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002685{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002686 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002687 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002688 LIST_DEL(&buffer_wait->list);
2689 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002690 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002691 }
2692
2693 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002694 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002695 b_free(buf);
2696 offer_buffers(buffer_wait->target,
2697 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 }
2699}
2700
Christopher Faulet4596fb72017-01-11 14:05:19 +01002701static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002702spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002703{
2704 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2705 return 1;
2706}
2707
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002708static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002709spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002710{
2711 struct spoe_config *conf = FLT_CONF(filter);
2712 struct spoe_context *ctx;
2713
Willy Tarreaubafbe012017-11-24 17:34:44 +01002714 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002715 if (ctx == NULL) {
2716 return NULL;
2717 }
2718 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002719 ctx->filter = filter;
2720 ctx->state = SPOE_CTX_ST_NONE;
2721 ctx->status_code = SPOE_CTX_ERR_NONE;
2722 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002723 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002724 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002725 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002726 LIST_INIT(&ctx->buffer_wait.list);
2727 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002728 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002729 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002730
Christopher Fauletf7a30922016-11-10 15:04:51 +01002731 ctx->stream_id = 0;
2732 ctx->frame_id = 1;
2733 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002734
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002735 ctx->strm = s;
2736 ctx->state = SPOE_CTX_ST_READY;
2737 filter->ctx = ctx;
2738
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002739 return ctx;
2740}
2741
2742static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002743spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002745 struct spoe_config *conf = FLT_CONF(filter);
2746 struct spoe_context *ctx = filter->ctx;
2747
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002748 if (!ctx)
2749 return;
2750
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002751 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002752 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002753 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002754}
2755
2756static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002757spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002758{
2759 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002760 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002761}
2762
2763
2764/***************************************************************************
2765 * Hooks that manage the filter lifecycle (init/check/deinit)
2766 **************************************************************************/
2767/* Signal handler: Do a soft stop, wakeup SPOE applet */
2768static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002769spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002770{
2771 struct proxy *p;
2772
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002773 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002774 while (p) {
2775 struct flt_conf *fconf;
2776
2777 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002778 struct spoe_config *conf;
2779 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002780 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002781 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002782
Christopher Faulet3b386a32017-02-23 10:17:15 +01002783 if (fconf->id != spoe_filter_id)
2784 continue;
2785
2786 conf = fconf->conf;
2787 agent = conf->agent;
2788
Christopher Faulet24289f22017-09-25 14:48:02 +02002789 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002790 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002791 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2792 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002793 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002794 }
2795 }
2796 p = p->next;
2797 }
2798}
2799
2800
2801/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2802static int
2803spoe_init(struct proxy *px, struct flt_conf *fconf)
2804{
2805 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002806
2807 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2808 init_new_proxy(&conf->agent_fe);
2809 conf->agent_fe.parent = conf->agent;
2810 conf->agent_fe.last_change = now.tv_sec;
2811 conf->agent_fe.id = conf->agent->id;
2812 conf->agent_fe.cap = PR_CAP_FE;
2813 conf->agent_fe.mode = PR_MODE_TCP;
2814 conf->agent_fe.maxconn = 0;
2815 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2816 conf->agent_fe.conn_retries = CONN_RETRIES;
2817 conf->agent_fe.accept = frontend_accept;
2818 conf->agent_fe.srv = NULL;
2819 conf->agent_fe.timeout.client = TICK_ETERNITY;
2820 conf->agent_fe.default_target = &spoe_applet.obj_type;
2821 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2822
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002823 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002824 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002825 sighandler_registered = 1;
2826 }
2827
2828 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002829}
2830
2831/* Free ressources allocated by the SPOE filter. */
2832static void
2833spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2834{
2835 struct spoe_config *conf = fconf->conf;
2836
2837 if (conf) {
2838 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002839
Christopher Faulet8ef75252017-02-20 22:56:03 +01002840 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002841 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002842 free(conf);
2843 }
2844 fconf->conf = NULL;
2845}
2846
2847/* Check configuration of a SPOE filter for a specified proxy.
2848 * Return 1 on error, else 0. */
2849static int
2850spoe_check(struct proxy *px, struct flt_conf *fconf)
2851{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002852 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853 struct spoe_config *conf = fconf->conf;
2854 struct proxy *target;
2855
Christopher Faulet7ee86672017-09-19 11:08:28 +02002856 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2857 * are uniq */
2858 list_for_each_entry(f, &px->filter_configs, list) {
2859 struct spoe_config *c = f->conf;
2860
2861 /* This is not an SPOE filter */
2862 if (f->id != spoe_filter_id)
2863 continue;
2864 /* This is the current SPOE filter */
2865 if (f == fconf)
2866 continue;
2867
2868 /* Check engine Id. It should be uniq */
2869 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002870 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2871 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002872 return 1;
2873 }
2874 }
2875
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002876 target = proxy_be_by_name(conf->agent->b.name);
2877 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002878 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2879 " declared at %s:%d.\n",
2880 px->id, conf->agent->b.name, conf->agent->id,
2881 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882 return 1;
2883 }
2884 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002885 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2886 " at %s:%d does not support HTTP mode.\n",
2887 px->id, target->id, conf->agent->id,
2888 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002889 return 1;
2890 }
2891
2892 free(conf->agent->b.name);
2893 conf->agent->b.name = NULL;
2894 conf->agent->b.be = target;
2895 return 0;
2896}
2897
2898/**************************************************************************
2899 * Hooks attached to a stream
2900 *************************************************************************/
2901/* Called when a filter instance is created and attach to a stream. It creates
2902 * the context that will be used to process this stream. */
2903static int
2904spoe_start(struct stream *s, struct filter *filter)
2905{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002906 struct spoe_config *conf = FLT_CONF(filter);
2907 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 struct spoe_context *ctx;
2909
2910 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002911 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002912 __FUNCTION__, s);
2913
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002914 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002915 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2916 " - failed to create SPOE context\n",
2917 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002918 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002920 "SPOE: [%s] failed to create SPOE context\n",
2921 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002922 return 0;
2923 }
2924
Christopher Faulet11610f32017-09-21 10:23:10 +02002925 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002926 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2927
Christopher Faulet11610f32017-09-21 10:23:10 +02002928 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002929 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2930
Christopher Faulet11610f32017-09-21 10:23:10 +02002931 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002932 filter->pre_analyzers |= AN_RES_INSPECT;
2933
Christopher Faulet11610f32017-09-21 10:23:10 +02002934 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2936
Christopher Faulet11610f32017-09-21 10:23:10 +02002937 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002938 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2939
Christopher Faulet11610f32017-09-21 10:23:10 +02002940 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002941 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2942
2943 return 1;
2944}
2945
2946/* Called when a filter instance is detached from a stream. It release the
2947 * attached SPOE context. */
2948static void
2949spoe_stop(struct stream *s, struct filter *filter)
2950{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002951 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2952 (int)now.tv_sec, (int)now.tv_usec,
2953 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2954 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002955 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956}
2957
Christopher Fauletf7a30922016-11-10 15:04:51 +01002958
2959/*
2960 * Called when the stream is woken up because of expired timer.
2961 */
2962static void
2963spoe_check_timeouts(struct stream *s, struct filter *filter)
2964{
2965 struct spoe_context *ctx = filter->ctx;
2966
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002967 if (tick_is_expired(ctx->process_exp, now_ms)) {
2968 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002969 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002970 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002971}
2972
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002973/* Called when we are ready to filter data on a channel */
2974static int
2975spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2976{
2977 struct spoe_context *ctx = filter->ctx;
2978 int ret = 1;
2979
2980 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2981 " - ctx-flags=0x%08x\n",
2982 (int)now.tv_sec, (int)now.tv_usec,
2983 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2984 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2985
Christopher Fauletb067b062017-01-04 16:39:11 +01002986 if (ctx->state == SPOE_CTX_ST_NONE)
2987 goto out;
2988
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989 if (!(chn->flags & CF_ISRESP)) {
2990 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2991 chn->analysers |= AN_REQ_INSPECT_FE;
2992 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2993 chn->analysers |= AN_REQ_INSPECT_BE;
2994
2995 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2996 goto out;
2997
2998 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002999 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003000 if (!ret)
3001 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3003 }
3004 else {
3005 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3006 chn->analysers |= AN_RES_INSPECT;
3007
3008 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3009 goto out;
3010
Christopher Faulet8ef75252017-02-20 22:56:03 +01003011 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003012 if (!ret) {
3013 channel_dont_read(chn);
3014 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003015 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003016 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003017 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018 }
3019
3020 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021 return ret;
3022}
3023
3024/* Called before a processing happens on a given channel */
3025static int
3026spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3027 struct channel *chn, unsigned an_bit)
3028{
3029 struct spoe_context *ctx = filter->ctx;
3030 int ret = 1;
3031
3032 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3033 " - ctx-flags=0x%08x - ana=0x%08x\n",
3034 (int)now.tv_sec, (int)now.tv_usec,
3035 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3036 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3037 ctx->flags, an_bit);
3038
Christopher Fauletb067b062017-01-04 16:39:11 +01003039 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003040 goto out;
3041
3042 switch (an_bit) {
3043 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003044 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 break;
3046 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003047 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 break;
3049 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003050 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003051 break;
3052 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003053 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003054 break;
3055 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003056 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057 break;
3058 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003059 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003060 break;
3061 }
3062
3063 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003064 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003065 channel_dont_read(chn);
3066 channel_dont_close(chn);
3067 }
3068 return ret;
3069}
3070
3071/* Called when the filtering on the channel ends. */
3072static int
3073spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3074{
3075 struct spoe_context *ctx = filter->ctx;
3076
3077 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3078 " - ctx-flags=0x%08x\n",
3079 (int)now.tv_sec, (int)now.tv_usec,
3080 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3081 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3082
3083 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003084 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003085 }
3086
3087 return 1;
3088}
3089
3090/********************************************************************
3091 * Functions that manage the filter initialization
3092 ********************************************************************/
3093struct flt_ops spoe_ops = {
3094 /* Manage SPOE filter, called for each filter declaration */
3095 .init = spoe_init,
3096 .deinit = spoe_deinit,
3097 .check = spoe_check,
3098
3099 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003100 .attach = spoe_start,
3101 .detach = spoe_stop,
3102 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103
3104 /* Handle channels activity */
3105 .channel_start_analyze = spoe_start_analyze,
3106 .channel_pre_analyze = spoe_chn_pre_analyze,
3107 .channel_end_analyze = spoe_end_analyze,
3108};
3109
3110
3111static int
3112cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3113{
3114 const char *err;
3115 int i, err_code = 0;
3116
3117 if ((cfg_scope == NULL && curengine != NULL) ||
3118 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003119 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003120 goto out;
3121
3122 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3123 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003124 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3125 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003126 err_code |= ERR_ALERT | ERR_ABORT;
3127 goto out;
3128 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003129 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3130 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003131 goto out;
3132 }
3133
3134 err = invalid_char(args[1]);
3135 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003136 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3137 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003138 err_code |= ERR_ALERT | ERR_ABORT;
3139 goto out;
3140 }
3141
3142 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003143 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3144 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003145 err_code |= ERR_ALERT | ERR_ABORT;
3146 goto out;
3147 }
3148 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003149 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003150 err_code |= ERR_ALERT | ERR_ABORT;
3151 goto out;
3152 }
3153
3154 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003155
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003156 curagent->conf.file = strdup(file);
3157 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003158
3159 curagent->timeout.hello = TICK_ETERNITY;
3160 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003161 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003162
3163 curagent->engine_id = NULL;
3164 curagent->var_pfx = NULL;
3165 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003166 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3167 if (global.nbthread == 1)
3168 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003169 curagent->cps_max = 0;
3170 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003171 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003172 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003173
3174 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003175 LIST_INIT(&curagent->events[i]);
3176 LIST_INIT(&curagent->groups);
3177 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003178
Christopher Faulet24289f22017-09-25 14:48:02 +02003179 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003180 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003181 err_code |= ERR_ALERT | ERR_ABORT;
3182 goto out;
3183 }
3184 for (i = 0; i < global.nbthread; ++i) {
3185 curagent->rt[i].frame_size = curagent->max_frame_size;
3186 curagent->rt[i].applets_act = 0;
3187 curagent->rt[i].applets_idle = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003188 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003189 LIST_INIT(&curagent->rt[i].applets);
3190 LIST_INIT(&curagent->rt[i].sending_queue);
3191 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003192 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003193 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003194 }
3195 else if (!strcmp(args[0], "use-backend")) {
3196 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003197 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3198 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003199 err_code |= ERR_ALERT | ERR_FATAL;
3200 goto out;
3201 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003202 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003203 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003204 free(curagent->b.name);
3205 curagent->b.name = strdup(args[1]);
3206 }
3207 else if (!strcmp(args[0], "messages")) {
3208 int cur_arg = 1;
3209 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003210 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211
Christopher Faulet11610f32017-09-21 10:23:10 +02003212 list_for_each_entry(ph, &curmphs, list) {
3213 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003214 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3215 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003216 err_code |= ERR_ALERT | ERR_FATAL;
3217 goto out;
3218 }
3219 }
3220
Christopher Faulet11610f32017-09-21 10:23:10 +02003221 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003222 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003223 err_code |= ERR_ALERT | ERR_ABORT;
3224 goto out;
3225 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003226 ph->id = strdup(args[cur_arg]);
3227 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 cur_arg++;
3229 }
3230 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003231 else if (!strcmp(args[0], "groups")) {
3232 int cur_arg = 1;
3233 while (*args[cur_arg]) {
3234 struct spoe_placeholder *ph = NULL;
3235
3236 list_for_each_entry(ph, &curgphs, list) {
3237 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003238 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3239 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003240 err_code |= ERR_ALERT | ERR_FATAL;
3241 goto out;
3242 }
3243 }
3244
3245 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003246 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003247 err_code |= ERR_ALERT | ERR_ABORT;
3248 goto out;
3249 }
3250 ph->id = strdup(args[cur_arg]);
3251 LIST_ADDQ(&curgphs, &ph->list);
3252 cur_arg++;
3253 }
3254 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 else if (!strcmp(args[0], "timeout")) {
3256 unsigned int *tv = NULL;
3257 const char *res;
3258 unsigned timeout;
3259
3260 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003261 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3262 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003263 err_code |= ERR_ALERT | ERR_FATAL;
3264 goto out;
3265 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003266 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3267 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003268 if (!strcmp(args[1], "hello"))
3269 tv = &curagent->timeout.hello;
3270 else if (!strcmp(args[1], "idle"))
3271 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003272 else if (!strcmp(args[1], "processing"))
3273 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003274 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003275 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3276 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003277 err_code |= ERR_ALERT | ERR_FATAL;
3278 goto out;
3279 }
3280 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003281 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3282 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003283 err_code |= ERR_ALERT | ERR_FATAL;
3284 goto out;
3285 }
3286 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3287 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003288 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3289 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003290 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003291 goto out;
3292 }
3293 *tv = MS_TO_TICKS(timeout);
3294 }
3295 else if (!strcmp(args[0], "option")) {
3296 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003297 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3298 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 err_code |= ERR_ALERT | ERR_FATAL;
3300 goto out;
3301 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003302
Christopher Faulet305c6072017-02-23 16:17:53 +01003303 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003304 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003305 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003306 if (kwm == 1)
3307 curagent->flags &= ~SPOE_FL_PIPELINING;
3308 else
3309 curagent->flags |= SPOE_FL_PIPELINING;
3310 goto out;
3311 }
3312 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003313 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003314 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003315 if (kwm == 1)
3316 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003317 else {
3318 if (global.nbthread == 1)
3319 curagent->flags |= SPOE_FL_ASYNC;
3320 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003321 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3322 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003323 err_code |= ERR_WARN;
3324 }
3325 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003326 goto out;
3327 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003328 else if (!strcmp(args[1], "send-frag-payload")) {
3329 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3330 goto out;
3331 if (kwm == 1)
3332 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3333 else
3334 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3335 goto out;
3336 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003337
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003338 /* Following options does not support negation */
3339 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003340 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3341 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003342 err_code |= ERR_ALERT | ERR_FATAL;
3343 goto out;
3344 }
3345
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003346 if (!strcmp(args[1], "var-prefix")) {
3347 char *tmp;
3348
3349 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003350 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3351 file, linenum, args[0],
3352 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353 err_code |= ERR_ALERT | ERR_FATAL;
3354 goto out;
3355 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003356 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3357 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003358 tmp = args[2];
3359 while (*tmp) {
3360 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003361 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3362 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003363 err_code |= ERR_ALERT | ERR_FATAL;
3364 goto out;
3365 }
3366 tmp++;
3367 }
3368 curagent->var_pfx = strdup(args[2]);
3369 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003370 else if (!strcmp(args[1], "force-set-var")) {
3371 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3372 goto out;
3373 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3374 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003375 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003376 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003377 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003378 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3379 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003380 else if (!strcmp(args[1], "set-on-error")) {
3381 char *tmp;
3382
3383 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003384 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3385 file, linenum, args[0],
3386 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003387 err_code |= ERR_ALERT | ERR_FATAL;
3388 goto out;
3389 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003390 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3391 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003392 tmp = args[2];
3393 while (*tmp) {
3394 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003395 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3396 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003397 err_code |= ERR_ALERT | ERR_FATAL;
3398 goto out;
3399 }
3400 tmp++;
3401 }
3402 curagent->var_on_error = strdup(args[2]);
3403 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003404 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003405 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3406 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003407 err_code |= ERR_ALERT | ERR_FATAL;
3408 goto out;
3409 }
Christopher Faulet48026722016-11-16 15:01:12 +01003410 }
3411 else if (!strcmp(args[0], "maxconnrate")) {
3412 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3414 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003415 err_code |= ERR_ALERT | ERR_FATAL;
3416 goto out;
3417 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003418 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003419 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003420 curagent->cps_max = atol(args[1]);
3421 }
3422 else if (!strcmp(args[0], "maxerrrate")) {
3423 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003424 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3425 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003426 err_code |= ERR_ALERT | ERR_FATAL;
3427 goto out;
3428 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003429 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003430 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003431 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003433 else if (!strcmp(args[0], "max-frame-size")) {
3434 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003435 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3436 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003437 err_code |= ERR_ALERT | ERR_FATAL;
3438 goto out;
3439 }
3440 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3441 goto out;
3442 curagent->max_frame_size = atol(args[1]);
3443 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3444 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003445 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3446 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003447 err_code |= ERR_ALERT | ERR_FATAL;
3448 goto out;
3449 }
3450 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003451 else if (!strcmp(args[0], "register-var-names")) {
3452 int cur_arg;
3453
3454 if (!*args[1]) {
3455 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3456 file, linenum, args[0]);
3457 err_code |= ERR_ALERT | ERR_FATAL;
3458 goto out;
3459 }
3460 cur_arg = 1;
3461 while (*args[cur_arg]) {
3462 struct spoe_var_placeholder *vph;
3463
3464 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3465 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3466 err_code |= ERR_ALERT | ERR_ABORT;
3467 goto out;
3468 }
3469 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3470 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3471 err_code |= ERR_ALERT | ERR_ABORT;
3472 goto out;
3473 }
3474 LIST_ADDQ(&curvars, &vph->list);
3475 cur_arg++;
3476 }
3477 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003479 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3480 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003481 err_code |= ERR_ALERT | ERR_FATAL;
3482 goto out;
3483 }
3484 out:
3485 return err_code;
3486}
Christopher Faulet11610f32017-09-21 10:23:10 +02003487static int
3488cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3489{
3490 struct spoe_group *grp;
3491 const char *err;
3492 int err_code = 0;
3493
3494 if ((cfg_scope == NULL && curengine != NULL) ||
3495 (cfg_scope != NULL && curengine == NULL) ||
3496 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3497 goto out;
3498
3499 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3500 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003501 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3502 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003503 err_code |= ERR_ALERT | ERR_ABORT;
3504 goto out;
3505 }
3506 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3507 err_code |= ERR_ABORT;
3508 goto out;
3509 }
3510
3511 err = invalid_char(args[1]);
3512 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003513 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3514 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003515 err_code |= ERR_ALERT | ERR_ABORT;
3516 goto out;
3517 }
3518
3519 list_for_each_entry(grp, &curgrps, list) {
3520 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003521 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3522 " name as another one declared at %s:%d.\n",
3523 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003524 err_code |= ERR_ALERT | ERR_FATAL;
3525 goto out;
3526 }
3527 }
3528
3529 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003530 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003531 err_code |= ERR_ALERT | ERR_ABORT;
3532 goto out;
3533 }
3534
3535 curgrp->id = strdup(args[1]);
3536 curgrp->conf.file = strdup(file);
3537 curgrp->conf.line = linenum;
3538 LIST_INIT(&curgrp->phs);
3539 LIST_INIT(&curgrp->messages);
3540 LIST_ADDQ(&curgrps, &curgrp->list);
3541 }
3542 else if (!strcmp(args[0], "messages")) {
3543 int cur_arg = 1;
3544 while (*args[cur_arg]) {
3545 struct spoe_placeholder *ph = NULL;
3546
3547 list_for_each_entry(ph, &curgrp->phs, list) {
3548 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003549 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3550 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003551 err_code |= ERR_ALERT | ERR_FATAL;
3552 goto out;
3553 }
3554 }
3555
3556 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003557 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003558 err_code |= ERR_ALERT | ERR_ABORT;
3559 goto out;
3560 }
3561 ph->id = strdup(args[cur_arg]);
3562 LIST_ADDQ(&curgrp->phs, &ph->list);
3563 cur_arg++;
3564 }
3565 }
3566 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003567 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3568 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003569 err_code |= ERR_ALERT | ERR_FATAL;
3570 goto out;
3571 }
3572 out:
3573 return err_code;
3574}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003575
3576static int
3577cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3578{
3579 struct spoe_message *msg;
3580 struct spoe_arg *arg;
3581 const char *err;
3582 char *errmsg = NULL;
3583 int err_code = 0;
3584
3585 if ((cfg_scope == NULL && curengine != NULL) ||
3586 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003587 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003588 goto out;
3589
3590 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3591 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003592 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3593 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003594 err_code |= ERR_ALERT | ERR_ABORT;
3595 goto out;
3596 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003597 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3598 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003599 goto out;
3600 }
3601
3602 err = invalid_char(args[1]);
3603 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003604 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3605 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003606 err_code |= ERR_ALERT | ERR_ABORT;
3607 goto out;
3608 }
3609
3610 list_for_each_entry(msg, &curmsgs, list) {
3611 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003612 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3613 " name as another one declared at %s:%d.\n",
3614 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003615 err_code |= ERR_ALERT | ERR_FATAL;
3616 goto out;
3617 }
3618 }
3619
3620 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003621 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003622 err_code |= ERR_ALERT | ERR_ABORT;
3623 goto out;
3624 }
3625
3626 curmsg->id = strdup(args[1]);
3627 curmsg->id_len = strlen(curmsg->id);
3628 curmsg->event = SPOE_EV_NONE;
3629 curmsg->conf.file = strdup(file);
3630 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003631 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003632 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003633 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003634 LIST_INIT(&curmsg->by_evt);
3635 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003636 LIST_ADDQ(&curmsgs, &curmsg->list);
3637 }
3638 else if (!strcmp(args[0], "args")) {
3639 int cur_arg = 1;
3640
3641 curproxy->conf.args.ctx = ARGC_SPOE;
3642 curproxy->conf.args.file = file;
3643 curproxy->conf.args.line = linenum;
3644 while (*args[cur_arg]) {
3645 char *delim = strchr(args[cur_arg], '=');
3646 int idx = 0;
3647
3648 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003649 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003650 err_code |= ERR_ALERT | ERR_ABORT;
3651 goto out;
3652 }
3653
3654 if (!delim) {
3655 arg->name = NULL;
3656 arg->name_len = 0;
3657 delim = args[cur_arg];
3658 }
3659 else {
3660 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3661 arg->name_len = delim - args[cur_arg];
3662 delim++;
3663 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003664 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3665 &idx, file, linenum, &errmsg,
3666 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003667 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003668 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003669 err_code |= ERR_ALERT | ERR_FATAL;
3670 free(arg->name);
3671 free(arg);
3672 goto out;
3673 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003674 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003675 LIST_ADDQ(&curmsg->args, &arg->list);
3676 cur_arg++;
3677 }
3678 curproxy->conf.args.file = NULL;
3679 curproxy->conf.args.line = 0;
3680 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003681 else if (!strcmp(args[0], "acl")) {
3682 err = invalid_char(args[1]);
3683 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003684 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3685 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003686 err_code |= ERR_ALERT | ERR_FATAL;
3687 goto out;
3688 }
3689 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003690 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3691 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003692 err_code |= ERR_ALERT | ERR_FATAL;
3693 goto out;
3694 }
3695 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003696 else if (!strcmp(args[0], "event")) {
3697 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003698 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003699 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003700 goto out;
3701 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003702 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3703 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003704
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003705 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3706 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3707 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3708 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3709
3710 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3711 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3712 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3713 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3714 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3715 curmsg->event = SPOE_EV_ON_TCP_RSP;
3716
3717 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3718 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3719 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3720 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3721 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3722 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3723 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003724 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3725 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003726 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003727 goto out;
3728 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003729
3730 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3731 struct acl_cond *cond;
3732
3733 cond = build_acl_cond(file, linenum, &curmsg->acls,
3734 curproxy, (const char **)args+2,
3735 &errmsg);
3736 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003737 ha_alert("parsing [%s:%d] : error detected while "
3738 "parsing an 'event %s' condition : %s.\n",
3739 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003740 err_code |= ERR_ALERT | ERR_FATAL;
3741 goto out;
3742 }
3743 curmsg->cond = cond;
3744 }
3745 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003746 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3747 "or 'unless' followed by a condition but found '%s'.\n",
3748 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003749 err_code |= ERR_ALERT | ERR_FATAL;
3750 goto out;
3751 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003752 }
3753 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003754 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3755 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003756 err_code |= ERR_ALERT | ERR_FATAL;
3757 goto out;
3758 }
3759 out:
3760 free(errmsg);
3761 return err_code;
3762}
3763
3764/* Return -1 on error, else 0 */
3765static int
3766parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3767 struct flt_conf *fconf, char **err, void *private)
3768{
3769 struct list backup_sections;
3770 struct spoe_config *conf;
3771 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003772 struct spoe_group *grp, *grpback;
3773 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003774 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003775 char *file = NULL, *engine = NULL;
3776 int ret, pos = *cur_arg + 1;
3777
3778 conf = calloc(1, sizeof(*conf));
3779 if (conf == NULL) {
3780 memprintf(err, "%s: out of memory", args[*cur_arg]);
3781 goto error;
3782 }
3783 conf->proxy = px;
3784
3785 while (*args[pos]) {
3786 if (!strcmp(args[pos], "config")) {
3787 if (!*args[pos+1]) {
3788 memprintf(err, "'%s' : '%s' option without value",
3789 args[*cur_arg], args[pos]);
3790 goto error;
3791 }
3792 file = args[pos+1];
3793 pos += 2;
3794 }
3795 else if (!strcmp(args[pos], "engine")) {
3796 if (!*args[pos+1]) {
3797 memprintf(err, "'%s' : '%s' option without value",
3798 args[*cur_arg], args[pos]);
3799 goto error;
3800 }
3801 engine = args[pos+1];
3802 pos += 2;
3803 }
3804 else {
3805 memprintf(err, "unknown keyword '%s'", args[pos]);
3806 goto error;
3807 }
3808 }
3809 if (file == NULL) {
3810 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3811 goto error;
3812 }
3813
3814 /* backup sections and register SPOE sections */
3815 LIST_INIT(&backup_sections);
3816 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003817 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3818 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003819 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003820
3821 /* Parse SPOE filter configuration file */
3822 curengine = engine;
3823 curproxy = px;
3824 curagent = NULL;
3825 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003826 LIST_INIT(&curmsgs);
3827 LIST_INIT(&curgrps);
3828 LIST_INIT(&curmphs);
3829 LIST_INIT(&curgphs);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003830 LIST_INIT(&curvars);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003831 ret = readcfgfile(file);
3832 curproxy = NULL;
3833
3834 /* unregister SPOE sections and restore previous sections */
3835 cfg_unregister_sections();
3836 cfg_restore_sections(&backup_sections);
3837
3838 if (ret == -1) {
3839 memprintf(err, "Could not open configuration file %s : %s",
3840 file, strerror(errno));
3841 goto error;
3842 }
3843 if (ret & (ERR_ABORT|ERR_FATAL)) {
3844 memprintf(err, "Error(s) found in configuration file %s", file);
3845 goto error;
3846 }
3847
3848 /* Check SPOE agent */
3849 if (curagent == NULL) {
3850 memprintf(err, "No SPOE agent found in file %s", file);
3851 goto error;
3852 }
3853 if (curagent->b.name == NULL) {
3854 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3855 curagent->id, curagent->conf.file, curagent->conf.line);
3856 goto error;
3857 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003858 if (curagent->timeout.hello == TICK_ETERNITY ||
3859 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003860 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3862 " | While not properly invalid, you will certainly encounter various problems\n"
3863 " | with such a configuration. To fix this, please ensure that all following\n"
3864 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3865 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003866 }
3867 if (curagent->var_pfx == NULL) {
3868 char *tmp = curagent->id;
3869
3870 while (*tmp) {
3871 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3872 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3873 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3874 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3875 goto error;
3876 }
3877 tmp++;
3878 }
3879 curagent->var_pfx = strdup(curagent->id);
3880 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003881 if (curagent->engine_id == NULL)
3882 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003883
Christopher Faulet11610f32017-09-21 10:23:10 +02003884 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003885 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3886 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003887 goto finish;
3888 }
3889
Christopher Faulet11610f32017-09-21 10:23:10 +02003890 /* Replace placeholders by the corresponding messages for the SPOE
3891 * agent */
3892 list_for_each_entry(ph, &curmphs, list) {
3893 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003894 struct spoe_arg *arg;
3895 unsigned int where;
3896
Christopher Faulet11610f32017-09-21 10:23:10 +02003897 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003898 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3899 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3900 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3901 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3902 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3903 }
3904 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3905 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3906 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003907 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3908 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003909 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003910 }
3911 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003912 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3913 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003914 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003915 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003916
3917 where = 0;
3918 switch (msg->event) {
3919 case SPOE_EV_ON_CLIENT_SESS:
3920 where |= SMP_VAL_FE_CON_ACC;
3921 break;
3922
3923 case SPOE_EV_ON_TCP_REQ_FE:
3924 where |= SMP_VAL_FE_REQ_CNT;
3925 break;
3926
3927 case SPOE_EV_ON_HTTP_REQ_FE:
3928 where |= SMP_VAL_FE_HRQ_HDR;
3929 break;
3930
3931 case SPOE_EV_ON_TCP_REQ_BE:
3932 if (px->cap & PR_CAP_FE)
3933 where |= SMP_VAL_FE_REQ_CNT;
3934 if (px->cap & PR_CAP_BE)
3935 where |= SMP_VAL_BE_REQ_CNT;
3936 break;
3937
3938 case SPOE_EV_ON_HTTP_REQ_BE:
3939 if (px->cap & PR_CAP_FE)
3940 where |= SMP_VAL_FE_HRQ_HDR;
3941 if (px->cap & PR_CAP_BE)
3942 where |= SMP_VAL_BE_HRQ_HDR;
3943 break;
3944
3945 case SPOE_EV_ON_SERVER_SESS:
3946 where |= SMP_VAL_BE_SRV_CON;
3947 break;
3948
3949 case SPOE_EV_ON_TCP_RSP:
3950 if (px->cap & PR_CAP_FE)
3951 where |= SMP_VAL_FE_RES_CNT;
3952 if (px->cap & PR_CAP_BE)
3953 where |= SMP_VAL_BE_RES_CNT;
3954 break;
3955
3956 case SPOE_EV_ON_HTTP_RSP:
3957 if (px->cap & PR_CAP_FE)
3958 where |= SMP_VAL_FE_HRS_HDR;
3959 if (px->cap & PR_CAP_BE)
3960 where |= SMP_VAL_BE_HRS_HDR;
3961 break;
3962
3963 default:
3964 break;
3965 }
3966
3967 list_for_each_entry(arg, &msg->args, list) {
3968 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003969 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003970 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003971 "none of which is available here ('%s')",
3972 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003973 sample_ckp_names(arg->expr->fetch->use),
3974 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003975 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003976 }
3977 }
3978
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003979 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003980 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3981 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 }
3983 }
3984 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003985 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003986 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003987 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003988 continue;
3989 }
3990
Christopher Faulet11610f32017-09-21 10:23:10 +02003991 /* Replace placeholders by the corresponding groups for the SPOE
3992 * agent */
3993 list_for_each_entry(ph, &curgphs, list) {
3994 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3995 if (!strcmp(grp->id, ph->id)) {
3996 grp->agent = curagent;
3997 LIST_DEL(&grp->list);
3998 LIST_ADDQ(&curagent->groups, &grp->list);
3999 goto next_aph;
4000 }
4001 }
4002 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4003 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4004 goto error;
4005 next_aph:
4006 continue;
4007 }
4008
4009 /* Replace placeholders by the corresponding message for each SPOE
4010 * group of the SPOE agent */
4011 list_for_each_entry(grp, &curagent->groups, list) {
4012 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4013 list_for_each_entry(msg, &curmsgs, list) {
4014 if (!strcmp(msg->id, ph->id)) {
4015 if (msg->group != NULL) {
4016 memprintf(err, "SPOE message '%s' already belongs to "
4017 "the SPOE group '%s' declare at %s:%d",
4018 msg->id, msg->group->id,
4019 msg->group->conf.file,
4020 msg->group->conf.line);
4021 goto error;
4022 }
4023
4024 /* Scope for arguments are not checked for now. We will check
4025 * them only if a rule use the corresponding SPOE group. */
4026 msg->agent = curagent;
4027 msg->group = grp;
4028 LIST_DEL(&ph->list);
4029 LIST_ADDQ(&grp->messages, &msg->by_grp);
4030 goto next_mph_grp;
4031 }
4032 }
4033 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4034 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4035 goto error;
4036 next_mph_grp:
4037 continue;
4038 }
4039 }
4040
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004041 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004042 /* move curmsgs to the agent message list */
4043 curmsgs.n->p = &curagent->messages;
4044 curmsgs.p->n = &curagent->messages;
4045 curagent->messages = curmsgs;
4046 LIST_INIT(&curmsgs);
4047
Christopher Faulet7ee86672017-09-19 11:08:28 +02004048 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004049 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004050 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4051 LIST_DEL(&ph->list);
4052 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004054 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4055 LIST_DEL(&ph->list);
4056 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004058 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4059 struct arg arg;
4060
4061 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4062 curagent->var_pfx, vph->name);
4063
4064 arg.type = ARGT_STR;
4065 arg.data.str.str = trash.str;
4066 arg.data.str.len = trash.len;
4067 if (!vars_check_arg(&arg, err)) {
4068 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4069 curagent->id, curagent->var_pfx, vph->name, *err);
4070 goto error;
4071 }
4072
4073 LIST_DEL(&vph->list);
4074 free(vph->name);
4075 free(vph);
4076 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004077 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4078 LIST_DEL(&grp->list);
4079 spoe_release_group(grp);
4080 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004081 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004082 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004083 fconf->ops = &spoe_ops;
4084 fconf->conf = conf;
4085 return 0;
4086
4087 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004088 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004089 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4090 LIST_DEL(&ph->list);
4091 spoe_release_placeholder(ph);
4092 }
4093 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4094 LIST_DEL(&ph->list);
4095 spoe_release_placeholder(ph);
4096 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004097 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4098 LIST_DEL(&vph->list);
4099 free(vph->name);
4100 free(vph);
4101 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004102 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4103 LIST_DEL(&grp->list);
4104 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004105 }
4106 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4107 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004108 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004109 }
4110 free(conf);
4111 return -1;
4112}
4113
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004114/* Send message of a SPOE group. This is the action_ptr callback of a rule
4115 * associated to a "send-spoe-group" action.
4116 *
4117 * It returns ACT_RET_CONT is processing is finished without error, it returns
4118 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4119 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004120static enum act_return
4121spoe_send_group(struct act_rule *rule, struct proxy *px,
4122 struct session *sess, struct stream *s, int flags)
4123{
4124 struct filter *filter;
4125 struct spoe_agent *agent = NULL;
4126 struct spoe_group *group = NULL;
4127 struct spoe_context *ctx = NULL;
4128 int ret, dir;
4129
4130 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4131 if (filter->config == rule->arg.act.p[0]) {
4132 agent = rule->arg.act.p[2];
4133 group = rule->arg.act.p[3];
4134 ctx = filter->ctx;
4135 break;
4136 }
4137 }
4138 if (agent == NULL || group == NULL || ctx == NULL)
4139 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004140 if (ctx->state == SPOE_CTX_ST_NONE)
4141 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004142
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004143 switch (rule->from) {
4144 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4145 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4146 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4147 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4148 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4149 default:
4150 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4151 " - internal error while execute spoe-send-group\n",
4152 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4153 __FUNCTION__, s);
4154 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4155 agent->id);
4156 return ACT_RET_CONT;
4157 }
4158
4159 ret = spoe_process_group(s, ctx, group, dir);
4160 if (ret == 1)
4161 return ACT_RET_CONT;
4162 else if (ret == 0) {
4163 if (flags & ACT_FLAG_FINAL) {
4164 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4165 " - failed to process group '%s': interrupted by caller\n",
4166 (int)now.tv_sec, (int)now.tv_usec,
4167 agent->id, __FUNCTION__, s, group->id);
4168 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4169 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004170 spoe_stop_processing(agent, ctx);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004171 return ACT_RET_CONT;
4172 }
4173 return ACT_RET_YIELD;
4174 }
4175 else
4176 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004177}
4178
4179/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4180 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4181 * action should be:
4182 *
4183 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4184 *
4185 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4186 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4187 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4188 * group.
4189 *
4190 * The function returns 1 in success case, otherwise, it returns 0 and err is
4191 * filled.
4192 */
4193static int
4194check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4195{
4196 struct flt_conf *fconf;
4197 struct spoe_config *conf;
4198 struct spoe_agent *agent = NULL;
4199 struct spoe_group *group;
4200 struct spoe_message *msg;
4201 char *engine_id = rule->arg.act.p[0];
4202 char *group_id = rule->arg.act.p[1];
4203 unsigned int where = 0;
4204
4205 switch (rule->from) {
4206 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4207 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4208 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4209 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4210 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4211 default:
4212 memprintf(err,
4213 "internal error, unexpected rule->from=%d, please report this bug!",
4214 rule->from);
4215 goto error;
4216 }
4217
4218 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4219 * <px> */
4220 list_for_each_entry(fconf, &px->filter_configs, list) {
4221 conf = fconf->conf;
4222
4223 /* This is not an SPOE filter */
4224 if (fconf->id != spoe_filter_id)
4225 continue;
4226
4227 /* This is the good engine */
4228 if (!strcmp(conf->id, engine_id)) {
4229 agent = conf->agent;
4230 break;
4231 }
4232 }
4233 if (agent == NULL) {
4234 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4235 engine_id, group_id);
4236 goto error;
4237 }
4238
4239 /* Try to find the right group */
4240 list_for_each_entry(group, &agent->groups, list) {
4241 /* This is the good group */
4242 if (!strcmp(group->id, group_id))
4243 break;
4244 }
4245 if (&group->list == &agent->groups) {
4246 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4247 group_id, engine_id);
4248 goto error;
4249 }
4250
4251 /* Ok, we found the group, we need to check messages and their
4252 * arguments */
4253 list_for_each_entry(msg, &group->messages, by_grp) {
4254 struct spoe_arg *arg;
4255
4256 list_for_each_entry(arg, &msg->args, list) {
4257 if (!(arg->expr->fetch->val & where)) {
4258 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4259 "some args extract information from '%s',"
4260 "none of which is available here ('%s')",
4261 msg->id, group->id, msg->conf.file, msg->conf.line,
4262 sample_ckp_names(arg->expr->fetch->use),
4263 sample_ckp_names(where));
4264 goto error;
4265 }
4266 }
4267 }
4268
4269 free(engine_id);
4270 free(group_id);
4271 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4272 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4273 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4274 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4275 return 1;
4276
4277 error:
4278 free(engine_id);
4279 free(group_id);
4280 return 0;
4281}
4282
4283/* Parse 'send-spoe-group' action following the format:
4284 *
4285 * ... send-spoe-group <engine-id> <group-id>
4286 *
4287 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4288 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4289 * ids are saved and used later, when the rule will be checked.
4290 */
4291static enum act_parse_ret
4292parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4293 struct act_rule *rule, char **err)
4294{
4295 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4296 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4297 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4298 return ACT_RET_PRS_ERR;
4299 }
4300 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4301 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4302
4303 (*orig_arg) += 2;
4304
4305 rule->action = ACT_CUSTOM;
4306 rule->action_ptr = spoe_send_group;
4307 rule->check_ptr = check_send_spoe_group;
4308 return ACT_RET_PRS_OK;
4309}
4310
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004311
4312/* Declare the filter parser for "spoe" keyword */
4313static struct flt_kw_list flt_kws = { "SPOE", { }, {
4314 { "spoe", parse_spoe_flt, NULL },
4315 { NULL, NULL, NULL },
4316 }
4317};
4318
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004319/* Delcate the action parser for "spoe-action" keyword */
4320static struct action_kw_list tcp_req_action_kws = { { }, {
4321 { "send-spoe-group", parse_send_spoe_group },
4322 { /* END */ },
4323 }
4324};
4325static struct action_kw_list tcp_res_action_kws = { { }, {
4326 { "send-spoe-group", parse_send_spoe_group },
4327 { /* END */ },
4328 }
4329};
4330static struct action_kw_list http_req_action_kws = { { }, {
4331 { "send-spoe-group", parse_send_spoe_group },
4332 { /* END */ },
4333 }
4334};
4335static struct action_kw_list http_res_action_kws = { { }, {
4336 { "send-spoe-group", parse_send_spoe_group },
4337 { /* END */ },
4338 }
4339};
4340
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004341__attribute__((constructor))
4342static void __spoe_init(void)
4343{
4344 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004345 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4346 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4347 http_req_keywords_register(&http_req_action_kws);
4348 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004349
Willy Tarreaubafbe012017-11-24 17:34:44 +01004350 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4351 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004352}
4353
4354__attribute__((destructor))
4355static void
4356__spoe_deinit(void)
4357{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004358 pool_destroy(pool_head_spoe_ctx);
4359 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004360}