blob: 4e27c63b2babc4a402422ad22659c613d85ca396 [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)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#else
51#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010052#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020053#endif
54
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010055/* Reserved 4 bytes to the frame size. So a frame and its size can be written
56 * together in a buffer */
57#define MAX_FRAME_SIZE global.tune.bufsize - 4
58
59/* The minimum size for a frame */
60#define MIN_FRAME_SIZE 256
61
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Reserved for the metadata and the frame type.
63 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010064#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020065
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010066/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010067#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
68
Christopher Faulet3b386a32017-02-23 10:17:15 +010069/* SPOE filter id. Used to identify SPOE filters */
70const char *spoe_filter_id = "SPOE filter";
71
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020072/* Set if the handle on SIGUSR1 is registered */
73static int sighandler_registered = 0;
74
75/* proxy used during the parsing */
76struct proxy *curproxy = NULL;
77
78/* The name of the SPOE engine, used during the parsing */
79char *curengine = NULL;
80
81/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020082/* SPOE agent/group/message used during the parsing */
83struct spoe_agent *curagent = NULL;
84struct spoe_group *curgrp = NULL;
85struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086
87/* list of SPOE messages and placeholders used during the parsing */
88struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020089struct list curgrps;
90struct list curmphs;
91struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010092struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Faulet7250b8f2018-03-26 17:19:01 +020094/* list of log servers used during the parsing */
95struct list curlogsrvs;
96
Christopher Faulet0e0f0852018-03-26 17:20:36 +020097/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
98int curpxopts;
99int curpxopts2;
100
Christopher Faulet42bfa462017-01-04 14:14:19 +0100101/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100102static struct pool_head *pool_head_spoe_ctx = NULL;
103static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105struct flt_ops spoe_ops;
106
Christopher Faulet8ef75252017-02-20 22:56:03 +0100107static int spoe_queue_context(struct spoe_context *ctx);
108static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
109static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110
111/********************************************************************
112 * helper functions/globals
113 ********************************************************************/
114static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200115spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 free(ph->id);
120 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121}
122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100124spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125{
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 struct spoe_arg *arg, *argback;
127 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128
129 if (!msg)
130 return;
131 free(msg->id);
132 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 release_sample_expr(arg->expr);
135 free(arg->name);
136 LIST_DEL(&arg->list);
137 free(arg);
138 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
140 LIST_DEL(&acl->list);
141 prune_acl(acl);
142 free(acl);
143 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100173 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100174 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100175 free(agent->var_t_process);
176 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200177 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
178 LIST_DEL(&msg->list);
179 spoe_release_message(msg);
180 }
181 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
182 LIST_DEL(&grp->list);
183 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200184 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200185 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100186 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
250 * allocated string. So it is the caller's reponsibility to release it. If the
251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
255 static int init = 0;
256
257 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
258 const char uuid_chr[] = "0123456789ABCDEF-";
259 char *uuid;
260 int i;
261
262 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
263 return NULL;
264
265 if (!init) {
266 srand(now_ms);
267 init = 1;
268 }
269
270 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
271 int r = rand () % 16;
272
273 switch (uuid_fmt[i]) {
274 case 'x' : uuid[i] = uuid_chr[r]; break;
275 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
276 default : uuid[i] = uuid_fmt[i]; break;
277 }
278 }
279 return uuid;
280}
281
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100282
283static inline void
284spoe_update_stat_time(struct timeval *tv, long *t)
285{
286 if (*t == -1)
287 *t = tv_ms_elapsed(tv, &now);
288 else
289 *t += tv_ms_elapsed(tv, &now);
290 tv_zero(tv);
291}
292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293/********************************************************************
294 * Functions that encode/decode SPOE frames
295 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296/* Helper to get static string length, excluding the terminating null byte */
297#define SLEN(str) (sizeof(str)-1)
298
299/* Predefined key used in HELLO/DISCONNECT frames */
300#define SUPPORTED_VERSIONS_KEY "supported-versions"
301#define VERSION_KEY "version"
302#define MAX_FRAME_SIZE_KEY "max-frame-size"
303#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100304#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100305#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306#define STATUS_CODE_KEY "status-code"
307#define MSG_KEY "message"
308
309struct spoe_version {
310 char *str;
311 int min;
312 int max;
313};
314
315/* All supported versions */
316static struct spoe_version supported_versions[] = {
317 {"1.0", 1000, 1000},
318 {NULL, 0, 0}
319};
320
321/* Comma-separated list of supported versions */
322#define SUPPORTED_VERSIONS_VAL "1.0"
323
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324/* Convert a string to a SPOE version value. The string must follow the format
325 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
326 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200327static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100328spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 const char *p, *end;
331 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p = str;
334 end = str+len;
335 maj = min = 0;
336 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 /* skip leading spaces */
339 while (p < end && isspace(*p))
340 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342 /* parse Major number, until the '.' */
343 while (*p != '.') {
344 if (p >= end || *p < '0' || *p > '9')
345 goto out;
346 maj *= 10;
347 maj += (*p - '0');
348 p++;
349 }
350
351 /* check Major version */
352 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200353 goto out;
354
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++; /* skip the '.' */
356 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
357 goto out;
358
359 /* Parse Minor number */
360 while (p < end) {
361 if (*p < '0' || *p > '9')
362 break;
363 min *= 10;
364 min += (*p - '0');
365 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367
368 /* check Minor number */
369 if (min > 999)
370 goto out;
371
372 /* skip trailing spaces */
373 while (p < end && isspace(*p))
374 p++;
375 if (p != end)
376 goto out;
377
378 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379 out:
380 return vsn;
381}
382
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
384 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
385 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388{
Christopher Faulet305c6072017-02-23 16:17:53 +0100389 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100390 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 char *p, *end;
392 unsigned int flags = SPOE_FRM_FL_FIN;
393 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200394
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 p = frame;
396 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 /* Set Frame type */
399 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100401 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200402 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 memcpy(p, (char *)&flags, 4);
404 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
406 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
410 * and "capabilities" */
411
412 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(SUPPORTED_VERSIONS_KEY);
414 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
415 goto too_big;
416
417 *p++ = SPOE_DATA_T_STR;
418 sz = SLEN(SUPPORTED_VERSIONS_VAL);
419 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
420 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200421
422 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100423 sz = SLEN(MAX_FRAME_SIZE_KEY);
424 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
425 goto too_big;
426
427 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200428 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
431 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 sz = SLEN(CAPABILITIES_KEY);
433 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
434 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100437 chk = get_trash_chunk();
438 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
439 memcpy(chk->str, "pipelining", 10);
440 chk->len += 10;
441 }
442 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
443 if (chk->len) chk->str[chk->len++] = ',';
444 memcpy(chk->str+chk->len, "async", 5);
445 chk->len += 5;
446 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100447 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
448 if (chk->len) chk->str[chk->len++] = ',';
449 memcpy(chk->str+chk->len, "fragmentation", 13);
450 chk->len += 5;
451 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100452 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453 goto too_big;
454
455 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100456 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 sz = SLEN(ENGINE_ID_KEY);
458 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
459 goto too_big;
460
461 *p++ = SPOE_DATA_T_STR;
462 sz = strlen(agent->engine_id);
463 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
464 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100465 }
466
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 return (p - frame);
468
469 too_big:
470 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
471 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200472}
473
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
475 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
476 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 const char *reason;
481 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100482 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 p = frame;
486 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200487
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 /* Set Frame type */
489 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100491 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200492 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 memcpy(p, (char *)&flags, 4);
494 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
496 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 *p++ = 0; *p++ = 0;
498
499 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
500 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501
502 /* There are 2 mandatory items: "status-code" and "message" */
503
504 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 sz = SLEN(STATUS_CODE_KEY);
506 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
507 goto too_big;
508
509 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200510 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512
513 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514 sz = SLEN(MSG_KEY);
515 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
516 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 /*Get the message corresponding to the status code */
519 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
520
521 *p++ = SPOE_DATA_T_STR;
522 sz = strlen(reason);
523 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
524 goto too_big;
525
526 return (p - frame);
527
528 too_big:
529 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
530 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200531}
532
Christopher Faulet8ef75252017-02-20 22:56:03 +0100533/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
534 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
535 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100537spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100538 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200539{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100540 char *p, *end;
541 unsigned int stream_id, frame_id;
542 unsigned int flags = SPOE_FRM_FL_FIN;
543 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200544
Christopher Faulet8ef75252017-02-20 22:56:03 +0100545 p = frame;
546 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200547
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 stream_id = ctx->stream_id;
549 frame_id = ctx->frame_id;
550
551 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
552 /* The fragmentation is not supported by the applet */
553 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
554 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
555 return -1;
556 }
557 flags = ctx->frag_ctx.flags;
558 }
559
560 /* Set Frame type */
561 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
562
563 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200564 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100565 memcpy(p, (char *)&flags, 4);
566 p += 4;
567
568 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200569 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100570 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200571 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100572 goto too_big;
573
574 /* Copy encoded messages, if possible */
575 sz = ctx->buffer->i;
576 if (p + sz >= end)
577 goto too_big;
578 memcpy(p, ctx->buffer->p, sz);
579 p += sz;
580
581 return (p - frame);
582
583 too_big:
584 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
585 return 0;
586}
587
588/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
589 * returns the number of encoded bytes in the frame on success, 0 if an encoding
590 * error occurred and -1 if a fatal error occurred. */
591static int
592spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
593 char *frame, size_t size)
594{
595 char *p, *end;
596 unsigned int stream_id, frame_id;
597 unsigned int flags;
598 size_t sz;
599
600 p = frame;
601 end = frame+size;
602
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 /* <ctx> is null when the stream has aborted the processing of a
604 * fragmented frame. In this case, we must notify the corresponding
605 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100606 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100607 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
609 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
610 }
611 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 stream_id = ctx->stream_id;
614 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100615 }
616
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100618 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100620 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200621 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 memcpy(p, (char *)&flags, 4);
623 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200624
625 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200626 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200628 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100631 if (ctx == NULL)
632 goto end;
633
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100635 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 if (p + sz >= end)
637 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100638 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100640
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100641 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 return (p - frame);
643
644 too_big:
645 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
646 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647}
648
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649/* Decode and process the HELLO frame sent by an agent. It returns the number of
650 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
651 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200652static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654{
Christopher Faulet305c6072017-02-23 16:17:53 +0100655 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
656 char *p, *end;
657 int vsn, max_frame_size;
658 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659
660 p = frame;
661 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662
663 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 if (size < 7 /* TYPE + METADATA */) {
670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
671 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672 }
673
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100674 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200676 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100679 /* Fragmentation is not supported for HELLO frame */
680 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100682 return -1;
683 }
684
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 if (*p != 0 || *(p+1) != 0) {
687 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
688 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691
692 /* There are 3 mandatory items: "version", "max-frame-size" and
693 * "capabilities" */
694
695 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100696 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 while (p < end) {
698 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200699 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701
702 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 ret = spoe_decode_buffer(&p, end, &str, &sz);
704 if (ret == -1 || !sz) {
705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
706 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 /* Check "version" K/V item */
710 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 int i, type = *p++;
712
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
716 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100718 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
719 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
720 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 }
722
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 return -1;
727 }
728 for (i = 0; supported_versions[i].str != NULL; ++i) {
729 if (vsn >= supported_versions[i].min &&
730 vsn <= supported_versions[i].max)
731 break;
732 }
733 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 }
738 /* Check "max-frame-size" K/V item */
739 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200741
742 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
744 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
745 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
746 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
748 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200749 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200750 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
752 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200753 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 if (sz < MIN_FRAME_SIZE ||
755 sz > SPOE_APPCTX(appctx)->max_frame_size) {
756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200757 return -1;
758 }
759 max_frame_size = sz;
760 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761 /* Check "capabilities" K/V item */
762 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764
765 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
767 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
768 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
771 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
772 return 0;
773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 char *delim;
777
778 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
782 str += 10; sz -= 10;
783 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 flags |= SPOE_APPCTX_FL_PIPELINING;
785 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 else if (sz >= 5 && !strncmp(str, "async", 5)) {
787 str += 5; sz -= 5;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 flags |= SPOE_APPCTX_FL_ASYNC;
790 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
792 str += 13; sz -= 13;
793 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100794 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
795 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100796
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 /* Get the next comma or break */
798 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100799 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 delim++;
801 sz -= (delim - str);
802 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100803 }
804 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 else {
806 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 if (spoe_skip_data(&p, end) == -1) {
808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
809 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200811 }
812 }
813
814 /* Final checks */
815 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200817 return -1;
818 }
819 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100820 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200821 return -1;
822 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100823 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
824 flags &= ~SPOE_APPCTX_FL_PIPELINING;
825 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
826 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827
Christopher Faulet42bfa462017-01-04 14:14:19 +0100828 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
829 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
830 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831
832 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833}
834
835/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
836 * bytes on success, 0 if the frame can be ignored and -1 if an error
837 * occurred. */
838static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100842 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843
844 p = frame;
845 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200846
847 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
849 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200850 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 if (size < 7 /* TYPE + METADATA */) {
854 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
855 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 }
857
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100858 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200860 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100863 /* Fragmentation is not supported for DISCONNECT frame */
864 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100866 return -1;
867 }
868
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 if (*p != 0 || *(p+1) != 0) {
871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
872 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875
876 /* There are 2 mandatory items: "status-code" and "message" */
877
878 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 while (p < end) {
880 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200881 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883
884 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 ret = spoe_decode_buffer(&p, end, &str, &sz);
886 if (ret == -1 || !sz) {
887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
890
891 /* Check "status-code" K/V item */
892 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100893 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200894
895 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
897 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
898 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
899 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200903 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
909
910 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911 else if (!memcmp(str, MSG_KEY, sz)) {
912 int type = *p++;
913
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100915 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
916 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
917 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100919 ret = spoe_decode_buffer(&p, end, &str, &sz);
920 if (ret == -1 || sz > 255) {
921 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
922 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
925 SPOE_APPCTX(appctx)->reason = str;
926 SPOE_APPCTX(appctx)->rlen = sz;
927#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 }
929 else {
930 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 if (spoe_skip_data(&p, end) == -1) {
932 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
933 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200934 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935 }
936 }
937
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939}
940
941
Christopher Fauleta1cda022016-12-21 08:58:06 +0100942/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 * success, 0 if the frame can be ignored and -1 if an error occurred. */
944static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100946 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100950 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100952 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953
954 p = frame;
955 end = frame + size;
956 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
958 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200961 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 if (size < 7 /* TYPE + METADATA */) {
965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
966 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200967 }
968
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100969 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200971 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100974 /* Fragmentation is not supported for now */
975 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100976 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100977 return -1;
978 }
979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200981 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100982 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100983 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200985 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200987 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100989
Christopher Faulet8ef75252017-02-20 22:56:03 +0100990 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100991 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200992 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100993 if ((*ctx)->stream_id == (unsigned int)stream_id &&
994 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 goto found;
996 }
997 }
998 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100999 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1000 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001001 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001002 goto found;
1003 }
1004 }
1005
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001006 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1007 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1008 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1009
1010 /* ABRT bit is set for an unfinished fragmented frame */
1011 if (flags & SPOE_FRM_FL_ABRT) {
1012 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001013 (*ctx)->state = SPOE_CTX_ST_ERROR;
1014 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1015 /* Ignore the payload */
1016 goto end;
1017 }
1018 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1019 /* For now, we ignore the ack */
1020 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1021 return 0;
1022 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001023
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001025 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1026 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001027 " - stream-id=%u - frame-id=%u\n",
1028 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1029 __FUNCTION__, appctx,
1030 (unsigned int)stream_id, (unsigned int)frame_id);
1031
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001032 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001033 return 0;
1034
1035 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1037 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001038 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001039 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001042 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 len = (end - p);
1044 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1045 SPOE_APPCTX(appctx)->buffer->i = len;
1046 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001047
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 /* Transfer the buffer ownership to the SPOE context */
1049 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1050 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001051
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 (*ctx)->state = SPOE_CTX_ST_DONE;
1053
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001054 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001055 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 " - ACK frame received"
1057 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1060 (*ctx)->frame_id, flags);
1061 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001062}
1063
Christopher Fauletba7bc162016-11-07 21:07:38 +01001064/* This function is used in cfgparse.c and declared in proto/checks.h. It
1065 * prepare the request to send to agents during a healthcheck. It returns 0 on
1066 * success and -1 if an error occurred. */
1067int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001068spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 struct appctx appctx;
1071 struct spoe_appctx spoe_appctx;
1072 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1073 size_t sz;
1074 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet42bfa462017-01-04 14:14:19 +01001076 memset(&appctx, 0, sizeof(appctx));
1077 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001079
1080 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001081 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1084 end = frame + MAX_FRAME_SIZE;
1085
1086 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1087 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001089 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 /* Add "healthcheck" K/V item */
1092 sz = SLEN(HEALTHCHECK_KEY);
1093 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1094 return -1;
1095 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 *len = frame - buf;
1098 sz = htonl(*len - 4);
1099 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104 return 0;
1105}
1106
1107/* This function is used in checks.c and declared in proto/checks.h. It decode
1108 * the response received from an agent during a healthcheck. It returns 0 on
1109 * success and -1 if an error occurred. */
1110int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001113 struct appctx appctx;
1114 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115
Christopher Faulet42bfa462017-01-04 14:14:19 +01001116 memset(&appctx, 0, sizeof(appctx));
1117 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118
Christopher Faulet42bfa462017-01-04 14:14:19 +01001119 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001120 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001121
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1123 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124 goto error;
1125 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1127 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128
1129 return 0;
1130
1131 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001132 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1133 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1134 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001135 return -1;
1136}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001137
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1139 * the frame can be ignored, 1 to retry later, and the frame legnth on
1140 * success. */
1141static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143{
1144 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001145 int ret;
1146 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1149 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 netint = htonl(framesz);
1151 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001152 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001154 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001155 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 }
1158 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 return -1; /* error */
1160 }
1161 return framesz;
1162}
1163
1164/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1165 * when the frame can be ignored, 1 to retry later and the frame length on
1166 * success. */
1167static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169{
1170 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 int ret;
1172 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 if (ret > 0) {
1176 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001177 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return -1;
1180 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001181 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 }
1183 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001184 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186 }
1187 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001188 return -1; /* error */
1189 }
1190 return framesz;
1191}
1192
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001193/********************************************************************
1194 * Functions that manage the SPOE applet
1195 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001196static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198{
1199 si_applet_want_get(appctx->owner);
1200 si_applet_want_put(appctx->owner);
1201 appctx_wakeup(appctx);
1202 return 1;
1203}
1204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205/* Callback function that catches applet timeouts. If a timeout occurred, we set
1206 * <appctx->st1> flag and the SPOE applet is woken up. */
1207static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209{
1210 struct appctx *appctx = task->context;
1211
1212 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1213 if (tick_is_expired(task->expire, now_ms)) {
1214 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218 return task;
1219}
1220
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221/* Callback function that releases a SPOE applet. This happens when the
1222 * connection with the agent is closed. */
1223static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 struct stream_interface *si = appctx->owner;
1227 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1228 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230
1231 if (spoe_appctx == NULL)
1232 return;
1233
1234 appctx->ctx.spoe.ptr = NULL;
1235 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236
1237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1238 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1239 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Remove applet from the list of running applets */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001242 SPOE_DEBUG_STMT(agent->rt[tid].applets_act--);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001243 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001244 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001245 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1246 LIST_DEL(&spoe_appctx->list);
1247 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001248 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001249 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001250
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001253 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1254 eb32_delete(&spoe_appctx->node);
1255 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001256 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001257 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001259 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1261 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001262
1263 si_shutw(si);
1264 si_shutr(si);
1265 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001266 }
1267
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 /* Destroy the task attached to this applet */
1269 if (spoe_appctx->task) {
1270 task_delete(spoe_appctx->task);
1271 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001272 }
1273
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 /* Notify all waiting streams */
1275 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001276 LIST_DEL(&ctx->list);
1277 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001278 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001279 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001280 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001282 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283 }
1284
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 /* If the applet was processing a fragmented frame, notify the
1286 * corresponding stream. */
1287 if (spoe_appctx->frag_ctx.ctx) {
1288 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001289 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001290 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001292 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1293 }
1294
Christopher Faulet8ef75252017-02-20 22:56:03 +01001295 /* Release allocated memory */
1296 spoe_release_buffer(&spoe_appctx->buffer,
1297 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001298 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001299
Christopher Faulet24289f22017-09-25 14:48:02 +02001300 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001301 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001302
Christopher Faulet8ef75252017-02-20 22:56:03 +01001303 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001304 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001305 LIST_DEL(&ctx->list);
1306 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001307 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001308 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001310 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001311 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001313 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 LIST_DEL(&ctx->list);
1315 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001316 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001317 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001318 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1321 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001322
1323 end:
1324 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001325 agent->rt[tid].frame_size = agent->max_frame_size;
1326 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1327 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001328}
1329
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001331spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001332{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001334 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001335 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 if (si->state <= SI_ST_CON) {
1339 si_applet_want_put(si);
1340 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1341 goto stop;
1342 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001343 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001346 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001347
Christopher Fauleta1cda022016-12-21 08:58:06 +01001348 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001349 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1350 " - Connection timed out\n",
1351 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1352 __FUNCTION__, appctx);
1353 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001355 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356
Christopher Faulet42bfa462017-01-04 14:14:19 +01001357 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 SPOE_APPCTX(appctx)->task->expire =
1359 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001360
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1362 * length. */
1363 buf = trash.str; frame = buf+4;
1364 ret = spoe_prepare_hahello_frame(appctx, frame,
1365 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001367 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368
1369 switch (ret) {
1370 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001371 case 0: /* ignore => an error, cannot be ignored */
1372 goto exit;
1373
1374 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001375 goto stop;
1376
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377 default:
1378 /* HELLO frame successfully sent, now wait for the
1379 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1381 goto next;
1382 }
1383
1384 next:
1385 return 0;
1386 stop:
1387 return 1;
1388 exit:
1389 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1390 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001391}
1392
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001394spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001395{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001396 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001397 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001398 char *frame;
1399 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001400
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401
Christopher Fauletb067b062017-01-04 16:39:11 +01001402 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001405 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1409 " - Connection timed out\n",
1410 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1411 __FUNCTION__, appctx);
1412 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001413 goto exit;
1414 }
1415
Christopher Faulet8ef75252017-02-20 22:56:03 +01001416 frame = trash.str; trash.len = 0;
1417 ret = spoe_recv_frame(appctx, frame,
1418 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 if (ret > 1) {
1420 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1421 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1422 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001423 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001424 trash.len = ret + 4;
1425 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001426 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001427
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 switch (ret) {
1429 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001430 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1432 goto next;
1433
1434 case 1: /* retry later */
1435 goto stop;
1436
1437 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 /* HELLO handshake is finished, set the idle timeout and
1439 * add the applet in the list of running applets. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001440 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001441 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001442 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001443 SPOE_APPCTX(appctx)->node.key = 0;
1444 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001445
1446 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001447 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001448 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001450
Christopher Fauleta1cda022016-12-21 08:58:06 +01001451 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452 /* Do not forget to remove processed frame from the output buffer */
1453 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001454 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455
1456 SPOE_APPCTX(appctx)->task->expire =
1457 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001458 return 0;
1459 stop:
1460 return 1;
1461 exit:
1462 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1463 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001464}
1465
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001466
Christopher Fauleta1cda022016-12-21 08:58:06 +01001467static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001468spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001469{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001470 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1471 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001472 char *frame, *buf;
1473 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001474
Christopher Faulet8ef75252017-02-20 22:56:03 +01001475 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1476 * length. */
1477 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001478
1479 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1480 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1481 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1482 SPOE_APPCTX(appctx)->max_frame_size);
1483 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001484 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485 *skip = 1;
1486 ret = 1;
1487 goto end;
1488 }
1489 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001490 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001491 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1492 SPOE_APPCTX(appctx)->max_frame_size);
1493
1494 }
1495
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 if (ret > 1)
1497 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001498
Christopher Faulet8ef75252017-02-20 22:56:03 +01001499 switch (ret) {
1500 case -1: /* error */
1501 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1502 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001503
Christopher Faulet8ef75252017-02-20 22:56:03 +01001504 case 0: /* ignore */
1505 if (ctx == NULL)
1506 goto abort_frag_frame;
1507
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001508 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509 LIST_DEL(&ctx->list);
1510 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001511 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001512 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001513 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 ctx->state = SPOE_CTX_ST_ERROR;
1515 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1516 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001517 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001518 break;
1519
1520 case 1: /* retry */
1521 *skip = 1;
1522 break;
1523
1524 default:
1525 if (ctx == NULL)
1526 goto abort_frag_frame;
1527
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001528 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001529 LIST_DEL(&ctx->list);
1530 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001531 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001532 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001533 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1535 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1536 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001537 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001538 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 }
1540 goto end;
1541
1542 frag_frame_sent:
1543 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001544 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1546 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1547 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001548 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1549 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1550 goto end;
1551
1552 no_frag_frame_sent:
1553 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1554 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001555 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001556 }
1557 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1558 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1559 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1560 }
1561 else {
1562 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001563 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1565 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001566 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001567 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1569 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1570 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001571 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572
Christopher Faulet8ef75252017-02-20 22:56:03 +01001573 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1574 goto end;
1575
1576 abort_frag_frame:
1577 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1578 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1579 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1580 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1581 goto end;
1582
1583 end:
1584 return ret;
1585}
1586
1587static int
1588spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1589{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001590 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 struct spoe_context *ctx = NULL;
1592 char *frame;
1593 int ret;
1594
1595 frame = trash.str; trash.len = 0;
1596 ret = spoe_recv_frame(appctx, frame,
1597 SPOE_APPCTX(appctx)->max_frame_size);
1598 if (ret > 1) {
1599 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1600 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001601 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 goto end;
1603 }
1604 trash.len = ret + 4;
1605 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1606 }
1607 switch (ret) {
1608 case -1: /* error */
1609 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1610 break;
1611
1612 case 0: /* ignore */
1613 break;
1614
1615 case 1: /* retry */
1616 *skip = 1;
1617 break;
1618
1619 default:
1620 LIST_DEL(&ctx->list);
1621 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001622 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001623 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1624 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001625 if (ctx->spoe_appctx) {
1626 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001627 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001628 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001629 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1630 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1631 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1632 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1633 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1634 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1635 }
1636 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1637 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001638 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1639 break;
1640 }
1641
1642 /* Do not forget to remove processed frame from the output buffer */
1643 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001644 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001645 end:
1646 return ret;
1647}
1648
1649static int
1650spoe_handle_processing_appctx(struct appctx *appctx)
1651{
1652 struct stream_interface *si = appctx->owner;
1653 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001654 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001655
1656 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1658 goto exit;
1659 }
1660
1661 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1663 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1664 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1665 goto next;
1666 }
1667
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001668 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001669 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001670 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1672 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001673 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1676 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001677
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 /* receiving_frame loop */
1679 while (!skip_receiving) {
1680 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1681 switch (ret) {
1682 case -1: /* error */
1683 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001684
Christopher Faulet8f82b202018-01-24 16:23:03 +01001685 case 0: /* ignore */
1686 active_r = 1;
1687 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 case 1: /* retry */
1690 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001691
Christopher Faulet8f82b202018-01-24 16:23:03 +01001692 default:
1693 active_r = 1;
1694 break;
1695 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697
Christopher Faulet8f82b202018-01-24 16:23:03 +01001698 /* send_frame loop */
1699 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1700 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1701 switch (ret) {
1702 case -1: /* error */
1703 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001706 if (SPOE_APPCTX(appctx)->node.key)
1707 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 active_s++;
1709 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 case 1: /* retry */
1712 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713
Christopher Faulet8f82b202018-01-24 16:23:03 +01001714 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001715 if (SPOE_APPCTX(appctx)->node.key)
1716 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 active_s++;
1718 break;
1719 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1724 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1725 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001726
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001728 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001729 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001730 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001731 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732 }
1733 return 1;
1734
Christopher Faulet8f82b202018-01-24 16:23:03 +01001735 next:
1736 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1737 return 0;
1738
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 exit:
1740 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1741 return 0;
1742}
1743
1744static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746{
1747 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001748 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 char *frame, *buf;
1750 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001751
Christopher Fauleta1cda022016-12-21 08:58:06 +01001752 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1753 goto exit;
1754
1755 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1756 goto exit;
1757
Christopher Faulet8ef75252017-02-20 22:56:03 +01001758 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1759 * length. */
1760 buf = trash.str; frame = buf+4;
1761 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1762 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765
1766 switch (ret) {
1767 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769 goto exit;
1770
1771 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772 goto stop;
1773
1774 default:
1775 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1776 " - disconnected by HAProxy (%d): %s\n",
1777 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001778 __FUNCTION__, appctx,
1779 SPOE_APPCTX(appctx)->status_code,
1780 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781
1782 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1783 goto next;
1784 }
1785
1786 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001787 SPOE_APPCTX(appctx)->task->expire =
1788 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 return 0;
1790 stop:
1791 return 1;
1792 exit:
1793 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1794 return 0;
1795}
1796
1797static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799{
1800 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 char *frame;
1802 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803
Christopher Fauletb067b062017-01-04 16:39:11 +01001804 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001807 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001808
Christopher Fauletb067b062017-01-04 16:39:11 +01001809 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001812 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 frame = trash.str; trash.len = 0;
1815 ret = spoe_recv_frame(appctx, frame,
1816 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001818 trash.len = ret + 4;
1819 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 }
1821
1822 switch (ret) {
1823 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1825 " - error on frame (%s)\n",
1826 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001827 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 goto exit;
1831
1832 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 goto next;
1834
1835 case 1: /* retry */
1836 goto stop;
1837
1838 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001839 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001842 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001843 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1844 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845 goto exit;
1846 }
1847
1848 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001849 /* Do not forget to remove processed frame from the output buffer */
1850 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001851 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 return 0;
1854 stop:
1855 return 1;
1856 exit:
1857 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1858 return 0;
1859}
1860
1861/* I/O Handler processing messages exchanged with the agent */
1862static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001865 struct stream_interface *si = appctx->owner;
1866 struct spoe_agent *agent;
1867
1868 if (SPOE_APPCTX(appctx) == NULL)
1869 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1872 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001873
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 switchstate:
1875 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1876 " - appctx-state=%s\n",
1877 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1878 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1879
1880 switch (appctx->st0) {
1881 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 goto out;
1884 goto switchstate;
1885
1886 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001887 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 goto out;
1889 goto switchstate;
1890
1891 case SPOE_APPCTX_ST_IDLE:
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001892 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001893 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001894 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001896 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001897 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001898 SPOE_APPCTX(appctx)->task->expire =
1899 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001901 goto switchstate;
1902 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001903 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1904 /* fall through */
1905
1906 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001907 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1908 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001909 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 goto out;
1911 goto switchstate;
1912
1913 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 goto out;
1916 goto switchstate;
1917
1918 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920 goto out;
1921 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922
1923 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001924 appctx->st0 = SPOE_APPCTX_ST_END;
1925 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1926
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001927 si_shutw(si);
1928 si_shutr(si);
1929 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930 /* fall through */
1931
1932 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001933 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001935 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001936 if (stopping)
1937 spoe_wakeup_appctx(appctx);
1938
Christopher Faulet42bfa462017-01-04 14:14:19 +01001939 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1940 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 si_oc(si)->flags |= CF_READ_DONTWAIT;
1942 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943}
1944
1945struct applet spoe_applet = {
1946 .obj_type = OBJ_TYPE_APPLET,
1947 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001948 .fct = spoe_handle_appctx,
1949 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001950};
1951
1952/* Create a SPOE applet. On success, the created applet is returned, else
1953 * NULL. */
1954static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001955spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001956{
1957 struct appctx *appctx;
1958 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960
Emeric Brun1138fd02017-06-19 12:38:55 +02001961 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001962 goto out_error;
1963
Willy Tarreaubafbe012017-11-24 17:34:44 +01001964 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001965 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001966 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001967 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001968
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001970 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001971 goto out_free_spoe_appctx;
1972
1973 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001974 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001975 SPOE_APPCTX(appctx)->task->context = appctx;
1976 SPOE_APPCTX(appctx)->agent = conf->agent;
1977 SPOE_APPCTX(appctx)->version = 0;
1978 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1979 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001980 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001981 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001982 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001983
1984 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1985 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001986 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001987
1988 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1989 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990
Willy Tarreau5820a362016-12-22 15:59:02 +01001991 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992 if (!sess)
1993 goto out_free_spoe;
1994
Willy Tarreau87787ac2017-08-28 16:22:54 +02001995 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 goto out_free_sess;
1997
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998 stream_set_backend(strm, conf->agent->b.be);
1999
2000 /* applet is waiting for data */
2001 si_applet_cant_get(&strm->si[0]);
2002 appctx_wakeup(appctx);
2003
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004 strm->do_log = NULL;
2005 strm->res.flags |= CF_READ_DONTWAIT;
2006
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002007 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002008 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002009 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002010 SPOE_DEBUG_STMT(conf->agent->rt[tid].applets_act++);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002011 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002012
Emeric Brunc60def82017-09-27 14:59:38 +02002013 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002014 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015 return appctx;
2016
2017 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 out_free_sess:
2019 session_free(sess);
2020 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002021 task_free(SPOE_APPCTX(appctx)->task);
2022 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002023 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 out_free_appctx:
2025 appctx_free(appctx);
2026 out_error:
2027 return NULL;
2028}
2029
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002031spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002032{
2033 struct spoe_config *conf = FLT_CONF(ctx->filter);
2034 struct spoe_agent *agent = conf->agent;
2035 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002036 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002037
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002039 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002040 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002042
2043 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002044 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2046 ctx->strm);
2047
Christopher Fauleta1cda022016-12-21 08:58:06 +01002048 /* Do not try to create a new applet if there is no server up for the
2049 * agent's backend. */
2050 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2052 " - cannot create SPOE appctx: no server up\n",
2053 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2054 __FUNCTION__, ctx->strm);
2055 goto end;
2056 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002057
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 /* Do not try to create a new applet if we have reached the maximum of
2059 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002060 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002061 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2063 " - cannot create SPOE appctx: max CPS reached\n",
2064 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2065 __FUNCTION__, ctx->strm);
2066 goto end;
2067 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068 }
2069
Christopher Faulet8ef75252017-02-20 22:56:03 +01002070 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002071 if (appctx == NULL) {
2072 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2073 " - failed to create SPOE appctx\n",
2074 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2075 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002076 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002077 "SPOE: [%s] failed to create SPOE applet\n",
2078 agent->id);
2079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002081 }
2082
Christopher Fauleta1cda022016-12-21 08:58:06 +01002083 /* Increase the per-process number of cumulated connections */
2084 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002085 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086
Christopher Fauleta1cda022016-12-21 08:58:06 +01002087 end:
2088 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002089 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002090 ctx->status_code = SPOE_CTX_ERR_RES;
2091 return -1;
2092 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002094 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002095 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002096 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002097 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2098 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099
2100 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002101 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002102 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002103 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002104 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002105 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002106
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002107 /* Finally try to wakeup an IDLE applet. */
2108 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2109 struct eb32_node *node;
2110
2111 node = eb32_first(&agent->rt[tid].idle_applets);
2112 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2113 if (node && spoe_appctx) {
2114 eb32_delete(&spoe_appctx->node);
2115 spoe_appctx->node.key++;
2116 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2117 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002118 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002119 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002120 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002121}
2122
2123/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002124 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002125 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002126/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2127 * fragmented_content. If the next message can be processed, it returns 0. If
2128 * the message is too big, it returns -1.*/
2129static int
2130spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2131 struct spoe_message *msg, int dir,
2132 char **buf, char *end)
2133{
2134 struct sample *smp;
2135 struct spoe_arg *arg;
2136 int ret;
2137
2138 if (msg->cond) {
2139 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2140 ret = acl_pass(ret);
2141 if (msg->cond->pol == ACL_COND_UNLESS)
2142 ret = !ret;
2143
2144 /* the rule does not match */
2145 if (!ret)
2146 goto next;
2147 }
2148
2149 /* Resume encoding of a SPOE argument */
2150 if (ctx->frag_ctx.curarg != NULL) {
2151 arg = ctx->frag_ctx.curarg;
2152 goto encode_argument;
2153 }
2154
2155 if (ctx->frag_ctx.curoff != UINT_MAX)
2156 goto encode_msg_payload;
2157
2158 /* Check if there is enough space for the message name and the
2159 * number of arguments. It implies <msg->id_len> is encoded on 2
2160 * bytes, at most (< 2288). */
2161 if (*buf + 2 + msg->id_len + 1 > end)
2162 goto too_big;
2163
2164 /* Encode the message name */
2165 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2166 goto too_big;
2167
2168 /* Set the number of arguments for this message */
2169 **buf = msg->nargs;
2170 (*buf)++;
2171
2172 ctx->frag_ctx.curoff = 0;
2173 encode_msg_payload:
2174
2175 /* Loop on arguments */
2176 list_for_each_entry(arg, &msg->args, list) {
2177 ctx->frag_ctx.curarg = arg;
2178 ctx->frag_ctx.curoff = UINT_MAX;
2179
2180 encode_argument:
2181 if (ctx->frag_ctx.curoff != UINT_MAX)
2182 goto encode_arg_value;
2183
2184 /* Encode the arguement name as a string. It can by NULL */
2185 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2186 goto too_big;
2187
2188 ctx->frag_ctx.curoff = 0;
2189 encode_arg_value:
2190
2191 /* Fetch the arguement value */
2192 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2193 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2194 if (ret == -1 || ctx->frag_ctx.curoff)
2195 goto too_big;
2196 }
2197
2198 next:
2199 return 0;
2200
2201 too_big:
2202 return -1;
2203}
2204
Christopher Fauletc718b822017-09-21 16:50:56 +02002205/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2206 * handle fragmented content. On success it returns 1. If an error occurred, -1
2207 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2208 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002209static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002210spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002211 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002212{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002213 struct spoe_config *conf = FLT_CONF(ctx->filter);
2214 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002215 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002216 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002217
Christopher Faulet8ef75252017-02-20 22:56:03 +01002218 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002219 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002220
Christopher Fauletc718b822017-09-21 16:50:56 +02002221 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2222 /* Resume encoding of a SPOE message */
2223 if (ctx->frag_ctx.curmsg != NULL) {
2224 msg = ctx->frag_ctx.curmsg;
2225 goto encode_evt_message;
2226 }
2227
2228 list_for_each_entry(msg, messages, by_evt) {
2229 ctx->frag_ctx.curmsg = msg;
2230 ctx->frag_ctx.curarg = NULL;
2231 ctx->frag_ctx.curoff = UINT_MAX;
2232
2233 encode_evt_message:
2234 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2235 goto too_big;
2236 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002237 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002238 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2239 /* Resume encoding of a SPOE message */
2240 if (ctx->frag_ctx.curmsg != NULL) {
2241 msg = ctx->frag_ctx.curmsg;
2242 goto encode_grp_message;
2243 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244
Christopher Fauletc718b822017-09-21 16:50:56 +02002245 list_for_each_entry(msg, messages, by_grp) {
2246 ctx->frag_ctx.curmsg = msg;
2247 ctx->frag_ctx.curarg = NULL;
2248 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249
Christopher Fauletc718b822017-09-21 16:50:56 +02002250 encode_grp_message:
2251 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2252 goto too_big;
2253 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002254 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002255 else
2256 goto skip;
2257
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002258
Christopher Faulet57583e42017-09-04 15:41:09 +02002259 /* nothing has been encoded for an unfragmented payload */
2260 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2261 goto skip;
2262
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002264 " - encode %s messages - spoe_appctx=%p"
2265 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266 (int)now.tv_sec, (int)now.tv_usec,
2267 agent->id, __FUNCTION__, s,
2268 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002269 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002270 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002271
Christopher Faulet8ef75252017-02-20 22:56:03 +01002272 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002273 ctx->frag_ctx.curmsg = NULL;
2274 ctx->frag_ctx.curarg = NULL;
2275 ctx->frag_ctx.curoff = 0;
2276 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002277
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002278 return 1;
2279
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002281 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2282 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2283 return -1;
2284 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002285
2286 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002287 " - encode fragmented messages - spoe_appctx=%p"
2288 " - curmsg=%p - curarg=%p - curoff=%u"
2289 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002290 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002291 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002293 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294
Christopher Faulet8ef75252017-02-20 22:56:03 +01002295 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002296 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2297 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2298 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002299
2300 skip:
2301 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2302 " - skip the frame because nothing has been encoded\n",
2303 (int)now.tv_sec, (int)now.tv_usec,
2304 agent->id, __FUNCTION__, s);
2305 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002306}
2307
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002308
2309/***************************************************************************
2310 * Functions that handle SPOE actions
2311 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002312/* Helper function to set a variable */
2313static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002314spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002315 struct sample *smp)
2316{
2317 struct spoe_config *conf = FLT_CONF(ctx->filter);
2318 struct spoe_agent *agent = conf->agent;
2319 char varname[64];
2320
2321 memset(varname, 0, sizeof(varname));
2322 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2323 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002324 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2325 vars_set_by_name(varname, len, smp);
2326 else
2327 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002328}
2329
2330/* Helper function to unset a variable */
2331static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002332spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002333 struct sample *smp)
2334{
2335 struct spoe_config *conf = FLT_CONF(ctx->filter);
2336 struct spoe_agent *agent = conf->agent;
2337 char varname[64];
2338
2339 memset(varname, 0, sizeof(varname));
2340 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2341 scope, agent->var_pfx, len, name);
2342 vars_unset_by_name_ifexist(varname, len, smp);
2343}
2344
2345
Christopher Faulet8ef75252017-02-20 22:56:03 +01002346static inline int
2347spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2348 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002349{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002350 char *str, *scope, *p = *buf;
2351 struct sample smp;
2352 uint64_t sz;
2353 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002354
Christopher Faulet8ef75252017-02-20 22:56:03 +01002355 if (p + 2 >= end)
2356 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 /* SET-VAR requires 3 arguments */
2359 if (*p++ != 3)
2360 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002361
Christopher Faulet8ef75252017-02-20 22:56:03 +01002362 switch (*p++) {
2363 case SPOE_SCOPE_PROC: scope = "proc"; break;
2364 case SPOE_SCOPE_SESS: scope = "sess"; break;
2365 case SPOE_SCOPE_TXN : scope = "txn"; break;
2366 case SPOE_SCOPE_REQ : scope = "req"; break;
2367 case SPOE_SCOPE_RES : scope = "res"; break;
2368 default: goto skip;
2369 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002370
Christopher Faulet8ef75252017-02-20 22:56:03 +01002371 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2372 goto skip;
2373 memset(&smp, 0, sizeof(smp));
2374 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 if (spoe_decode_data(&p, end, &smp) == -1)
2377 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378
Christopher Faulet8ef75252017-02-20 22:56:03 +01002379 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2380 " - set-var '%s.%s.%.*s'\n",
2381 (int)now.tv_sec, (int)now.tv_usec,
2382 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2383 __FUNCTION__, s, scope,
2384 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2385 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002386
Christopher Faulet8ef75252017-02-20 22:56:03 +01002387 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002388
Christopher Faulet8ef75252017-02-20 22:56:03 +01002389 ret = (p - *buf);
2390 *buf = p;
2391 return ret;
2392 skip:
2393 return 0;
2394}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002395
Christopher Faulet8ef75252017-02-20 22:56:03 +01002396static inline int
2397spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2398 char **buf, char *end, int dir)
2399{
2400 char *str, *scope, *p = *buf;
2401 struct sample smp;
2402 uint64_t sz;
2403 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002404
Christopher Faulet8ef75252017-02-20 22:56:03 +01002405 if (p + 2 >= end)
2406 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002407
Christopher Faulet8ef75252017-02-20 22:56:03 +01002408 /* UNSET-VAR requires 2 arguments */
2409 if (*p++ != 2)
2410 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412 switch (*p++) {
2413 case SPOE_SCOPE_PROC: scope = "proc"; break;
2414 case SPOE_SCOPE_SESS: scope = "sess"; break;
2415 case SPOE_SCOPE_TXN : scope = "txn"; break;
2416 case SPOE_SCOPE_REQ : scope = "req"; break;
2417 case SPOE_SCOPE_RES : scope = "res"; break;
2418 default: goto skip;
2419 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002420
Christopher Faulet8ef75252017-02-20 22:56:03 +01002421 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2422 goto skip;
2423 memset(&smp, 0, sizeof(smp));
2424 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002425
Christopher Faulet8ef75252017-02-20 22:56:03 +01002426 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2427 " - unset-var '%s.%s.%.*s'\n",
2428 (int)now.tv_sec, (int)now.tv_usec,
2429 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2430 __FUNCTION__, s, scope,
2431 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2432 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002433
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434 spoe_unset_var(ctx, scope, str, sz, &smp);
2435
2436 ret = (p - *buf);
2437 *buf = p;
2438 return ret;
2439 skip:
2440 return 0;
2441}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443/* Process SPOE actions for a specific event. It returns 1 on success. If an
2444 * error occurred, 0 is returned. */
2445static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002446spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002447{
2448 char *p, *end;
2449 int ret;
2450
2451 p = ctx->buffer->p;
2452 end = p + ctx->buffer->i;
2453
2454 while (p < end) {
2455 enum spoe_action_type type;
2456
2457 type = *p++;
2458 switch (type) {
2459 case SPOE_ACT_T_SET_VAR:
2460 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2461 if (!ret)
2462 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002463 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002464
Christopher Faulet8ef75252017-02-20 22:56:03 +01002465 case SPOE_ACT_T_UNSET_VAR:
2466 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2467 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002468 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002469 break;
2470
2471 default:
2472 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002473 }
2474 }
2475
2476 return 1;
2477 skip:
2478 return 0;
2479}
2480
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002481/***************************************************************************
2482 * Functions that process SPOE events
2483 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002484static void
2485spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2486 struct spoe_context *ctx, int dir)
2487{
2488 if (!tv_iszero(&ctx->stats.tv_start)) {
2489 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2490 ctx->stats.t_total += ctx->stats.t_process;
2491 tv_zero(&ctx->stats.tv_request);
2492 tv_zero(&ctx->stats.tv_queue);
2493 tv_zero(&ctx->stats.tv_wait);
2494 tv_zero(&ctx->stats.tv_response);
2495 }
2496
2497 if (agent->var_t_process) {
2498 struct sample smp;
2499
2500 memset(&smp, 0, sizeof(smp));
2501 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2502 smp.data.u.sint = ctx->stats.t_process;
2503 smp.data.type = SMP_T_SINT;
2504
2505 spoe_set_var(ctx, "txn", agent->var_t_process,
2506 strlen(agent->var_t_process), &smp);
2507 }
2508
2509 if (agent->var_t_total) {
2510 struct sample smp;
2511
2512 memset(&smp, 0, sizeof(smp));
2513 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2514 smp.data.u.sint = ctx->stats.t_total;
2515 smp.data.type = SMP_T_SINT;
2516
2517 spoe_set_var(ctx, "txn", agent->var_t_total,
2518 strlen(agent->var_t_total), &smp);
2519 }
2520}
2521
2522static void
2523spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2524 struct spoe_context *ctx, int dir)
2525{
2526 if (agent->eps_max > 0)
2527 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2528
2529 if (agent->var_on_error) {
2530 struct sample smp;
2531
2532 memset(&smp, 0, sizeof(smp));
2533 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2534 smp.data.u.sint = ctx->status_code;
2535 smp.data.type = SMP_T_BOOL;
2536
2537 spoe_set_var(ctx, "txn", agent->var_on_error,
2538 strlen(agent->var_on_error), &smp);
2539 }
2540
2541 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2542 ? SPOE_CTX_ST_READY
2543 : SPOE_CTX_ST_NONE);
2544}
2545
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002546static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002547spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002549 /* If a process is already started for this SPOE context, retry
2550 * later. */
2551 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002552 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002553
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002554 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002555 ctx->stats.tv_start = now;
2556 ctx->stats.tv_request = now;
2557 ctx->stats.t_request = -1;
2558 ctx->stats.t_queue = -1;
2559 ctx->stats.t_waiting = -1;
2560 ctx->stats.t_response = -1;
2561 ctx->stats.t_process = -1;
2562
2563 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002564
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565 /* Set the right flag to prevent request and response processing
2566 * in same time. */
2567 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2568 ? SPOE_CTX_FL_REQ_PROCESS
2569 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002571}
2572
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002573static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002574spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002575{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002576 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002577
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002578 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2579 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002580 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002581 if (sa) {
2582 if (sa->frag_ctx.ctx == ctx) {
2583 sa->frag_ctx.ctx = NULL;
2584 spoe_wakeup_appctx(sa->owner);
2585 }
2586 else
2587 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002588 }
2589
Christopher Fauleta1cda022016-12-21 08:58:06 +01002590 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002591 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002592 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593
2594 /* Reset processing timer */
2595 ctx->process_exp = TICK_ETERNITY;
2596
Christopher Faulet8ef75252017-02-20 22:56:03 +01002597 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598
Christopher Fauletfce747b2018-01-24 15:59:32 +01002599 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002600 ctx->frag_ctx.curmsg = NULL;
2601 ctx->frag_ctx.curarg = NULL;
2602 ctx->frag_ctx.curoff = 0;
2603 ctx->frag_ctx.flags = 0;
2604
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002606 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002607 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002608 else
2609 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2610
Christopher Fauleta1cda022016-12-21 08:58:06 +01002611 LIST_DEL(&ctx->list);
2612 LIST_INIT(&ctx->list);
2613 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002614}
2615
Christopher Faulet58d03682017-09-21 16:57:24 +02002616/* Process a list of SPOE messages. First, this functions will process messages
2617 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2618 * to process corresponding actions. During all the processing, it returns 0
2619 * and it returns 1 when the processing is finished. If an error occurred, -1
2620 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002621static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002622spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2623 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002624{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002625 struct spoe_config *conf = FLT_CONF(ctx->filter);
2626 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002627 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002628
2629 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002630 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002631
2632 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2633 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002634 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002635 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002636 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002637 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002638 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002639 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002640
2641 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002643 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002644 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002645 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002647 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002648 goto skip;
2649 }
2650 }
2651
Christopher Fauletf7a30922016-11-10 15:04:51 +01002652 if (!tick_isset(ctx->process_exp)) {
2653 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2654 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2655 ctx->process_exp);
2656 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002657 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002658 if (!ret)
2659 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002660
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002661 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002662 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002663 }
2664
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002665 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002666 if (!tv_iszero(&ctx->stats.tv_request))
2667 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002668 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002669 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002670 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002671 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002672 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002673 if (!ret)
2674 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002675 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002676 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002677 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2678 }
2679
2680 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002681 if (ctx->spoe_appctx)
2682 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002683 ret = 0;
2684 goto out;
2685 }
2686
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002687 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2688 ret = 0;
2689 goto out;
2690 }
2691
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002692 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002693 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002694 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002695 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002697 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002699 }
2700
2701 out:
2702 return ret;
2703
Christopher Fauleta1cda022016-12-21 08:58:06 +01002704 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002705 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002706 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002707 spoe_stop_processing(agent, ctx);
2708 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002709
Christopher Fauleta1cda022016-12-21 08:58:06 +01002710 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002711 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002712 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002713 if (ctx->status_code) {
2714 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2715 spoe_handle_processing_error(s, agent, ctx, dir);
2716 ret = 1;
2717 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002718 return ret;
2719}
2720
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002721/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2722 * See spoe_process_message for details. */
2723static int
2724spoe_process_group(struct stream *s, struct spoe_context *ctx,
2725 struct spoe_group *group, int dir)
2726{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002727 struct spoe_config *conf = FLT_CONF(ctx->filter);
2728 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002729 int ret;
2730
2731 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2732 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002733 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002734 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2735 group->id);
2736
2737 if (LIST_ISEMPTY(&group->messages))
2738 return 1;
2739
2740 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002741 if (ret && ctx->stats.t_process != -1) {
2742 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002743 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002745 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002746 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002747 ctx->stats.t_response, ctx->stats.t_process,
2748 agent->counters.idles, agent->counters.applets,
2749 agent->counters.nb_sending, agent->counters.nb_waiting,
2750 agent->counters.nb_errors, agent->counters.nb_processed,
2751 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2752 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2753 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2754 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2755 agent->id, group->id, s->uniq_id, ctx->status_code,
2756 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2757 ctx->stats.t_response, ctx->stats.t_process,
2758 agent->counters.idles, agent->counters.applets,
2759 agent->counters.nb_sending, agent->counters.nb_waiting,
2760 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002761 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002762 return ret;
2763}
2764
Christopher Faulet58d03682017-09-21 16:57:24 +02002765/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2766 * See spoe_process_message for details. */
2767static int
2768spoe_process_event(struct stream *s, struct spoe_context *ctx,
2769 enum spoe_event ev)
2770{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002771 struct spoe_config *conf = FLT_CONF(ctx->filter);
2772 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002773 int dir, ret;
2774
2775 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002776 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002777 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002778 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2779 spoe_event_str[ev]);
2780
2781 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2782
2783 if (LIST_ISEMPTY(&(ctx->events[ev])))
2784 return 1;
2785
2786 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002787 if (ret && ctx->stats.t_process != -1) {
2788 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002789 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002790 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2791 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2792 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002793 ctx->stats.t_response, ctx->stats.t_process,
2794 agent->counters.idles, agent->counters.applets,
2795 agent->counters.nb_sending, agent->counters.nb_waiting,
2796 agent->counters.nb_errors, agent->counters.nb_processed,
2797 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2798 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2799 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2800 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2801 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2802 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2803 ctx->stats.t_response, ctx->stats.t_process,
2804 agent->counters.idles, agent->counters.applets,
2805 agent->counters.nb_sending, agent->counters.nb_waiting,
2806 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002807 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002808 return ret;
2809}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002810
2811/***************************************************************************
2812 * Functions that create/destroy SPOE contexts
2813 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002814static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002815spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002816{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002817 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002818 return 1;
2819
Christopher Faulet4596fb72017-01-11 14:05:19 +01002820 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002821 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002822 LIST_DEL(&buffer_wait->list);
2823 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002824 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002825 }
2826
Christopher Faulet4596fb72017-01-11 14:05:19 +01002827 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002828 return 1;
2829
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002830 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002831 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002832 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002833 return 0;
2834}
2835
2836static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002837spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002838{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002839 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002840 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002841 LIST_DEL(&buffer_wait->list);
2842 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002843 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844 }
2845
2846 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002847 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002848 b_free(buf);
2849 offer_buffers(buffer_wait->target,
2850 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002851 }
2852}
2853
Christopher Faulet4596fb72017-01-11 14:05:19 +01002854static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002855spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002856{
2857 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2858 return 1;
2859}
2860
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002862spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002863{
2864 struct spoe_config *conf = FLT_CONF(filter);
2865 struct spoe_context *ctx;
2866
Willy Tarreaubafbe012017-11-24 17:34:44 +01002867 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002868 if (ctx == NULL) {
2869 return NULL;
2870 }
2871 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002872 ctx->filter = filter;
2873 ctx->state = SPOE_CTX_ST_NONE;
2874 ctx->status_code = SPOE_CTX_ERR_NONE;
2875 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002876 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002877 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002878 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002879 LIST_INIT(&ctx->buffer_wait.list);
2880 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002881 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002882 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002883
Christopher Fauletf7a30922016-11-10 15:04:51 +01002884 ctx->stream_id = 0;
2885 ctx->frame_id = 1;
2886 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002888 tv_zero(&ctx->stats.tv_start);
2889 tv_zero(&ctx->stats.tv_request);
2890 tv_zero(&ctx->stats.tv_queue);
2891 tv_zero(&ctx->stats.tv_wait);
2892 tv_zero(&ctx->stats.tv_response);
2893 ctx->stats.t_request = -1;
2894 ctx->stats.t_queue = -1;
2895 ctx->stats.t_waiting = -1;
2896 ctx->stats.t_response = -1;
2897 ctx->stats.t_process = -1;
2898 ctx->stats.t_total = 0;
2899
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002900 ctx->strm = s;
2901 ctx->state = SPOE_CTX_ST_READY;
2902 filter->ctx = ctx;
2903
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002904 return ctx;
2905}
2906
2907static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002908spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002909{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002910 struct spoe_config *conf = FLT_CONF(filter);
2911 struct spoe_context *ctx = filter->ctx;
2912
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913 if (!ctx)
2914 return;
2915
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002916 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002917 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002918 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919}
2920
2921static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002922spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923{
2924 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002925 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002926
2927 tv_zero(&ctx->stats.tv_start);
2928 tv_zero(&ctx->stats.tv_request);
2929 tv_zero(&ctx->stats.tv_queue);
2930 tv_zero(&ctx->stats.tv_wait);
2931 tv_zero(&ctx->stats.tv_response);
2932 ctx->stats.t_request = -1;
2933 ctx->stats.t_queue = -1;
2934 ctx->stats.t_waiting = -1;
2935 ctx->stats.t_response = -1;
2936 ctx->stats.t_process = -1;
2937 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002938}
2939
2940
2941/***************************************************************************
2942 * Hooks that manage the filter lifecycle (init/check/deinit)
2943 **************************************************************************/
2944/* Signal handler: Do a soft stop, wakeup SPOE applet */
2945static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002946spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002947{
2948 struct proxy *p;
2949
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002950 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002951 while (p) {
2952 struct flt_conf *fconf;
2953
2954 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002955 struct spoe_config *conf;
2956 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002957 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002958 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002959
Christopher Faulet3b386a32017-02-23 10:17:15 +01002960 if (fconf->id != spoe_filter_id)
2961 continue;
2962
2963 conf = fconf->conf;
2964 agent = conf->agent;
2965
Christopher Faulet24289f22017-09-25 14:48:02 +02002966 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002967 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002968 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2969 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002970 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002971 }
2972 }
2973 p = p->next;
2974 }
2975}
2976
2977
2978/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2979static int
2980spoe_init(struct proxy *px, struct flt_conf *fconf)
2981{
2982 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002984 /* conf->agent_fe was already initialized during the config
2985 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002987 conf->agent_fe.cap = PR_CAP_FE;
2988 conf->agent_fe.mode = PR_MODE_TCP;
2989 conf->agent_fe.maxconn = 0;
2990 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2991 conf->agent_fe.conn_retries = CONN_RETRIES;
2992 conf->agent_fe.accept = frontend_accept;
2993 conf->agent_fe.srv = NULL;
2994 conf->agent_fe.timeout.client = TICK_ETERNITY;
2995 conf->agent_fe.default_target = &spoe_applet.obj_type;
2996 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2997
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002998 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002999 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003000 sighandler_registered = 1;
3001 }
3002
3003 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003004}
3005
3006/* Free ressources allocated by the SPOE filter. */
3007static void
3008spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3009{
3010 struct spoe_config *conf = fconf->conf;
3011
3012 if (conf) {
3013 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014
Christopher Faulet8ef75252017-02-20 22:56:03 +01003015 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003016 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003017 free(conf);
3018 }
3019 fconf->conf = NULL;
3020}
3021
3022/* Check configuration of a SPOE filter for a specified proxy.
3023 * Return 1 on error, else 0. */
3024static int
3025spoe_check(struct proxy *px, struct flt_conf *fconf)
3026{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003027 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003028 struct spoe_config *conf = fconf->conf;
3029 struct proxy *target;
3030
Christopher Faulet7ee86672017-09-19 11:08:28 +02003031 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3032 * are uniq */
3033 list_for_each_entry(f, &px->filter_configs, list) {
3034 struct spoe_config *c = f->conf;
3035
3036 /* This is not an SPOE filter */
3037 if (f->id != spoe_filter_id)
3038 continue;
3039 /* This is the current SPOE filter */
3040 if (f == fconf)
3041 continue;
3042
3043 /* Check engine Id. It should be uniq */
3044 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003045 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3046 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003047 return 1;
3048 }
3049 }
3050
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003051 target = proxy_be_by_name(conf->agent->b.name);
3052 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003053 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3054 " declared at %s:%d.\n",
3055 px->id, conf->agent->b.name, conf->agent->id,
3056 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057 return 1;
3058 }
3059 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003060 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3061 " at %s:%d does not support HTTP mode.\n",
3062 px->id, target->id, conf->agent->id,
3063 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003064 return 1;
3065 }
3066
3067 free(conf->agent->b.name);
3068 conf->agent->b.name = NULL;
3069 conf->agent->b.be = target;
3070 return 0;
3071}
3072
3073/**************************************************************************
3074 * Hooks attached to a stream
3075 *************************************************************************/
3076/* Called when a filter instance is created and attach to a stream. It creates
3077 * the context that will be used to process this stream. */
3078static int
3079spoe_start(struct stream *s, struct filter *filter)
3080{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003081 struct spoe_config *conf = FLT_CONF(filter);
3082 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003083 struct spoe_context *ctx;
3084
3085 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003086 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003087 __FUNCTION__, s);
3088
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003089 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003090 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3091 " - failed to create SPOE context\n",
3092 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003093 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003094 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003095 "SPOE: [%s] failed to create SPOE context\n",
3096 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003097 return 0;
3098 }
3099
Christopher Faulet11610f32017-09-21 10:23:10 +02003100 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003101 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3102
Christopher Faulet11610f32017-09-21 10:23:10 +02003103 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3105
Christopher Faulet11610f32017-09-21 10:23:10 +02003106 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003107 filter->pre_analyzers |= AN_RES_INSPECT;
3108
Christopher Faulet11610f32017-09-21 10:23:10 +02003109 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003110 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3111
Christopher Faulet11610f32017-09-21 10:23:10 +02003112 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003113 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3114
Christopher Faulet11610f32017-09-21 10:23:10 +02003115 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003116 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3117
3118 return 1;
3119}
3120
3121/* Called when a filter instance is detached from a stream. It release the
3122 * attached SPOE context. */
3123static void
3124spoe_stop(struct stream *s, struct filter *filter)
3125{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003126 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3127 (int)now.tv_sec, (int)now.tv_usec,
3128 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3129 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003130 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003131}
3132
Christopher Fauletf7a30922016-11-10 15:04:51 +01003133
3134/*
3135 * Called when the stream is woken up because of expired timer.
3136 */
3137static void
3138spoe_check_timeouts(struct stream *s, struct filter *filter)
3139{
3140 struct spoe_context *ctx = filter->ctx;
3141
Christopher Fauletac580602018-03-20 16:09:20 +01003142 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003143 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003144}
3145
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146/* Called when we are ready to filter data on a channel */
3147static int
3148spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3149{
3150 struct spoe_context *ctx = filter->ctx;
3151 int ret = 1;
3152
3153 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3154 " - ctx-flags=0x%08x\n",
3155 (int)now.tv_sec, (int)now.tv_usec,
3156 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3157 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3158
Christopher Fauletb067b062017-01-04 16:39:11 +01003159 if (ctx->state == SPOE_CTX_ST_NONE)
3160 goto out;
3161
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162 if (!(chn->flags & CF_ISRESP)) {
3163 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3164 chn->analysers |= AN_REQ_INSPECT_FE;
3165 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3166 chn->analysers |= AN_REQ_INSPECT_BE;
3167
3168 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3169 goto out;
3170
3171 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003172 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003173 if (!ret)
3174 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003175 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3176 }
3177 else {
3178 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3179 chn->analysers |= AN_RES_INSPECT;
3180
3181 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3182 goto out;
3183
Christopher Faulet8ef75252017-02-20 22:56:03 +01003184 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003185 if (!ret) {
3186 channel_dont_read(chn);
3187 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003188 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003189 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003190 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 }
3192
3193 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003194 return ret;
3195}
3196
3197/* Called before a processing happens on a given channel */
3198static int
3199spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3200 struct channel *chn, unsigned an_bit)
3201{
3202 struct spoe_context *ctx = filter->ctx;
3203 int ret = 1;
3204
3205 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3206 " - ctx-flags=0x%08x - ana=0x%08x\n",
3207 (int)now.tv_sec, (int)now.tv_usec,
3208 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3209 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3210 ctx->flags, an_bit);
3211
Christopher Fauletb067b062017-01-04 16:39:11 +01003212 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213 goto out;
3214
3215 switch (an_bit) {
3216 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003217 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003218 break;
3219 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003220 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 break;
3222 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003223 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 break;
3225 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003226 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227 break;
3228 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003229 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 break;
3231 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003232 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233 break;
3234 }
3235
3236 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003237 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003238 channel_dont_read(chn);
3239 channel_dont_close(chn);
3240 }
3241 return ret;
3242}
3243
3244/* Called when the filtering on the channel ends. */
3245static int
3246spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3247{
3248 struct spoe_context *ctx = filter->ctx;
3249
3250 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3251 " - ctx-flags=0x%08x\n",
3252 (int)now.tv_sec, (int)now.tv_usec,
3253 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3254 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3255
3256 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003257 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003258 }
3259
3260 return 1;
3261}
3262
3263/********************************************************************
3264 * Functions that manage the filter initialization
3265 ********************************************************************/
3266struct flt_ops spoe_ops = {
3267 /* Manage SPOE filter, called for each filter declaration */
3268 .init = spoe_init,
3269 .deinit = spoe_deinit,
3270 .check = spoe_check,
3271
3272 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003273 .attach = spoe_start,
3274 .detach = spoe_stop,
3275 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276
3277 /* Handle channels activity */
3278 .channel_start_analyze = spoe_start_analyze,
3279 .channel_pre_analyze = spoe_chn_pre_analyze,
3280 .channel_end_analyze = spoe_end_analyze,
3281};
3282
3283
3284static int
3285cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3286{
3287 const char *err;
3288 int i, err_code = 0;
3289
3290 if ((cfg_scope == NULL && curengine != NULL) ||
3291 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003292 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003293 goto out;
3294
3295 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3296 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003297 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3298 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 err_code |= ERR_ALERT | ERR_ABORT;
3300 goto out;
3301 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003302 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3303 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 goto out;
3305 }
3306
3307 err = invalid_char(args[1]);
3308 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003309 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3310 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 err_code |= ERR_ALERT | ERR_ABORT;
3312 goto out;
3313 }
3314
3315 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003316 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3317 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003318 err_code |= ERR_ALERT | ERR_ABORT;
3319 goto out;
3320 }
3321 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003322 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003323 err_code |= ERR_ALERT | ERR_ABORT;
3324 goto out;
3325 }
3326
3327 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003328
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003329 curagent->conf.file = strdup(file);
3330 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003331
3332 curagent->timeout.hello = TICK_ETERNITY;
3333 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003334 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003335
3336 curagent->engine_id = NULL;
3337 curagent->var_pfx = NULL;
3338 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003339 curagent->var_t_process = NULL;
3340 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003341 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3342 if (global.nbthread == 1)
3343 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003344 curagent->cps_max = 0;
3345 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003346 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003347 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003348
3349 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003350 LIST_INIT(&curagent->events[i]);
3351 LIST_INIT(&curagent->groups);
3352 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003353
Christopher Faulet24289f22017-09-25 14:48:02 +02003354 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003355 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003356 err_code |= ERR_ALERT | ERR_ABORT;
3357 goto out;
3358 }
3359 for (i = 0; i < global.nbthread; ++i) {
3360 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003361 SPOE_DEBUG_STMT(curagent->rt[i].applets_act = 0);
3362 SPOE_DEBUG_STMT(curagent->rt[i].applets_idle = 0);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003363 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003364 LIST_INIT(&curagent->rt[i].applets);
3365 LIST_INIT(&curagent->rt[i].sending_queue);
3366 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003367 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003368 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003369 }
3370 else if (!strcmp(args[0], "use-backend")) {
3371 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003372 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3373 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003374 err_code |= ERR_ALERT | ERR_FATAL;
3375 goto out;
3376 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003377 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379 free(curagent->b.name);
3380 curagent->b.name = strdup(args[1]);
3381 }
3382 else if (!strcmp(args[0], "messages")) {
3383 int cur_arg = 1;
3384 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003385 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003386
Christopher Faulet11610f32017-09-21 10:23:10 +02003387 list_for_each_entry(ph, &curmphs, list) {
3388 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003389 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3390 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391 err_code |= ERR_ALERT | ERR_FATAL;
3392 goto out;
3393 }
3394 }
3395
Christopher Faulet11610f32017-09-21 10:23:10 +02003396 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003397 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 err_code |= ERR_ALERT | ERR_ABORT;
3399 goto out;
3400 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003401 ph->id = strdup(args[cur_arg]);
3402 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003403 cur_arg++;
3404 }
3405 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003406 else if (!strcmp(args[0], "groups")) {
3407 int cur_arg = 1;
3408 while (*args[cur_arg]) {
3409 struct spoe_placeholder *ph = NULL;
3410
3411 list_for_each_entry(ph, &curgphs, list) {
3412 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3414 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003415 err_code |= ERR_ALERT | ERR_FATAL;
3416 goto out;
3417 }
3418 }
3419
3420 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003421 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003422 err_code |= ERR_ALERT | ERR_ABORT;
3423 goto out;
3424 }
3425 ph->id = strdup(args[cur_arg]);
3426 LIST_ADDQ(&curgphs, &ph->list);
3427 cur_arg++;
3428 }
3429 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003430 else if (!strcmp(args[0], "timeout")) {
3431 unsigned int *tv = NULL;
3432 const char *res;
3433 unsigned timeout;
3434
3435 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003436 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3437 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003438 err_code |= ERR_ALERT | ERR_FATAL;
3439 goto out;
3440 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003441 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3442 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 if (!strcmp(args[1], "hello"))
3444 tv = &curagent->timeout.hello;
3445 else if (!strcmp(args[1], "idle"))
3446 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003447 else if (!strcmp(args[1], "processing"))
3448 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003450 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3451 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003452 err_code |= ERR_ALERT | ERR_FATAL;
3453 goto out;
3454 }
3455 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003456 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3457 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003458 err_code |= ERR_ALERT | ERR_FATAL;
3459 goto out;
3460 }
3461 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3462 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003463 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3464 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003465 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003466 goto out;
3467 }
3468 *tv = MS_TO_TICKS(timeout);
3469 }
3470 else if (!strcmp(args[0], "option")) {
3471 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003472 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3473 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003474 err_code |= ERR_ALERT | ERR_FATAL;
3475 goto out;
3476 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003477
Christopher Faulet305c6072017-02-23 16:17:53 +01003478 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003479 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003480 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003481 if (kwm == 1)
3482 curagent->flags &= ~SPOE_FL_PIPELINING;
3483 else
3484 curagent->flags |= SPOE_FL_PIPELINING;
3485 goto out;
3486 }
3487 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003488 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003489 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003490 if (kwm == 1)
3491 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003492 else {
3493 if (global.nbthread == 1)
3494 curagent->flags |= SPOE_FL_ASYNC;
3495 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003496 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3497 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003498 err_code |= ERR_WARN;
3499 }
3500 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003501 goto out;
3502 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003503 else if (!strcmp(args[1], "send-frag-payload")) {
3504 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3505 goto out;
3506 if (kwm == 1)
3507 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3508 else
3509 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3510 goto out;
3511 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003512 else if (!strcmp(args[1], "dontlog-normal")) {
3513 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3514 goto out;
3515 if (kwm == 1)
3516 curpxopts2 &= ~PR_O2_NOLOGNORM;
3517 else
3518 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003519 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003520 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003521
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003522 /* Following options does not support negation */
3523 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003524 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3525 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003526 err_code |= ERR_ALERT | ERR_FATAL;
3527 goto out;
3528 }
3529
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003530 if (!strcmp(args[1], "var-prefix")) {
3531 char *tmp;
3532
3533 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003534 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3535 file, linenum, args[0],
3536 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003537 err_code |= ERR_ALERT | ERR_FATAL;
3538 goto out;
3539 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003540 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3541 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003542 tmp = args[2];
3543 while (*tmp) {
3544 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003545 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003546 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003547 err_code |= ERR_ALERT | ERR_FATAL;
3548 goto out;
3549 }
3550 tmp++;
3551 }
3552 curagent->var_pfx = strdup(args[2]);
3553 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003554 else if (!strcmp(args[1], "force-set-var")) {
3555 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3556 goto out;
3557 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3558 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003559 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003560 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003561 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003562 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3563 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003564 else if (!strcmp(args[1], "set-on-error")) {
3565 char *tmp;
3566
3567 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003568 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3569 file, linenum, args[0],
3570 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003571 err_code |= ERR_ALERT | ERR_FATAL;
3572 goto out;
3573 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003574 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3575 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003576 tmp = args[2];
3577 while (*tmp) {
3578 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003579 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003580 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003581 err_code |= ERR_ALERT | ERR_FATAL;
3582 goto out;
3583 }
3584 tmp++;
3585 }
3586 curagent->var_on_error = strdup(args[2]);
3587 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003588 else if (!strcmp(args[1], "set-process-time")) {
3589 char *tmp;
3590
3591 if (!*args[2]) {
3592 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3593 file, linenum, args[0],
3594 args[1]);
3595 err_code |= ERR_ALERT | ERR_FATAL;
3596 goto out;
3597 }
3598 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3599 goto out;
3600 tmp = args[2];
3601 while (*tmp) {
3602 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003603 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003604 file, linenum, args[0], args[1]);
3605 err_code |= ERR_ALERT | ERR_FATAL;
3606 goto out;
3607 }
3608 tmp++;
3609 }
3610 curagent->var_t_process = strdup(args[2]);
3611 }
3612 else if (!strcmp(args[1], "set-total-time")) {
3613 char *tmp;
3614
3615 if (!*args[2]) {
3616 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3617 file, linenum, args[0],
3618 args[1]);
3619 err_code |= ERR_ALERT | ERR_FATAL;
3620 goto out;
3621 }
3622 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3623 goto out;
3624 tmp = args[2];
3625 while (*tmp) {
3626 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003627 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003628 file, linenum, args[0], args[1]);
3629 err_code |= ERR_ALERT | ERR_FATAL;
3630 goto out;
3631 }
3632 tmp++;
3633 }
3634 curagent->var_t_total = strdup(args[2]);
3635 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003636 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003637 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3638 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
Christopher Faulet48026722016-11-16 15:01:12 +01003642 }
3643 else if (!strcmp(args[0], "maxconnrate")) {
3644 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003645 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3646 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003647 err_code |= ERR_ALERT | ERR_FATAL;
3648 goto out;
3649 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003650 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003651 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003652 curagent->cps_max = atol(args[1]);
3653 }
3654 else if (!strcmp(args[0], "maxerrrate")) {
3655 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003656 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3657 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003658 err_code |= ERR_ALERT | ERR_FATAL;
3659 goto out;
3660 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003661 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003662 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003663 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003664 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003665 else if (!strcmp(args[0], "max-frame-size")) {
3666 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003667 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3668 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003669 err_code |= ERR_ALERT | ERR_FATAL;
3670 goto out;
3671 }
3672 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3673 goto out;
3674 curagent->max_frame_size = atol(args[1]);
3675 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3676 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003677 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3678 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003679 err_code |= ERR_ALERT | ERR_FATAL;
3680 goto out;
3681 }
3682 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003683 else if (!strcmp(args[0], "max-waiting-frames")) {
3684 if (!*args[1]) {
3685 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3686 file, linenum, args[0]);
3687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
3690 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3691 goto out;
3692 curagent->max_fpa = atol(args[1]);
3693 if (curagent->max_fpa < 1) {
3694 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3695 file, linenum, args[0]);
3696 err_code |= ERR_ALERT | ERR_FATAL;
3697 goto out;
3698 }
3699 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003700 else if (!strcmp(args[0], "register-var-names")) {
3701 int cur_arg;
3702
3703 if (!*args[1]) {
3704 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3705 file, linenum, args[0]);
3706 err_code |= ERR_ALERT | ERR_FATAL;
3707 goto out;
3708 }
3709 cur_arg = 1;
3710 while (*args[cur_arg]) {
3711 struct spoe_var_placeholder *vph;
3712
3713 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3714 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3715 err_code |= ERR_ALERT | ERR_ABORT;
3716 goto out;
3717 }
3718 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3719 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3720 err_code |= ERR_ALERT | ERR_ABORT;
3721 goto out;
3722 }
3723 LIST_ADDQ(&curvars, &vph->list);
3724 cur_arg++;
3725 }
3726 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003727 else if (!strcmp(args[0], "log")) {
3728 char *errmsg = NULL;
3729
3730 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3731 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
3735 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003736 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003737 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3738 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003739 err_code |= ERR_ALERT | ERR_FATAL;
3740 goto out;
3741 }
3742 out:
3743 return err_code;
3744}
Christopher Faulet11610f32017-09-21 10:23:10 +02003745static int
3746cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3747{
3748 struct spoe_group *grp;
3749 const char *err;
3750 int err_code = 0;
3751
3752 if ((cfg_scope == NULL && curengine != NULL) ||
3753 (cfg_scope != NULL && curengine == NULL) ||
3754 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3755 goto out;
3756
3757 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3758 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003759 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3760 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003761 err_code |= ERR_ALERT | ERR_ABORT;
3762 goto out;
3763 }
3764 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3765 err_code |= ERR_ABORT;
3766 goto out;
3767 }
3768
3769 err = invalid_char(args[1]);
3770 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003771 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3772 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003773 err_code |= ERR_ALERT | ERR_ABORT;
3774 goto out;
3775 }
3776
3777 list_for_each_entry(grp, &curgrps, list) {
3778 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003779 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3780 " name as another one declared at %s:%d.\n",
3781 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003782 err_code |= ERR_ALERT | ERR_FATAL;
3783 goto out;
3784 }
3785 }
3786
3787 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003788 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003789 err_code |= ERR_ALERT | ERR_ABORT;
3790 goto out;
3791 }
3792
3793 curgrp->id = strdup(args[1]);
3794 curgrp->conf.file = strdup(file);
3795 curgrp->conf.line = linenum;
3796 LIST_INIT(&curgrp->phs);
3797 LIST_INIT(&curgrp->messages);
3798 LIST_ADDQ(&curgrps, &curgrp->list);
3799 }
3800 else if (!strcmp(args[0], "messages")) {
3801 int cur_arg = 1;
3802 while (*args[cur_arg]) {
3803 struct spoe_placeholder *ph = NULL;
3804
3805 list_for_each_entry(ph, &curgrp->phs, list) {
3806 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003807 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3808 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003809 err_code |= ERR_ALERT | ERR_FATAL;
3810 goto out;
3811 }
3812 }
3813
3814 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003815 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003816 err_code |= ERR_ALERT | ERR_ABORT;
3817 goto out;
3818 }
3819 ph->id = strdup(args[cur_arg]);
3820 LIST_ADDQ(&curgrp->phs, &ph->list);
3821 cur_arg++;
3822 }
3823 }
3824 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003825 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3826 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 err_code |= ERR_ALERT | ERR_FATAL;
3828 goto out;
3829 }
3830 out:
3831 return err_code;
3832}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003833
3834static int
3835cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3836{
3837 struct spoe_message *msg;
3838 struct spoe_arg *arg;
3839 const char *err;
3840 char *errmsg = NULL;
3841 int err_code = 0;
3842
3843 if ((cfg_scope == NULL && curengine != NULL) ||
3844 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003845 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003846 goto out;
3847
3848 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3849 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003850 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3851 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003852 err_code |= ERR_ALERT | ERR_ABORT;
3853 goto out;
3854 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003855 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3856 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003857 goto out;
3858 }
3859
3860 err = invalid_char(args[1]);
3861 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3863 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003864 err_code |= ERR_ALERT | ERR_ABORT;
3865 goto out;
3866 }
3867
3868 list_for_each_entry(msg, &curmsgs, list) {
3869 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003870 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3871 " name as another one declared at %s:%d.\n",
3872 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003873 err_code |= ERR_ALERT | ERR_FATAL;
3874 goto out;
3875 }
3876 }
3877
3878 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003879 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003880 err_code |= ERR_ALERT | ERR_ABORT;
3881 goto out;
3882 }
3883
3884 curmsg->id = strdup(args[1]);
3885 curmsg->id_len = strlen(curmsg->id);
3886 curmsg->event = SPOE_EV_NONE;
3887 curmsg->conf.file = strdup(file);
3888 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003889 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003890 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003891 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003892 LIST_INIT(&curmsg->by_evt);
3893 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003894 LIST_ADDQ(&curmsgs, &curmsg->list);
3895 }
3896 else if (!strcmp(args[0], "args")) {
3897 int cur_arg = 1;
3898
3899 curproxy->conf.args.ctx = ARGC_SPOE;
3900 curproxy->conf.args.file = file;
3901 curproxy->conf.args.line = linenum;
3902 while (*args[cur_arg]) {
3903 char *delim = strchr(args[cur_arg], '=');
3904 int idx = 0;
3905
3906 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003907 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003908 err_code |= ERR_ALERT | ERR_ABORT;
3909 goto out;
3910 }
3911
3912 if (!delim) {
3913 arg->name = NULL;
3914 arg->name_len = 0;
3915 delim = args[cur_arg];
3916 }
3917 else {
3918 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3919 arg->name_len = delim - args[cur_arg];
3920 delim++;
3921 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003922 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3923 &idx, file, linenum, &errmsg,
3924 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003925 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003926 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 err_code |= ERR_ALERT | ERR_FATAL;
3928 free(arg->name);
3929 free(arg);
3930 goto out;
3931 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003932 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003933 LIST_ADDQ(&curmsg->args, &arg->list);
3934 cur_arg++;
3935 }
3936 curproxy->conf.args.file = NULL;
3937 curproxy->conf.args.line = 0;
3938 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003939 else if (!strcmp(args[0], "acl")) {
3940 err = invalid_char(args[1]);
3941 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003942 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3943 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003944 err_code |= ERR_ALERT | ERR_FATAL;
3945 goto out;
3946 }
3947 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003948 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3949 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003950 err_code |= ERR_ALERT | ERR_FATAL;
3951 goto out;
3952 }
3953 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003954 else if (!strcmp(args[0], "event")) {
3955 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003956 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003957 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003958 goto out;
3959 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003960 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3961 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003962
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003963 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3964 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3965 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3966 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3967
3968 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3969 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3970 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3971 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3972 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3973 curmsg->event = SPOE_EV_ON_TCP_RSP;
3974
3975 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3976 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3977 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3978 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3979 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3980 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3981 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003982 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3983 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003984 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003985 goto out;
3986 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003987
3988 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3989 struct acl_cond *cond;
3990
3991 cond = build_acl_cond(file, linenum, &curmsg->acls,
3992 curproxy, (const char **)args+2,
3993 &errmsg);
3994 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003995 ha_alert("parsing [%s:%d] : error detected while "
3996 "parsing an 'event %s' condition : %s.\n",
3997 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003998 err_code |= ERR_ALERT | ERR_FATAL;
3999 goto out;
4000 }
4001 curmsg->cond = cond;
4002 }
4003 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004004 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4005 "or 'unless' followed by a condition but found '%s'.\n",
4006 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004007 err_code |= ERR_ALERT | ERR_FATAL;
4008 goto out;
4009 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004010 }
4011 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004012 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4013 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004014 err_code |= ERR_ALERT | ERR_FATAL;
4015 goto out;
4016 }
4017 out:
4018 free(errmsg);
4019 return err_code;
4020}
4021
4022/* Return -1 on error, else 0 */
4023static int
4024parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4025 struct flt_conf *fconf, char **err, void *private)
4026{
4027 struct list backup_sections;
4028 struct spoe_config *conf;
4029 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004030 struct spoe_group *grp, *grpback;
4031 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004032 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004033 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004034 char *file = NULL, *engine = NULL;
4035 int ret, pos = *cur_arg + 1;
4036
Christopher Faulet84c844e2018-03-23 14:37:14 +01004037 LIST_INIT(&curmsgs);
4038 LIST_INIT(&curgrps);
4039 LIST_INIT(&curmphs);
4040 LIST_INIT(&curgphs);
4041 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004042 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004043 curpxopts = 0;
4044 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004045
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004046 conf = calloc(1, sizeof(*conf));
4047 if (conf == NULL) {
4048 memprintf(err, "%s: out of memory", args[*cur_arg]);
4049 goto error;
4050 }
4051 conf->proxy = px;
4052
4053 while (*args[pos]) {
4054 if (!strcmp(args[pos], "config")) {
4055 if (!*args[pos+1]) {
4056 memprintf(err, "'%s' : '%s' option without value",
4057 args[*cur_arg], args[pos]);
4058 goto error;
4059 }
4060 file = args[pos+1];
4061 pos += 2;
4062 }
4063 else if (!strcmp(args[pos], "engine")) {
4064 if (!*args[pos+1]) {
4065 memprintf(err, "'%s' : '%s' option without value",
4066 args[*cur_arg], args[pos]);
4067 goto error;
4068 }
4069 engine = args[pos+1];
4070 pos += 2;
4071 }
4072 else {
4073 memprintf(err, "unknown keyword '%s'", args[pos]);
4074 goto error;
4075 }
4076 }
4077 if (file == NULL) {
4078 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4079 goto error;
4080 }
4081
4082 /* backup sections and register SPOE sections */
4083 LIST_INIT(&backup_sections);
4084 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004085 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4086 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004087 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004088
4089 /* Parse SPOE filter configuration file */
4090 curengine = engine;
4091 curproxy = px;
4092 curagent = NULL;
4093 curmsg = NULL;
4094 ret = readcfgfile(file);
4095 curproxy = NULL;
4096
4097 /* unregister SPOE sections and restore previous sections */
4098 cfg_unregister_sections();
4099 cfg_restore_sections(&backup_sections);
4100
4101 if (ret == -1) {
4102 memprintf(err, "Could not open configuration file %s : %s",
4103 file, strerror(errno));
4104 goto error;
4105 }
4106 if (ret & (ERR_ABORT|ERR_FATAL)) {
4107 memprintf(err, "Error(s) found in configuration file %s", file);
4108 goto error;
4109 }
4110
4111 /* Check SPOE agent */
4112 if (curagent == NULL) {
4113 memprintf(err, "No SPOE agent found in file %s", file);
4114 goto error;
4115 }
4116 if (curagent->b.name == NULL) {
4117 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4118 curagent->id, curagent->conf.file, curagent->conf.line);
4119 goto error;
4120 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004121 if (curagent->timeout.hello == TICK_ETERNITY ||
4122 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004123 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004124 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4125 " | While not properly invalid, you will certainly encounter various problems\n"
4126 " | with such a configuration. To fix this, please ensure that all following\n"
4127 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4128 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004129 }
4130 if (curagent->var_pfx == NULL) {
4131 char *tmp = curagent->id;
4132
4133 while (*tmp) {
4134 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4135 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4136 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4137 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4138 goto error;
4139 }
4140 tmp++;
4141 }
4142 curagent->var_pfx = strdup(curagent->id);
4143 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004144 if (curagent->engine_id == NULL)
4145 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004146
Christopher Fauletb7426d12018-03-21 14:12:17 +01004147 if (curagent->var_on_error) {
4148 struct arg arg;
4149
4150 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4151 curagent->var_pfx, curagent->var_on_error);
4152
4153 arg.type = ARGT_STR;
4154 arg.data.str.str = trash.str;
4155 arg.data.str.len = trash.len;
4156 if (!vars_check_arg(&arg, err)) {
4157 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4158 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4159 goto error;
4160 }
4161 }
4162
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004163 if (curagent->var_t_process) {
4164 struct arg arg;
4165
4166 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4167 curagent->var_pfx, curagent->var_t_process);
4168
4169 arg.type = ARGT_STR;
4170 arg.data.str.str = trash.str;
4171 arg.data.str.len = trash.len;
4172 if (!vars_check_arg(&arg, err)) {
4173 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4174 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4175 goto error;
4176 }
4177 }
4178
4179 if (curagent->var_t_total) {
4180 struct arg arg;
4181
4182 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4183 curagent->var_pfx, curagent->var_t_total);
4184
4185 arg.type = ARGT_STR;
4186 arg.data.str.str = trash.str;
4187 arg.data.str.len = trash.len;
4188 if (!vars_check_arg(&arg, err)) {
4189 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4190 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4191 goto error;
4192 }
4193 }
4194
Christopher Faulet11610f32017-09-21 10:23:10 +02004195 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004196 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4197 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004198 goto finish;
4199 }
4200
Christopher Faulet11610f32017-09-21 10:23:10 +02004201 /* Replace placeholders by the corresponding messages for the SPOE
4202 * agent */
4203 list_for_each_entry(ph, &curmphs, list) {
4204 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004205 struct spoe_arg *arg;
4206 unsigned int where;
4207
Christopher Faulet11610f32017-09-21 10:23:10 +02004208 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004209 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4210 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4211 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4212 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4213 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4214 }
4215 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4216 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4217 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004218 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4219 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004220 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004221 }
4222 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004223 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4224 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004225 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004226 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004227
4228 where = 0;
4229 switch (msg->event) {
4230 case SPOE_EV_ON_CLIENT_SESS:
4231 where |= SMP_VAL_FE_CON_ACC;
4232 break;
4233
4234 case SPOE_EV_ON_TCP_REQ_FE:
4235 where |= SMP_VAL_FE_REQ_CNT;
4236 break;
4237
4238 case SPOE_EV_ON_HTTP_REQ_FE:
4239 where |= SMP_VAL_FE_HRQ_HDR;
4240 break;
4241
4242 case SPOE_EV_ON_TCP_REQ_BE:
4243 if (px->cap & PR_CAP_FE)
4244 where |= SMP_VAL_FE_REQ_CNT;
4245 if (px->cap & PR_CAP_BE)
4246 where |= SMP_VAL_BE_REQ_CNT;
4247 break;
4248
4249 case SPOE_EV_ON_HTTP_REQ_BE:
4250 if (px->cap & PR_CAP_FE)
4251 where |= SMP_VAL_FE_HRQ_HDR;
4252 if (px->cap & PR_CAP_BE)
4253 where |= SMP_VAL_BE_HRQ_HDR;
4254 break;
4255
4256 case SPOE_EV_ON_SERVER_SESS:
4257 where |= SMP_VAL_BE_SRV_CON;
4258 break;
4259
4260 case SPOE_EV_ON_TCP_RSP:
4261 if (px->cap & PR_CAP_FE)
4262 where |= SMP_VAL_FE_RES_CNT;
4263 if (px->cap & PR_CAP_BE)
4264 where |= SMP_VAL_BE_RES_CNT;
4265 break;
4266
4267 case SPOE_EV_ON_HTTP_RSP:
4268 if (px->cap & PR_CAP_FE)
4269 where |= SMP_VAL_FE_HRS_HDR;
4270 if (px->cap & PR_CAP_BE)
4271 where |= SMP_VAL_BE_HRS_HDR;
4272 break;
4273
4274 default:
4275 break;
4276 }
4277
4278 list_for_each_entry(arg, &msg->args, list) {
4279 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004280 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004281 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004282 "none of which is available here ('%s')",
4283 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004284 sample_ckp_names(arg->expr->fetch->use),
4285 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004286 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004287 }
4288 }
4289
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004290 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004291 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4292 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004293 }
4294 }
4295 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004296 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004297 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004298 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004299 continue;
4300 }
4301
Christopher Faulet11610f32017-09-21 10:23:10 +02004302 /* Replace placeholders by the corresponding groups for the SPOE
4303 * agent */
4304 list_for_each_entry(ph, &curgphs, list) {
4305 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4306 if (!strcmp(grp->id, ph->id)) {
4307 grp->agent = curagent;
4308 LIST_DEL(&grp->list);
4309 LIST_ADDQ(&curagent->groups, &grp->list);
4310 goto next_aph;
4311 }
4312 }
4313 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4314 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4315 goto error;
4316 next_aph:
4317 continue;
4318 }
4319
4320 /* Replace placeholders by the corresponding message for each SPOE
4321 * group of the SPOE agent */
4322 list_for_each_entry(grp, &curagent->groups, list) {
4323 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4324 list_for_each_entry(msg, &curmsgs, list) {
4325 if (!strcmp(msg->id, ph->id)) {
4326 if (msg->group != NULL) {
4327 memprintf(err, "SPOE message '%s' already belongs to "
4328 "the SPOE group '%s' declare at %s:%d",
4329 msg->id, msg->group->id,
4330 msg->group->conf.file,
4331 msg->group->conf.line);
4332 goto error;
4333 }
4334
4335 /* Scope for arguments are not checked for now. We will check
4336 * them only if a rule use the corresponding SPOE group. */
4337 msg->agent = curagent;
4338 msg->group = grp;
4339 LIST_DEL(&ph->list);
4340 LIST_ADDQ(&grp->messages, &msg->by_grp);
4341 goto next_mph_grp;
4342 }
4343 }
4344 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4345 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4346 goto error;
4347 next_mph_grp:
4348 continue;
4349 }
4350 }
4351
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004352 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004353 /* move curmsgs to the agent message list */
4354 curmsgs.n->p = &curagent->messages;
4355 curmsgs.p->n = &curagent->messages;
4356 curagent->messages = curmsgs;
4357 LIST_INIT(&curmsgs);
4358
Christopher Faulet7ee86672017-09-19 11:08:28 +02004359 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004360 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004361
4362 /* Start agent's proxy initialization here. It will be finished during
4363 * the filter init. */
4364 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4365 init_new_proxy(&conf->agent_fe);
4366 conf->agent_fe.id = conf->agent->id;
4367 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004368 conf->agent_fe.options |= curpxopts;
4369 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004370
4371 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4372 LIST_DEL(&logsrv->list);
4373 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4374 }
4375
Christopher Faulet11610f32017-09-21 10:23:10 +02004376 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4377 LIST_DEL(&ph->list);
4378 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004379 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004380 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4381 LIST_DEL(&ph->list);
4382 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004383 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004384 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4385 struct arg arg;
4386
4387 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4388 curagent->var_pfx, vph->name);
4389
4390 arg.type = ARGT_STR;
4391 arg.data.str.str = trash.str;
4392 arg.data.str.len = trash.len;
4393 if (!vars_check_arg(&arg, err)) {
4394 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4395 curagent->id, curagent->var_pfx, vph->name, *err);
4396 goto error;
4397 }
4398
4399 LIST_DEL(&vph->list);
4400 free(vph->name);
4401 free(vph);
4402 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004403 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4404 LIST_DEL(&grp->list);
4405 spoe_release_group(grp);
4406 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004407 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004408 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004409 fconf->ops = &spoe_ops;
4410 fconf->conf = conf;
4411 return 0;
4412
4413 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004414 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004415 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4416 LIST_DEL(&ph->list);
4417 spoe_release_placeholder(ph);
4418 }
4419 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4420 LIST_DEL(&ph->list);
4421 spoe_release_placeholder(ph);
4422 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004423 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4424 LIST_DEL(&vph->list);
4425 free(vph->name);
4426 free(vph);
4427 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004428 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4429 LIST_DEL(&grp->list);
4430 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004431 }
4432 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4433 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004434 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004435 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004436 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4437 LIST_DEL(&logsrv->list);
4438 free(logsrv);
4439 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004440 free(conf);
4441 return -1;
4442}
4443
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004444/* Send message of a SPOE group. This is the action_ptr callback of a rule
4445 * associated to a "send-spoe-group" action.
4446 *
4447 * It returns ACT_RET_CONT is processing is finished without error, it returns
4448 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4449 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004450static enum act_return
4451spoe_send_group(struct act_rule *rule, struct proxy *px,
4452 struct session *sess, struct stream *s, int flags)
4453{
4454 struct filter *filter;
4455 struct spoe_agent *agent = NULL;
4456 struct spoe_group *group = NULL;
4457 struct spoe_context *ctx = NULL;
4458 int ret, dir;
4459
4460 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4461 if (filter->config == rule->arg.act.p[0]) {
4462 agent = rule->arg.act.p[2];
4463 group = rule->arg.act.p[3];
4464 ctx = filter->ctx;
4465 break;
4466 }
4467 }
4468 if (agent == NULL || group == NULL || ctx == NULL)
4469 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004470 if (ctx->state == SPOE_CTX_ST_NONE)
4471 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004472
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004473 switch (rule->from) {
4474 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4475 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4476 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4477 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4478 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4479 default:
4480 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4481 " - internal error while execute spoe-send-group\n",
4482 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4483 __FUNCTION__, s);
4484 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4485 agent->id);
4486 return ACT_RET_CONT;
4487 }
4488
4489 ret = spoe_process_group(s, ctx, group, dir);
4490 if (ret == 1)
4491 return ACT_RET_CONT;
4492 else if (ret == 0) {
4493 if (flags & ACT_FLAG_FINAL) {
4494 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4495 " - failed to process group '%s': interrupted by caller\n",
4496 (int)now.tv_sec, (int)now.tv_usec,
4497 agent->id, __FUNCTION__, s, group->id);
4498 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004499 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004500 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004501 return ACT_RET_CONT;
4502 }
4503 return ACT_RET_YIELD;
4504 }
4505 else
4506 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004507}
4508
4509/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4510 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4511 * action should be:
4512 *
4513 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4514 *
4515 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4516 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4517 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4518 * group.
4519 *
4520 * The function returns 1 in success case, otherwise, it returns 0 and err is
4521 * filled.
4522 */
4523static int
4524check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4525{
4526 struct flt_conf *fconf;
4527 struct spoe_config *conf;
4528 struct spoe_agent *agent = NULL;
4529 struct spoe_group *group;
4530 struct spoe_message *msg;
4531 char *engine_id = rule->arg.act.p[0];
4532 char *group_id = rule->arg.act.p[1];
4533 unsigned int where = 0;
4534
4535 switch (rule->from) {
4536 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4537 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4538 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4539 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4540 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4541 default:
4542 memprintf(err,
4543 "internal error, unexpected rule->from=%d, please report this bug!",
4544 rule->from);
4545 goto error;
4546 }
4547
4548 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4549 * <px> */
4550 list_for_each_entry(fconf, &px->filter_configs, list) {
4551 conf = fconf->conf;
4552
4553 /* This is not an SPOE filter */
4554 if (fconf->id != spoe_filter_id)
4555 continue;
4556
4557 /* This is the good engine */
4558 if (!strcmp(conf->id, engine_id)) {
4559 agent = conf->agent;
4560 break;
4561 }
4562 }
4563 if (agent == NULL) {
4564 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4565 engine_id, group_id);
4566 goto error;
4567 }
4568
4569 /* Try to find the right group */
4570 list_for_each_entry(group, &agent->groups, list) {
4571 /* This is the good group */
4572 if (!strcmp(group->id, group_id))
4573 break;
4574 }
4575 if (&group->list == &agent->groups) {
4576 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4577 group_id, engine_id);
4578 goto error;
4579 }
4580
4581 /* Ok, we found the group, we need to check messages and their
4582 * arguments */
4583 list_for_each_entry(msg, &group->messages, by_grp) {
4584 struct spoe_arg *arg;
4585
4586 list_for_each_entry(arg, &msg->args, list) {
4587 if (!(arg->expr->fetch->val & where)) {
4588 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4589 "some args extract information from '%s',"
4590 "none of which is available here ('%s')",
4591 msg->id, group->id, msg->conf.file, msg->conf.line,
4592 sample_ckp_names(arg->expr->fetch->use),
4593 sample_ckp_names(where));
4594 goto error;
4595 }
4596 }
4597 }
4598
4599 free(engine_id);
4600 free(group_id);
4601 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4602 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4603 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4604 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4605 return 1;
4606
4607 error:
4608 free(engine_id);
4609 free(group_id);
4610 return 0;
4611}
4612
4613/* Parse 'send-spoe-group' action following the format:
4614 *
4615 * ... send-spoe-group <engine-id> <group-id>
4616 *
4617 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4618 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4619 * ids are saved and used later, when the rule will be checked.
4620 */
4621static enum act_parse_ret
4622parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4623 struct act_rule *rule, char **err)
4624{
4625 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4626 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4627 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4628 return ACT_RET_PRS_ERR;
4629 }
4630 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4631 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4632
4633 (*orig_arg) += 2;
4634
4635 rule->action = ACT_CUSTOM;
4636 rule->action_ptr = spoe_send_group;
4637 rule->check_ptr = check_send_spoe_group;
4638 return ACT_RET_PRS_OK;
4639}
4640
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004641
4642/* Declare the filter parser for "spoe" keyword */
4643static struct flt_kw_list flt_kws = { "SPOE", { }, {
4644 { "spoe", parse_spoe_flt, NULL },
4645 { NULL, NULL, NULL },
4646 }
4647};
4648
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004649/* Delcate the action parser for "spoe-action" keyword */
4650static struct action_kw_list tcp_req_action_kws = { { }, {
4651 { "send-spoe-group", parse_send_spoe_group },
4652 { /* END */ },
4653 }
4654};
4655static struct action_kw_list tcp_res_action_kws = { { }, {
4656 { "send-spoe-group", parse_send_spoe_group },
4657 { /* END */ },
4658 }
4659};
4660static struct action_kw_list http_req_action_kws = { { }, {
4661 { "send-spoe-group", parse_send_spoe_group },
4662 { /* END */ },
4663 }
4664};
4665static struct action_kw_list http_res_action_kws = { { }, {
4666 { "send-spoe-group", parse_send_spoe_group },
4667 { /* END */ },
4668 }
4669};
4670
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004671__attribute__((constructor))
4672static void __spoe_init(void)
4673{
4674 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004675 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4676 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4677 http_req_keywords_register(&http_req_action_kws);
4678 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004679
Willy Tarreaubafbe012017-11-24 17:34:44 +01004680 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4681 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004682}
4683
4684__attribute__((destructor))
4685static void
4686__spoe_deinit(void)
4687{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004688 pool_destroy(pool_head_spoe_ctx);
4689 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004690}