blob: 0f72de8cdedb80afc61ee4f31e17f38d1cfbf8db [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/hathreads.h>
20#include <common/initcall.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020021#include <common/memory.h>
22#include <common/time.h>
23
24#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010026#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020027
Christopher Faulet57583e42017-09-04 15:41:09 +020028#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020029#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030#include <proto/arg.h>
31#include <proto/backend.h>
32#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010033#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020034#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020035#include <proto/http_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020036#include <proto/log.h>
37#include <proto/proto_http.h>
38#include <proto/proxy.h>
39#include <proto/sample.h>
40#include <proto/session.h>
41#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010042#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043#include <proto/stream.h>
44#include <proto/stream_interface.h>
45#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020046#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020047#include <proto/vars.h>
48
49#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
50#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#else
53#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010054#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020055#endif
56
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010057/* Reserved 4 bytes to the frame size. So a frame and its size can be written
58 * together in a buffer */
59#define MAX_FRAME_SIZE global.tune.bufsize - 4
60
61/* The minimum size for a frame */
62#define MIN_FRAME_SIZE 256
63
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Reserved for the metadata and the frame type.
65 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010066#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010068/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010069#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
70
Christopher Faulet3b386a32017-02-23 10:17:15 +010071/* SPOE filter id. Used to identify SPOE filters */
72const char *spoe_filter_id = "SPOE filter";
73
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020074/* Set if the handle on SIGUSR1 is registered */
75static int sighandler_registered = 0;
76
77/* proxy used during the parsing */
78struct proxy *curproxy = NULL;
79
80/* The name of the SPOE engine, used during the parsing */
81char *curengine = NULL;
82
83/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020084/* SPOE agent/group/message used during the parsing */
85struct spoe_agent *curagent = NULL;
86struct spoe_group *curgrp = NULL;
87struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
89/* list of SPOE messages and placeholders used during the parsing */
90struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020091struct list curgrps;
92struct list curmphs;
93struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010094struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Faulet7250b8f2018-03-26 17:19:01 +020096/* list of log servers used during the parsing */
97struct list curlogsrvs;
98
Christopher Faulet0e0f0852018-03-26 17:20:36 +020099/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
100int curpxopts;
101int curpxopts2;
102
Christopher Faulet42bfa462017-01-04 14:14:19 +0100103/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100104DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
105DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107struct flt_ops spoe_ops;
108
Christopher Faulet8ef75252017-02-20 22:56:03 +0100109static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200110static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
111static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112
113/********************************************************************
114 * helper functions/globals
115 ********************************************************************/
116static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200117spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118{
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200121 free(ph->id);
122 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123}
124
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100126spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127{
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 struct spoe_arg *arg, *argback;
129 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200130
131 if (!msg)
132 return;
133 free(msg->id);
134 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200135 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200136 release_sample_expr(arg->expr);
137 free(arg->name);
138 LIST_DEL(&arg->list);
139 free(arg);
140 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200141 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
142 LIST_DEL(&acl->list);
143 prune_acl(acl);
144 free(acl);
145 }
146 if (msg->cond) {
147 prune_acl_cond(msg->cond);
148 free(msg->cond);
149 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200150 free(msg);
151}
152
153static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200154spoe_release_group(struct spoe_group *grp)
155{
156 if (!grp)
157 return;
158 free(grp->id);
159 free(grp->conf.file);
160 free(grp);
161}
162
163static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100164spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200165{
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 struct spoe_message *msg, *msgback;
167 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200168 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200169
170 if (!agent)
171 return;
172 free(agent->id);
173 free(agent->conf.file);
174 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100175 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100176 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100177 free(agent->var_t_process);
178 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200179 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
180 LIST_DEL(&msg->list);
181 spoe_release_message(msg);
182 }
183 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
184 LIST_DEL(&grp->list);
185 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200186 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 if (agent->rt) {
188 for (i = 0; i < global.nbthread; ++i)
189 HA_SPIN_DESTROY(&agent->rt[i].lock);
190 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200191 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200192 free(agent);
193}
194
195static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100196 [SPOE_FRM_ERR_NONE] = "normal",
197 [SPOE_FRM_ERR_IO] = "I/O error",
198 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
199 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
200 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
201 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
202 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
203 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
204 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
205 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
206 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
207 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100208 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100209 [SPOE_FRM_ERR_RES] = "resource allocation error",
210 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200211};
212
213static const char *spoe_event_str[SPOE_EV_EVENTS] = {
214 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
215 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
216 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
217 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
218 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
219
220 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
221 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
222 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
223};
224
225
226#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
227
228static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100229 [SPOE_CTX_ST_NONE] = "NONE",
230 [SPOE_CTX_ST_READY] = "READY",
231 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
232 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
233 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
234 [SPOE_CTX_ST_DONE] = "DONE",
235 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200236};
237
238static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100239 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
240 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
241 [SPOE_APPCTX_ST_IDLE] = "IDLE",
242 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
243 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
244 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
245 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
246 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
247 [SPOE_APPCTX_ST_EXIT] = "EXIT",
248 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200249};
250
251#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252
Christopher Faulet8ef75252017-02-20 22:56:03 +0100253/* Used to generates a unique id for an engine. On success, it returns a
254 * allocated string. So it is the caller's reponsibility to release it. If the
255 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100256static char *
257generate_pseudo_uuid()
258{
259 static int init = 0;
260
261 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
262 const char uuid_chr[] = "0123456789ABCDEF-";
263 char *uuid;
264 int i;
265
266 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
267 return NULL;
268
269 if (!init) {
270 srand(now_ms);
271 init = 1;
272 }
273
274 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
275 int r = rand () % 16;
276
277 switch (uuid_fmt[i]) {
278 case 'x' : uuid[i] = uuid_chr[r]; break;
279 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
280 default : uuid[i] = uuid_fmt[i]; break;
281 }
282 }
283 return uuid;
284}
285
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100286
287static inline void
288spoe_update_stat_time(struct timeval *tv, long *t)
289{
290 if (*t == -1)
291 *t = tv_ms_elapsed(tv, &now);
292 else
293 *t += tv_ms_elapsed(tv, &now);
294 tv_zero(tv);
295}
296
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297/********************************************************************
298 * Functions that encode/decode SPOE frames
299 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200300/* Helper to get static string length, excluding the terminating null byte */
301#define SLEN(str) (sizeof(str)-1)
302
303/* Predefined key used in HELLO/DISCONNECT frames */
304#define SUPPORTED_VERSIONS_KEY "supported-versions"
305#define VERSION_KEY "version"
306#define MAX_FRAME_SIZE_KEY "max-frame-size"
307#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100308#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100309#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310#define STATUS_CODE_KEY "status-code"
311#define MSG_KEY "message"
312
313struct spoe_version {
314 char *str;
315 int min;
316 int max;
317};
318
319/* All supported versions */
320static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200321 /* 1.0 is now unsupported because of a bug about frame's flags*/
322 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200323 {NULL, 0, 0}
324};
325
326/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200327#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329/* Convert a string to a SPOE version value. The string must follow the format
330 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
331 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 const char *p, *end;
336 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 p = str;
339 end = str+len;
340 maj = min = 0;
341 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200342
Christopher Faulet8ef75252017-02-20 22:56:03 +0100343 /* skip leading spaces */
344 while (p < end && isspace(*p))
345 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347 /* parse Major number, until the '.' */
348 while (*p != '.') {
349 if (p >= end || *p < '0' || *p > '9')
350 goto out;
351 maj *= 10;
352 maj += (*p - '0');
353 p++;
354 }
355
356 /* check Major version */
357 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200358 goto out;
359
Christopher Faulet8ef75252017-02-20 22:56:03 +0100360 p++; /* skip the '.' */
361 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
362 goto out;
363
364 /* Parse Minor number */
365 while (p < end) {
366 if (*p < '0' || *p > '9')
367 break;
368 min *= 10;
369 min += (*p - '0');
370 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200371 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372
373 /* check Minor number */
374 if (min > 999)
375 goto out;
376
377 /* skip trailing spaces */
378 while (p < end && isspace(*p))
379 p++;
380 if (p != end)
381 goto out;
382
383 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384 out:
385 return vsn;
386}
387
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800389 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393{
Willy Tarreau83061a82018-07-13 11:56:34 +0200394 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100395 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100396 char *p, *end;
397 unsigned int flags = SPOE_FRM_FL_FIN;
398 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200399
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 p = frame;
401 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 /* Set Frame type */
404 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100406 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200407 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 memcpy(p, (char *)&flags, 4);
409 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200410
411 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
414 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
415 * and "capabilities" */
416
417 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100418 sz = SLEN(SUPPORTED_VERSIONS_KEY);
419 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
420 goto too_big;
421
422 *p++ = SPOE_DATA_T_STR;
423 sz = SLEN(SUPPORTED_VERSIONS_VAL);
424 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
425 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200426
427 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 sz = SLEN(MAX_FRAME_SIZE_KEY);
429 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
430 goto too_big;
431
432 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200433 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435
436 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100437 sz = SLEN(CAPABILITIES_KEY);
438 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
439 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200440
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100442 chk = get_trash_chunk();
443 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200444 memcpy(chk->area, "pipelining", 10);
445 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100446 }
447 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200448 if (chk->data) chk->area[chk->data++] = ',';
449 memcpy(chk->area+chk->data, "async", 5);
450 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100451 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100452 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 if (chk->data) chk->area[chk->data++] = ',';
454 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100455 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100456 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 goto too_big;
459
460 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100461 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 sz = SLEN(ENGINE_ID_KEY);
463 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
464 goto too_big;
465
466 *p++ = SPOE_DATA_T_STR;
467 sz = strlen(agent->engine_id);
468 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
469 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100470 }
471
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 return (p - frame);
473
474 too_big:
475 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
476 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477}
478
Christopher Faulet8ef75252017-02-20 22:56:03 +0100479/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
480 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
481 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 const char *reason;
486 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100487 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 p = frame;
491 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 /* Set Frame type */
494 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100496 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200497 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100498 memcpy(p, (char *)&flags, 4);
499 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200500
501 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 *p++ = 0; *p++ = 0;
503
504 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
505 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
507 /* There are 2 mandatory items: "status-code" and "message" */
508
509 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100510 sz = SLEN(STATUS_CODE_KEY);
511 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
512 goto too_big;
513
514 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200515 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517
518 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100519 sz = SLEN(MSG_KEY);
520 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
521 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 /*Get the message corresponding to the status code */
524 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
525
526 *p++ = SPOE_DATA_T_STR;
527 sz = strlen(reason);
528 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
529 goto too_big;
530
531 return (p - frame);
532
533 too_big:
534 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
535 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536}
537
Christopher Faulet8ef75252017-02-20 22:56:03 +0100538/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
539 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
540 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200541static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100542spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100543 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200544{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100545 char *p, *end;
546 unsigned int stream_id, frame_id;
547 unsigned int flags = SPOE_FRM_FL_FIN;
548 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200549
Christopher Faulet8ef75252017-02-20 22:56:03 +0100550 p = frame;
551 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200552
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 stream_id = ctx->stream_id;
554 frame_id = ctx->frame_id;
555
556 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
557 /* The fragmentation is not supported by the applet */
558 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
559 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
560 return -1;
561 }
562 flags = ctx->frag_ctx.flags;
563 }
564
565 /* Set Frame type */
566 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
567
568 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200569 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100570 memcpy(p, (char *)&flags, 4);
571 p += 4;
572
573 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200574 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100575 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200576 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100577 goto too_big;
578
579 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200580 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100581 if (p + sz >= end)
582 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200583 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100584 p += sz;
585
586 return (p - frame);
587
588 too_big:
589 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
590 return 0;
591}
592
593/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
594 * returns the number of encoded bytes in the frame on success, 0 if an encoding
595 * error occurred and -1 if a fatal error occurred. */
596static int
597spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
598 char *frame, size_t size)
599{
600 char *p, *end;
601 unsigned int stream_id, frame_id;
602 unsigned int flags;
603 size_t sz;
604
605 p = frame;
606 end = frame+size;
607
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 /* <ctx> is null when the stream has aborted the processing of a
609 * fragmented frame. In this case, we must notify the corresponding
610 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100611 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
614 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
615 }
616 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100617 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100618 stream_id = ctx->stream_id;
619 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100620 }
621
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100623 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100625 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200626 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 memcpy(p, (char *)&flags, 4);
628 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200629
630 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200631 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200633 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100636 if (ctx == NULL)
637 goto end;
638
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200640 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641 if (p + sz >= end)
642 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200643 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100645
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100646 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 return (p - frame);
648
649 too_big:
650 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
651 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200652}
653
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654/* Decode and process the HELLO frame sent by an agent. It returns the number of
655 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
656 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200657static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659{
Christopher Faulet305c6072017-02-23 16:17:53 +0100660 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
661 char *p, *end;
662 int vsn, max_frame_size;
663 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664
665 p = frame;
666 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667
668 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200673
Christopher Faulet8ef75252017-02-20 22:56:03 +0100674 if (size < 7 /* TYPE + METADATA */) {
675 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
676 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677 }
678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100679 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200681 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100684 /* Fragmentation is not supported for HELLO frame */
685 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100687 return -1;
688 }
689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 if (*p != 0 || *(p+1) != 0) {
692 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
693 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696
697 /* There are 3 mandatory items: "version", "max-frame-size" and
698 * "capabilities" */
699
700 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100701 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 while (p < end) {
703 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200704 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200706
707 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708 ret = spoe_decode_buffer(&p, end, &str, &sz);
709 if (ret == -1 || !sz) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714 /* Check "version" K/V item */
715 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 int i, type = *p++;
717
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100719 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
721 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
727
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200731 return -1;
732 }
733 for (i = 0; supported_versions[i].str != NULL; ++i) {
734 if (vsn >= supported_versions[i].min &&
735 vsn <= supported_versions[i].max)
736 break;
737 }
738 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200740 return -1;
741 }
742 }
743 /* Check "max-frame-size" K/V item */
744 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200746
747 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
749 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
750 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
751 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
753 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200754 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200755 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
757 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200758 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 if (sz < MIN_FRAME_SIZE ||
760 sz > SPOE_APPCTX(appctx)->max_frame_size) {
761 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200762 return -1;
763 }
764 max_frame_size = sz;
765 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766 /* Check "capabilities" K/V item */
767 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100768 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769
770 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100771 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
772 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
773 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
776 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
777 return 0;
778 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781 char *delim;
782
783 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
787 str += 10; sz -= 10;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 flags |= SPOE_APPCTX_FL_PIPELINING;
790 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 else if (sz >= 5 && !strncmp(str, "async", 5)) {
792 str += 5; sz -= 5;
793 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100794 flags |= SPOE_APPCTX_FL_ASYNC;
795 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100796 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
797 str += 13; sz -= 13;
798 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100799 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
800 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100801
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 /* Get the next comma or break */
803 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100804 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100805 delim++;
806 sz -= (delim - str);
807 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100808 }
809 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 else {
811 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812 if (spoe_skip_data(&p, end) == -1) {
813 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
814 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816 }
817 }
818
819 /* Final checks */
820 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822 return -1;
823 }
824 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 return -1;
827 }
Christopher Faulet11389012019-02-07 16:13:26 +0100828 if (!agent)
829 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
830 else {
831 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
832 flags &= ~SPOE_APPCTX_FL_PIPELINING;
833 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
834 flags &= ~SPOE_APPCTX_FL_ASYNC;
835 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836
Christopher Faulet42bfa462017-01-04 14:14:19 +0100837 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
838 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
839 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840
841 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842}
843
844/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
845 * bytes on success, 0 if the frame can be ignored and -1 if an error
846 * occurred. */
847static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852
853 p = frame;
854 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
856 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
858 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200861
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 if (size < 7 /* TYPE + METADATA */) {
863 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
864 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865 }
866
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100867 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200869 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100872 /* Fragmentation is not supported for DISCONNECT frame */
873 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100875 return -1;
876 }
877
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 if (*p != 0 || *(p+1) != 0) {
880 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
881 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884
885 /* There are 2 mandatory items: "status-code" and "message" */
886
887 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100888 while (p < end) {
889 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200890 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892
893 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 ret = spoe_decode_buffer(&p, end, &str, &sz);
895 if (ret == -1 || !sz) {
896 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
897 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 }
899
900 /* Check "status-code" K/V item */
901 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903
904 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
906 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
907 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
908 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
910 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200912 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
914 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
918
919 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 else if (!memcmp(str, MSG_KEY, sz)) {
921 int type = *p++;
922
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
925 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
926 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 ret = spoe_decode_buffer(&p, end, &str, &sz);
929 if (ret == -1 || sz > 255) {
930 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
931 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
934 SPOE_APPCTX(appctx)->reason = str;
935 SPOE_APPCTX(appctx)->rlen = sz;
936#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 }
938 else {
939 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940 if (spoe_skip_data(&p, end) == -1) {
941 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
942 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944 }
945 }
946
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948}
949
950
Christopher Fauleta1cda022016-12-21 08:58:06 +0100951/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 * success, 0 if the frame can be ignored and -1 if an error occurred. */
953static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100955 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200956{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962
963 p = frame;
964 end = frame + size;
965 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966
967 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200972
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 if (size < 7 /* TYPE + METADATA */) {
974 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
975 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976 }
977
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200980 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200982
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100983 /* Fragmentation is not supported for now */
984 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100986 return -1;
987 }
988
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200989 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200990 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100991 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100992 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100993 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200994 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100995 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200996 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100997 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100998
Christopher Faulet8ef75252017-02-20 22:56:03 +0100999 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +01001000 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +02001001 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001002 if ((*ctx)->stream_id == (unsigned int)stream_id &&
1003 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004 goto found;
1005 }
1006 }
1007 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1009 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001011 goto found;
1012 }
1013 }
1014
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001015 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1016 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1017 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1018
1019 /* ABRT bit is set for an unfinished fragmented frame */
1020 if (flags & SPOE_FRM_FL_ABRT) {
1021 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001022 (*ctx)->state = SPOE_CTX_ST_ERROR;
1023 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1024 /* Ignore the payload */
1025 goto end;
1026 }
1027 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1028 /* For now, we ignore the ack */
1029 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1030 return 0;
1031 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001032
Christopher Fauleta1cda022016-12-21 08:58:06 +01001033 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001034 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1035 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036 " - stream-id=%u - frame-id=%u\n",
1037 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1038 __FUNCTION__, appctx,
1039 (unsigned int)stream_id, (unsigned int)frame_id);
1040
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001041 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001042 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1043 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044 return 0;
1045
1046 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1048 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001049 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001050 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001052
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001053 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001055 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1056 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001057 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001058
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001059 /* Transfer the buffer ownership to the SPOE context */
1060 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001061 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001062
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 (*ctx)->state = SPOE_CTX_ST_DONE;
1064
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001065 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001066 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 " - ACK frame received"
1068 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001069 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1071 (*ctx)->frame_id, flags);
1072 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001073}
1074
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075/* This function is used in cfgparse.c and declared in proto/checks.h. It
1076 * prepare the request to send to agents during a healthcheck. It returns 0 on
1077 * success and -1 if an error occurred. */
1078int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001081 struct appctx appctx;
1082 struct spoe_appctx spoe_appctx;
1083 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1084 size_t sz;
1085 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet42bfa462017-01-04 14:14:19 +01001087 memset(&appctx, 0, sizeof(appctx));
1088 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001090
1091 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001092 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1095 end = frame + MAX_FRAME_SIZE;
1096
1097 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1098 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001100 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102 /* Add "healthcheck" K/V item */
1103 sz = SLEN(HEALTHCHECK_KEY);
1104 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1105 return -1;
1106 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108 *len = frame - buf;
1109 sz = htonl(*len - 4);
1110 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet8ef75252017-02-20 22:56:03 +01001112 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001113 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115 return 0;
1116}
1117
1118/* This function is used in checks.c and declared in proto/checks.h. It decode
1119 * the response received from an agent during a healthcheck. It returns 0 on
1120 * success and -1 if an error occurred. */
1121int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001123{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001124 struct appctx appctx;
1125 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126
Christopher Faulet42bfa462017-01-04 14:14:19 +01001127 memset(&appctx, 0, sizeof(appctx));
1128 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001129
Christopher Faulet42bfa462017-01-04 14:14:19 +01001130 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001131 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001132
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1134 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001135 goto error;
1136 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001137 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1138 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001139
1140 return 0;
1141
1142 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1144 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1145 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001146 return -1;
1147}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001148
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1150 * the frame can be ignored, 1 to retry later, and the frame legnth on
1151 * success. */
1152static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154{
1155 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156 int ret;
1157 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1160 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 netint = htonl(framesz);
1162 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001163 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001165 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001166 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168 }
1169 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170 return -1; /* error */
1171 }
1172 return framesz;
1173}
1174
1175/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1176 * when the frame can be ignored, 1 to retry later and the frame length on
1177 * success. */
1178static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001179spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180{
1181 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 int ret;
1183 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184
Willy Tarreau06d80a92017-10-19 14:32:15 +02001185 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001186 if (ret > 0) {
1187 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001188 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001189 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001190 return -1;
1191 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001192 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001193 }
1194 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001195 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001196 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197 }
1198 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001199 return -1; /* error */
1200 }
1201 return framesz;
1202}
1203
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204/********************************************************************
1205 * Functions that manage the SPOE applet
1206 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001207static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001209{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001210 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001211 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001212 appctx_wakeup(appctx);
1213 return 1;
1214}
1215
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216/* Callback function that catches applet timeouts. If a timeout occurred, we set
1217 * <appctx->st1> flag and the SPOE applet is woken up. */
1218static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001219spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001221 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001222
1223 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1224 if (tick_is_expired(task->expire, now_ms)) {
1225 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001227 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001228 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001229 return task;
1230}
1231
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001232/* Callback function that releases a SPOE applet. This happens when the
1233 * connection with the agent is closed. */
1234static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001236{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 struct stream_interface *si = appctx->owner;
1238 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1239 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241
1242 if (spoe_appctx == NULL)
1243 return;
1244
1245 appctx->ctx.spoe.ptr = NULL;
1246 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247
1248 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1249 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1250 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251
Christopher Faulet8ef75252017-02-20 22:56:03 +01001252 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001253 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001254 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1256 LIST_DEL(&spoe_appctx->list);
1257 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001259 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001262 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001263 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1264 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001265 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001266 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001268 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1270 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001271
1272 si_shutw(si);
1273 si_shutr(si);
1274 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 }
1276
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 /* Destroy the task attached to this applet */
1278 if (spoe_appctx->task) {
Olivier Houchard3f795f72019-04-17 22:51:06 +02001279 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001280 }
1281
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 /* Notify all waiting streams */
1283 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001284 LIST_DEL(&ctx->list);
1285 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001286 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001287 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001288 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001290 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001291 }
1292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* If the applet was processing a fragmented frame, notify the
1294 * corresponding stream. */
1295 if (spoe_appctx->frag_ctx.ctx) {
1296 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001297 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001298 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001299 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001300 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1301 }
1302
Christopher Faulet8ef75252017-02-20 22:56:03 +01001303 /* Release allocated memory */
1304 spoe_release_buffer(&spoe_appctx->buffer,
1305 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001306 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001307
Christopher Faulet24289f22017-09-25 14:48:02 +02001308 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001309 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001310
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001312 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001313 LIST_DEL(&ctx->list);
1314 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001315 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001316 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001318 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001320 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001321 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 LIST_DEL(&ctx->list);
1323 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001324 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001325 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001326 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001327 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001328 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1329 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001330
1331 end:
1332 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001333 agent->rt[tid].frame_size = agent->max_frame_size;
1334 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1335 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001336}
1337
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001339spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001340{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001342 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001343 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001344 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001345
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 if (si->state <= SI_ST_CON) {
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001347 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001348 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1349 goto stop;
1350 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001351 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001352 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001354 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001355
Christopher Fauleta1cda022016-12-21 08:58:06 +01001356 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001357 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1358 " - Connection timed out\n",
1359 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1360 __FUNCTION__, appctx);
1361 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001362 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001363 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001364
Christopher Faulet42bfa462017-01-04 14:14:19 +01001365 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001366 SPOE_APPCTX(appctx)->task->expire =
1367 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368
Christopher Faulet8ef75252017-02-20 22:56:03 +01001369 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1370 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001371 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001372 ret = spoe_prepare_hahello_frame(appctx, frame,
1373 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001376
1377 switch (ret) {
1378 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001379 case 0: /* ignore => an error, cannot be ignored */
1380 goto exit;
1381
1382 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 goto stop;
1384
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 default:
1386 /* HELLO frame successfully sent, now wait for the
1387 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1389 goto next;
1390 }
1391
1392 next:
1393 return 0;
1394 stop:
1395 return 1;
1396 exit:
1397 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1398 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399}
1400
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001402spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001405 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406 char *frame;
1407 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001408
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001409
Christopher Fauletb067b062017-01-04 16:39:11 +01001410 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001416 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1417 " - Connection timed out\n",
1418 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1419 __FUNCTION__, appctx);
1420 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001421 goto exit;
1422 }
1423
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001424 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 ret = spoe_recv_frame(appctx, frame,
1426 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001427 if (ret > 1) {
1428 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1429 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1430 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001432 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001433 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001434 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 switch (ret) {
1437 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1440 goto next;
1441
1442 case 1: /* retry later */
1443 goto stop;
1444
1445 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001446 /* HELLO handshake is finished, set the idle timeout and
1447 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001448 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001449 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001450 SPOE_APPCTX(appctx)->node.key = 0;
1451 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001452
1453 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001454 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001456 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457
Christopher Fauleta1cda022016-12-21 08:58:06 +01001458 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001459 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001460 if (trash.data)
1461 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001462
1463 SPOE_APPCTX(appctx)->task->expire =
1464 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001465 return 0;
1466 stop:
1467 return 1;
1468 exit:
1469 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1470 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001471}
1472
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473
Christopher Fauleta1cda022016-12-21 08:58:06 +01001474static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001476{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1478 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 char *frame, *buf;
1480 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1483 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001484 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485
1486 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1487 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1488 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1489 SPOE_APPCTX(appctx)->max_frame_size);
1490 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001491 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001492 *skip = 1;
1493 ret = 1;
1494 goto end;
1495 }
1496 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001497 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001498 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1499 SPOE_APPCTX(appctx)->max_frame_size);
1500
1501 }
1502
Christopher Faulet8ef75252017-02-20 22:56:03 +01001503 if (ret > 1)
1504 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001505
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 switch (ret) {
1507 case -1: /* error */
1508 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1509 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001510
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 case 0: /* ignore */
1512 if (ctx == NULL)
1513 goto abort_frag_frame;
1514
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001515 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001516 LIST_DEL(&ctx->list);
1517 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001518 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001519 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001520 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001521 ctx->state = SPOE_CTX_ST_ERROR;
1522 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1523 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001524 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 break;
1526
1527 case 1: /* retry */
1528 *skip = 1;
1529 break;
1530
1531 default:
1532 if (ctx == NULL)
1533 goto abort_frag_frame;
1534
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001535 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 LIST_DEL(&ctx->list);
1537 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001538 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001539 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001540 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1542 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1543 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001544 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 }
1547 goto end;
1548
1549 frag_frame_sent:
1550 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001551 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001552 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1553 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1554 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1556 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1557 goto end;
1558
1559 no_frag_frame_sent:
1560 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1561 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001562 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001563 }
1564 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1565 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1566 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1567 }
1568 else {
1569 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001570 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001571 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1572 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001573 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001574 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1576 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1577 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001578 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579
Christopher Faulet8ef75252017-02-20 22:56:03 +01001580 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1581 goto end;
1582
1583 abort_frag_frame:
1584 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1585 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1586 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1587 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1588 goto end;
1589
1590 end:
1591 return ret;
1592}
1593
1594static int
1595spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1596{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001597 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 struct spoe_context *ctx = NULL;
1599 char *frame;
1600 int ret;
1601
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001602 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001603 ret = spoe_recv_frame(appctx, frame,
1604 SPOE_APPCTX(appctx)->max_frame_size);
1605 if (ret > 1) {
1606 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1607 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001608 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001609 goto end;
1610 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001611 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001612 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1613 }
1614 switch (ret) {
1615 case -1: /* error */
1616 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1617 break;
1618
1619 case 0: /* ignore */
1620 break;
1621
1622 case 1: /* retry */
1623 *skip = 1;
1624 break;
1625
1626 default:
1627 LIST_DEL(&ctx->list);
1628 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001629 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001630 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1631 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001632 if (ctx->spoe_appctx) {
1633 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001634 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001635 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001636 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1637 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1638 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1639 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1640 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1641 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1642 }
1643 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1644 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001645 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1646 break;
1647 }
1648
1649 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001650 if (trash.data)
1651 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652 end:
1653 return ret;
1654}
1655
1656static int
1657spoe_handle_processing_appctx(struct appctx *appctx)
1658{
1659 struct stream_interface *si = appctx->owner;
1660 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001661 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001662
1663 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1664 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1665 goto exit;
1666 }
1667
1668 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1669 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1670 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1671 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1672 goto next;
1673 }
1674
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001675 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001676 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001677 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1679 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001680 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001681
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1683 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001684
Christopher Faulet8f82b202018-01-24 16:23:03 +01001685 /* receiving_frame loop */
1686 while (!skip_receiving) {
1687 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1688 switch (ret) {
1689 case -1: /* error */
1690 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001691
Christopher Faulet8f82b202018-01-24 16:23:03 +01001692 case 0: /* ignore */
1693 active_r = 1;
1694 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001695
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 case 1: /* retry */
1697 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001698
Christopher Faulet8f82b202018-01-24 16:23:03 +01001699 default:
1700 active_r = 1;
1701 break;
1702 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001703 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 /* send_frame loop */
1706 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1707 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1708 switch (ret) {
1709 case -1: /* error */
1710 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001711
Christopher Faulet8f82b202018-01-24 16:23:03 +01001712 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001713 if (SPOE_APPCTX(appctx)->node.key)
1714 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 active_s++;
1716 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717
Christopher Faulet8f82b202018-01-24 16:23:03 +01001718 case 1: /* retry */
1719 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720
Christopher Faulet8f82b202018-01-24 16:23:03 +01001721 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001722 if (SPOE_APPCTX(appctx)->node.key)
1723 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 active_s++;
1725 break;
1726 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001727 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001730 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1731 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1732 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001733
Christopher Faulet8f82b202018-01-24 16:23:03 +01001734 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001735 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001736 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001737 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001738 }
1739 return 1;
1740
Christopher Faulet8f82b202018-01-24 16:23:03 +01001741 next:
1742 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1743 return 0;
1744
Christopher Fauleta1cda022016-12-21 08:58:06 +01001745 exit:
1746 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1747 return 0;
1748}
1749
1750static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001751spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001752{
1753 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001754 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 char *frame, *buf;
1756 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001757
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1759 goto exit;
1760
1761 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1762 goto exit;
1763
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1765 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001766 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001767 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1768 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001770 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771
1772 switch (ret) {
1773 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001774 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001775 goto exit;
1776
1777 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 goto stop;
1779
1780 default:
1781 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1782 " - disconnected by HAProxy (%d): %s\n",
1783 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001784 __FUNCTION__, appctx,
1785 SPOE_APPCTX(appctx)->status_code,
1786 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787
1788 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1789 goto next;
1790 }
1791
1792 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001793 SPOE_APPCTX(appctx)->task->expire =
1794 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 return 0;
1796 stop:
1797 return 1;
1798 exit:
1799 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1800 return 0;
1801}
1802
1803static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805{
1806 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001807 char *frame;
1808 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809
Christopher Fauletb067b062017-01-04 16:39:11 +01001810 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001813 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814
Christopher Fauletb067b062017-01-04 16:39:11 +01001815 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001816 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001818 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001820 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 ret = spoe_recv_frame(appctx, frame,
1822 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001824 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 }
1827
1828 switch (ret) {
1829 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1831 " - error on frame (%s)\n",
1832 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001833 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001835 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836 goto exit;
1837
1838 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001839 goto next;
1840
1841 case 1: /* retry */
1842 goto stop;
1843
1844 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001846 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001848 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001849 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1850 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 goto exit;
1852 }
1853
1854 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001855 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001856 if (trash.data)
1857 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001858
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 return 0;
1860 stop:
1861 return 1;
1862 exit:
1863 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1864 return 0;
1865}
1866
1867/* I/O Handler processing messages exchanged with the agent */
1868static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001869spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 struct stream_interface *si = appctx->owner;
1872 struct spoe_agent *agent;
1873
1874 if (SPOE_APPCTX(appctx) == NULL)
1875 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876
Christopher Faulet8ef75252017-02-20 22:56:03 +01001877 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1878 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001879
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880 switchstate:
1881 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1882 " - appctx-state=%s\n",
1883 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1884 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1885
1886 switch (appctx->st0) {
1887 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001888 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 goto out;
1890 goto switchstate;
1891
1892 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 goto out;
1895 goto switchstate;
1896
1897 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001898 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001899 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001901 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001902 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903 SPOE_APPCTX(appctx)->task->expire =
1904 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001905 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001906 goto switchstate;
1907 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001908 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1909 /* fall through */
1910
1911 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001912 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1913 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 goto out;
1916 goto switchstate;
1917
1918 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920 goto out;
1921 goto switchstate;
1922
1923 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001924 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001925 goto out;
1926 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001927
1928 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001929 appctx->st0 = SPOE_APPCTX_ST_END;
1930 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1931
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932 si_shutw(si);
1933 si_shutr(si);
1934 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001935 /* fall through */
1936
1937 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001938 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001940 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001941 if (stopping)
1942 spoe_wakeup_appctx(appctx);
1943
Christopher Faulet42bfa462017-01-04 14:14:19 +01001944 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1945 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001946 si_oc(si)->flags |= CF_READ_DONTWAIT;
1947 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948}
1949
1950struct applet spoe_applet = {
1951 .obj_type = OBJ_TYPE_APPLET,
1952 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001953 .fct = spoe_handle_appctx,
1954 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955};
1956
1957/* Create a SPOE applet. On success, the created applet is returned, else
1958 * NULL. */
1959static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001960spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961{
1962 struct appctx *appctx;
1963 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001964 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965
Emeric Brun1138fd02017-06-19 12:38:55 +02001966 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 goto out_error;
1968
Willy Tarreaubafbe012017-11-24 17:34:44 +01001969 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001970 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001972 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001973
Christopher Faulet42bfa462017-01-04 14:14:19 +01001974 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001975 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001976 goto out_free_spoe_appctx;
1977
1978 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001979 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001980 SPOE_APPCTX(appctx)->task->context = appctx;
1981 SPOE_APPCTX(appctx)->agent = conf->agent;
1982 SPOE_APPCTX(appctx)->version = 0;
1983 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1984 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001985 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001986 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001987 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001988
1989 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1990 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001991 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001992
1993 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1994 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995
Willy Tarreau5820a362016-12-22 15:59:02 +01001996 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 if (!sess)
1998 goto out_free_spoe;
1999
Willy Tarreau87787ac2017-08-28 16:22:54 +02002000 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002001 goto out_free_sess;
2002
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003 stream_set_backend(strm, conf->agent->b.be);
2004
2005 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002006 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002007 appctx_wakeup(appctx);
2008
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009 strm->do_log = NULL;
2010 strm->res.flags |= CF_READ_DONTWAIT;
2011
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002012 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002013 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002014 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002015 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002016
Emeric Brunc60def82017-09-27 14:59:38 +02002017 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002018 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002019 return appctx;
2020
2021 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022 out_free_sess:
2023 session_free(sess);
2024 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002025 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002026 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002027 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028 out_free_appctx:
2029 appctx_free(appctx);
2030 out_error:
2031 return NULL;
2032}
2033
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002034static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002035spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036{
2037 struct spoe_config *conf = FLT_CONF(ctx->filter);
2038 struct spoe_agent *agent = conf->agent;
2039 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002040 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002043 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002044 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002045 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002046
2047 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002048 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2050 ctx->strm);
2051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 /* Do not try to create a new applet if there is no server up for the
2053 * agent's backend. */
2054 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2055 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2056 " - cannot create SPOE appctx: no server up\n",
2057 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2058 __FUNCTION__, ctx->strm);
2059 goto end;
2060 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002061
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 /* Do not try to create a new applet if we have reached the maximum of
2063 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002064 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002065 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002066 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2067 " - cannot create SPOE appctx: max CPS reached\n",
2068 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2069 __FUNCTION__, ctx->strm);
2070 goto end;
2071 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072 }
2073
Christopher Faulet8ef75252017-02-20 22:56:03 +01002074 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002075 if (appctx == NULL) {
2076 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2077 " - failed to create SPOE appctx\n",
2078 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2079 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002080 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002081 "SPOE: [%s] failed to create SPOE applet\n",
2082 agent->id);
2083
Christopher Fauleta1cda022016-12-21 08:58:06 +01002084 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085 }
2086
Christopher Fauleta1cda022016-12-21 08:58:06 +01002087 /* Increase the per-process number of cumulated connections */
2088 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002089 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002090
Christopher Fauleta1cda022016-12-21 08:58:06 +01002091 end:
2092 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002093 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002094 ctx->status_code = SPOE_CTX_ERR_RES;
2095 return -1;
2096 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002097
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002098 /* Add the SPOE context in the sending queue if the stream has no applet
2099 * already assigned and wakeup all idle applets. Otherwise, don't queue
2100 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002101 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002102 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2103 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002104 if (ctx->spoe_appctx)
2105 return 1;
2106 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107
2108 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002109 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002110 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002111 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002112 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002113 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002114
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002115 /* Finally try to wakeup an IDLE applet. */
2116 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2117 struct eb32_node *node;
2118
2119 node = eb32_first(&agent->rt[tid].idle_applets);
2120 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2121 if (node && spoe_appctx) {
2122 eb32_delete(&spoe_appctx->node);
2123 spoe_appctx->node.key++;
2124 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2125 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002126 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002127 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002128 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002129}
2130
2131/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002132 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002133 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002134/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2135 * fragmented_content. If the next message can be processed, it returns 0. If
2136 * the message is too big, it returns -1.*/
2137static int
2138spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2139 struct spoe_message *msg, int dir,
2140 char **buf, char *end)
2141{
2142 struct sample *smp;
2143 struct spoe_arg *arg;
2144 int ret;
2145
2146 if (msg->cond) {
2147 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2148 ret = acl_pass(ret);
2149 if (msg->cond->pol == ACL_COND_UNLESS)
2150 ret = !ret;
2151
2152 /* the rule does not match */
2153 if (!ret)
2154 goto next;
2155 }
2156
2157 /* Resume encoding of a SPOE argument */
2158 if (ctx->frag_ctx.curarg != NULL) {
2159 arg = ctx->frag_ctx.curarg;
2160 goto encode_argument;
2161 }
2162
2163 if (ctx->frag_ctx.curoff != UINT_MAX)
2164 goto encode_msg_payload;
2165
2166 /* Check if there is enough space for the message name and the
2167 * number of arguments. It implies <msg->id_len> is encoded on 2
2168 * bytes, at most (< 2288). */
2169 if (*buf + 2 + msg->id_len + 1 > end)
2170 goto too_big;
2171
2172 /* Encode the message name */
2173 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2174 goto too_big;
2175
2176 /* Set the number of arguments for this message */
2177 **buf = msg->nargs;
2178 (*buf)++;
2179
2180 ctx->frag_ctx.curoff = 0;
2181 encode_msg_payload:
2182
2183 /* Loop on arguments */
2184 list_for_each_entry(arg, &msg->args, list) {
2185 ctx->frag_ctx.curarg = arg;
2186 ctx->frag_ctx.curoff = UINT_MAX;
2187
2188 encode_argument:
2189 if (ctx->frag_ctx.curoff != UINT_MAX)
2190 goto encode_arg_value;
2191
Joseph Herlantf7f60312018-11-15 13:46:49 -08002192 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002193 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2194 goto too_big;
2195
2196 ctx->frag_ctx.curoff = 0;
2197 encode_arg_value:
2198
Joseph Herlantf7f60312018-11-15 13:46:49 -08002199 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002200 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2201 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2202 if (ret == -1 || ctx->frag_ctx.curoff)
2203 goto too_big;
2204 }
2205
2206 next:
2207 return 0;
2208
2209 too_big:
2210 return -1;
2211}
2212
Christopher Fauletc718b822017-09-21 16:50:56 +02002213/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2214 * handle fragmented content. On success it returns 1. If an error occurred, -1
2215 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2216 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002217static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002218spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002219 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002220{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002221 struct spoe_config *conf = FLT_CONF(ctx->filter);
2222 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002223 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002224 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002225
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002226 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002227 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002228
Christopher Fauletc718b822017-09-21 16:50:56 +02002229 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2230 /* Resume encoding of a SPOE message */
2231 if (ctx->frag_ctx.curmsg != NULL) {
2232 msg = ctx->frag_ctx.curmsg;
2233 goto encode_evt_message;
2234 }
2235
2236 list_for_each_entry(msg, messages, by_evt) {
2237 ctx->frag_ctx.curmsg = msg;
2238 ctx->frag_ctx.curarg = NULL;
2239 ctx->frag_ctx.curoff = UINT_MAX;
2240
2241 encode_evt_message:
2242 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2243 goto too_big;
2244 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002246 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2247 /* Resume encoding of a SPOE message */
2248 if (ctx->frag_ctx.curmsg != NULL) {
2249 msg = ctx->frag_ctx.curmsg;
2250 goto encode_grp_message;
2251 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252
Christopher Fauletc718b822017-09-21 16:50:56 +02002253 list_for_each_entry(msg, messages, by_grp) {
2254 ctx->frag_ctx.curmsg = msg;
2255 ctx->frag_ctx.curarg = NULL;
2256 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
Christopher Fauletc718b822017-09-21 16:50:56 +02002258 encode_grp_message:
2259 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2260 goto too_big;
2261 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002262 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002263 else
2264 goto skip;
2265
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266
Christopher Faulet57583e42017-09-04 15:41:09 +02002267 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002268 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002269 goto skip;
2270
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002271 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002272 " - encode %s messages - spoe_appctx=%p"
2273 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274 (int)now.tv_sec, (int)now.tv_usec,
2275 agent->id, __FUNCTION__, s,
2276 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002277 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002278 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002280 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281 ctx->frag_ctx.curmsg = NULL;
2282 ctx->frag_ctx.curarg = NULL;
2283 ctx->frag_ctx.curoff = 0;
2284 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002285
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002286 return 1;
2287
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002289 /* Return an error if fragmentation is unsupported or if nothing has
2290 * been encoded because its too big and not splittable. */
2291 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002292 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2293 return -1;
2294 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002295
2296 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002297 " - encode fragmented messages - spoe_appctx=%p"
2298 " - curmsg=%p - curarg=%p - curoff=%u"
2299 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002300 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002301 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002302 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002303 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002304
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002305 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002306 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2307 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2308 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002309
2310 skip:
2311 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2312 " - skip the frame because nothing has been encoded\n",
2313 (int)now.tv_sec, (int)now.tv_usec,
2314 agent->id, __FUNCTION__, s);
2315 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002316}
2317
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002318
2319/***************************************************************************
2320 * Functions that handle SPOE actions
2321 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002322/* Helper function to set a variable */
2323static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002325 struct sample *smp)
2326{
2327 struct spoe_config *conf = FLT_CONF(ctx->filter);
2328 struct spoe_agent *agent = conf->agent;
2329 char varname[64];
2330
2331 memset(varname, 0, sizeof(varname));
2332 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2333 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002334 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2335 vars_set_by_name(varname, len, smp);
2336 else
2337 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002338}
2339
2340/* Helper function to unset a variable */
2341static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343 struct sample *smp)
2344{
2345 struct spoe_config *conf = FLT_CONF(ctx->filter);
2346 struct spoe_agent *agent = conf->agent;
2347 char varname[64];
2348
2349 memset(varname, 0, sizeof(varname));
2350 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2351 scope, agent->var_pfx, len, name);
2352 vars_unset_by_name_ifexist(varname, len, smp);
2353}
2354
2355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356static inline int
2357spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2358 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 char *str, *scope, *p = *buf;
2361 struct sample smp;
2362 uint64_t sz;
2363 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 if (p + 2 >= end)
2366 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002367
Christopher Faulet8ef75252017-02-20 22:56:03 +01002368 /* SET-VAR requires 3 arguments */
2369 if (*p++ != 3)
2370 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 switch (*p++) {
2373 case SPOE_SCOPE_PROC: scope = "proc"; break;
2374 case SPOE_SCOPE_SESS: scope = "sess"; break;
2375 case SPOE_SCOPE_TXN : scope = "txn"; break;
2376 case SPOE_SCOPE_REQ : scope = "req"; break;
2377 case SPOE_SCOPE_RES : scope = "res"; break;
2378 default: goto skip;
2379 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2382 goto skip;
2383 memset(&smp, 0, sizeof(smp));
2384 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002385
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386 if (spoe_decode_data(&p, end, &smp) == -1)
2387 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002388
Christopher Faulet8ef75252017-02-20 22:56:03 +01002389 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2390 " - set-var '%s.%s.%.*s'\n",
2391 (int)now.tv_sec, (int)now.tv_usec,
2392 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2393 __FUNCTION__, s, scope,
2394 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2395 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002398
Christopher Faulet8ef75252017-02-20 22:56:03 +01002399 ret = (p - *buf);
2400 *buf = p;
2401 return ret;
2402 skip:
2403 return 0;
2404}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406static inline int
2407spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2408 char **buf, char *end, int dir)
2409{
2410 char *str, *scope, *p = *buf;
2411 struct sample smp;
2412 uint64_t sz;
2413 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
Christopher Faulet8ef75252017-02-20 22:56:03 +01002415 if (p + 2 >= end)
2416 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 /* UNSET-VAR requires 2 arguments */
2419 if (*p++ != 2)
2420 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 switch (*p++) {
2423 case SPOE_SCOPE_PROC: scope = "proc"; break;
2424 case SPOE_SCOPE_SESS: scope = "sess"; break;
2425 case SPOE_SCOPE_TXN : scope = "txn"; break;
2426 case SPOE_SCOPE_REQ : scope = "req"; break;
2427 case SPOE_SCOPE_RES : scope = "res"; break;
2428 default: goto skip;
2429 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002430
Christopher Faulet8ef75252017-02-20 22:56:03 +01002431 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2432 goto skip;
2433 memset(&smp, 0, sizeof(smp));
2434 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002435
Christopher Faulet8ef75252017-02-20 22:56:03 +01002436 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2437 " - unset-var '%s.%s.%.*s'\n",
2438 (int)now.tv_sec, (int)now.tv_usec,
2439 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2440 __FUNCTION__, s, scope,
2441 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2442 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443
Christopher Faulet8ef75252017-02-20 22:56:03 +01002444 spoe_unset_var(ctx, scope, str, sz, &smp);
2445
2446 ret = (p - *buf);
2447 *buf = p;
2448 return ret;
2449 skip:
2450 return 0;
2451}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002452
Christopher Faulet8ef75252017-02-20 22:56:03 +01002453/* Process SPOE actions for a specific event. It returns 1 on success. If an
2454 * error occurred, 0 is returned. */
2455static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002456spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002457{
2458 char *p, *end;
2459 int ret;
2460
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002461 p = b_head(&ctx->buffer);
2462 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002463
2464 while (p < end) {
2465 enum spoe_action_type type;
2466
2467 type = *p++;
2468 switch (type) {
2469 case SPOE_ACT_T_SET_VAR:
2470 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2471 if (!ret)
2472 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002473 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002474
Christopher Faulet8ef75252017-02-20 22:56:03 +01002475 case SPOE_ACT_T_UNSET_VAR:
2476 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2477 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002478 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002479 break;
2480
2481 default:
2482 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002483 }
2484 }
2485
2486 return 1;
2487 skip:
2488 return 0;
2489}
2490
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002491/***************************************************************************
2492 * Functions that process SPOE events
2493 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002494static void
2495spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2496 struct spoe_context *ctx, int dir)
2497{
2498 if (!tv_iszero(&ctx->stats.tv_start)) {
2499 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2500 ctx->stats.t_total += ctx->stats.t_process;
2501 tv_zero(&ctx->stats.tv_request);
2502 tv_zero(&ctx->stats.tv_queue);
2503 tv_zero(&ctx->stats.tv_wait);
2504 tv_zero(&ctx->stats.tv_response);
2505 }
2506
2507 if (agent->var_t_process) {
2508 struct sample smp;
2509
2510 memset(&smp, 0, sizeof(smp));
2511 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2512 smp.data.u.sint = ctx->stats.t_process;
2513 smp.data.type = SMP_T_SINT;
2514
2515 spoe_set_var(ctx, "txn", agent->var_t_process,
2516 strlen(agent->var_t_process), &smp);
2517 }
2518
2519 if (agent->var_t_total) {
2520 struct sample smp;
2521
2522 memset(&smp, 0, sizeof(smp));
2523 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2524 smp.data.u.sint = ctx->stats.t_total;
2525 smp.data.type = SMP_T_SINT;
2526
2527 spoe_set_var(ctx, "txn", agent->var_t_total,
2528 strlen(agent->var_t_total), &smp);
2529 }
2530}
2531
2532static void
2533spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2534 struct spoe_context *ctx, int dir)
2535{
2536 if (agent->eps_max > 0)
2537 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2538
2539 if (agent->var_on_error) {
2540 struct sample smp;
2541
2542 memset(&smp, 0, sizeof(smp));
2543 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2544 smp.data.u.sint = ctx->status_code;
2545 smp.data.type = SMP_T_BOOL;
2546
2547 spoe_set_var(ctx, "txn", agent->var_on_error,
2548 strlen(agent->var_on_error), &smp);
2549 }
2550
2551 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2552 ? SPOE_CTX_ST_READY
2553 : SPOE_CTX_ST_NONE);
2554}
2555
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002556static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002557spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002558{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559 /* If a process is already started for this SPOE context, retry
2560 * later. */
2561 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002562 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002564 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002565 ctx->stats.tv_start = now;
2566 ctx->stats.tv_request = now;
2567 ctx->stats.t_request = -1;
2568 ctx->stats.t_queue = -1;
2569 ctx->stats.t_waiting = -1;
2570 ctx->stats.t_response = -1;
2571 ctx->stats.t_process = -1;
2572
2573 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002574
Christopher Fauleta1cda022016-12-21 08:58:06 +01002575 /* Set the right flag to prevent request and response processing
2576 * in same time. */
2577 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2578 ? SPOE_CTX_FL_REQ_PROCESS
2579 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002580 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581}
2582
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002583static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002584spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002585{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002586 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002587
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002588 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2589 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002590 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002591 if (sa) {
2592 if (sa->frag_ctx.ctx == ctx) {
2593 sa->frag_ctx.ctx = NULL;
2594 spoe_wakeup_appctx(sa->owner);
2595 }
2596 else
2597 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002598 }
2599
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002601 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002602 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002603
2604 /* Reset processing timer */
2605 ctx->process_exp = TICK_ETERNITY;
2606
Christopher Faulet8ef75252017-02-20 22:56:03 +01002607 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002608
Christopher Fauletfce747b2018-01-24 15:59:32 +01002609 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002610 ctx->frag_ctx.curmsg = NULL;
2611 ctx->frag_ctx.curarg = NULL;
2612 ctx->frag_ctx.curoff = 0;
2613 ctx->frag_ctx.flags = 0;
2614
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002616 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002617 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002618 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002619 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002620
Christopher Fauleta1cda022016-12-21 08:58:06 +01002621 LIST_DEL(&ctx->list);
2622 LIST_INIT(&ctx->list);
2623 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002624}
2625
Christopher Faulet58d03682017-09-21 16:57:24 +02002626/* Process a list of SPOE messages. First, this functions will process messages
2627 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2628 * to process corresponding actions. During all the processing, it returns 0
2629 * and it returns 1 when the processing is finished. If an error occurred, -1
2630 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002631static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002632spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2633 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002634{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002635 struct spoe_config *conf = FLT_CONF(ctx->filter);
2636 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002637 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002638
2639 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002640 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002641
2642 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2643 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002645 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002646 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002647 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002648 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002649 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002650
2651 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002652 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002653 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002654 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002655 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002656 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002657 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002658 goto skip;
2659 }
2660 }
2661
Christopher Fauletf7a30922016-11-10 15:04:51 +01002662 if (!tick_isset(ctx->process_exp)) {
2663 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2664 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2665 ctx->process_exp);
2666 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002667 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002668 if (!ret)
2669 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002670
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002671 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002672 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002673 }
2674
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002675 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002676 if (tv_iszero(&ctx->stats.tv_request))
2677 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002678 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002679 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002680 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002681 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002682 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002683 if (!ret)
2684 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002685 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002686 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002687 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2688 }
2689
2690 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002691 if (ctx->spoe_appctx)
2692 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 ret = 0;
2694 goto out;
2695 }
2696
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002697 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2698 ret = 0;
2699 goto out;
2700 }
2701
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002702 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002703 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002704 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002705 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002706 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002707 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002708 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002709 }
2710
2711 out:
2712 return ret;
2713
Christopher Fauleta1cda022016-12-21 08:58:06 +01002714 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002715 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002716 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002717 spoe_stop_processing(agent, ctx);
2718 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002719
Christopher Fauleta1cda022016-12-21 08:58:06 +01002720 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002721 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002722 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002723 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002724 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002725 spoe_handle_processing_error(s, agent, ctx, dir);
2726 ret = 1;
2727 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002728 return ret;
2729}
2730
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002731/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2732 * See spoe_process_message for details. */
2733static int
2734spoe_process_group(struct stream *s, struct spoe_context *ctx,
2735 struct spoe_group *group, int dir)
2736{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002737 struct spoe_config *conf = FLT_CONF(ctx->filter);
2738 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002739 int ret;
2740
2741 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2742 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002743 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002744 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2745 group->id);
2746
2747 if (LIST_ISEMPTY(&group->messages))
2748 return 1;
2749
2750 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002751 if (ret && ctx->stats.t_process != -1) {
2752 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002753 " - <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 +01002754 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002755 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002756 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002757 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,
2761 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2762 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2763 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2764 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2765 agent->id, group->id, s->uniq_id, ctx->status_code,
2766 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2767 ctx->stats.t_response, ctx->stats.t_process,
2768 agent->counters.idles, agent->counters.applets,
2769 agent->counters.nb_sending, agent->counters.nb_waiting,
2770 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002771 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002772 return ret;
2773}
2774
Christopher Faulet58d03682017-09-21 16:57:24 +02002775/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2776 * See spoe_process_message for details. */
2777static int
2778spoe_process_event(struct stream *s, struct spoe_context *ctx,
2779 enum spoe_event ev)
2780{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002781 struct spoe_config *conf = FLT_CONF(ctx->filter);
2782 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002783 int dir, ret;
2784
2785 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002786 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002787 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002788 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2789 spoe_event_str[ev]);
2790
2791 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2792
2793 if (LIST_ISEMPTY(&(ctx->events[ev])))
2794 return 1;
2795
2796 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002797 if (ret && ctx->stats.t_process != -1) {
2798 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002799 " - <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 +01002800 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2801 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2802 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002803 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,
2807 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2808 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2809 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2810 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2811 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2812 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2813 ctx->stats.t_response, ctx->stats.t_process,
2814 agent->counters.idles, agent->counters.applets,
2815 agent->counters.nb_sending, agent->counters.nb_waiting,
2816 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002817 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002818 return ret;
2819}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002820
2821/***************************************************************************
2822 * Functions that create/destroy SPOE contexts
2823 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002824static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002825spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002826{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002827 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002828 return 1;
2829
Christopher Faulet4596fb72017-01-11 14:05:19 +01002830 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002831 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002832 LIST_DEL(&buffer_wait->list);
2833 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002834 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002835 }
2836
Christopher Faulet4596fb72017-01-11 14:05:19 +01002837 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002838 return 1;
2839
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_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002842 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002843 return 0;
2844}
2845
2846static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002848{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002849 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002850 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002851 LIST_DEL(&buffer_wait->list);
2852 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002853 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002854 }
2855
2856 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002857 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002858 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002859 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002860 }
2861}
2862
Christopher Faulet4596fb72017-01-11 14:05:19 +01002863static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002864spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002865{
2866 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2867 return 1;
2868}
2869
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002870static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002871spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872{
2873 struct spoe_config *conf = FLT_CONF(filter);
2874 struct spoe_context *ctx;
2875
Willy Tarreaubafbe012017-11-24 17:34:44 +01002876 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002877 if (ctx == NULL) {
2878 return NULL;
2879 }
2880 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002881 ctx->filter = filter;
2882 ctx->state = SPOE_CTX_ST_NONE;
2883 ctx->status_code = SPOE_CTX_ERR_NONE;
2884 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002885 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002886 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002887 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002888 LIST_INIT(&ctx->buffer_wait.list);
2889 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002890 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002891 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002892
Christopher Fauletf7a30922016-11-10 15:04:51 +01002893 ctx->stream_id = 0;
2894 ctx->frame_id = 1;
2895 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002897 tv_zero(&ctx->stats.tv_start);
2898 tv_zero(&ctx->stats.tv_request);
2899 tv_zero(&ctx->stats.tv_queue);
2900 tv_zero(&ctx->stats.tv_wait);
2901 tv_zero(&ctx->stats.tv_response);
2902 ctx->stats.t_request = -1;
2903 ctx->stats.t_queue = -1;
2904 ctx->stats.t_waiting = -1;
2905 ctx->stats.t_response = -1;
2906 ctx->stats.t_process = -1;
2907 ctx->stats.t_total = 0;
2908
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002909 ctx->strm = s;
2910 ctx->state = SPOE_CTX_ST_READY;
2911 filter->ctx = ctx;
2912
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913 return ctx;
2914}
2915
2916static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002917spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002918{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002919 struct spoe_config *conf = FLT_CONF(filter);
2920 struct spoe_context *ctx = filter->ctx;
2921
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002922 if (!ctx)
2923 return;
2924
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002925 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002926 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002927 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002928}
2929
2930static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002931spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002932{
2933 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002934 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002935
2936 tv_zero(&ctx->stats.tv_start);
2937 tv_zero(&ctx->stats.tv_request);
2938 tv_zero(&ctx->stats.tv_queue);
2939 tv_zero(&ctx->stats.tv_wait);
2940 tv_zero(&ctx->stats.tv_response);
2941 ctx->stats.t_request = -1;
2942 ctx->stats.t_queue = -1;
2943 ctx->stats.t_waiting = -1;
2944 ctx->stats.t_response = -1;
2945 ctx->stats.t_process = -1;
2946 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002947}
2948
2949
2950/***************************************************************************
2951 * Hooks that manage the filter lifecycle (init/check/deinit)
2952 **************************************************************************/
2953/* Signal handler: Do a soft stop, wakeup SPOE applet */
2954static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002955spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956{
2957 struct proxy *p;
2958
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002959 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002960 while (p) {
2961 struct flt_conf *fconf;
2962
2963 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002964 struct spoe_config *conf;
2965 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002966 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002967 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002968
Christopher Faulet3b386a32017-02-23 10:17:15 +01002969 if (fconf->id != spoe_filter_id)
2970 continue;
2971
2972 conf = fconf->conf;
2973 agent = conf->agent;
2974
Christopher Faulet24289f22017-09-25 14:48:02 +02002975 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002976 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002977 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2978 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002979 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980 }
2981 }
2982 p = p->next;
2983 }
2984}
2985
2986
2987/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2988static int
2989spoe_init(struct proxy *px, struct flt_conf *fconf)
2990{
2991 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002993 /* conf->agent_fe was already initialized during the config
2994 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996 conf->agent_fe.cap = PR_CAP_FE;
2997 conf->agent_fe.mode = PR_MODE_TCP;
2998 conf->agent_fe.maxconn = 0;
2999 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3000 conf->agent_fe.conn_retries = CONN_RETRIES;
3001 conf->agent_fe.accept = frontend_accept;
3002 conf->agent_fe.srv = NULL;
3003 conf->agent_fe.timeout.client = TICK_ETERNITY;
3004 conf->agent_fe.default_target = &spoe_applet.obj_type;
3005 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3006
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003008 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009 sighandler_registered = 1;
3010 }
3011
Christopher Faulet00292352019-01-09 15:05:10 +01003012 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014}
3015
3016/* Free ressources allocated by the SPOE filter. */
3017static void
3018spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3019{
3020 struct spoe_config *conf = fconf->conf;
3021
3022 if (conf) {
3023 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024
Christopher Faulet8ef75252017-02-20 22:56:03 +01003025 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003026 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003027 free(conf);
3028 }
3029 fconf->conf = NULL;
3030}
3031
3032/* Check configuration of a SPOE filter for a specified proxy.
3033 * Return 1 on error, else 0. */
3034static int
3035spoe_check(struct proxy *px, struct flt_conf *fconf)
3036{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003037 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 struct spoe_config *conf = fconf->conf;
3039 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003040 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003041
Christopher Faulet7ee86672017-09-19 11:08:28 +02003042 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3043 * are uniq */
3044 list_for_each_entry(f, &px->filter_configs, list) {
3045 struct spoe_config *c = f->conf;
3046
3047 /* This is not an SPOE filter */
3048 if (f->id != spoe_filter_id)
3049 continue;
3050 /* This is the current SPOE filter */
3051 if (f == fconf)
3052 continue;
3053
3054 /* Check engine Id. It should be uniq */
3055 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003056 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3057 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003058 return 1;
3059 }
3060 }
3061
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062 target = proxy_be_by_name(conf->agent->b.name);
3063 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003064 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3065 " declared at %s:%d.\n",
3066 px->id, conf->agent->b.name, conf->agent->id,
3067 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003068 return 1;
3069 }
3070 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003071 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3072 " at %s:%d does not support HTTP mode.\n",
3073 px->id, target->id, conf->agent->id,
3074 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003075 return 1;
3076 }
3077
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003078 if (px->bind_proc & ~target->bind_proc) {
3079 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3080 " at %s:%d does not cover all of its processes.\n",
3081 px->id, target->id, conf->agent->id,
3082 conf->agent->conf.file, conf->agent->conf.line);
3083 return 1;
3084 }
3085
Willy Tarreaub0769b22019-02-07 13:40:33 +01003086 /* finish per-thread agent initialization */
3087 if (global.nbthread == 1)
3088 conf->agent->flags |= SPOE_FL_ASYNC;
3089
Christopher Fauletfe261552019-03-18 13:57:42 +01003090 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003091 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3092 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3093 return 1;
3094 }
3095 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletfe261552019-03-18 13:57:42 +01003096 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3097 conf->agent->rt[i].processing = 0;
3098 LIST_INIT(&conf->agent->rt[i].applets);
3099 LIST_INIT(&conf->agent->rt[i].sending_queue);
3100 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3101 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003102 }
3103
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104 free(conf->agent->b.name);
3105 conf->agent->b.name = NULL;
3106 conf->agent->b.be = target;
3107 return 0;
3108}
3109
3110/**************************************************************************
3111 * Hooks attached to a stream
3112 *************************************************************************/
3113/* Called when a filter instance is created and attach to a stream. It creates
3114 * the context that will be used to process this stream. */
3115static int
3116spoe_start(struct stream *s, struct filter *filter)
3117{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003118 struct spoe_config *conf = FLT_CONF(filter);
3119 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003120 struct spoe_context *ctx;
3121
3122 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003123 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 __FUNCTION__, s);
3125
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003126 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003127 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3128 " - failed to create SPOE context\n",
3129 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003130 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003131 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003132 "SPOE: [%s] failed to create SPOE context\n",
3133 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003134 return 0;
3135 }
3136
Christopher Faulet11610f32017-09-21 10:23:10 +02003137 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003138 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3139
Christopher Faulet11610f32017-09-21 10:23:10 +02003140 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003141 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3142
Christopher Faulet11610f32017-09-21 10:23:10 +02003143 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003144 filter->pre_analyzers |= AN_RES_INSPECT;
3145
Christopher Faulet11610f32017-09-21 10:23:10 +02003146 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003147 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3148
Christopher Faulet11610f32017-09-21 10:23:10 +02003149 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003150 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3151
Christopher Faulet11610f32017-09-21 10:23:10 +02003152 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003153 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3154
3155 return 1;
3156}
3157
3158/* Called when a filter instance is detached from a stream. It release the
3159 * attached SPOE context. */
3160static void
3161spoe_stop(struct stream *s, struct filter *filter)
3162{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3164 (int)now.tv_sec, (int)now.tv_usec,
3165 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3166 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003167 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168}
3169
Christopher Fauletf7a30922016-11-10 15:04:51 +01003170
3171/*
3172 * Called when the stream is woken up because of expired timer.
3173 */
3174static void
3175spoe_check_timeouts(struct stream *s, struct filter *filter)
3176{
3177 struct spoe_context *ctx = filter->ctx;
3178
Christopher Fauletac580602018-03-20 16:09:20 +01003179 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003180 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003181}
3182
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003183/* Called when we are ready to filter data on a channel */
3184static int
3185spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3186{
3187 struct spoe_context *ctx = filter->ctx;
3188 int ret = 1;
3189
3190 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3191 " - ctx-flags=0x%08x\n",
3192 (int)now.tv_sec, (int)now.tv_usec,
3193 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3194 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3195
Christopher Fauletb067b062017-01-04 16:39:11 +01003196 if (ctx->state == SPOE_CTX_ST_NONE)
3197 goto out;
3198
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003199 if (!(chn->flags & CF_ISRESP)) {
3200 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3201 chn->analysers |= AN_REQ_INSPECT_FE;
3202 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3203 chn->analysers |= AN_REQ_INSPECT_BE;
3204
3205 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3206 goto out;
3207
3208 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003209 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003210 if (!ret)
3211 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003212 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3213 }
3214 else {
3215 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3216 chn->analysers |= AN_RES_INSPECT;
3217
3218 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3219 goto out;
3220
Christopher Faulet8ef75252017-02-20 22:56:03 +01003221 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003222 if (!ret) {
3223 channel_dont_read(chn);
3224 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003225 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003226 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003227 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 }
3229
3230 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003231 return ret;
3232}
3233
3234/* Called before a processing happens on a given channel */
3235static int
3236spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3237 struct channel *chn, unsigned an_bit)
3238{
3239 struct spoe_context *ctx = filter->ctx;
3240 int ret = 1;
3241
3242 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3243 " - ctx-flags=0x%08x - ana=0x%08x\n",
3244 (int)now.tv_sec, (int)now.tv_usec,
3245 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3246 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3247 ctx->flags, an_bit);
3248
Christopher Fauletb067b062017-01-04 16:39:11 +01003249 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 goto out;
3251
3252 switch (an_bit) {
3253 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003254 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 break;
3256 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003257 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003258 break;
3259 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003260 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003261 break;
3262 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003263 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003264 break;
3265 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003266 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003267 break;
3268 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003269 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003270 break;
3271 }
3272
3273 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003274 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003275 channel_dont_read(chn);
3276 channel_dont_close(chn);
3277 }
3278 return ret;
3279}
3280
3281/* Called when the filtering on the channel ends. */
3282static int
3283spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3284{
3285 struct spoe_context *ctx = filter->ctx;
3286
3287 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3288 " - ctx-flags=0x%08x\n",
3289 (int)now.tv_sec, (int)now.tv_usec,
3290 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3291 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3292
3293 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003294 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295 }
3296
3297 return 1;
3298}
3299
3300/********************************************************************
3301 * Functions that manage the filter initialization
3302 ********************************************************************/
3303struct flt_ops spoe_ops = {
3304 /* Manage SPOE filter, called for each filter declaration */
3305 .init = spoe_init,
3306 .deinit = spoe_deinit,
3307 .check = spoe_check,
3308
3309 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003310 .attach = spoe_start,
3311 .detach = spoe_stop,
3312 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003313
3314 /* Handle channels activity */
3315 .channel_start_analyze = spoe_start_analyze,
3316 .channel_pre_analyze = spoe_chn_pre_analyze,
3317 .channel_end_analyze = spoe_end_analyze,
3318};
3319
3320
3321static int
3322cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3323{
3324 const char *err;
3325 int i, err_code = 0;
3326
3327 if ((cfg_scope == NULL && curengine != NULL) ||
3328 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003329 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003330 goto out;
3331
3332 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3333 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003334 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3335 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003336 err_code |= ERR_ALERT | ERR_ABORT;
3337 goto out;
3338 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003339 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3340 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003341 goto out;
3342 }
3343
3344 err = invalid_char(args[1]);
3345 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003346 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3347 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003348 err_code |= ERR_ALERT | ERR_ABORT;
3349 goto out;
3350 }
3351
3352 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003353 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3354 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003355 err_code |= ERR_ALERT | ERR_ABORT;
3356 goto out;
3357 }
3358 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003359 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003360 err_code |= ERR_ALERT | ERR_ABORT;
3361 goto out;
3362 }
3363
3364 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003365
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366 curagent->conf.file = strdup(file);
3367 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003368
3369 curagent->timeout.hello = TICK_ETERNITY;
3370 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003371 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003372
3373 curagent->engine_id = NULL;
3374 curagent->var_pfx = NULL;
3375 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003376 curagent->var_t_process = NULL;
3377 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003378 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003379 curagent->cps_max = 0;
3380 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003381 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003382 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003383
3384 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003385 LIST_INIT(&curagent->events[i]);
3386 LIST_INIT(&curagent->groups);
3387 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003388 }
3389 else if (!strcmp(args[0], "use-backend")) {
3390 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003391 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3392 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003393 err_code |= ERR_ALERT | ERR_FATAL;
3394 goto out;
3395 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003396 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003397 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 free(curagent->b.name);
3399 curagent->b.name = strdup(args[1]);
3400 }
3401 else if (!strcmp(args[0], "messages")) {
3402 int cur_arg = 1;
3403 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003404 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405
Christopher Faulet11610f32017-09-21 10:23:10 +02003406 list_for_each_entry(ph, &curmphs, list) {
3407 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003408 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3409 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 err_code |= ERR_ALERT | ERR_FATAL;
3411 goto out;
3412 }
3413 }
3414
Christopher Faulet11610f32017-09-21 10:23:10 +02003415 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003416 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 err_code |= ERR_ALERT | ERR_ABORT;
3418 goto out;
3419 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003420 ph->id = strdup(args[cur_arg]);
3421 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 cur_arg++;
3423 }
3424 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003425 else if (!strcmp(args[0], "groups")) {
3426 int cur_arg = 1;
3427 while (*args[cur_arg]) {
3428 struct spoe_placeholder *ph = NULL;
3429
3430 list_for_each_entry(ph, &curgphs, list) {
3431 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003432 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3433 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003434 err_code |= ERR_ALERT | ERR_FATAL;
3435 goto out;
3436 }
3437 }
3438
3439 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003440 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003441 err_code |= ERR_ALERT | ERR_ABORT;
3442 goto out;
3443 }
3444 ph->id = strdup(args[cur_arg]);
3445 LIST_ADDQ(&curgphs, &ph->list);
3446 cur_arg++;
3447 }
3448 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449 else if (!strcmp(args[0], "timeout")) {
3450 unsigned int *tv = NULL;
3451 const char *res;
3452 unsigned timeout;
3453
3454 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003455 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3456 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003457 err_code |= ERR_ALERT | ERR_FATAL;
3458 goto out;
3459 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003460 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3461 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003462 if (!strcmp(args[1], "hello"))
3463 tv = &curagent->timeout.hello;
3464 else if (!strcmp(args[1], "idle"))
3465 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003466 else if (!strcmp(args[1], "processing"))
3467 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003468 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003469 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3470 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003471 err_code |= ERR_ALERT | ERR_FATAL;
3472 goto out;
3473 }
3474 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003475 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3476 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003477 err_code |= ERR_ALERT | ERR_FATAL;
3478 goto out;
3479 }
3480 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3481 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003482 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3483 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003484 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003485 goto out;
3486 }
3487 *tv = MS_TO_TICKS(timeout);
3488 }
3489 else if (!strcmp(args[0], "option")) {
3490 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003491 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3492 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003493 err_code |= ERR_ALERT | ERR_FATAL;
3494 goto out;
3495 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003496
Christopher Faulet305c6072017-02-23 16:17:53 +01003497 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003498 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003499 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003500 if (kwm == 1)
3501 curagent->flags &= ~SPOE_FL_PIPELINING;
3502 else
3503 curagent->flags |= SPOE_FL_PIPELINING;
3504 goto out;
3505 }
3506 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003507 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003508 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003509 if (kwm == 1)
3510 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003511 else {
3512 if (global.nbthread == 1)
3513 curagent->flags |= SPOE_FL_ASYNC;
3514 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003515 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3516 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003517 err_code |= ERR_WARN;
3518 }
3519 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003520 goto out;
3521 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003522 else if (!strcmp(args[1], "send-frag-payload")) {
3523 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3524 goto out;
3525 if (kwm == 1)
3526 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3527 else
3528 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3529 goto out;
3530 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003531 else if (!strcmp(args[1], "dontlog-normal")) {
3532 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3533 goto out;
3534 if (kwm == 1)
3535 curpxopts2 &= ~PR_O2_NOLOGNORM;
3536 else
3537 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003538 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003539 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003540
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003541 /* Following options does not support negation */
3542 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003543 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3544 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003545 err_code |= ERR_ALERT | ERR_FATAL;
3546 goto out;
3547 }
3548
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003549 if (!strcmp(args[1], "var-prefix")) {
3550 char *tmp;
3551
3552 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003553 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3554 file, linenum, args[0],
3555 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003556 err_code |= ERR_ALERT | ERR_FATAL;
3557 goto out;
3558 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003559 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3560 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003561 tmp = args[2];
3562 while (*tmp) {
3563 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003564 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003565 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003566 err_code |= ERR_ALERT | ERR_FATAL;
3567 goto out;
3568 }
3569 tmp++;
3570 }
3571 curagent->var_pfx = strdup(args[2]);
3572 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003573 else if (!strcmp(args[1], "force-set-var")) {
3574 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3575 goto out;
3576 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3577 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003578 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003579 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003580 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003581 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3582 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003583 else if (!strcmp(args[1], "set-on-error")) {
3584 char *tmp;
3585
3586 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003587 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3588 file, linenum, args[0],
3589 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003590 err_code |= ERR_ALERT | ERR_FATAL;
3591 goto out;
3592 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003593 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3594 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003595 tmp = args[2];
3596 while (*tmp) {
3597 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003598 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003599 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003600 err_code |= ERR_ALERT | ERR_FATAL;
3601 goto out;
3602 }
3603 tmp++;
3604 }
3605 curagent->var_on_error = strdup(args[2]);
3606 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003607 else if (!strcmp(args[1], "set-process-time")) {
3608 char *tmp;
3609
3610 if (!*args[2]) {
3611 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3612 file, linenum, args[0],
3613 args[1]);
3614 err_code |= ERR_ALERT | ERR_FATAL;
3615 goto out;
3616 }
3617 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3618 goto out;
3619 tmp = args[2];
3620 while (*tmp) {
3621 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003622 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003623 file, linenum, args[0], args[1]);
3624 err_code |= ERR_ALERT | ERR_FATAL;
3625 goto out;
3626 }
3627 tmp++;
3628 }
3629 curagent->var_t_process = strdup(args[2]);
3630 }
3631 else if (!strcmp(args[1], "set-total-time")) {
3632 char *tmp;
3633
3634 if (!*args[2]) {
3635 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3636 file, linenum, args[0],
3637 args[1]);
3638 err_code |= ERR_ALERT | ERR_FATAL;
3639 goto out;
3640 }
3641 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3642 goto out;
3643 tmp = args[2];
3644 while (*tmp) {
3645 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003646 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003647 file, linenum, args[0], args[1]);
3648 err_code |= ERR_ALERT | ERR_FATAL;
3649 goto out;
3650 }
3651 tmp++;
3652 }
3653 curagent->var_t_total = strdup(args[2]);
3654 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003655 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003656 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3657 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003658 err_code |= ERR_ALERT | ERR_FATAL;
3659 goto out;
3660 }
Christopher Faulet48026722016-11-16 15:01:12 +01003661 }
3662 else if (!strcmp(args[0], "maxconnrate")) {
3663 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003664 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3665 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003666 err_code |= ERR_ALERT | ERR_FATAL;
3667 goto out;
3668 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003669 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003670 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003671 curagent->cps_max = atol(args[1]);
3672 }
3673 else if (!strcmp(args[0], "maxerrrate")) {
3674 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003675 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3676 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003677 err_code |= ERR_ALERT | ERR_FATAL;
3678 goto out;
3679 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003680 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003681 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003682 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003683 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003684 else if (!strcmp(args[0], "max-frame-size")) {
3685 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003686 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3687 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003688 err_code |= ERR_ALERT | ERR_FATAL;
3689 goto out;
3690 }
3691 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3692 goto out;
3693 curagent->max_frame_size = atol(args[1]);
3694 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3695 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003696 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3697 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003698 err_code |= ERR_ALERT | ERR_FATAL;
3699 goto out;
3700 }
3701 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003702 else if (!strcmp(args[0], "max-waiting-frames")) {
3703 if (!*args[1]) {
3704 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3705 file, linenum, args[0]);
3706 err_code |= ERR_ALERT | ERR_FATAL;
3707 goto out;
3708 }
3709 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3710 goto out;
3711 curagent->max_fpa = atol(args[1]);
3712 if (curagent->max_fpa < 1) {
3713 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3714 file, linenum, args[0]);
3715 err_code |= ERR_ALERT | ERR_FATAL;
3716 goto out;
3717 }
3718 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003719 else if (!strcmp(args[0], "register-var-names")) {
3720 int cur_arg;
3721
3722 if (!*args[1]) {
3723 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3724 file, linenum, args[0]);
3725 err_code |= ERR_ALERT | ERR_FATAL;
3726 goto out;
3727 }
3728 cur_arg = 1;
3729 while (*args[cur_arg]) {
3730 struct spoe_var_placeholder *vph;
3731
3732 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3733 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3734 err_code |= ERR_ALERT | ERR_ABORT;
3735 goto out;
3736 }
3737 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3738 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3739 err_code |= ERR_ALERT | ERR_ABORT;
3740 goto out;
3741 }
3742 LIST_ADDQ(&curvars, &vph->list);
3743 cur_arg++;
3744 }
3745 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003746 else if (!strcmp(args[0], "log")) {
3747 char *errmsg = NULL;
3748
3749 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3750 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3751 err_code |= ERR_ALERT | ERR_FATAL;
3752 goto out;
3753 }
3754 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003755 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003756 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3757 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003758 err_code |= ERR_ALERT | ERR_FATAL;
3759 goto out;
3760 }
3761 out:
3762 return err_code;
3763}
Christopher Faulet11610f32017-09-21 10:23:10 +02003764static int
3765cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3766{
3767 struct spoe_group *grp;
3768 const char *err;
3769 int err_code = 0;
3770
3771 if ((cfg_scope == NULL && curengine != NULL) ||
3772 (cfg_scope != NULL && curengine == NULL) ||
3773 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3774 goto out;
3775
3776 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3777 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003778 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3779 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003780 err_code |= ERR_ALERT | ERR_ABORT;
3781 goto out;
3782 }
3783 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3784 err_code |= ERR_ABORT;
3785 goto out;
3786 }
3787
3788 err = invalid_char(args[1]);
3789 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003790 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3791 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003792 err_code |= ERR_ALERT | ERR_ABORT;
3793 goto out;
3794 }
3795
3796 list_for_each_entry(grp, &curgrps, list) {
3797 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003798 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3799 " name as another one declared at %s:%d.\n",
3800 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003801 err_code |= ERR_ALERT | ERR_FATAL;
3802 goto out;
3803 }
3804 }
3805
3806 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003807 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003808 err_code |= ERR_ALERT | ERR_ABORT;
3809 goto out;
3810 }
3811
3812 curgrp->id = strdup(args[1]);
3813 curgrp->conf.file = strdup(file);
3814 curgrp->conf.line = linenum;
3815 LIST_INIT(&curgrp->phs);
3816 LIST_INIT(&curgrp->messages);
3817 LIST_ADDQ(&curgrps, &curgrp->list);
3818 }
3819 else if (!strcmp(args[0], "messages")) {
3820 int cur_arg = 1;
3821 while (*args[cur_arg]) {
3822 struct spoe_placeholder *ph = NULL;
3823
3824 list_for_each_entry(ph, &curgrp->phs, list) {
3825 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003826 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3827 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003828 err_code |= ERR_ALERT | ERR_FATAL;
3829 goto out;
3830 }
3831 }
3832
3833 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003834 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003835 err_code |= ERR_ALERT | ERR_ABORT;
3836 goto out;
3837 }
3838 ph->id = strdup(args[cur_arg]);
3839 LIST_ADDQ(&curgrp->phs, &ph->list);
3840 cur_arg++;
3841 }
3842 }
3843 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003844 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3845 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003846 err_code |= ERR_ALERT | ERR_FATAL;
3847 goto out;
3848 }
3849 out:
3850 return err_code;
3851}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003852
3853static int
3854cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3855{
3856 struct spoe_message *msg;
3857 struct spoe_arg *arg;
3858 const char *err;
3859 char *errmsg = NULL;
3860 int err_code = 0;
3861
3862 if ((cfg_scope == NULL && curengine != NULL) ||
3863 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003864 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003865 goto out;
3866
3867 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3868 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003869 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3870 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003871 err_code |= ERR_ALERT | ERR_ABORT;
3872 goto out;
3873 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003874 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3875 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003876 goto out;
3877 }
3878
3879 err = invalid_char(args[1]);
3880 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003881 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3882 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003883 err_code |= ERR_ALERT | ERR_ABORT;
3884 goto out;
3885 }
3886
3887 list_for_each_entry(msg, &curmsgs, list) {
3888 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003889 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3890 " name as another one declared at %s:%d.\n",
3891 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003892 err_code |= ERR_ALERT | ERR_FATAL;
3893 goto out;
3894 }
3895 }
3896
3897 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003898 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 err_code |= ERR_ALERT | ERR_ABORT;
3900 goto out;
3901 }
3902
3903 curmsg->id = strdup(args[1]);
3904 curmsg->id_len = strlen(curmsg->id);
3905 curmsg->event = SPOE_EV_NONE;
3906 curmsg->conf.file = strdup(file);
3907 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003908 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003909 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003910 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003911 LIST_INIT(&curmsg->by_evt);
3912 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003913 LIST_ADDQ(&curmsgs, &curmsg->list);
3914 }
3915 else if (!strcmp(args[0], "args")) {
3916 int cur_arg = 1;
3917
3918 curproxy->conf.args.ctx = ARGC_SPOE;
3919 curproxy->conf.args.file = file;
3920 curproxy->conf.args.line = linenum;
3921 while (*args[cur_arg]) {
3922 char *delim = strchr(args[cur_arg], '=');
3923 int idx = 0;
3924
3925 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003926 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 err_code |= ERR_ALERT | ERR_ABORT;
3928 goto out;
3929 }
3930
3931 if (!delim) {
3932 arg->name = NULL;
3933 arg->name_len = 0;
3934 delim = args[cur_arg];
3935 }
3936 else {
3937 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3938 arg->name_len = delim - args[cur_arg];
3939 delim++;
3940 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003941 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3942 &idx, file, linenum, &errmsg,
3943 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003944 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003945 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 err_code |= ERR_ALERT | ERR_FATAL;
3947 free(arg->name);
3948 free(arg);
3949 goto out;
3950 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003951 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003952 LIST_ADDQ(&curmsg->args, &arg->list);
3953 cur_arg++;
3954 }
3955 curproxy->conf.args.file = NULL;
3956 curproxy->conf.args.line = 0;
3957 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003958 else if (!strcmp(args[0], "acl")) {
3959 err = invalid_char(args[1]);
3960 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003961 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3962 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003963 err_code |= ERR_ALERT | ERR_FATAL;
3964 goto out;
3965 }
3966 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003967 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3968 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003969 err_code |= ERR_ALERT | ERR_FATAL;
3970 goto out;
3971 }
3972 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003973 else if (!strcmp(args[0], "event")) {
3974 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003975 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003976 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003977 goto out;
3978 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003979 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3980 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003981
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3983 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3984 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3985 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3986
3987 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3988 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3989 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3990 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3991 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3992 curmsg->event = SPOE_EV_ON_TCP_RSP;
3993
3994 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3995 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3996 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3997 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3998 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3999 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4000 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004001 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004002 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004003 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004004 goto out;
4005 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004006
4007 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4008 struct acl_cond *cond;
4009
4010 cond = build_acl_cond(file, linenum, &curmsg->acls,
4011 curproxy, (const char **)args+2,
4012 &errmsg);
4013 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004014 ha_alert("parsing [%s:%d] : error detected while "
4015 "parsing an 'event %s' condition : %s.\n",
4016 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004017 err_code |= ERR_ALERT | ERR_FATAL;
4018 goto out;
4019 }
4020 curmsg->cond = cond;
4021 }
4022 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004023 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4024 "or 'unless' followed by a condition but found '%s'.\n",
4025 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004026 err_code |= ERR_ALERT | ERR_FATAL;
4027 goto out;
4028 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004029 }
4030 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004031 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4032 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004033 err_code |= ERR_ALERT | ERR_FATAL;
4034 goto out;
4035 }
4036 out:
4037 free(errmsg);
4038 return err_code;
4039}
4040
4041/* Return -1 on error, else 0 */
4042static int
4043parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4044 struct flt_conf *fconf, char **err, void *private)
4045{
4046 struct list backup_sections;
4047 struct spoe_config *conf;
4048 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004049 struct spoe_group *grp, *grpback;
4050 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004051 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004052 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 char *file = NULL, *engine = NULL;
4054 int ret, pos = *cur_arg + 1;
4055
Christopher Faulet84c844e2018-03-23 14:37:14 +01004056 LIST_INIT(&curmsgs);
4057 LIST_INIT(&curgrps);
4058 LIST_INIT(&curmphs);
4059 LIST_INIT(&curgphs);
4060 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004061 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004062 curpxopts = 0;
4063 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004064
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004065 conf = calloc(1, sizeof(*conf));
4066 if (conf == NULL) {
4067 memprintf(err, "%s: out of memory", args[*cur_arg]);
4068 goto error;
4069 }
4070 conf->proxy = px;
4071
4072 while (*args[pos]) {
4073 if (!strcmp(args[pos], "config")) {
4074 if (!*args[pos+1]) {
4075 memprintf(err, "'%s' : '%s' option without value",
4076 args[*cur_arg], args[pos]);
4077 goto error;
4078 }
4079 file = args[pos+1];
4080 pos += 2;
4081 }
4082 else if (!strcmp(args[pos], "engine")) {
4083 if (!*args[pos+1]) {
4084 memprintf(err, "'%s' : '%s' option without value",
4085 args[*cur_arg], args[pos]);
4086 goto error;
4087 }
4088 engine = args[pos+1];
4089 pos += 2;
4090 }
4091 else {
4092 memprintf(err, "unknown keyword '%s'", args[pos]);
4093 goto error;
4094 }
4095 }
4096 if (file == NULL) {
4097 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4098 goto error;
4099 }
4100
4101 /* backup sections and register SPOE sections */
4102 LIST_INIT(&backup_sections);
4103 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004104 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4105 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004106 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004107
4108 /* Parse SPOE filter configuration file */
4109 curengine = engine;
4110 curproxy = px;
4111 curagent = NULL;
4112 curmsg = NULL;
4113 ret = readcfgfile(file);
4114 curproxy = NULL;
4115
4116 /* unregister SPOE sections and restore previous sections */
4117 cfg_unregister_sections();
4118 cfg_restore_sections(&backup_sections);
4119
4120 if (ret == -1) {
4121 memprintf(err, "Could not open configuration file %s : %s",
4122 file, strerror(errno));
4123 goto error;
4124 }
4125 if (ret & (ERR_ABORT|ERR_FATAL)) {
4126 memprintf(err, "Error(s) found in configuration file %s", file);
4127 goto error;
4128 }
4129
4130 /* Check SPOE agent */
4131 if (curagent == NULL) {
4132 memprintf(err, "No SPOE agent found in file %s", file);
4133 goto error;
4134 }
4135 if (curagent->b.name == NULL) {
4136 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4137 curagent->id, curagent->conf.file, curagent->conf.line);
4138 goto error;
4139 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004140 if (curagent->timeout.hello == TICK_ETERNITY ||
4141 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004142 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004143 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4144 " | While not properly invalid, you will certainly encounter various problems\n"
4145 " | with such a configuration. To fix this, please ensure that all following\n"
4146 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4147 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004148 }
4149 if (curagent->var_pfx == NULL) {
4150 char *tmp = curagent->id;
4151
4152 while (*tmp) {
4153 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4154 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4155 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4156 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4157 goto error;
4158 }
4159 tmp++;
4160 }
4161 curagent->var_pfx = strdup(curagent->id);
4162 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004163 if (curagent->engine_id == NULL)
4164 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004165
Christopher Fauletb7426d12018-03-21 14:12:17 +01004166 if (curagent->var_on_error) {
4167 struct arg arg;
4168
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004169 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004170 curagent->var_pfx, curagent->var_on_error);
4171
4172 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004173 arg.data.str.area = trash.area;
4174 arg.data.str.data = trash.data;
Christopher Fauletb7426d12018-03-21 14:12:17 +01004175 if (!vars_check_arg(&arg, err)) {
4176 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4177 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4178 goto error;
4179 }
4180 }
4181
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004182 if (curagent->var_t_process) {
4183 struct arg arg;
4184
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004185 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004186 curagent->var_pfx, curagent->var_t_process);
4187
4188 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 arg.data.str.area = trash.area;
4190 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004191 if (!vars_check_arg(&arg, err)) {
4192 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4193 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4194 goto error;
4195 }
4196 }
4197
4198 if (curagent->var_t_total) {
4199 struct arg arg;
4200
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004201 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004202 curagent->var_pfx, curagent->var_t_total);
4203
4204 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004205 arg.data.str.area = trash.area;
4206 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004207 if (!vars_check_arg(&arg, err)) {
4208 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4209 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4210 goto error;
4211 }
4212 }
4213
Christopher Faulet11610f32017-09-21 10:23:10 +02004214 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004215 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4216 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004217 goto finish;
4218 }
4219
Christopher Faulet11610f32017-09-21 10:23:10 +02004220 /* Replace placeholders by the corresponding messages for the SPOE
4221 * agent */
4222 list_for_each_entry(ph, &curmphs, list) {
4223 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004224 struct spoe_arg *arg;
4225 unsigned int where;
4226
Christopher Faulet11610f32017-09-21 10:23:10 +02004227 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004228 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4229 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4230 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4231 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4232 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4233 }
4234 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4235 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4236 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004237 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4238 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004239 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004240 }
4241 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004242 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4243 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004244 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004245 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004246
4247 where = 0;
4248 switch (msg->event) {
4249 case SPOE_EV_ON_CLIENT_SESS:
4250 where |= SMP_VAL_FE_CON_ACC;
4251 break;
4252
4253 case SPOE_EV_ON_TCP_REQ_FE:
4254 where |= SMP_VAL_FE_REQ_CNT;
4255 break;
4256
4257 case SPOE_EV_ON_HTTP_REQ_FE:
4258 where |= SMP_VAL_FE_HRQ_HDR;
4259 break;
4260
4261 case SPOE_EV_ON_TCP_REQ_BE:
4262 if (px->cap & PR_CAP_FE)
4263 where |= SMP_VAL_FE_REQ_CNT;
4264 if (px->cap & PR_CAP_BE)
4265 where |= SMP_VAL_BE_REQ_CNT;
4266 break;
4267
4268 case SPOE_EV_ON_HTTP_REQ_BE:
4269 if (px->cap & PR_CAP_FE)
4270 where |= SMP_VAL_FE_HRQ_HDR;
4271 if (px->cap & PR_CAP_BE)
4272 where |= SMP_VAL_BE_HRQ_HDR;
4273 break;
4274
4275 case SPOE_EV_ON_SERVER_SESS:
4276 where |= SMP_VAL_BE_SRV_CON;
4277 break;
4278
4279 case SPOE_EV_ON_TCP_RSP:
4280 if (px->cap & PR_CAP_FE)
4281 where |= SMP_VAL_FE_RES_CNT;
4282 if (px->cap & PR_CAP_BE)
4283 where |= SMP_VAL_BE_RES_CNT;
4284 break;
4285
4286 case SPOE_EV_ON_HTTP_RSP:
4287 if (px->cap & PR_CAP_FE)
4288 where |= SMP_VAL_FE_HRS_HDR;
4289 if (px->cap & PR_CAP_BE)
4290 where |= SMP_VAL_BE_HRS_HDR;
4291 break;
4292
4293 default:
4294 break;
4295 }
4296
4297 list_for_each_entry(arg, &msg->args, list) {
4298 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004299 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004300 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004301 "none of which is available here ('%s')",
4302 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004303 sample_ckp_names(arg->expr->fetch->use),
4304 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004305 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004306 }
4307 }
4308
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004309 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004310 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4311 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004312 }
4313 }
4314 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004315 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004316 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004317 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004318 continue;
4319 }
4320
Christopher Faulet11610f32017-09-21 10:23:10 +02004321 /* Replace placeholders by the corresponding groups for the SPOE
4322 * agent */
4323 list_for_each_entry(ph, &curgphs, list) {
4324 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4325 if (!strcmp(grp->id, ph->id)) {
4326 grp->agent = curagent;
4327 LIST_DEL(&grp->list);
4328 LIST_ADDQ(&curagent->groups, &grp->list);
4329 goto next_aph;
4330 }
4331 }
4332 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4333 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4334 goto error;
4335 next_aph:
4336 continue;
4337 }
4338
4339 /* Replace placeholders by the corresponding message for each SPOE
4340 * group of the SPOE agent */
4341 list_for_each_entry(grp, &curagent->groups, list) {
4342 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4343 list_for_each_entry(msg, &curmsgs, list) {
4344 if (!strcmp(msg->id, ph->id)) {
4345 if (msg->group != NULL) {
4346 memprintf(err, "SPOE message '%s' already belongs to "
4347 "the SPOE group '%s' declare at %s:%d",
4348 msg->id, msg->group->id,
4349 msg->group->conf.file,
4350 msg->group->conf.line);
4351 goto error;
4352 }
4353
4354 /* Scope for arguments are not checked for now. We will check
4355 * them only if a rule use the corresponding SPOE group. */
4356 msg->agent = curagent;
4357 msg->group = grp;
4358 LIST_DEL(&ph->list);
4359 LIST_ADDQ(&grp->messages, &msg->by_grp);
4360 goto next_mph_grp;
4361 }
4362 }
4363 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4364 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4365 goto error;
4366 next_mph_grp:
4367 continue;
4368 }
4369 }
4370
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004371 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004372 /* move curmsgs to the agent message list */
4373 curmsgs.n->p = &curagent->messages;
4374 curmsgs.p->n = &curagent->messages;
4375 curagent->messages = curmsgs;
4376 LIST_INIT(&curmsgs);
4377
Christopher Faulet7ee86672017-09-19 11:08:28 +02004378 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004379 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004380
4381 /* Start agent's proxy initialization here. It will be finished during
4382 * the filter init. */
4383 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4384 init_new_proxy(&conf->agent_fe);
4385 conf->agent_fe.id = conf->agent->id;
4386 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004387 conf->agent_fe.options |= curpxopts;
4388 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004389
4390 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4391 LIST_DEL(&logsrv->list);
4392 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4393 }
4394
Christopher Faulet11610f32017-09-21 10:23:10 +02004395 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4396 LIST_DEL(&ph->list);
4397 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004398 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004399 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4400 LIST_DEL(&ph->list);
4401 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004402 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004403 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4404 struct arg arg;
4405
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004406 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004407 curagent->var_pfx, vph->name);
4408
4409 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004410 arg.data.str.area = trash.area;
4411 arg.data.str.data = trash.data;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004412 if (!vars_check_arg(&arg, err)) {
4413 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4414 curagent->id, curagent->var_pfx, vph->name, *err);
4415 goto error;
4416 }
4417
4418 LIST_DEL(&vph->list);
4419 free(vph->name);
4420 free(vph);
4421 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004422 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4423 LIST_DEL(&grp->list);
4424 spoe_release_group(grp);
4425 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004426 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004427 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004428 fconf->ops = &spoe_ops;
4429 fconf->conf = conf;
4430 return 0;
4431
4432 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004433 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004434 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4435 LIST_DEL(&ph->list);
4436 spoe_release_placeholder(ph);
4437 }
4438 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4439 LIST_DEL(&ph->list);
4440 spoe_release_placeholder(ph);
4441 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004442 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4443 LIST_DEL(&vph->list);
4444 free(vph->name);
4445 free(vph);
4446 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004447 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4448 LIST_DEL(&grp->list);
4449 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004450 }
4451 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4452 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004453 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004454 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004455 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4456 LIST_DEL(&logsrv->list);
4457 free(logsrv);
4458 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004459 free(conf);
4460 return -1;
4461}
4462
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004463/* Send message of a SPOE group. This is the action_ptr callback of a rule
4464 * associated to a "send-spoe-group" action.
4465 *
4466 * It returns ACT_RET_CONT is processing is finished without error, it returns
4467 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4468 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004469static enum act_return
4470spoe_send_group(struct act_rule *rule, struct proxy *px,
4471 struct session *sess, struct stream *s, int flags)
4472{
4473 struct filter *filter;
4474 struct spoe_agent *agent = NULL;
4475 struct spoe_group *group = NULL;
4476 struct spoe_context *ctx = NULL;
4477 int ret, dir;
4478
4479 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4480 if (filter->config == rule->arg.act.p[0]) {
4481 agent = rule->arg.act.p[2];
4482 group = rule->arg.act.p[3];
4483 ctx = filter->ctx;
4484 break;
4485 }
4486 }
4487 if (agent == NULL || group == NULL || ctx == NULL)
4488 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004489 if (ctx->state == SPOE_CTX_ST_NONE)
4490 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004491
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004492 switch (rule->from) {
4493 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4494 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4495 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4496 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4497 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4498 default:
4499 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4500 " - internal error while execute spoe-send-group\n",
4501 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4502 __FUNCTION__, s);
4503 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4504 agent->id);
4505 return ACT_RET_CONT;
4506 }
4507
4508 ret = spoe_process_group(s, ctx, group, dir);
4509 if (ret == 1)
4510 return ACT_RET_CONT;
4511 else if (ret == 0) {
4512 if (flags & ACT_FLAG_FINAL) {
4513 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4514 " - failed to process group '%s': interrupted by caller\n",
4515 (int)now.tv_sec, (int)now.tv_usec,
4516 agent->id, __FUNCTION__, s, group->id);
4517 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004518 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004519 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004520 return ACT_RET_CONT;
4521 }
4522 return ACT_RET_YIELD;
4523 }
4524 else
4525 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004526}
4527
4528/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4529 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4530 * action should be:
4531 *
4532 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4533 *
4534 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4535 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4536 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4537 * group.
4538 *
4539 * The function returns 1 in success case, otherwise, it returns 0 and err is
4540 * filled.
4541 */
4542static int
4543check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4544{
4545 struct flt_conf *fconf;
4546 struct spoe_config *conf;
4547 struct spoe_agent *agent = NULL;
4548 struct spoe_group *group;
4549 struct spoe_message *msg;
4550 char *engine_id = rule->arg.act.p[0];
4551 char *group_id = rule->arg.act.p[1];
4552 unsigned int where = 0;
4553
4554 switch (rule->from) {
4555 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4556 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4557 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4558 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4559 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4560 default:
4561 memprintf(err,
4562 "internal error, unexpected rule->from=%d, please report this bug!",
4563 rule->from);
4564 goto error;
4565 }
4566
4567 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4568 * <px> */
4569 list_for_each_entry(fconf, &px->filter_configs, list) {
4570 conf = fconf->conf;
4571
4572 /* This is not an SPOE filter */
4573 if (fconf->id != spoe_filter_id)
4574 continue;
4575
4576 /* This is the good engine */
4577 if (!strcmp(conf->id, engine_id)) {
4578 agent = conf->agent;
4579 break;
4580 }
4581 }
4582 if (agent == NULL) {
4583 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4584 engine_id, group_id);
4585 goto error;
4586 }
4587
4588 /* Try to find the right group */
4589 list_for_each_entry(group, &agent->groups, list) {
4590 /* This is the good group */
4591 if (!strcmp(group->id, group_id))
4592 break;
4593 }
4594 if (&group->list == &agent->groups) {
4595 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4596 group_id, engine_id);
4597 goto error;
4598 }
4599
4600 /* Ok, we found the group, we need to check messages and their
4601 * arguments */
4602 list_for_each_entry(msg, &group->messages, by_grp) {
4603 struct spoe_arg *arg;
4604
4605 list_for_each_entry(arg, &msg->args, list) {
4606 if (!(arg->expr->fetch->val & where)) {
4607 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4608 "some args extract information from '%s',"
4609 "none of which is available here ('%s')",
4610 msg->id, group->id, msg->conf.file, msg->conf.line,
4611 sample_ckp_names(arg->expr->fetch->use),
4612 sample_ckp_names(where));
4613 goto error;
4614 }
4615 }
4616 }
4617
4618 free(engine_id);
4619 free(group_id);
4620 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4621 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4622 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4623 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4624 return 1;
4625
4626 error:
4627 free(engine_id);
4628 free(group_id);
4629 return 0;
4630}
4631
4632/* Parse 'send-spoe-group' action following the format:
4633 *
4634 * ... send-spoe-group <engine-id> <group-id>
4635 *
4636 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4637 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4638 * ids are saved and used later, when the rule will be checked.
4639 */
4640static enum act_parse_ret
4641parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4642 struct act_rule *rule, char **err)
4643{
4644 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4645 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4646 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4647 return ACT_RET_PRS_ERR;
4648 }
4649 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4650 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4651
4652 (*orig_arg) += 2;
4653
4654 rule->action = ACT_CUSTOM;
4655 rule->action_ptr = spoe_send_group;
4656 rule->check_ptr = check_send_spoe_group;
4657 return ACT_RET_PRS_OK;
4658}
4659
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004660
4661/* Declare the filter parser for "spoe" keyword */
4662static struct flt_kw_list flt_kws = { "SPOE", { }, {
4663 { "spoe", parse_spoe_flt, NULL },
4664 { NULL, NULL, NULL },
4665 }
4666};
4667
Willy Tarreau0108d902018-11-25 19:14:37 +01004668INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4669
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004670/* Delcate the action parser for "spoe-action" keyword */
4671static struct action_kw_list tcp_req_action_kws = { { }, {
4672 { "send-spoe-group", parse_send_spoe_group },
4673 { /* END */ },
4674 }
4675};
Willy Tarreau0108d902018-11-25 19:14:37 +01004676
4677INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4678
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004679static struct action_kw_list tcp_res_action_kws = { { }, {
4680 { "send-spoe-group", parse_send_spoe_group },
4681 { /* END */ },
4682 }
4683};
Willy Tarreau0108d902018-11-25 19:14:37 +01004684
4685INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4686
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004687static struct action_kw_list http_req_action_kws = { { }, {
4688 { "send-spoe-group", parse_send_spoe_group },
4689 { /* END */ },
4690 }
4691};
Willy Tarreau0108d902018-11-25 19:14:37 +01004692
4693INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4694
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004695static struct action_kw_list http_res_action_kws = { { }, {
4696 { "send-spoe-group", parse_send_spoe_group },
4697 { /* END */ },
4698 }
4699};
4700
Willy Tarreau0108d902018-11-25 19:14:37 +01004701INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);