blob: 1d8e4e9ffb1a9e8610b5d06501ba5e9044918a87 [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;
1526
Christopher Faulet8ef75252017-02-20 22:56:03 +01001527 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1528 goto end;
1529
1530 abort_frag_frame:
1531 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1532 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1533 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1534 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1535 goto end;
1536
1537 end:
1538 return ret;
1539}
1540
1541static int
1542spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1543{
1544 struct spoe_context *ctx = NULL;
1545 char *frame;
1546 int ret;
1547
1548 frame = trash.str; trash.len = 0;
1549 ret = spoe_recv_frame(appctx, frame,
1550 SPOE_APPCTX(appctx)->max_frame_size);
1551 if (ret > 1) {
1552 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1553 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1554 goto end;
1555 }
1556 trash.len = ret + 4;
1557 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1558 }
1559 switch (ret) {
1560 case -1: /* error */
1561 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1562 break;
1563
1564 case 0: /* ignore */
1565 break;
1566
1567 case 1: /* retry */
1568 *skip = 1;
1569 break;
1570
1571 default:
1572 LIST_DEL(&ctx->list);
1573 LIST_INIT(&ctx->list);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001574 if (ctx->spoe_appctx)
1575 ctx->spoe_appctx = NULL;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001576 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1577 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1578 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1579 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1580 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1581 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1582 }
1583 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1584 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1585
Christopher Faulet8ef75252017-02-20 22:56:03 +01001586 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1587 break;
1588 }
1589
1590 /* Do not forget to remove processed frame from the output buffer */
1591 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001592 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001593 end:
1594 return ret;
1595}
1596
1597static int
1598spoe_handle_processing_appctx(struct appctx *appctx)
1599{
1600 struct stream_interface *si = appctx->owner;
1601 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 unsigned int fpa = 0;
1603 int ret, skip_sending = 0, skip_receiving = 0;
1604
1605 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1606 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1607 goto exit;
1608 }
1609
1610 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1611 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1612 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1613 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1614 goto next;
1615 }
1616
1617 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001618 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1619 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1620 " - appctx-state=%s\n",
1621 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1622 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001623 skip_sending, skip_receiving,
1624 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001625
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001626 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001627 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001628 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001629 if (skip_receiving)
1630 goto stop;
1631 goto recv_frame;
1632 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001633
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001634 /* send_frame */
1635 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001636 switch (ret) {
1637 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001638 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001639
Christopher Fauleta1cda022016-12-21 08:58:06 +01001640 case 0: /* ignore */
Christopher Faulet24289f22017-09-25 14:48:02 +02001641 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001642 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001643 break;
1644
Christopher Fauleta1cda022016-12-21 08:58:06 +01001645 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001646 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001647
Christopher Fauleta1cda022016-12-21 08:58:06 +01001648 default:
Christopher Faulet24289f22017-09-25 14:48:02 +02001649 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001650 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001651 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001652 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001653 if (fpa > agent->max_fpa)
1654 goto stop;
1655
1656 recv_frame:
1657 if (skip_receiving)
1658 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001659 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001660 switch (ret) {
1661 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001662 goto next;
1663
1664 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001665 fpa++;
1666 break;
1667
1668 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001669 break;
1670
1671 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001672 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001673 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 }
1675 goto process;
1676
1677 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001678 SPOE_APPCTX(appctx)->task->expire =
1679 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001680 return 0;
1681 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001682 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet24289f22017-09-25 14:48:02 +02001684 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001685 }
Christopher Faulet42097792018-01-24 15:49:45 +01001686 if (fpa) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001687 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001688 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001689 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001690 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001691 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001692 SPOE_APPCTX(appctx)->task->expire =
1693 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001694 }
1695 return 1;
1696
1697 exit:
1698 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1699 return 0;
1700}
1701
1702static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001703spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704{
1705 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001706 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001707 char *frame, *buf;
1708 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001709
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1711 goto exit;
1712
1713 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1714 goto exit;
1715
Christopher Faulet8ef75252017-02-20 22:56:03 +01001716 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1717 * length. */
1718 buf = trash.str; frame = buf+4;
1719 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1720 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001722 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723
1724 switch (ret) {
1725 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001726 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001727 goto exit;
1728
1729 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001730 goto stop;
1731
1732 default:
1733 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1734 " - disconnected by HAProxy (%d): %s\n",
1735 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001736 __FUNCTION__, appctx,
1737 SPOE_APPCTX(appctx)->status_code,
1738 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739
1740 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1741 goto next;
1742 }
1743
1744 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745 SPOE_APPCTX(appctx)->task->expire =
1746 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747 return 0;
1748 stop:
1749 return 1;
1750 exit:
1751 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1752 return 0;
1753}
1754
1755static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001756spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757{
1758 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 char *frame;
1760 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761
Christopher Fauletb067b062017-01-04 16:39:11 +01001762 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001765 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766
Christopher Fauletb067b062017-01-04 16:39:11 +01001767 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001770 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 frame = trash.str; trash.len = 0;
1773 ret = spoe_recv_frame(appctx, frame,
1774 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001775 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001776 trash.len = ret + 4;
1777 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 }
1779
1780 switch (ret) {
1781 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1783 " - error on frame (%s)\n",
1784 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001785 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001786 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001787 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788 goto exit;
1789
1790 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 goto next;
1792
1793 case 1: /* retry */
1794 goto stop;
1795
1796 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001800 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1802 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803 goto exit;
1804 }
1805
1806 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001807 /* Do not forget to remove processed frame from the output buffer */
1808 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001809 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 return 0;
1812 stop:
1813 return 1;
1814 exit:
1815 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1816 return 0;
1817}
1818
1819/* I/O Handler processing messages exchanged with the agent */
1820static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001822{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001823 struct stream_interface *si = appctx->owner;
1824 struct spoe_agent *agent;
1825
1826 if (SPOE_APPCTX(appctx) == NULL)
1827 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1830 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001831
Christopher Fauleta1cda022016-12-21 08:58:06 +01001832 switchstate:
1833 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1834 " - appctx-state=%s\n",
1835 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1836 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1837
1838 switch (appctx->st0) {
1839 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 goto out;
1842 goto switchstate;
1843
1844 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 goto out;
1847 goto switchstate;
1848
1849 case SPOE_APPCTX_ST_IDLE:
1850 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001851 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001852 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853 SPOE_APPCTX(appctx)->task->expire =
1854 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001856 goto switchstate;
1857 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001858 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1860 /* fall through */
1861
1862 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001863 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1864 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001865 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001866 goto out;
1867 goto switchstate;
1868
1869 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001870 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871 goto out;
1872 goto switchstate;
1873
1874 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 goto out;
1877 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001878
1879 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 appctx->st0 = SPOE_APPCTX_ST_END;
1881 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1882
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001883 si_shutw(si);
1884 si_shutr(si);
1885 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001886 /* fall through */
1887
1888 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001889 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001890 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001892 if (stopping)
1893 spoe_wakeup_appctx(appctx);
1894
Christopher Faulet42bfa462017-01-04 14:14:19 +01001895 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1896 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001897 si_oc(si)->flags |= CF_READ_DONTWAIT;
1898 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001899}
1900
1901struct applet spoe_applet = {
1902 .obj_type = OBJ_TYPE_APPLET,
1903 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 .fct = spoe_handle_appctx,
1905 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001906};
1907
1908/* Create a SPOE applet. On success, the created applet is returned, else
1909 * NULL. */
1910static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001911spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001912{
1913 struct appctx *appctx;
1914 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001915 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916
Emeric Brun1138fd02017-06-19 12:38:55 +02001917 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001918 goto out_error;
1919
Willy Tarreaubafbe012017-11-24 17:34:44 +01001920 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001921 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001923 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001924
Christopher Faulet42bfa462017-01-04 14:14:19 +01001925 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001926 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001927 goto out_free_spoe_appctx;
1928
1929 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001931 SPOE_APPCTX(appctx)->task->context = appctx;
1932 SPOE_APPCTX(appctx)->agent = conf->agent;
1933 SPOE_APPCTX(appctx)->version = 0;
1934 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1935 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001936 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001937 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1938
1939 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1940 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001941 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001942
1943 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1944 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945
Willy Tarreau5820a362016-12-22 15:59:02 +01001946 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001947 if (!sess)
1948 goto out_free_spoe;
1949
Willy Tarreau87787ac2017-08-28 16:22:54 +02001950 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951 goto out_free_sess;
1952
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001953 stream_set_backend(strm, conf->agent->b.be);
1954
1955 /* applet is waiting for data */
1956 si_applet_cant_get(&strm->si[0]);
1957 appctx_wakeup(appctx);
1958
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 strm->do_log = NULL;
1960 strm->res.flags |= CF_READ_DONTWAIT;
1961
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001962 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001963 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001964 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001965 conf->agent->rt[tid].applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001966
Emeric Brunc60def82017-09-27 14:59:38 +02001967 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001968 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 return appctx;
1970
1971 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001972 out_free_sess:
1973 session_free(sess);
1974 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001975 task_free(SPOE_APPCTX(appctx)->task);
1976 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001977 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001978 out_free_appctx:
1979 appctx_free(appctx);
1980 out_error:
1981 return NULL;
1982}
1983
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001985spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001986{
1987 struct spoe_config *conf = FLT_CONF(ctx->filter);
1988 struct spoe_agent *agent = conf->agent;
1989 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001990 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991
Christopher Fauleta1cda022016-12-21 08:58:06 +01001992 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet42097792018-01-24 15:49:45 +01001993 if (agent->rt[tid].applets_idle &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001994 agent->rt[tid].sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001995 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996
1997 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001998 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2000 ctx->strm);
2001
Christopher Fauleta1cda022016-12-21 08:58:06 +01002002 /* Do not try to create a new applet if there is no server up for the
2003 * agent's backend. */
2004 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2005 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2006 " - cannot create SPOE appctx: no server up\n",
2007 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2008 __FUNCTION__, ctx->strm);
2009 goto end;
2010 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011
Christopher Fauleta1cda022016-12-21 08:58:06 +01002012 /* Do not try to create a new applet if we have reached the maximum of
2013 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002014 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002015 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002016 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2017 " - cannot create SPOE appctx: max CPS reached\n",
2018 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2019 __FUNCTION__, ctx->strm);
2020 goto end;
2021 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022 }
2023
Christopher Faulet8ef75252017-02-20 22:56:03 +01002024 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 if (appctx == NULL) {
2026 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2027 " - failed to create SPOE appctx\n",
2028 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2029 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002030 send_log(ctx->strm->be, LOG_EMERG,
2031 "SPOE: [%s] failed to create SPOE applet\n",
2032 agent->id);
2033
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035 }
2036
Christopher Fauleta1cda022016-12-21 08:58:06 +01002037 /* Increase the per-process number of cumulated connections */
2038 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002039 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 end:
2042 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002043 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002044 ctx->status_code = SPOE_CTX_ERR_RES;
2045 return -1;
2046 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002047
Christopher Fauleta1cda022016-12-21 08:58:06 +01002048 /* Add the SPOE context in the sending queue and update all running
2049 * info */
Christopher Faulet24289f22017-09-25 14:48:02 +02002050 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
2051 if (agent->rt[tid].sending_rate)
2052 agent->rt[tid].sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053
2054 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002055 " - Add stream in sending queue"
2056 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002057 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002058 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
2059 agent->rt[tid].sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 /* Finally try to wakeup the first IDLE applet found and move it at the
2062 * end of the list. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002063 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) {
Christopher Faulet42bfa462017-01-04 14:14:19 +01002064 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002066 spoe_wakeup_appctx(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002067 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002068 LIST_DEL(&spoe_appctx->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02002069 LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002070 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002071 break;
2072 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002073 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002075}
2076
2077/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002078 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002080/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2081 * fragmented_content. If the next message can be processed, it returns 0. If
2082 * the message is too big, it returns -1.*/
2083static int
2084spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2085 struct spoe_message *msg, int dir,
2086 char **buf, char *end)
2087{
2088 struct sample *smp;
2089 struct spoe_arg *arg;
2090 int ret;
2091
2092 if (msg->cond) {
2093 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2094 ret = acl_pass(ret);
2095 if (msg->cond->pol == ACL_COND_UNLESS)
2096 ret = !ret;
2097
2098 /* the rule does not match */
2099 if (!ret)
2100 goto next;
2101 }
2102
2103 /* Resume encoding of a SPOE argument */
2104 if (ctx->frag_ctx.curarg != NULL) {
2105 arg = ctx->frag_ctx.curarg;
2106 goto encode_argument;
2107 }
2108
2109 if (ctx->frag_ctx.curoff != UINT_MAX)
2110 goto encode_msg_payload;
2111
2112 /* Check if there is enough space for the message name and the
2113 * number of arguments. It implies <msg->id_len> is encoded on 2
2114 * bytes, at most (< 2288). */
2115 if (*buf + 2 + msg->id_len + 1 > end)
2116 goto too_big;
2117
2118 /* Encode the message name */
2119 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2120 goto too_big;
2121
2122 /* Set the number of arguments for this message */
2123 **buf = msg->nargs;
2124 (*buf)++;
2125
2126 ctx->frag_ctx.curoff = 0;
2127 encode_msg_payload:
2128
2129 /* Loop on arguments */
2130 list_for_each_entry(arg, &msg->args, list) {
2131 ctx->frag_ctx.curarg = arg;
2132 ctx->frag_ctx.curoff = UINT_MAX;
2133
2134 encode_argument:
2135 if (ctx->frag_ctx.curoff != UINT_MAX)
2136 goto encode_arg_value;
2137
2138 /* Encode the arguement name as a string. It can by NULL */
2139 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2140 goto too_big;
2141
2142 ctx->frag_ctx.curoff = 0;
2143 encode_arg_value:
2144
2145 /* Fetch the arguement value */
2146 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2147 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2148 if (ret == -1 || ctx->frag_ctx.curoff)
2149 goto too_big;
2150 }
2151
2152 next:
2153 return 0;
2154
2155 too_big:
2156 return -1;
2157}
2158
Christopher Fauletc718b822017-09-21 16:50:56 +02002159/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2160 * handle fragmented content. On success it returns 1. If an error occurred, -1
2161 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2162 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002163static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002164spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002165 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002166{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002167 struct spoe_config *conf = FLT_CONF(ctx->filter);
2168 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002169 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002170 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002171
Christopher Faulet8ef75252017-02-20 22:56:03 +01002172 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002173 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002174
Christopher Fauletc718b822017-09-21 16:50:56 +02002175 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2176 /* Resume encoding of a SPOE message */
2177 if (ctx->frag_ctx.curmsg != NULL) {
2178 msg = ctx->frag_ctx.curmsg;
2179 goto encode_evt_message;
2180 }
2181
2182 list_for_each_entry(msg, messages, by_evt) {
2183 ctx->frag_ctx.curmsg = msg;
2184 ctx->frag_ctx.curarg = NULL;
2185 ctx->frag_ctx.curoff = UINT_MAX;
2186
2187 encode_evt_message:
2188 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2189 goto too_big;
2190 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002191 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002192 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2193 /* Resume encoding of a SPOE message */
2194 if (ctx->frag_ctx.curmsg != NULL) {
2195 msg = ctx->frag_ctx.curmsg;
2196 goto encode_grp_message;
2197 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002198
Christopher Fauletc718b822017-09-21 16:50:56 +02002199 list_for_each_entry(msg, messages, by_grp) {
2200 ctx->frag_ctx.curmsg = msg;
2201 ctx->frag_ctx.curarg = NULL;
2202 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002203
Christopher Fauletc718b822017-09-21 16:50:56 +02002204 encode_grp_message:
2205 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2206 goto too_big;
2207 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002208 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002209 else
2210 goto skip;
2211
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002212
Christopher Faulet57583e42017-09-04 15:41:09 +02002213 /* nothing has been encoded for an unfragmented payload */
2214 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2215 goto skip;
2216
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002217 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002218 " - encode %s messages - spoe_appctx=%p"
2219 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002220 (int)now.tv_sec, (int)now.tv_usec,
2221 agent->id, __FUNCTION__, s,
2222 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002223 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002224 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002225
Christopher Faulet8ef75252017-02-20 22:56:03 +01002226 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002227 ctx->frag_ctx.curmsg = NULL;
2228 ctx->frag_ctx.curarg = NULL;
2229 ctx->frag_ctx.curoff = 0;
2230 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002231
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232 return 1;
2233
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002235 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2236 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2237 return -1;
2238 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002239
2240 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002241 " - encode fragmented messages - spoe_appctx=%p"
2242 " - curmsg=%p - curarg=%p - curoff=%u"
2243 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002245 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002246 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002247 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002248
Christopher Faulet8ef75252017-02-20 22:56:03 +01002249 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002250 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2251 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2252 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002253
2254 skip:
2255 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2256 " - skip the frame because nothing has been encoded\n",
2257 (int)now.tv_sec, (int)now.tv_usec,
2258 agent->id, __FUNCTION__, s);
2259 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002260}
2261
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002262
2263/***************************************************************************
2264 * Functions that handle SPOE actions
2265 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002266/* Helper function to set a variable */
2267static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002268spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002269 struct sample *smp)
2270{
2271 struct spoe_config *conf = FLT_CONF(ctx->filter);
2272 struct spoe_agent *agent = conf->agent;
2273 char varname[64];
2274
2275 memset(varname, 0, sizeof(varname));
2276 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2277 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002278 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2279 vars_set_by_name(varname, len, smp);
2280 else
2281 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002282}
2283
2284/* Helper function to unset a variable */
2285static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002287 struct sample *smp)
2288{
2289 struct spoe_config *conf = FLT_CONF(ctx->filter);
2290 struct spoe_agent *agent = conf->agent;
2291 char varname[64];
2292
2293 memset(varname, 0, sizeof(varname));
2294 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2295 scope, agent->var_pfx, len, name);
2296 vars_unset_by_name_ifexist(varname, len, smp);
2297}
2298
2299
Christopher Faulet8ef75252017-02-20 22:56:03 +01002300static inline int
2301spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2302 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002303{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002304 char *str, *scope, *p = *buf;
2305 struct sample smp;
2306 uint64_t sz;
2307 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002308
Christopher Faulet8ef75252017-02-20 22:56:03 +01002309 if (p + 2 >= end)
2310 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002311
Christopher Faulet8ef75252017-02-20 22:56:03 +01002312 /* SET-VAR requires 3 arguments */
2313 if (*p++ != 3)
2314 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002315
Christopher Faulet8ef75252017-02-20 22:56:03 +01002316 switch (*p++) {
2317 case SPOE_SCOPE_PROC: scope = "proc"; break;
2318 case SPOE_SCOPE_SESS: scope = "sess"; break;
2319 case SPOE_SCOPE_TXN : scope = "txn"; break;
2320 case SPOE_SCOPE_REQ : scope = "req"; break;
2321 case SPOE_SCOPE_RES : scope = "res"; break;
2322 default: goto skip;
2323 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002324
Christopher Faulet8ef75252017-02-20 22:56:03 +01002325 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2326 goto skip;
2327 memset(&smp, 0, sizeof(smp));
2328 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329
Christopher Faulet8ef75252017-02-20 22:56:03 +01002330 if (spoe_decode_data(&p, end, &smp) == -1)
2331 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002332
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2334 " - set-var '%s.%s.%.*s'\n",
2335 (int)now.tv_sec, (int)now.tv_usec,
2336 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2337 __FUNCTION__, s, scope,
2338 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2339 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002340
Christopher Faulet8ef75252017-02-20 22:56:03 +01002341 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002342
Christopher Faulet8ef75252017-02-20 22:56:03 +01002343 ret = (p - *buf);
2344 *buf = p;
2345 return ret;
2346 skip:
2347 return 0;
2348}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002349
Christopher Faulet8ef75252017-02-20 22:56:03 +01002350static inline int
2351spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2352 char **buf, char *end, int dir)
2353{
2354 char *str, *scope, *p = *buf;
2355 struct sample smp;
2356 uint64_t sz;
2357 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002358
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359 if (p + 2 >= end)
2360 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002361
Christopher Faulet8ef75252017-02-20 22:56:03 +01002362 /* UNSET-VAR requires 2 arguments */
2363 if (*p++ != 2)
2364 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002365
Christopher Faulet8ef75252017-02-20 22:56:03 +01002366 switch (*p++) {
2367 case SPOE_SCOPE_PROC: scope = "proc"; break;
2368 case SPOE_SCOPE_SESS: scope = "sess"; break;
2369 case SPOE_SCOPE_TXN : scope = "txn"; break;
2370 case SPOE_SCOPE_REQ : scope = "req"; break;
2371 case SPOE_SCOPE_RES : scope = "res"; break;
2372 default: goto skip;
2373 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2376 goto skip;
2377 memset(&smp, 0, sizeof(smp));
2378 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002379
Christopher Faulet8ef75252017-02-20 22:56:03 +01002380 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2381 " - unset-var '%s.%s.%.*s'\n",
2382 (int)now.tv_sec, (int)now.tv_usec,
2383 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2384 __FUNCTION__, s, scope,
2385 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2386 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388 spoe_unset_var(ctx, scope, str, sz, &smp);
2389
2390 ret = (p - *buf);
2391 *buf = p;
2392 return ret;
2393 skip:
2394 return 0;
2395}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397/* Process SPOE actions for a specific event. It returns 1 on success. If an
2398 * error occurred, 0 is returned. */
2399static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002400spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401{
2402 char *p, *end;
2403 int ret;
2404
2405 p = ctx->buffer->p;
2406 end = p + ctx->buffer->i;
2407
2408 while (p < end) {
2409 enum spoe_action_type type;
2410
2411 type = *p++;
2412 switch (type) {
2413 case SPOE_ACT_T_SET_VAR:
2414 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2415 if (!ret)
2416 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 case SPOE_ACT_T_UNSET_VAR:
2420 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2421 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 break;
2424
2425 default:
2426 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002427 }
2428 }
2429
2430 return 1;
2431 skip:
2432 return 0;
2433}
2434
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002435/***************************************************************************
2436 * Functions that process SPOE events
2437 **************************************************************************/
2438static inline int
Christopher Faulet58d03682017-09-21 16:57:24 +02002439spoe_start_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002440{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002441 /* If a process is already started for this SPOE context, retry
2442 * later. */
2443 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002444 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002445
2446 /* Set the right flag to prevent request and response processing
2447 * in same time. */
2448 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2449 ? SPOE_CTX_FL_REQ_PROCESS
2450 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002451 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002452}
2453
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002454static inline void
Christopher Faulet58d03682017-09-21 16:57:24 +02002455spoe_stop_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002456{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002457 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002458
Christopher Fauletfce747b2018-01-24 15:59:32 +01002459 if (sa && sa->frag_ctx.ctx == ctx) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002460 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002461 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002462 }
2463
Christopher Fauleta1cda022016-12-21 08:58:06 +01002464 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002465 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002466
Christopher Fauletb067b062017-01-04 16:39:11 +01002467 ctx->status_code = 0;
2468
Christopher Fauleta1cda022016-12-21 08:58:06 +01002469 /* Reset processing timer */
2470 ctx->process_exp = TICK_ETERNITY;
2471
Christopher Faulet8ef75252017-02-20 22:56:03 +01002472 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002473
Christopher Fauletfce747b2018-01-24 15:59:32 +01002474 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002475 ctx->frag_ctx.curmsg = NULL;
2476 ctx->frag_ctx.curarg = NULL;
2477 ctx->frag_ctx.curoff = 0;
2478 ctx->frag_ctx.flags = 0;
2479
Christopher Fauleta1cda022016-12-21 08:58:06 +01002480 if (!LIST_ISEMPTY(&ctx->list)) {
2481 LIST_DEL(&ctx->list);
2482 LIST_INIT(&ctx->list);
2483 }
2484}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002485
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002486static void
2487spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2488 struct spoe_context *ctx, int dir)
2489{
2490 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002491 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002492
2493 if (agent->var_on_error) {
2494 struct sample smp;
2495
2496 memset(&smp, 0, sizeof(smp));
2497 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2498 smp.data.u.sint = ctx->status_code;
2499 smp.data.type = SMP_T_BOOL;
2500
2501 spoe_set_var(ctx, "txn", agent->var_on_error,
2502 strlen(agent->var_on_error), &smp);
2503 }
2504 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2505 " - failed to process messages: code=%u\n",
2506 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2507 __FUNCTION__, s, ctx->status_code);
2508 send_log(ctx->strm->be, LOG_WARNING,
2509 "SPOE: [%s] failed to process messages: code=%u\n",
2510 agent->id, ctx->status_code);
2511
2512 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2513 ? SPOE_CTX_ST_READY
2514 : SPOE_CTX_ST_NONE);
2515}
2516
Christopher Faulet58d03682017-09-21 16:57:24 +02002517/* Process a list of SPOE messages. First, this functions will process messages
2518 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2519 * to process corresponding actions. During all the processing, it returns 0
2520 * and it returns 1 when the processing is finished. If an error occurred, -1
2521 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002522static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002523spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2524 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002525{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002526 struct spoe_config *conf = FLT_CONF(ctx->filter);
2527 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002528 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002529
2530 if (ctx->state == SPOE_CTX_ST_ERROR)
2531 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002532
2533 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2534 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002535 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002536 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002537 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002538 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002539 goto error;
2540 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002541
2542 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002544 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002545 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002546 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002547 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002548 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002549 goto skip;
2550 }
2551 }
2552
Christopher Fauletf7a30922016-11-10 15:04:51 +01002553 if (!tick_isset(ctx->process_exp)) {
2554 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2555 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2556 ctx->process_exp);
2557 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002558 ret = spoe_start_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002559 if (!ret)
2560 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002561
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002562 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002564 }
2565
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002566 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002567 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002568 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002569 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002570 if (ret < 0)
2571 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002572 if (!ret)
2573 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002574 if (spoe_queue_context(ctx) < 0)
2575 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002576 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2577 }
2578
2579 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002580 if (ctx->spoe_appctx)
2581 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002582 ret = 0;
2583 goto out;
2584 }
2585
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002586 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2587 ret = 0;
2588 goto out;
2589 }
2590
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002591 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002592 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002593 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002594 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002595 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002597 }
2598
2599 out:
2600 return ret;
2601
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002602 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002603 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002604 ret = 1;
2605 goto end;
2606
2607 skip:
2608 ctx->state = SPOE_CTX_ST_READY;
2609 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002610
Christopher Fauleta1cda022016-12-21 08:58:06 +01002611 end:
Christopher Faulet58d03682017-09-21 16:57:24 +02002612 spoe_stop_processing(ctx);
2613 return ret;
2614}
2615
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002616/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2617 * See spoe_process_message for details. */
2618static int
2619spoe_process_group(struct stream *s, struct spoe_context *ctx,
2620 struct spoe_group *group, int dir)
2621{
2622 int ret;
2623
2624 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2625 " - ctx-state=%s - Process messages for group=%s\n",
2626 (int)now.tv_sec, (int)now.tv_usec,
2627 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2628 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2629 group->id);
2630
2631 if (LIST_ISEMPTY(&group->messages))
2632 return 1;
2633
2634 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2635 return ret;
2636}
2637
Christopher Faulet58d03682017-09-21 16:57:24 +02002638/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2639 * See spoe_process_message for details. */
2640static int
2641spoe_process_event(struct stream *s, struct spoe_context *ctx,
2642 enum spoe_event ev)
2643{
2644 int dir, ret;
2645
2646 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002647 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002648 (int)now.tv_sec, (int)now.tv_usec,
2649 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2650 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2651 spoe_event_str[ev]);
2652
2653 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2654
2655 if (LIST_ISEMPTY(&(ctx->events[ev])))
2656 return 1;
2657
2658 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659 return ret;
2660}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002661
2662/***************************************************************************
2663 * Functions that create/destroy SPOE contexts
2664 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002666spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002667{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002668 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002669 return 1;
2670
Christopher Faulet4596fb72017-01-11 14:05:19 +01002671 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002672 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002673 LIST_DEL(&buffer_wait->list);
2674 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002675 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002676 }
2677
Christopher Faulet4596fb72017-01-11 14:05:19 +01002678 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002679 return 1;
2680
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002681 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002682 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002683 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002684 return 0;
2685}
2686
2687static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002688spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002689{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002690 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002691 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002692 LIST_DEL(&buffer_wait->list);
2693 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002694 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002695 }
2696
2697 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002698 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002699 b_free(buf);
2700 offer_buffers(buffer_wait->target,
2701 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 }
2703}
2704
Christopher Faulet4596fb72017-01-11 14:05:19 +01002705static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002706spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002707{
2708 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2709 return 1;
2710}
2711
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002712static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002713spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002714{
2715 struct spoe_config *conf = FLT_CONF(filter);
2716 struct spoe_context *ctx;
2717
Willy Tarreaubafbe012017-11-24 17:34:44 +01002718 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002719 if (ctx == NULL) {
2720 return NULL;
2721 }
2722 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002723 ctx->filter = filter;
2724 ctx->state = SPOE_CTX_ST_NONE;
2725 ctx->status_code = SPOE_CTX_ERR_NONE;
2726 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002727 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002728 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002729 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002730 LIST_INIT(&ctx->buffer_wait.list);
2731 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002732 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002733 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002734
Christopher Fauletf7a30922016-11-10 15:04:51 +01002735 ctx->stream_id = 0;
2736 ctx->frame_id = 1;
2737 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002738
2739 return ctx;
2740}
2741
2742static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002743spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744{
2745 if (!ctx)
2746 return;
2747
Christopher Faulet58d03682017-09-21 16:57:24 +02002748 spoe_stop_processing(ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002749 pool_free(pool_head_spoe_ctx, ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002750}
2751
2752static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002753spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002754{
2755 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002756 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002757}
2758
2759
2760/***************************************************************************
2761 * Hooks that manage the filter lifecycle (init/check/deinit)
2762 **************************************************************************/
2763/* Signal handler: Do a soft stop, wakeup SPOE applet */
2764static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002765spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766{
2767 struct proxy *p;
2768
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002769 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002770 while (p) {
2771 struct flt_conf *fconf;
2772
2773 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002774 struct spoe_config *conf;
2775 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002776 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002777 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002778
Christopher Faulet3b386a32017-02-23 10:17:15 +01002779 if (fconf->id != spoe_filter_id)
2780 continue;
2781
2782 conf = fconf->conf;
2783 agent = conf->agent;
2784
Christopher Faulet24289f22017-09-25 14:48:02 +02002785 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002786 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002787 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2788 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002789 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002790 }
2791 }
2792 p = p->next;
2793 }
2794}
2795
2796
2797/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2798static int
2799spoe_init(struct proxy *px, struct flt_conf *fconf)
2800{
2801 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002802
2803 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2804 init_new_proxy(&conf->agent_fe);
2805 conf->agent_fe.parent = conf->agent;
2806 conf->agent_fe.last_change = now.tv_sec;
2807 conf->agent_fe.id = conf->agent->id;
2808 conf->agent_fe.cap = PR_CAP_FE;
2809 conf->agent_fe.mode = PR_MODE_TCP;
2810 conf->agent_fe.maxconn = 0;
2811 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2812 conf->agent_fe.conn_retries = CONN_RETRIES;
2813 conf->agent_fe.accept = frontend_accept;
2814 conf->agent_fe.srv = NULL;
2815 conf->agent_fe.timeout.client = TICK_ETERNITY;
2816 conf->agent_fe.default_target = &spoe_applet.obj_type;
2817 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2818
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002819 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002820 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002821 sighandler_registered = 1;
2822 }
2823
2824 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002825}
2826
2827/* Free ressources allocated by the SPOE filter. */
2828static void
2829spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2830{
2831 struct spoe_config *conf = fconf->conf;
2832
2833 if (conf) {
2834 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002835
Christopher Faulet8ef75252017-02-20 22:56:03 +01002836 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002837 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002838 free(conf);
2839 }
2840 fconf->conf = NULL;
2841}
2842
2843/* Check configuration of a SPOE filter for a specified proxy.
2844 * Return 1 on error, else 0. */
2845static int
2846spoe_check(struct proxy *px, struct flt_conf *fconf)
2847{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002848 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002849 struct spoe_config *conf = fconf->conf;
2850 struct proxy *target;
2851
Christopher Faulet7ee86672017-09-19 11:08:28 +02002852 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2853 * are uniq */
2854 list_for_each_entry(f, &px->filter_configs, list) {
2855 struct spoe_config *c = f->conf;
2856
2857 /* This is not an SPOE filter */
2858 if (f->id != spoe_filter_id)
2859 continue;
2860 /* This is the current SPOE filter */
2861 if (f == fconf)
2862 continue;
2863
2864 /* Check engine Id. It should be uniq */
2865 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002866 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2867 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002868 return 1;
2869 }
2870 }
2871
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872 target = proxy_be_by_name(conf->agent->b.name);
2873 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002874 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2875 " declared at %s:%d.\n",
2876 px->id, conf->agent->b.name, conf->agent->id,
2877 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002878 return 1;
2879 }
2880 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002881 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2882 " at %s:%d does not support HTTP mode.\n",
2883 px->id, target->id, conf->agent->id,
2884 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002885 return 1;
2886 }
2887
2888 free(conf->agent->b.name);
2889 conf->agent->b.name = NULL;
2890 conf->agent->b.be = target;
2891 return 0;
2892}
2893
2894/**************************************************************************
2895 * Hooks attached to a stream
2896 *************************************************************************/
2897/* Called when a filter instance is created and attach to a stream. It creates
2898 * the context that will be used to process this stream. */
2899static int
2900spoe_start(struct stream *s, struct filter *filter)
2901{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002902 struct spoe_config *conf = FLT_CONF(filter);
2903 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002904 struct spoe_context *ctx;
2905
2906 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002907 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 __FUNCTION__, s);
2909
Christopher Faulet8ef75252017-02-20 22:56:03 +01002910 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002911 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002912 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2913 " - failed to create SPOE context\n",
2914 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002915 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002916 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002917 "SPOE: [%s] failed to create SPOE context\n",
2918 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919 return 0;
2920 }
2921
2922 ctx->strm = s;
2923 ctx->state = SPOE_CTX_ST_READY;
2924 filter->ctx = ctx;
2925
Christopher Faulet11610f32017-09-21 10:23:10 +02002926 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002927 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2928
Christopher Faulet11610f32017-09-21 10:23:10 +02002929 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002930 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2931
Christopher Faulet11610f32017-09-21 10:23:10 +02002932 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002933 filter->pre_analyzers |= AN_RES_INSPECT;
2934
Christopher Faulet11610f32017-09-21 10:23:10 +02002935 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2937
Christopher Faulet11610f32017-09-21 10:23:10 +02002938 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002939 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2940
Christopher Faulet11610f32017-09-21 10:23:10 +02002941 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2943
2944 return 1;
2945}
2946
2947/* Called when a filter instance is detached from a stream. It release the
2948 * attached SPOE context. */
2949static void
2950spoe_stop(struct stream *s, struct filter *filter)
2951{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2953 (int)now.tv_sec, (int)now.tv_usec,
2954 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2955 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002956 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957}
2958
Christopher Fauletf7a30922016-11-10 15:04:51 +01002959
2960/*
2961 * Called when the stream is woken up because of expired timer.
2962 */
2963static void
2964spoe_check_timeouts(struct stream *s, struct filter *filter)
2965{
2966 struct spoe_context *ctx = filter->ctx;
2967
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002968 if (tick_is_expired(ctx->process_exp, now_ms)) {
2969 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002970 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002971 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002972}
2973
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002974/* Called when we are ready to filter data on a channel */
2975static int
2976spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2977{
2978 struct spoe_context *ctx = filter->ctx;
2979 int ret = 1;
2980
2981 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2982 " - ctx-flags=0x%08x\n",
2983 (int)now.tv_sec, (int)now.tv_usec,
2984 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2985 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2986
Christopher Fauletb067b062017-01-04 16:39:11 +01002987 if (ctx->state == SPOE_CTX_ST_NONE)
2988 goto out;
2989
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002990 if (!(chn->flags & CF_ISRESP)) {
2991 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2992 chn->analysers |= AN_REQ_INSPECT_FE;
2993 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2994 chn->analysers |= AN_REQ_INSPECT_BE;
2995
2996 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2997 goto out;
2998
2999 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003000 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003001 if (!ret)
3002 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003003 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3004 }
3005 else {
3006 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3007 chn->analysers |= AN_RES_INSPECT;
3008
3009 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3010 goto out;
3011
Christopher Faulet8ef75252017-02-20 22:56:03 +01003012 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003013 if (!ret) {
3014 channel_dont_read(chn);
3015 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003016 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003017 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003018 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003019 }
3020
3021 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003022 return ret;
3023}
3024
3025/* Called before a processing happens on a given channel */
3026static int
3027spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3028 struct channel *chn, unsigned an_bit)
3029{
3030 struct spoe_context *ctx = filter->ctx;
3031 int ret = 1;
3032
3033 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3034 " - ctx-flags=0x%08x - ana=0x%08x\n",
3035 (int)now.tv_sec, (int)now.tv_usec,
3036 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3037 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3038 ctx->flags, an_bit);
3039
Christopher Fauletb067b062017-01-04 16:39:11 +01003040 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003041 goto out;
3042
3043 switch (an_bit) {
3044 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003045 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046 break;
3047 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003048 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049 break;
3050 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003051 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 break;
3053 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003054 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003055 break;
3056 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003057 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 break;
3059 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003060 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003061 break;
3062 }
3063
3064 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003065 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003066 channel_dont_read(chn);
3067 channel_dont_close(chn);
3068 }
3069 return ret;
3070}
3071
3072/* Called when the filtering on the channel ends. */
3073static int
3074spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3075{
3076 struct spoe_context *ctx = filter->ctx;
3077
3078 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3079 " - ctx-flags=0x%08x\n",
3080 (int)now.tv_sec, (int)now.tv_usec,
3081 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3082 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3083
3084 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003085 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003086 }
3087
3088 return 1;
3089}
3090
3091/********************************************************************
3092 * Functions that manage the filter initialization
3093 ********************************************************************/
3094struct flt_ops spoe_ops = {
3095 /* Manage SPOE filter, called for each filter declaration */
3096 .init = spoe_init,
3097 .deinit = spoe_deinit,
3098 .check = spoe_check,
3099
3100 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003101 .attach = spoe_start,
3102 .detach = spoe_stop,
3103 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104
3105 /* Handle channels activity */
3106 .channel_start_analyze = spoe_start_analyze,
3107 .channel_pre_analyze = spoe_chn_pre_analyze,
3108 .channel_end_analyze = spoe_end_analyze,
3109};
3110
3111
3112static int
3113cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3114{
3115 const char *err;
3116 int i, err_code = 0;
3117
3118 if ((cfg_scope == NULL && curengine != NULL) ||
3119 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003120 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003121 goto out;
3122
3123 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3124 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003125 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3126 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127 err_code |= ERR_ALERT | ERR_ABORT;
3128 goto out;
3129 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003130 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3131 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132 goto out;
3133 }
3134
3135 err = invalid_char(args[1]);
3136 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003137 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3138 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139 err_code |= ERR_ALERT | ERR_ABORT;
3140 goto out;
3141 }
3142
3143 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003144 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3145 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146 err_code |= ERR_ALERT | ERR_ABORT;
3147 goto out;
3148 }
3149 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003150 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003151 err_code |= ERR_ALERT | ERR_ABORT;
3152 goto out;
3153 }
3154
3155 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003156
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003157 curagent->conf.file = strdup(file);
3158 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003159
3160 curagent->timeout.hello = TICK_ETERNITY;
3161 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003162 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003163
3164 curagent->engine_id = NULL;
3165 curagent->var_pfx = NULL;
3166 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003167 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3168 if (global.nbthread == 1)
3169 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003170 curagent->cps_max = 0;
3171 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003172 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003173 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003174
3175 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003176 LIST_INIT(&curagent->events[i]);
3177 LIST_INIT(&curagent->groups);
3178 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003179
Christopher Faulet24289f22017-09-25 14:48:02 +02003180 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003181 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003182 err_code |= ERR_ALERT | ERR_ABORT;
3183 goto out;
3184 }
3185 for (i = 0; i < global.nbthread; ++i) {
3186 curagent->rt[i].frame_size = curagent->max_frame_size;
3187 curagent->rt[i].applets_act = 0;
3188 curagent->rt[i].applets_idle = 0;
3189 curagent->rt[i].sending_rate = 0;
3190 LIST_INIT(&curagent->rt[i].applets);
3191 LIST_INIT(&curagent->rt[i].sending_queue);
3192 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003193 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003194 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 }
3196 else if (!strcmp(args[0], "use-backend")) {
3197 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003198 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3199 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 err_code |= ERR_ALERT | ERR_FATAL;
3201 goto out;
3202 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003203 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003204 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205 free(curagent->b.name);
3206 curagent->b.name = strdup(args[1]);
3207 }
3208 else if (!strcmp(args[0], "messages")) {
3209 int cur_arg = 1;
3210 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003211 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003212
Christopher Faulet11610f32017-09-21 10:23:10 +02003213 list_for_each_entry(ph, &curmphs, list) {
3214 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003215 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3216 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003217 err_code |= ERR_ALERT | ERR_FATAL;
3218 goto out;
3219 }
3220 }
3221
Christopher Faulet11610f32017-09-21 10:23:10 +02003222 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003223 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 err_code |= ERR_ALERT | ERR_ABORT;
3225 goto out;
3226 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003227 ph->id = strdup(args[cur_arg]);
3228 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 cur_arg++;
3230 }
3231 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003232 else if (!strcmp(args[0], "groups")) {
3233 int cur_arg = 1;
3234 while (*args[cur_arg]) {
3235 struct spoe_placeholder *ph = NULL;
3236
3237 list_for_each_entry(ph, &curgphs, list) {
3238 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003239 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3240 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003241 err_code |= ERR_ALERT | ERR_FATAL;
3242 goto out;
3243 }
3244 }
3245
3246 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003247 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003248 err_code |= ERR_ALERT | ERR_ABORT;
3249 goto out;
3250 }
3251 ph->id = strdup(args[cur_arg]);
3252 LIST_ADDQ(&curgphs, &ph->list);
3253 cur_arg++;
3254 }
3255 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 else if (!strcmp(args[0], "timeout")) {
3257 unsigned int *tv = NULL;
3258 const char *res;
3259 unsigned timeout;
3260
3261 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003262 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3263 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003264 err_code |= ERR_ALERT | ERR_FATAL;
3265 goto out;
3266 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003267 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3268 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003269 if (!strcmp(args[1], "hello"))
3270 tv = &curagent->timeout.hello;
3271 else if (!strcmp(args[1], "idle"))
3272 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003273 else if (!strcmp(args[1], "processing"))
3274 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003275 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003276 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3277 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003278 err_code |= ERR_ALERT | ERR_FATAL;
3279 goto out;
3280 }
3281 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003282 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3283 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003284 err_code |= ERR_ALERT | ERR_FATAL;
3285 goto out;
3286 }
3287 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3288 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003289 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3290 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003291 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 goto out;
3293 }
3294 *tv = MS_TO_TICKS(timeout);
3295 }
3296 else if (!strcmp(args[0], "option")) {
3297 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003298 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3299 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003300 err_code |= ERR_ALERT | ERR_FATAL;
3301 goto out;
3302 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003303
Christopher Faulet305c6072017-02-23 16:17:53 +01003304 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003305 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003306 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003307 if (kwm == 1)
3308 curagent->flags &= ~SPOE_FL_PIPELINING;
3309 else
3310 curagent->flags |= SPOE_FL_PIPELINING;
3311 goto out;
3312 }
3313 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003314 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003315 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003316 if (kwm == 1)
3317 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003318 else {
3319 if (global.nbthread == 1)
3320 curagent->flags |= SPOE_FL_ASYNC;
3321 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003322 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3323 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003324 err_code |= ERR_WARN;
3325 }
3326 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003327 goto out;
3328 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003329 else if (!strcmp(args[1], "send-frag-payload")) {
3330 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3331 goto out;
3332 if (kwm == 1)
3333 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3334 else
3335 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3336 goto out;
3337 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003338
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003339 /* Following options does not support negation */
3340 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003341 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3342 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003343 err_code |= ERR_ALERT | ERR_FATAL;
3344 goto out;
3345 }
3346
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003347 if (!strcmp(args[1], "var-prefix")) {
3348 char *tmp;
3349
3350 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003351 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3352 file, linenum, args[0],
3353 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003354 err_code |= ERR_ALERT | ERR_FATAL;
3355 goto out;
3356 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003357 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3358 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003359 tmp = args[2];
3360 while (*tmp) {
3361 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003362 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3363 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003364 err_code |= ERR_ALERT | ERR_FATAL;
3365 goto out;
3366 }
3367 tmp++;
3368 }
3369 curagent->var_pfx = strdup(args[2]);
3370 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003371 else if (!strcmp(args[1], "force-set-var")) {
3372 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3373 goto out;
3374 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3375 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003376 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003377 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003378 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003379 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3380 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003381 else if (!strcmp(args[1], "set-on-error")) {
3382 char *tmp;
3383
3384 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003385 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3386 file, linenum, args[0],
3387 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003388 err_code |= ERR_ALERT | ERR_FATAL;
3389 goto out;
3390 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003391 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3392 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003393 tmp = args[2];
3394 while (*tmp) {
3395 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3397 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003398 err_code |= ERR_ALERT | ERR_FATAL;
3399 goto out;
3400 }
3401 tmp++;
3402 }
3403 curagent->var_on_error = strdup(args[2]);
3404 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003406 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3407 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003408 err_code |= ERR_ALERT | ERR_FATAL;
3409 goto out;
3410 }
Christopher Faulet48026722016-11-16 15:01:12 +01003411 }
3412 else if (!strcmp(args[0], "maxconnrate")) {
3413 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003414 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3415 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003416 err_code |= ERR_ALERT | ERR_FATAL;
3417 goto out;
3418 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003419 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003420 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003421 curagent->cps_max = atol(args[1]);
3422 }
3423 else if (!strcmp(args[0], "maxerrrate")) {
3424 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003425 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3426 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003427 err_code |= ERR_ALERT | ERR_FATAL;
3428 goto out;
3429 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003430 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003431 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003432 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003433 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003434 else if (!strcmp(args[0], "max-frame-size")) {
3435 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003436 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3437 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003438 err_code |= ERR_ALERT | ERR_FATAL;
3439 goto out;
3440 }
3441 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3442 goto out;
3443 curagent->max_frame_size = atol(args[1]);
3444 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3445 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003446 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3447 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003448 err_code |= ERR_ALERT | ERR_FATAL;
3449 goto out;
3450 }
3451 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003452 else if (!strcmp(args[0], "register-var-names")) {
3453 int cur_arg;
3454
3455 if (!*args[1]) {
3456 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3457 file, linenum, args[0]);
3458 err_code |= ERR_ALERT | ERR_FATAL;
3459 goto out;
3460 }
3461 cur_arg = 1;
3462 while (*args[cur_arg]) {
3463 struct spoe_var_placeholder *vph;
3464
3465 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3466 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3467 err_code |= ERR_ALERT | ERR_ABORT;
3468 goto out;
3469 }
3470 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3471 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3472 err_code |= ERR_ALERT | ERR_ABORT;
3473 goto out;
3474 }
3475 LIST_ADDQ(&curvars, &vph->list);
3476 cur_arg++;
3477 }
3478 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003479 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003480 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3481 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003482 err_code |= ERR_ALERT | ERR_FATAL;
3483 goto out;
3484 }
3485 out:
3486 return err_code;
3487}
Christopher Faulet11610f32017-09-21 10:23:10 +02003488static int
3489cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3490{
3491 struct spoe_group *grp;
3492 const char *err;
3493 int err_code = 0;
3494
3495 if ((cfg_scope == NULL && curengine != NULL) ||
3496 (cfg_scope != NULL && curengine == NULL) ||
3497 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3498 goto out;
3499
3500 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3501 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003502 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3503 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003504 err_code |= ERR_ALERT | ERR_ABORT;
3505 goto out;
3506 }
3507 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3508 err_code |= ERR_ABORT;
3509 goto out;
3510 }
3511
3512 err = invalid_char(args[1]);
3513 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003514 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3515 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003516 err_code |= ERR_ALERT | ERR_ABORT;
3517 goto out;
3518 }
3519
3520 list_for_each_entry(grp, &curgrps, list) {
3521 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003522 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3523 " name as another one declared at %s:%d.\n",
3524 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003525 err_code |= ERR_ALERT | ERR_FATAL;
3526 goto out;
3527 }
3528 }
3529
3530 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003531 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003532 err_code |= ERR_ALERT | ERR_ABORT;
3533 goto out;
3534 }
3535
3536 curgrp->id = strdup(args[1]);
3537 curgrp->conf.file = strdup(file);
3538 curgrp->conf.line = linenum;
3539 LIST_INIT(&curgrp->phs);
3540 LIST_INIT(&curgrp->messages);
3541 LIST_ADDQ(&curgrps, &curgrp->list);
3542 }
3543 else if (!strcmp(args[0], "messages")) {
3544 int cur_arg = 1;
3545 while (*args[cur_arg]) {
3546 struct spoe_placeholder *ph = NULL;
3547
3548 list_for_each_entry(ph, &curgrp->phs, list) {
3549 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003550 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3551 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003552 err_code |= ERR_ALERT | ERR_FATAL;
3553 goto out;
3554 }
3555 }
3556
3557 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003558 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003559 err_code |= ERR_ALERT | ERR_ABORT;
3560 goto out;
3561 }
3562 ph->id = strdup(args[cur_arg]);
3563 LIST_ADDQ(&curgrp->phs, &ph->list);
3564 cur_arg++;
3565 }
3566 }
3567 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003568 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3569 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003570 err_code |= ERR_ALERT | ERR_FATAL;
3571 goto out;
3572 }
3573 out:
3574 return err_code;
3575}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003576
3577static int
3578cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3579{
3580 struct spoe_message *msg;
3581 struct spoe_arg *arg;
3582 const char *err;
3583 char *errmsg = NULL;
3584 int err_code = 0;
3585
3586 if ((cfg_scope == NULL && curengine != NULL) ||
3587 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003588 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003589 goto out;
3590
3591 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3592 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003593 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3594 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003595 err_code |= ERR_ALERT | ERR_ABORT;
3596 goto out;
3597 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003598 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3599 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003600 goto out;
3601 }
3602
3603 err = invalid_char(args[1]);
3604 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003605 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3606 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003607 err_code |= ERR_ALERT | ERR_ABORT;
3608 goto out;
3609 }
3610
3611 list_for_each_entry(msg, &curmsgs, list) {
3612 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003613 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3614 " name as another one declared at %s:%d.\n",
3615 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003616 err_code |= ERR_ALERT | ERR_FATAL;
3617 goto out;
3618 }
3619 }
3620
3621 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003622 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003623 err_code |= ERR_ALERT | ERR_ABORT;
3624 goto out;
3625 }
3626
3627 curmsg->id = strdup(args[1]);
3628 curmsg->id_len = strlen(curmsg->id);
3629 curmsg->event = SPOE_EV_NONE;
3630 curmsg->conf.file = strdup(file);
3631 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003632 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003633 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003634 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003635 LIST_INIT(&curmsg->by_evt);
3636 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003637 LIST_ADDQ(&curmsgs, &curmsg->list);
3638 }
3639 else if (!strcmp(args[0], "args")) {
3640 int cur_arg = 1;
3641
3642 curproxy->conf.args.ctx = ARGC_SPOE;
3643 curproxy->conf.args.file = file;
3644 curproxy->conf.args.line = linenum;
3645 while (*args[cur_arg]) {
3646 char *delim = strchr(args[cur_arg], '=');
3647 int idx = 0;
3648
3649 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003650 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003651 err_code |= ERR_ALERT | ERR_ABORT;
3652 goto out;
3653 }
3654
3655 if (!delim) {
3656 arg->name = NULL;
3657 arg->name_len = 0;
3658 delim = args[cur_arg];
3659 }
3660 else {
3661 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3662 arg->name_len = delim - args[cur_arg];
3663 delim++;
3664 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003665 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3666 &idx, file, linenum, &errmsg,
3667 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003668 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003669 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003670 err_code |= ERR_ALERT | ERR_FATAL;
3671 free(arg->name);
3672 free(arg);
3673 goto out;
3674 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003675 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003676 LIST_ADDQ(&curmsg->args, &arg->list);
3677 cur_arg++;
3678 }
3679 curproxy->conf.args.file = NULL;
3680 curproxy->conf.args.line = 0;
3681 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003682 else if (!strcmp(args[0], "acl")) {
3683 err = invalid_char(args[1]);
3684 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003685 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3686 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
3690 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003691 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3692 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003693 err_code |= ERR_ALERT | ERR_FATAL;
3694 goto out;
3695 }
3696 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003697 else if (!strcmp(args[0], "event")) {
3698 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003699 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003700 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003701 goto out;
3702 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003703 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3704 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003705
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003706 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3707 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3708 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3709 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3710
3711 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3712 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3713 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3714 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3715 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3716 curmsg->event = SPOE_EV_ON_TCP_RSP;
3717
3718 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3719 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3720 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3721 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3722 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3723 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3724 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003725 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3726 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003727 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003728 goto out;
3729 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003730
3731 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3732 struct acl_cond *cond;
3733
3734 cond = build_acl_cond(file, linenum, &curmsg->acls,
3735 curproxy, (const char **)args+2,
3736 &errmsg);
3737 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003738 ha_alert("parsing [%s:%d] : error detected while "
3739 "parsing an 'event %s' condition : %s.\n",
3740 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003741 err_code |= ERR_ALERT | ERR_FATAL;
3742 goto out;
3743 }
3744 curmsg->cond = cond;
3745 }
3746 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003747 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3748 "or 'unless' followed by a condition but found '%s'.\n",
3749 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003750 err_code |= ERR_ALERT | ERR_FATAL;
3751 goto out;
3752 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003753 }
3754 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003755 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3756 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003757 err_code |= ERR_ALERT | ERR_FATAL;
3758 goto out;
3759 }
3760 out:
3761 free(errmsg);
3762 return err_code;
3763}
3764
3765/* Return -1 on error, else 0 */
3766static int
3767parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3768 struct flt_conf *fconf, char **err, void *private)
3769{
3770 struct list backup_sections;
3771 struct spoe_config *conf;
3772 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003773 struct spoe_group *grp, *grpback;
3774 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003775 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003776 char *file = NULL, *engine = NULL;
3777 int ret, pos = *cur_arg + 1;
3778
3779 conf = calloc(1, sizeof(*conf));
3780 if (conf == NULL) {
3781 memprintf(err, "%s: out of memory", args[*cur_arg]);
3782 goto error;
3783 }
3784 conf->proxy = px;
3785
3786 while (*args[pos]) {
3787 if (!strcmp(args[pos], "config")) {
3788 if (!*args[pos+1]) {
3789 memprintf(err, "'%s' : '%s' option without value",
3790 args[*cur_arg], args[pos]);
3791 goto error;
3792 }
3793 file = args[pos+1];
3794 pos += 2;
3795 }
3796 else if (!strcmp(args[pos], "engine")) {
3797 if (!*args[pos+1]) {
3798 memprintf(err, "'%s' : '%s' option without value",
3799 args[*cur_arg], args[pos]);
3800 goto error;
3801 }
3802 engine = args[pos+1];
3803 pos += 2;
3804 }
3805 else {
3806 memprintf(err, "unknown keyword '%s'", args[pos]);
3807 goto error;
3808 }
3809 }
3810 if (file == NULL) {
3811 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3812 goto error;
3813 }
3814
3815 /* backup sections and register SPOE sections */
3816 LIST_INIT(&backup_sections);
3817 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003818 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3819 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003820 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003821
3822 /* Parse SPOE filter configuration file */
3823 curengine = engine;
3824 curproxy = px;
3825 curagent = NULL;
3826 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 LIST_INIT(&curmsgs);
3828 LIST_INIT(&curgrps);
3829 LIST_INIT(&curmphs);
3830 LIST_INIT(&curgphs);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003831 LIST_INIT(&curvars);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003832 ret = readcfgfile(file);
3833 curproxy = NULL;
3834
3835 /* unregister SPOE sections and restore previous sections */
3836 cfg_unregister_sections();
3837 cfg_restore_sections(&backup_sections);
3838
3839 if (ret == -1) {
3840 memprintf(err, "Could not open configuration file %s : %s",
3841 file, strerror(errno));
3842 goto error;
3843 }
3844 if (ret & (ERR_ABORT|ERR_FATAL)) {
3845 memprintf(err, "Error(s) found in configuration file %s", file);
3846 goto error;
3847 }
3848
3849 /* Check SPOE agent */
3850 if (curagent == NULL) {
3851 memprintf(err, "No SPOE agent found in file %s", file);
3852 goto error;
3853 }
3854 if (curagent->b.name == NULL) {
3855 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3856 curagent->id, curagent->conf.file, curagent->conf.line);
3857 goto error;
3858 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003859 if (curagent->timeout.hello == TICK_ETERNITY ||
3860 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003861 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3863 " | While not properly invalid, you will certainly encounter various problems\n"
3864 " | with such a configuration. To fix this, please ensure that all following\n"
3865 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3866 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003867 }
3868 if (curagent->var_pfx == NULL) {
3869 char *tmp = curagent->id;
3870
3871 while (*tmp) {
3872 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3873 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3874 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3875 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3876 goto error;
3877 }
3878 tmp++;
3879 }
3880 curagent->var_pfx = strdup(curagent->id);
3881 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003882 if (curagent->engine_id == NULL)
3883 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003884
Christopher Faulet11610f32017-09-21 10:23:10 +02003885 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003886 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3887 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003888 goto finish;
3889 }
3890
Christopher Faulet11610f32017-09-21 10:23:10 +02003891 /* Replace placeholders by the corresponding messages for the SPOE
3892 * agent */
3893 list_for_each_entry(ph, &curmphs, list) {
3894 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003895 struct spoe_arg *arg;
3896 unsigned int where;
3897
Christopher Faulet11610f32017-09-21 10:23:10 +02003898 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3900 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3901 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3902 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3903 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3904 }
3905 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3906 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3907 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003908 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3909 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003910 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003911 }
3912 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003913 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3914 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003915 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003916 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003917
3918 where = 0;
3919 switch (msg->event) {
3920 case SPOE_EV_ON_CLIENT_SESS:
3921 where |= SMP_VAL_FE_CON_ACC;
3922 break;
3923
3924 case SPOE_EV_ON_TCP_REQ_FE:
3925 where |= SMP_VAL_FE_REQ_CNT;
3926 break;
3927
3928 case SPOE_EV_ON_HTTP_REQ_FE:
3929 where |= SMP_VAL_FE_HRQ_HDR;
3930 break;
3931
3932 case SPOE_EV_ON_TCP_REQ_BE:
3933 if (px->cap & PR_CAP_FE)
3934 where |= SMP_VAL_FE_REQ_CNT;
3935 if (px->cap & PR_CAP_BE)
3936 where |= SMP_VAL_BE_REQ_CNT;
3937 break;
3938
3939 case SPOE_EV_ON_HTTP_REQ_BE:
3940 if (px->cap & PR_CAP_FE)
3941 where |= SMP_VAL_FE_HRQ_HDR;
3942 if (px->cap & PR_CAP_BE)
3943 where |= SMP_VAL_BE_HRQ_HDR;
3944 break;
3945
3946 case SPOE_EV_ON_SERVER_SESS:
3947 where |= SMP_VAL_BE_SRV_CON;
3948 break;
3949
3950 case SPOE_EV_ON_TCP_RSP:
3951 if (px->cap & PR_CAP_FE)
3952 where |= SMP_VAL_FE_RES_CNT;
3953 if (px->cap & PR_CAP_BE)
3954 where |= SMP_VAL_BE_RES_CNT;
3955 break;
3956
3957 case SPOE_EV_ON_HTTP_RSP:
3958 if (px->cap & PR_CAP_FE)
3959 where |= SMP_VAL_FE_HRS_HDR;
3960 if (px->cap & PR_CAP_BE)
3961 where |= SMP_VAL_BE_HRS_HDR;
3962 break;
3963
3964 default:
3965 break;
3966 }
3967
3968 list_for_each_entry(arg, &msg->args, list) {
3969 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003970 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003971 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003972 "none of which is available here ('%s')",
3973 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003974 sample_ckp_names(arg->expr->fetch->use),
3975 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003976 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003977 }
3978 }
3979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003981 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3982 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003983 }
3984 }
3985 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003986 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003987 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003988 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003989 continue;
3990 }
3991
Christopher Faulet11610f32017-09-21 10:23:10 +02003992 /* Replace placeholders by the corresponding groups for the SPOE
3993 * agent */
3994 list_for_each_entry(ph, &curgphs, list) {
3995 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3996 if (!strcmp(grp->id, ph->id)) {
3997 grp->agent = curagent;
3998 LIST_DEL(&grp->list);
3999 LIST_ADDQ(&curagent->groups, &grp->list);
4000 goto next_aph;
4001 }
4002 }
4003 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4004 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4005 goto error;
4006 next_aph:
4007 continue;
4008 }
4009
4010 /* Replace placeholders by the corresponding message for each SPOE
4011 * group of the SPOE agent */
4012 list_for_each_entry(grp, &curagent->groups, list) {
4013 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4014 list_for_each_entry(msg, &curmsgs, list) {
4015 if (!strcmp(msg->id, ph->id)) {
4016 if (msg->group != NULL) {
4017 memprintf(err, "SPOE message '%s' already belongs to "
4018 "the SPOE group '%s' declare at %s:%d",
4019 msg->id, msg->group->id,
4020 msg->group->conf.file,
4021 msg->group->conf.line);
4022 goto error;
4023 }
4024
4025 /* Scope for arguments are not checked for now. We will check
4026 * them only if a rule use the corresponding SPOE group. */
4027 msg->agent = curagent;
4028 msg->group = grp;
4029 LIST_DEL(&ph->list);
4030 LIST_ADDQ(&grp->messages, &msg->by_grp);
4031 goto next_mph_grp;
4032 }
4033 }
4034 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4035 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4036 goto error;
4037 next_mph_grp:
4038 continue;
4039 }
4040 }
4041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004042 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004043 /* move curmsgs to the agent message list */
4044 curmsgs.n->p = &curagent->messages;
4045 curmsgs.p->n = &curagent->messages;
4046 curagent->messages = curmsgs;
4047 LIST_INIT(&curmsgs);
4048
Christopher Faulet7ee86672017-09-19 11:08:28 +02004049 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004050 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004051 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4052 LIST_DEL(&ph->list);
4053 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004054 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004055 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4056 LIST_DEL(&ph->list);
4057 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004059 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4060 struct arg arg;
4061
4062 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4063 curagent->var_pfx, vph->name);
4064
4065 arg.type = ARGT_STR;
4066 arg.data.str.str = trash.str;
4067 arg.data.str.len = trash.len;
4068 if (!vars_check_arg(&arg, err)) {
4069 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4070 curagent->id, curagent->var_pfx, vph->name, *err);
4071 goto error;
4072 }
4073
4074 LIST_DEL(&vph->list);
4075 free(vph->name);
4076 free(vph);
4077 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004078 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4079 LIST_DEL(&grp->list);
4080 spoe_release_group(grp);
4081 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004082 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004083 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004084 fconf->ops = &spoe_ops;
4085 fconf->conf = conf;
4086 return 0;
4087
4088 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004089 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004090 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4091 LIST_DEL(&ph->list);
4092 spoe_release_placeholder(ph);
4093 }
4094 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4095 LIST_DEL(&ph->list);
4096 spoe_release_placeholder(ph);
4097 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004098 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4099 LIST_DEL(&vph->list);
4100 free(vph->name);
4101 free(vph);
4102 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004103 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4104 LIST_DEL(&grp->list);
4105 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004106 }
4107 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4108 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004109 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004110 }
4111 free(conf);
4112 return -1;
4113}
4114
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004115/* Send message of a SPOE group. This is the action_ptr callback of a rule
4116 * associated to a "send-spoe-group" action.
4117 *
4118 * It returns ACT_RET_CONT is processing is finished without error, it returns
4119 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4120 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004121static enum act_return
4122spoe_send_group(struct act_rule *rule, struct proxy *px,
4123 struct session *sess, struct stream *s, int flags)
4124{
4125 struct filter *filter;
4126 struct spoe_agent *agent = NULL;
4127 struct spoe_group *group = NULL;
4128 struct spoe_context *ctx = NULL;
4129 int ret, dir;
4130
4131 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4132 if (filter->config == rule->arg.act.p[0]) {
4133 agent = rule->arg.act.p[2];
4134 group = rule->arg.act.p[3];
4135 ctx = filter->ctx;
4136 break;
4137 }
4138 }
4139 if (agent == NULL || group == NULL || ctx == NULL)
4140 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004141 if (ctx->state == SPOE_CTX_ST_NONE)
4142 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004143
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004144 switch (rule->from) {
4145 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4146 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4147 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4148 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4149 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4150 default:
4151 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4152 " - internal error while execute spoe-send-group\n",
4153 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4154 __FUNCTION__, s);
4155 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4156 agent->id);
4157 return ACT_RET_CONT;
4158 }
4159
4160 ret = spoe_process_group(s, ctx, group, dir);
4161 if (ret == 1)
4162 return ACT_RET_CONT;
4163 else if (ret == 0) {
4164 if (flags & ACT_FLAG_FINAL) {
4165 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4166 " - failed to process group '%s': interrupted by caller\n",
4167 (int)now.tv_sec, (int)now.tv_usec,
4168 agent->id, __FUNCTION__, s, group->id);
4169 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4170 spoe_handle_processing_error(s, agent, ctx, dir);
4171 spoe_stop_processing(ctx);
4172 return ACT_RET_CONT;
4173 }
4174 return ACT_RET_YIELD;
4175 }
4176 else
4177 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004178}
4179
4180/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4181 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4182 * action should be:
4183 *
4184 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4185 *
4186 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4187 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4188 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4189 * group.
4190 *
4191 * The function returns 1 in success case, otherwise, it returns 0 and err is
4192 * filled.
4193 */
4194static int
4195check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4196{
4197 struct flt_conf *fconf;
4198 struct spoe_config *conf;
4199 struct spoe_agent *agent = NULL;
4200 struct spoe_group *group;
4201 struct spoe_message *msg;
4202 char *engine_id = rule->arg.act.p[0];
4203 char *group_id = rule->arg.act.p[1];
4204 unsigned int where = 0;
4205
4206 switch (rule->from) {
4207 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4208 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4209 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4210 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4211 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4212 default:
4213 memprintf(err,
4214 "internal error, unexpected rule->from=%d, please report this bug!",
4215 rule->from);
4216 goto error;
4217 }
4218
4219 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4220 * <px> */
4221 list_for_each_entry(fconf, &px->filter_configs, list) {
4222 conf = fconf->conf;
4223
4224 /* This is not an SPOE filter */
4225 if (fconf->id != spoe_filter_id)
4226 continue;
4227
4228 /* This is the good engine */
4229 if (!strcmp(conf->id, engine_id)) {
4230 agent = conf->agent;
4231 break;
4232 }
4233 }
4234 if (agent == NULL) {
4235 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4236 engine_id, group_id);
4237 goto error;
4238 }
4239
4240 /* Try to find the right group */
4241 list_for_each_entry(group, &agent->groups, list) {
4242 /* This is the good group */
4243 if (!strcmp(group->id, group_id))
4244 break;
4245 }
4246 if (&group->list == &agent->groups) {
4247 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4248 group_id, engine_id);
4249 goto error;
4250 }
4251
4252 /* Ok, we found the group, we need to check messages and their
4253 * arguments */
4254 list_for_each_entry(msg, &group->messages, by_grp) {
4255 struct spoe_arg *arg;
4256
4257 list_for_each_entry(arg, &msg->args, list) {
4258 if (!(arg->expr->fetch->val & where)) {
4259 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4260 "some args extract information from '%s',"
4261 "none of which is available here ('%s')",
4262 msg->id, group->id, msg->conf.file, msg->conf.line,
4263 sample_ckp_names(arg->expr->fetch->use),
4264 sample_ckp_names(where));
4265 goto error;
4266 }
4267 }
4268 }
4269
4270 free(engine_id);
4271 free(group_id);
4272 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4273 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4274 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4275 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4276 return 1;
4277
4278 error:
4279 free(engine_id);
4280 free(group_id);
4281 return 0;
4282}
4283
4284/* Parse 'send-spoe-group' action following the format:
4285 *
4286 * ... send-spoe-group <engine-id> <group-id>
4287 *
4288 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4289 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4290 * ids are saved and used later, when the rule will be checked.
4291 */
4292static enum act_parse_ret
4293parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4294 struct act_rule *rule, char **err)
4295{
4296 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4297 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4298 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4299 return ACT_RET_PRS_ERR;
4300 }
4301 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4302 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4303
4304 (*orig_arg) += 2;
4305
4306 rule->action = ACT_CUSTOM;
4307 rule->action_ptr = spoe_send_group;
4308 rule->check_ptr = check_send_spoe_group;
4309 return ACT_RET_PRS_OK;
4310}
4311
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004312
4313/* Declare the filter parser for "spoe" keyword */
4314static struct flt_kw_list flt_kws = { "SPOE", { }, {
4315 { "spoe", parse_spoe_flt, NULL },
4316 { NULL, NULL, NULL },
4317 }
4318};
4319
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004320/* Delcate the action parser for "spoe-action" keyword */
4321static struct action_kw_list tcp_req_action_kws = { { }, {
4322 { "send-spoe-group", parse_send_spoe_group },
4323 { /* END */ },
4324 }
4325};
4326static struct action_kw_list tcp_res_action_kws = { { }, {
4327 { "send-spoe-group", parse_send_spoe_group },
4328 { /* END */ },
4329 }
4330};
4331static struct action_kw_list http_req_action_kws = { { }, {
4332 { "send-spoe-group", parse_send_spoe_group },
4333 { /* END */ },
4334 }
4335};
4336static struct action_kw_list http_res_action_kws = { { }, {
4337 { "send-spoe-group", parse_send_spoe_group },
4338 { /* END */ },
4339 }
4340};
4341
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004342__attribute__((constructor))
4343static void __spoe_init(void)
4344{
4345 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004346 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4347 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4348 http_req_keywords_register(&http_req_action_kws);
4349 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004350
Willy Tarreaubafbe012017-11-24 17:34:44 +01004351 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4352 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004353}
4354
4355__attribute__((destructor))
4356static void
4357__spoe_deinit(void)
4358{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004359 pool_destroy(pool_head_spoe_ctx);
4360 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004361}