blob: fe219ea3060587491af148f287a4680f7140d981 [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>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020037#include <proto/http_ana.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020038#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 Faulet985532d2016-11-16 15:36:19 +0100175 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100176 free(agent->var_t_process);
177 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
179 LIST_DEL(&msg->list);
180 spoe_release_message(msg);
181 }
182 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
183 LIST_DEL(&grp->list);
184 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200185 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100186 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200187 for (i = 0; i < global.nbthread; ++i) {
188 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100189 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200190 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100191 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200192 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200193 free(agent);
194}
195
196static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100197 [SPOE_FRM_ERR_NONE] = "normal",
198 [SPOE_FRM_ERR_IO] = "I/O error",
199 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
200 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
201 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
202 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
203 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
204 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
205 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
206 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
207 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
208 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100209 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100210 [SPOE_FRM_ERR_RES] = "resource allocation error",
211 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200212};
213
214static const char *spoe_event_str[SPOE_EV_EVENTS] = {
215 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
216 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
217 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
218 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
219 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
220
221 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
222 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
223 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
224};
225
226
227#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
228
229static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100230 [SPOE_CTX_ST_NONE] = "NONE",
231 [SPOE_CTX_ST_READY] = "READY",
232 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
233 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
234 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
235 [SPOE_CTX_ST_DONE] = "DONE",
236 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200237};
238
239static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100240 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
241 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
242 [SPOE_APPCTX_ST_IDLE] = "IDLE",
243 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
244 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
245 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
246 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
247 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
248 [SPOE_APPCTX_ST_EXIT] = "EXIT",
249 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200250};
251
252#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100253
Christopher Faulet8ef75252017-02-20 22:56:03 +0100254/* Used to generates a unique id for an engine. On success, it returns a
255 * allocated string. So it is the caller's reponsibility to release it. If the
256 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100257static char *
258generate_pseudo_uuid()
259{
Christopher Fauleta1cda022016-12-21 08:58:06 +0100260 char *uuid;
Christopher Faulet09bd9aa2019-09-17 15:07:02 +0200261 uint32_t rnd[4] = { 0, 0, 0, 0 };
262 uint64_t last = 0;
263 int byte = 0;
264 uint8_t bits = 0;
265 unsigned int rand_max_bits = my_flsl(RAND_MAX);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100266
Christopher Faulet09bd9aa2019-09-17 15:07:02 +0200267 if ((uuid = calloc(1, 37)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100268 return NULL;
269
Christopher Faulet09bd9aa2019-09-17 15:07:02 +0200270 while (byte < 4) {
271 while (bits < 32) {
272 last |= (uint64_t)random() << bits;
273 bits += rand_max_bits;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100274 }
Christopher Faulet09bd9aa2019-09-17 15:07:02 +0200275 rnd[byte++] = last;
276 last >>= 32u;
277 bits -= 32;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100278 }
Christopher Faulet09bd9aa2019-09-17 15:07:02 +0200279 snprintf(uuid, 36, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
280 rnd[0],
281 rnd[1] & 0xFFFF,
282 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
283 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
284 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
285 );
Christopher Fauleta1cda022016-12-21 08:58:06 +0100286 return uuid;
287}
288
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100289
290static inline void
291spoe_update_stat_time(struct timeval *tv, long *t)
292{
293 if (*t == -1)
294 *t = tv_ms_elapsed(tv, &now);
295 else
296 *t += tv_ms_elapsed(tv, &now);
297 tv_zero(tv);
298}
299
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200300/********************************************************************
301 * Functions that encode/decode SPOE frames
302 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200303/* Helper to get static string length, excluding the terminating null byte */
304#define SLEN(str) (sizeof(str)-1)
305
306/* Predefined key used in HELLO/DISCONNECT frames */
307#define SUPPORTED_VERSIONS_KEY "supported-versions"
308#define VERSION_KEY "version"
309#define MAX_FRAME_SIZE_KEY "max-frame-size"
310#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100311#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100312#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200313#define STATUS_CODE_KEY "status-code"
314#define MSG_KEY "message"
315
316struct spoe_version {
317 char *str;
318 int min;
319 int max;
320};
321
322/* All supported versions */
323static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200324 /* 1.0 is now unsupported because of a bug about frame's flags*/
325 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200326 {NULL, 0, 0}
327};
328
329/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200330#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331
Christopher Faulet8ef75252017-02-20 22:56:03 +0100332/* Convert a string to a SPOE version value. The string must follow the format
333 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
334 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200335static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100336spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 const char *p, *end;
339 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200340
Christopher Faulet8ef75252017-02-20 22:56:03 +0100341 p = str;
342 end = str+len;
343 maj = min = 0;
344 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200345
Christopher Faulet8ef75252017-02-20 22:56:03 +0100346 /* skip leading spaces */
347 while (p < end && isspace(*p))
348 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200349
Christopher Faulet8ef75252017-02-20 22:56:03 +0100350 /* parse Major number, until the '.' */
351 while (*p != '.') {
352 if (p >= end || *p < '0' || *p > '9')
353 goto out;
354 maj *= 10;
355 maj += (*p - '0');
356 p++;
357 }
358
359 /* check Major version */
360 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200361 goto out;
362
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363 p++; /* skip the '.' */
364 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
365 goto out;
366
367 /* Parse Minor number */
368 while (p < end) {
369 if (*p < '0' || *p > '9')
370 break;
371 min *= 10;
372 min += (*p - '0');
373 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200374 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100375
376 /* check Minor number */
377 if (min > 999)
378 goto out;
379
380 /* skip trailing spaces */
381 while (p < end && isspace(*p))
382 p++;
383 if (p != end)
384 goto out;
385
386 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200387 out:
388 return vsn;
389}
390
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800392 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100393 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200394static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200396{
Willy Tarreau83061a82018-07-13 11:56:34 +0200397 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100398 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 char *p, *end;
400 unsigned int flags = SPOE_FRM_FL_FIN;
401 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 p = frame;
404 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 /* Set Frame type */
407 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100409 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200410 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100411 memcpy(p, (char *)&flags, 4);
412 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
414 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200416
417 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
418 * and "capabilities" */
419
420 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100421 sz = SLEN(SUPPORTED_VERSIONS_KEY);
422 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
423 goto too_big;
424
425 *p++ = SPOE_DATA_T_STR;
426 sz = SLEN(SUPPORTED_VERSIONS_VAL);
427 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
428 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200429
430 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 sz = SLEN(MAX_FRAME_SIZE_KEY);
432 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
433 goto too_big;
434
435 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200436 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100437 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200438
439 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100440 sz = SLEN(CAPABILITIES_KEY);
441 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
442 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200443
Christopher Faulet8ef75252017-02-20 22:56:03 +0100444 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100445 chk = get_trash_chunk();
446 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200447 memcpy(chk->area, "pipelining", 10);
448 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100449 }
450 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200451 if (chk->data) chk->area[chk->data++] = ',';
452 memcpy(chk->area+chk->data, "async", 5);
453 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100454 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100455 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200456 if (chk->data) chk->area[chk->data++] = ',';
457 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100458 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100459 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200460 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 goto too_big;
462
463 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200464 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100465 sz = SLEN(ENGINE_ID_KEY);
466 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
467 goto too_big;
468
469 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200470 sz = strlen(agent->rt[tid].engine_id);
471 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100473 }
474
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 return (p - frame);
476
477 too_big:
478 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
479 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480}
481
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
483 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
484 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200485static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200487{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 const char *reason;
489 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100490 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 p = frame;
494 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 /* Set Frame type */
497 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200498
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100499 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200500 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100501 memcpy(p, (char *)&flags, 4);
502 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200503
504 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 *p++ = 0; *p++ = 0;
506
507 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
508 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509
510 /* There are 2 mandatory items: "status-code" and "message" */
511
512 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513 sz = SLEN(STATUS_CODE_KEY);
514 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
515 goto too_big;
516
517 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200518 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100519 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
521 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100522 sz = SLEN(MSG_KEY);
523 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
524 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Faulet8ef75252017-02-20 22:56:03 +0100526 /*Get the message corresponding to the status code */
527 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
528
529 *p++ = SPOE_DATA_T_STR;
530 sz = strlen(reason);
531 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
532 goto too_big;
533
534 return (p - frame);
535
536 too_big:
537 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
538 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200539}
540
Christopher Faulet8ef75252017-02-20 22:56:03 +0100541/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
542 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
543 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200544static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100545spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100546 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200547{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100548 char *p, *end;
549 unsigned int stream_id, frame_id;
550 unsigned int flags = SPOE_FRM_FL_FIN;
551 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200552
Christopher Faulet8ef75252017-02-20 22:56:03 +0100553 p = frame;
554 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200555
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100556 stream_id = ctx->stream_id;
557 frame_id = ctx->frame_id;
558
559 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
560 /* The fragmentation is not supported by the applet */
561 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
562 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
563 return -1;
564 }
565 flags = ctx->frag_ctx.flags;
566 }
567
568 /* Set Frame type */
569 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
570
571 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200572 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100573 memcpy(p, (char *)&flags, 4);
574 p += 4;
575
576 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200577 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100578 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200579 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100580 goto too_big;
581
582 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200583 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100584 if (p + sz >= end)
585 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200586 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100587 p += sz;
588
589 return (p - frame);
590
591 too_big:
592 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
593 return 0;
594}
595
596/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
597 * returns the number of encoded bytes in the frame on success, 0 if an encoding
598 * error occurred and -1 if a fatal error occurred. */
599static int
600spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
601 char *frame, size_t size)
602{
603 char *p, *end;
604 unsigned int stream_id, frame_id;
605 unsigned int flags;
606 size_t sz;
607
608 p = frame;
609 end = frame+size;
610
Christopher Faulet8ef75252017-02-20 22:56:03 +0100611 /* <ctx> is null when the stream has aborted the processing of a
612 * fragmented frame. In this case, we must notify the corresponding
613 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100614 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100615 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100616 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
617 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
618 }
619 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100620 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100621 stream_id = ctx->stream_id;
622 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100623 }
624
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100626 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100628 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200629 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 memcpy(p, (char *)&flags, 4);
631 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632
633 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200634 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200636 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100637 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200638
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100639 if (ctx == NULL)
640 goto end;
641
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200643 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 if (p + sz >= end)
645 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200646 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100648
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100649 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 return (p - frame);
651
652 too_big:
653 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
654 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655}
656
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657/* Decode and process the HELLO frame sent by an agent. It returns the number of
658 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
659 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200660static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662{
Christopher Faulet305c6072017-02-23 16:17:53 +0100663 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
664 char *p, *end;
665 int vsn, max_frame_size;
666 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667
668 p = frame;
669 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670
671 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200674 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200676
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 if (size < 7 /* TYPE + METADATA */) {
678 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
679 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 }
681
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100682 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200684 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100685 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100687 /* Fragmentation is not supported for HELLO frame */
688 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100690 return -1;
691 }
692
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 if (*p != 0 || *(p+1) != 0) {
695 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
696 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699
700 /* There are 3 mandatory items: "version", "max-frame-size" and
701 * "capabilities" */
702
703 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100704 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 while (p < end) {
706 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200707 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709
710 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 ret = spoe_decode_buffer(&p, end, &str, &sz);
712 if (ret == -1 || !sz) {
713 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
714 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200715 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 /* Check "version" K/V item */
718 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100719 int i, type = *p++;
720
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100722 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
723 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
724 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200725 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
727 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
728 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 }
730
Christopher Faulet8ef75252017-02-20 22:56:03 +0100731 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 return -1;
735 }
736 for (i = 0; supported_versions[i].str != NULL; ++i) {
737 if (vsn >= supported_versions[i].min &&
738 vsn <= supported_versions[i].max)
739 break;
740 }
741 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743 return -1;
744 }
745 }
746 /* Check "max-frame-size" K/V item */
747 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200749
750 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200751 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
752 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
753 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
754 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100755 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
756 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200757 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200758 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
760 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200761 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 if (sz < MIN_FRAME_SIZE ||
763 sz > SPOE_APPCTX(appctx)->max_frame_size) {
764 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200765 return -1;
766 }
767 max_frame_size = sz;
768 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769 /* Check "capabilities" K/V item */
770 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100771 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772
773 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
775 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
776 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
779 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
780 return 0;
781 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 char *delim;
785
786 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100787 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100788
Christopher Faulet8ef75252017-02-20 22:56:03 +0100789 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
790 str += 10; sz -= 10;
791 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100792 flags |= SPOE_APPCTX_FL_PIPELINING;
793 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100794 else if (sz >= 5 && !strncmp(str, "async", 5)) {
795 str += 5; sz -= 5;
796 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100797 flags |= SPOE_APPCTX_FL_ASYNC;
798 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100799 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
800 str += 13; sz -= 13;
801 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100802 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
803 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100804
Christopher Faulet8ef75252017-02-20 22:56:03 +0100805 /* Get the next comma or break */
806 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100807 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100808 delim++;
809 sz -= (delim - str);
810 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100811 }
812 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813 else {
814 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100815 if (spoe_skip_data(&p, end) == -1) {
816 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
817 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819 }
820 }
821
822 /* Final checks */
823 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825 return -1;
826 }
827 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829 return -1;
830 }
Christopher Faulet11389012019-02-07 16:13:26 +0100831 if (!agent)
832 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
833 else {
834 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
835 flags &= ~SPOE_APPCTX_FL_PIPELINING;
836 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
837 flags &= ~SPOE_APPCTX_FL_ASYNC;
838 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200839
Christopher Faulet42bfa462017-01-04 14:14:19 +0100840 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
841 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
842 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843
844 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845}
846
847/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
848 * bytes on success, 0 if the frame can be ignored and -1 if an error
849 * occurred. */
850static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100854 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855
856 p = frame;
857 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858
859 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
861 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100863 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200864
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 if (size < 7 /* TYPE + METADATA */) {
866 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
867 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868 }
869
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100870 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100871 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200872 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100875 /* Fragmentation is not supported for DISCONNECT frame */
876 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100877 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100878 return -1;
879 }
880
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 if (*p != 0 || *(p+1) != 0) {
883 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
884 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200885 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887
888 /* There are 2 mandatory items: "status-code" and "message" */
889
890 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 while (p < end) {
892 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200893 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895
896 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 ret = spoe_decode_buffer(&p, end, &str, &sz);
898 if (ret == -1 || !sz) {
899 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
900 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 }
902
903 /* Check "status-code" K/V item */
904 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906
907 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
909 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
910 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
911 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100912 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
913 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200915 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
917 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100919 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921
922 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 else if (!memcmp(str, MSG_KEY, sz)) {
924 int type = *p++;
925
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
928 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
929 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 ret = spoe_decode_buffer(&p, end, &str, &sz);
932 if (ret == -1 || sz > 255) {
933 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
934 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
937 SPOE_APPCTX(appctx)->reason = str;
938 SPOE_APPCTX(appctx)->rlen = sz;
939#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200940 }
941 else {
942 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 if (spoe_skip_data(&p, end) == -1) {
944 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
945 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947 }
948 }
949
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951}
952
953
Christopher Fauleta1cda022016-12-21 08:58:06 +0100954/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955 * success, 0 if the frame can be ignored and -1 if an error occurred. */
956static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100958 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200959{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100960 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100962 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100963 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100964 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965
966 p = frame;
967 end = frame + size;
968 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969
970 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
972 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200975
Christopher Faulet8ef75252017-02-20 22:56:03 +0100976 if (size < 7 /* TYPE + METADATA */) {
977 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
978 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200979 }
980
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100981 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100982 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200983 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200985
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100986 /* Fragmentation is not supported for now */
987 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100989 return -1;
990 }
991
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200992 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200993 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100994 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100996 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200997 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100998 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200999 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001000 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001001
Christopher Faulet8ef75252017-02-20 22:56:03 +01001002 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +01001003 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +02001004 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001005 if ((*ctx)->stream_id == (unsigned int)stream_id &&
1006 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001007 goto found;
1008 }
1009 }
1010 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1012 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001013 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001014 goto found;
1015 }
1016 }
1017
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001018 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1019 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1020 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1021
1022 /* ABRT bit is set for an unfinished fragmented frame */
1023 if (flags & SPOE_FRM_FL_ABRT) {
1024 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001025 (*ctx)->state = SPOE_CTX_ST_ERROR;
1026 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1027 /* Ignore the payload */
1028 goto end;
1029 }
1030 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1031 /* For now, we ignore the ack */
1032 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1033 return 0;
1034 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001035
Christopher Fauleta1cda022016-12-21 08:58:06 +01001036 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1038 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001039 " - stream-id=%u - frame-id=%u\n",
1040 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1041 __FUNCTION__, appctx,
1042 (unsigned int)stream_id, (unsigned int)frame_id);
1043
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001044 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001045 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1046 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001047 return 0;
1048
1049 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1051 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001052 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001053 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001054 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001055
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001056 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001057 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001058 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1059 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001061
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001062 /* Transfer the buffer ownership to the SPOE context */
1063 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001064 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001065
Christopher Faulet8ef75252017-02-20 22:56:03 +01001066 (*ctx)->state = SPOE_CTX_ST_DONE;
1067
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001068 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001069 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 " - ACK frame received"
1071 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001072 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1074 (*ctx)->frame_id, flags);
1075 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001076}
1077
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078/* This function is used in cfgparse.c and declared in proto/checks.h. It
1079 * prepare the request to send to agents during a healthcheck. It returns 0 on
1080 * success and -1 if an error occurred. */
1081int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 struct appctx appctx;
1085 struct spoe_appctx spoe_appctx;
1086 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1087 size_t sz;
1088 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet42bfa462017-01-04 14:14:19 +01001090 memset(&appctx, 0, sizeof(appctx));
1091 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001093
1094 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001095 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1098 end = frame + MAX_FRAME_SIZE;
1099
1100 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1101 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104
Christopher Faulet8ef75252017-02-20 22:56:03 +01001105 /* Add "healthcheck" K/V item */
1106 sz = SLEN(HEALTHCHECK_KEY);
1107 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1108 return -1;
1109 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001110
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111 *len = frame - buf;
1112 sz = htonl(*len - 4);
1113 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001116 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118 return 0;
1119}
1120
1121/* This function is used in checks.c and declared in proto/checks.h. It decode
1122 * the response received from an agent during a healthcheck. It returns 0 on
1123 * success and -1 if an error occurred. */
1124int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001127 struct appctx appctx;
1128 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001129
Christopher Faulet42bfa462017-01-04 14:14:19 +01001130 memset(&appctx, 0, sizeof(appctx));
1131 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001132
Christopher Faulet42bfa462017-01-04 14:14:19 +01001133 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001134 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001135
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1137 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001138 goto error;
1139 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001140 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1141 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001142
1143 return 0;
1144
1145 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001146 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1147 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1148 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001149 return -1;
1150}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001151
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1153 * the frame can be ignored, 1 to retry later, and the frame legnth on
1154 * success. */
1155static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001157{
1158 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 int ret;
1160 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161
Christopher Faulet8ef75252017-02-20 22:56:03 +01001162 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1163 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164 netint = htonl(framesz);
1165 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001166 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001168 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001169 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 }
1172 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 return -1; /* error */
1174 }
1175 return framesz;
1176}
1177
1178/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1179 * when the frame can be ignored, 1 to retry later and the frame length on
1180 * success. */
1181static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001183{
1184 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001185 int ret;
1186 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001187
Willy Tarreau06d80a92017-10-19 14:32:15 +02001188 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001189 if (ret > 0) {
1190 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001191 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001192 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001193 return -1;
1194 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001195 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001196 }
1197 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001198 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001199 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001200 }
1201 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001202 return -1; /* error */
1203 }
1204 return framesz;
1205}
1206
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207/********************************************************************
1208 * Functions that manage the SPOE applet
1209 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001210static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001211spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001212{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001213 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001214 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001215 appctx_wakeup(appctx);
1216 return 1;
1217}
1218
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001219/* Callback function that catches applet timeouts. If a timeout occurred, we set
1220 * <appctx->st1> flag and the SPOE applet is woken up. */
1221static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001222spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001223{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001224 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225
1226 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1227 if (tick_is_expired(task->expire, now_ms)) {
1228 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001230 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001231 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001232 return task;
1233}
1234
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001235/* Callback function that releases a SPOE applet. This happens when the
1236 * connection with the agent is closed. */
1237static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001238spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001239{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 struct stream_interface *si = appctx->owner;
1241 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1242 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001243 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244
1245 if (spoe_appctx == NULL)
1246 return;
1247
1248 appctx->ctx.spoe.ptr = NULL;
1249 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001250
1251 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1252 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1253 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001254
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001256 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001257 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1259 LIST_DEL(&spoe_appctx->list);
1260 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001261 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001262 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001263
Christopher Faulet8ef75252017-02-20 22:56:03 +01001264 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001265 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001266 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1267 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001268 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001269 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001271 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1273 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001274
1275 si_shutw(si);
1276 si_shutr(si);
1277 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001278 }
1279
Christopher Faulet8ef75252017-02-20 22:56:03 +01001280 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001281 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001282
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 /* Notify all waiting streams */
1284 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001285 LIST_DEL(&ctx->list);
1286 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001287 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001288 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001289 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001291 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292 }
1293
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 /* If the applet was processing a fragmented frame, notify the
1295 * corresponding stream. */
1296 if (spoe_appctx->frag_ctx.ctx) {
1297 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001298 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001299 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001301 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1302 }
1303
Christopher Faulet24289f22017-09-25 14:48:02 +02001304 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001305 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001306
Christopher Faulet8ef75252017-02-20 22:56:03 +01001307 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001308 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 LIST_DEL(&ctx->list);
1310 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001311 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001312 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001313 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001314 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001317 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001318 LIST_DEL(&ctx->list);
1319 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001320 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001321 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001324 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1325 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001326
1327 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001328 /* Release allocated memory */
1329 spoe_release_buffer(&spoe_appctx->buffer,
1330 &spoe_appctx->buffer_wait);
1331 pool_free(pool_head_spoe_appctx, spoe_appctx);
1332
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001333 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001334 agent->rt[tid].frame_size = agent->max_frame_size;
1335 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1336 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337}
1338
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001340spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001341{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001343 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001346
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001347 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1348 /* closed */
1349 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1350 goto exit;
1351 }
1352
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001353 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001354 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001355 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001356 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1357 goto stop;
1358 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001359
Christopher Fauleta1cda022016-12-21 08:58:06 +01001360 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1362 " - Connection timed out\n",
1363 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1364 __FUNCTION__, appctx);
1365 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001367 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001368
Christopher Faulet42bfa462017-01-04 14:14:19 +01001369 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 SPOE_APPCTX(appctx)->task->expire =
1371 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372
Christopher Faulet8ef75252017-02-20 22:56:03 +01001373 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1374 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001376 ret = spoe_prepare_hahello_frame(appctx, frame,
1377 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001379 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380
1381 switch (ret) {
1382 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 case 0: /* ignore => an error, cannot be ignored */
1384 goto exit;
1385
1386 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001387 goto stop;
1388
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 default:
1390 /* HELLO frame successfully sent, now wait for the
1391 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1393 goto next;
1394 }
1395
1396 next:
1397 return 0;
1398 stop:
1399 return 1;
1400 exit:
1401 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1402 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403}
1404
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001407{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001408 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001409 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001410 char *frame;
1411 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001412
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413
Christopher Fauletb067b062017-01-04 16:39:11 +01001414 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001415 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001417 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001418
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1421 " - Connection timed out\n",
1422 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1423 __FUNCTION__, appctx);
1424 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 goto exit;
1426 }
1427
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001428 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001429 ret = spoe_recv_frame(appctx, frame,
1430 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 if (ret > 1) {
1432 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1433 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1434 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001436 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001437 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001438 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001439
Christopher Fauleta1cda022016-12-21 08:58:06 +01001440 switch (ret) {
1441 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001442 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001443 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1444 goto next;
1445
1446 case 1: /* retry later */
1447 goto stop;
1448
1449 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001450 /* HELLO handshake is finished, set the idle timeout and
1451 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001452 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001454 SPOE_APPCTX(appctx)->node.key = 0;
1455 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001456
1457 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001458 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001459 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001460 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001461
Christopher Fauleta1cda022016-12-21 08:58:06 +01001462 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001463 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001464 if (trash.data)
1465 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001466
1467 SPOE_APPCTX(appctx)->task->expire =
1468 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001469 return 0;
1470 stop:
1471 return 1;
1472 exit:
1473 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1474 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001475}
1476
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477
Christopher Fauleta1cda022016-12-21 08:58:06 +01001478static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001479spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001480{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001481 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1482 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001483 char *frame, *buf;
1484 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001485
Christopher Faulet8ef75252017-02-20 22:56:03 +01001486 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1487 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001488 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001489
1490 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1491 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1492 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1493 SPOE_APPCTX(appctx)->max_frame_size);
1494 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001495 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001496 *skip = 1;
1497 ret = 1;
1498 goto end;
1499 }
1500 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001501 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001502 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1503 SPOE_APPCTX(appctx)->max_frame_size);
1504
1505 }
1506
Christopher Faulet8ef75252017-02-20 22:56:03 +01001507 if (ret > 1)
1508 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001509
Christopher Faulet8ef75252017-02-20 22:56:03 +01001510 switch (ret) {
1511 case -1: /* error */
1512 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1513 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001514
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 case 0: /* ignore */
1516 if (ctx == NULL)
1517 goto abort_frag_frame;
1518
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001519 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 LIST_DEL(&ctx->list);
1521 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001522 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001523 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001524 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 ctx->state = SPOE_CTX_ST_ERROR;
1526 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1527 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001528 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001529 break;
1530
1531 case 1: /* retry */
1532 *skip = 1;
1533 break;
1534
1535 default:
1536 if (ctx == NULL)
1537 goto abort_frag_frame;
1538
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001539 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 LIST_DEL(&ctx->list);
1541 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001542 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001543 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001544 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1546 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1547 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001548 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 }
1551 goto end;
1552
1553 frag_frame_sent:
1554 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001555 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001556 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1557 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1558 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001559 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1560 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1561 goto end;
1562
1563 no_frag_frame_sent:
1564 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1565 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001566 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 }
1568 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1569 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1570 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1571 }
1572 else {
1573 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001574 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1576 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001577 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001578 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1580 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1581 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001582 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001583
Christopher Faulet8ef75252017-02-20 22:56:03 +01001584 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1585 goto end;
1586
1587 abort_frag_frame:
1588 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1589 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1590 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1591 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1592 goto end;
1593
1594 end:
1595 return ret;
1596}
1597
1598static int
1599spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1600{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001601 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 struct spoe_context *ctx = NULL;
1603 char *frame;
1604 int ret;
1605
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001606 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001607 ret = spoe_recv_frame(appctx, frame,
1608 SPOE_APPCTX(appctx)->max_frame_size);
1609 if (ret > 1) {
1610 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1611 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001612 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001613 goto end;
1614 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001615 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001616 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1617 }
1618 switch (ret) {
1619 case -1: /* error */
1620 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1621 break;
1622
1623 case 0: /* ignore */
1624 break;
1625
1626 case 1: /* retry */
1627 *skip = 1;
1628 break;
1629
1630 default:
1631 LIST_DEL(&ctx->list);
1632 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001633 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001634 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1635 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001636 if (ctx->spoe_appctx) {
1637 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001638 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001639 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001640 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1641 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1642 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1643 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1644 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1645 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1646 }
1647 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1648 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001649 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1650 break;
1651 }
1652
1653 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001654 if (trash.data)
1655 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001656 end:
1657 return ret;
1658}
1659
1660static int
1661spoe_handle_processing_appctx(struct appctx *appctx)
1662{
1663 struct stream_interface *si = appctx->owner;
1664 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001665 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001666
1667 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1668 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1669 goto exit;
1670 }
1671
1672 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1674 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1675 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1676 goto next;
1677 }
1678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001679 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001680 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001681 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1683 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001684 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001685
Christopher Faulet8f82b202018-01-24 16:23:03 +01001686 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1687 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 /* receiving_frame loop */
1690 while (!skip_receiving) {
1691 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1692 switch (ret) {
1693 case -1: /* error */
1694 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001695
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 case 0: /* ignore */
1697 active_r = 1;
1698 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001699
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 case 1: /* retry */
1701 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001702
Christopher Faulet8f82b202018-01-24 16:23:03 +01001703 default:
1704 active_r = 1;
1705 break;
1706 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 /* send_frame loop */
1710 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1711 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1712 switch (ret) {
1713 case -1: /* error */
1714 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001717 if (SPOE_APPCTX(appctx)->node.key)
1718 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 active_s++;
1720 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 case 1: /* retry */
1723 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724
Christopher Faulet8f82b202018-01-24 16:23:03 +01001725 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001726 if (SPOE_APPCTX(appctx)->node.key)
1727 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001728 active_s++;
1729 break;
1730 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732
Christopher Faulet8f82b202018-01-24 16:23:03 +01001733 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001734 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1735 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1736 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001737
Christopher Faulet8f82b202018-01-24 16:23:03 +01001738 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001739 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001740 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001741 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742 }
1743 return 1;
1744
Christopher Faulet8f82b202018-01-24 16:23:03 +01001745 next:
1746 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1747 return 0;
1748
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749 exit:
1750 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1751 return 0;
1752}
1753
1754static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756{
1757 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001758 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 char *frame, *buf;
1760 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001761
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1763 goto exit;
1764
1765 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1766 goto exit;
1767
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1769 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001770 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1772 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001774 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001775
1776 switch (ret) {
1777 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001778 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001779 goto exit;
1780
1781 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782 goto stop;
1783
1784 default:
1785 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1786 " - disconnected by HAProxy (%d): %s\n",
1787 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001788 __FUNCTION__, appctx,
1789 SPOE_APPCTX(appctx)->status_code,
1790 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791
1792 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1793 goto next;
1794 }
1795
1796 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001797 SPOE_APPCTX(appctx)->task->expire =
1798 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799 return 0;
1800 stop:
1801 return 1;
1802 exit:
1803 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1804 return 0;
1805}
1806
1807static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809{
1810 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 char *frame;
1812 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813
Christopher Fauletb067b062017-01-04 16:39:11 +01001814 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001817 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818
Christopher Fauletb067b062017-01-04 16:39:11 +01001819 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001820 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001822 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001824 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 ret = spoe_recv_frame(appctx, frame,
1826 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001827 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001828 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 }
1831
1832 switch (ret) {
1833 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1835 " - error on frame (%s)\n",
1836 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001837 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001839 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840 goto exit;
1841
1842 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001843 goto next;
1844
1845 case 1: /* retry */
1846 goto stop;
1847
1848 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001852 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1854 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 goto exit;
1856 }
1857
1858 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001859 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001860 if (trash.data)
1861 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 return 0;
1864 stop:
1865 return 1;
1866 exit:
1867 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1868 return 0;
1869}
1870
1871/* I/O Handler processing messages exchanged with the agent */
1872static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001873spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 struct stream_interface *si = appctx->owner;
1876 struct spoe_agent *agent;
1877
1878 if (SPOE_APPCTX(appctx) == NULL)
1879 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880
Christopher Faulet8ef75252017-02-20 22:56:03 +01001881 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1882 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001883
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 switchstate:
1885 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1886 " - appctx-state=%s\n",
1887 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1888 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1889
1890 switch (appctx->st0) {
1891 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001892 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 goto out;
1894 goto switchstate;
1895
1896 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001897 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 goto out;
1899 goto switchstate;
1900
1901 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001902 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001903 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001905 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001906 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001907 SPOE_APPCTX(appctx)->task->expire =
1908 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001910 goto switchstate;
1911 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001912 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1913 /* fall through */
1914
1915 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001916 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1917 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001918 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001919 goto out;
1920 goto switchstate;
1921
1922 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001923 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001924 goto out;
1925 goto switchstate;
1926
1927 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001928 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929 goto out;
1930 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001931
1932 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001933 appctx->st0 = SPOE_APPCTX_ST_END;
1934 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1935
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 si_shutw(si);
1937 si_shutr(si);
1938 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939 /* fall through */
1940
1941 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001942 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001944 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001945 if (stopping)
1946 spoe_wakeup_appctx(appctx);
1947
Christopher Faulet42bfa462017-01-04 14:14:19 +01001948 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1949 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001950}
1951
1952struct applet spoe_applet = {
1953 .obj_type = OBJ_TYPE_APPLET,
1954 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001955 .fct = spoe_handle_appctx,
1956 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957};
1958
1959/* Create a SPOE applet. On success, the created applet is returned, else
1960 * NULL. */
1961static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001962spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963{
1964 struct appctx *appctx;
1965 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001966 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967
Emeric Brun1138fd02017-06-19 12:38:55 +02001968 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 goto out_error;
1970
Willy Tarreaubafbe012017-11-24 17:34:44 +01001971 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001972 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001974 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001975
Christopher Faulet42bfa462017-01-04 14:14:19 +01001976 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001977 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001978 goto out_free_spoe_appctx;
1979
1980 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001981 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001982 SPOE_APPCTX(appctx)->task->context = appctx;
1983 SPOE_APPCTX(appctx)->agent = conf->agent;
1984 SPOE_APPCTX(appctx)->version = 0;
1985 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1986 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001987 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001988 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001989 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001990
1991 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1992 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001993 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001994
1995 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1996 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997
Willy Tarreau5820a362016-12-22 15:59:02 +01001998 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 if (!sess)
2000 goto out_free_spoe;
2001
Willy Tarreau87787ac2017-08-28 16:22:54 +02002002 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003 goto out_free_sess;
2004
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 stream_set_backend(strm, conf->agent->b.be);
2006
2007 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002008 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009 appctx_wakeup(appctx);
2010
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011 strm->do_log = NULL;
2012 strm->res.flags |= CF_READ_DONTWAIT;
2013
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002014 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002015 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002016 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002017 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002018
Emeric Brunc60def82017-09-27 14:59:38 +02002019 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002020 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021 return appctx;
2022
2023 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 out_free_sess:
2025 session_free(sess);
2026 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002027 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002028 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002029 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030 out_free_appctx:
2031 appctx_free(appctx);
2032 out_error:
2033 return NULL;
2034}
2035
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002037spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038{
2039 struct spoe_config *conf = FLT_CONF(ctx->filter);
2040 struct spoe_agent *agent = conf->agent;
2041 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002042 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002043
Christopher Fauleta1cda022016-12-21 08:58:06 +01002044 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002045 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002046 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002047 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048
2049 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002050 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2052 ctx->strm);
2053
Christopher Fauleta1cda022016-12-21 08:58:06 +01002054 /* Do not try to create a new applet if there is no server up for the
2055 * agent's backend. */
2056 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2057 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2058 " - cannot create SPOE appctx: no server up\n",
2059 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2060 __FUNCTION__, ctx->strm);
2061 goto end;
2062 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 /* Do not try to create a new applet if we have reached the maximum of
2065 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002066 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002067 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002068 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2069 " - cannot create SPOE appctx: max CPS reached\n",
2070 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2071 __FUNCTION__, ctx->strm);
2072 goto end;
2073 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074 }
2075
Christopher Faulet8ef75252017-02-20 22:56:03 +01002076 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 if (appctx == NULL) {
2078 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2079 " - failed to create SPOE appctx\n",
2080 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2081 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002082 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002083 "SPOE: [%s] failed to create SPOE applet\n",
2084 agent->id);
2085
Christopher Fauleta1cda022016-12-21 08:58:06 +01002086 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087 }
2088
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 /* Increase the per-process number of cumulated connections */
2090 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002091 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092
Christopher Fauleta1cda022016-12-21 08:58:06 +01002093 end:
2094 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002095 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002096 ctx->status_code = SPOE_CTX_ERR_RES;
2097 return -1;
2098 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002100 /* Add the SPOE context in the sending queue if the stream has no applet
2101 * already assigned and wakeup all idle applets. Otherwise, don't queue
2102 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002103 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002104 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2105 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002106 if (ctx->spoe_appctx)
2107 return 1;
2108 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002109
2110 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002111 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002112 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002113 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002114 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002115 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002116
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002117 /* Finally try to wakeup an IDLE applet. */
2118 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2119 struct eb32_node *node;
2120
2121 node = eb32_first(&agent->rt[tid].idle_applets);
2122 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2123 if (node && spoe_appctx) {
2124 eb32_delete(&spoe_appctx->node);
2125 spoe_appctx->node.key++;
2126 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2127 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002128 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002129 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002130 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002131}
2132
2133/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002134 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002135 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002136/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2137 * fragmented_content. If the next message can be processed, it returns 0. If
2138 * the message is too big, it returns -1.*/
2139static int
2140spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2141 struct spoe_message *msg, int dir,
2142 char **buf, char *end)
2143{
2144 struct sample *smp;
2145 struct spoe_arg *arg;
2146 int ret;
2147
2148 if (msg->cond) {
2149 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2150 ret = acl_pass(ret);
2151 if (msg->cond->pol == ACL_COND_UNLESS)
2152 ret = !ret;
2153
2154 /* the rule does not match */
2155 if (!ret)
2156 goto next;
2157 }
2158
2159 /* Resume encoding of a SPOE argument */
2160 if (ctx->frag_ctx.curarg != NULL) {
2161 arg = ctx->frag_ctx.curarg;
2162 goto encode_argument;
2163 }
2164
2165 if (ctx->frag_ctx.curoff != UINT_MAX)
2166 goto encode_msg_payload;
2167
2168 /* Check if there is enough space for the message name and the
2169 * number of arguments. It implies <msg->id_len> is encoded on 2
2170 * bytes, at most (< 2288). */
2171 if (*buf + 2 + msg->id_len + 1 > end)
2172 goto too_big;
2173
2174 /* Encode the message name */
2175 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2176 goto too_big;
2177
2178 /* Set the number of arguments for this message */
2179 **buf = msg->nargs;
2180 (*buf)++;
2181
2182 ctx->frag_ctx.curoff = 0;
2183 encode_msg_payload:
2184
2185 /* Loop on arguments */
2186 list_for_each_entry(arg, &msg->args, list) {
2187 ctx->frag_ctx.curarg = arg;
2188 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002189 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002190
2191 encode_argument:
2192 if (ctx->frag_ctx.curoff != UINT_MAX)
2193 goto encode_arg_value;
2194
Joseph Herlantf7f60312018-11-15 13:46:49 -08002195 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002196 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2197 goto too_big;
2198
2199 ctx->frag_ctx.curoff = 0;
2200 encode_arg_value:
2201
Joseph Herlantf7f60312018-11-15 13:46:49 -08002202 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002203 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002204 if (smp) {
2205 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2206 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2207 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002208 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002209 if (ret == -1 || ctx->frag_ctx.curoff)
2210 goto too_big;
2211 }
2212
2213 next:
2214 return 0;
2215
2216 too_big:
2217 return -1;
2218}
2219
Christopher Fauletc718b822017-09-21 16:50:56 +02002220/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2221 * handle fragmented content. On success it returns 1. If an error occurred, -1
2222 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2223 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002224static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002225spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002226 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002227{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002228 struct spoe_config *conf = FLT_CONF(ctx->filter);
2229 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002230 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002231 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002233 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002234 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002235
Christopher Fauletc718b822017-09-21 16:50:56 +02002236 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2237 /* Resume encoding of a SPOE message */
2238 if (ctx->frag_ctx.curmsg != NULL) {
2239 msg = ctx->frag_ctx.curmsg;
2240 goto encode_evt_message;
2241 }
2242
2243 list_for_each_entry(msg, messages, by_evt) {
2244 ctx->frag_ctx.curmsg = msg;
2245 ctx->frag_ctx.curarg = NULL;
2246 ctx->frag_ctx.curoff = UINT_MAX;
2247
2248 encode_evt_message:
2249 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2250 goto too_big;
2251 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002253 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2254 /* Resume encoding of a SPOE message */
2255 if (ctx->frag_ctx.curmsg != NULL) {
2256 msg = ctx->frag_ctx.curmsg;
2257 goto encode_grp_message;
2258 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259
Christopher Fauletc718b822017-09-21 16:50:56 +02002260 list_for_each_entry(msg, messages, by_grp) {
2261 ctx->frag_ctx.curmsg = msg;
2262 ctx->frag_ctx.curarg = NULL;
2263 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002264
Christopher Fauletc718b822017-09-21 16:50:56 +02002265 encode_grp_message:
2266 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2267 goto too_big;
2268 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002269 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002270 else
2271 goto skip;
2272
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002273
Christopher Faulet57583e42017-09-04 15:41:09 +02002274 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002275 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002276 goto skip;
2277
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002278 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002279 " - encode %s messages - spoe_appctx=%p"
2280 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281 (int)now.tv_sec, (int)now.tv_usec,
2282 agent->id, __FUNCTION__, s,
2283 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002284 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002285 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002287 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 ctx->frag_ctx.curmsg = NULL;
2289 ctx->frag_ctx.curarg = NULL;
2290 ctx->frag_ctx.curoff = 0;
2291 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002293 return 1;
2294
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002295 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002296 /* Return an error if fragmentation is unsupported or if nothing has
2297 * been encoded because its too big and not splittable. */
2298 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002299 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2300 return -1;
2301 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002302
2303 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002304 " - encode fragmented messages - spoe_appctx=%p"
2305 " - curmsg=%p - curarg=%p - curoff=%u"
2306 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002307 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002308 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002309 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002310 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002311
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002312 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002313 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2314 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2315 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002316
2317 skip:
2318 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2319 " - skip the frame because nothing has been encoded\n",
2320 (int)now.tv_sec, (int)now.tv_usec,
2321 agent->id, __FUNCTION__, s);
2322 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323}
2324
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002325
2326/***************************************************************************
2327 * Functions that handle SPOE actions
2328 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329/* Helper function to set a variable */
2330static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002332 struct sample *smp)
2333{
2334 struct spoe_config *conf = FLT_CONF(ctx->filter);
2335 struct spoe_agent *agent = conf->agent;
2336 char varname[64];
2337
2338 memset(varname, 0, sizeof(varname));
2339 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2340 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002341 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2342 vars_set_by_name(varname, len, smp);
2343 else
2344 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002345}
2346
2347/* Helper function to unset a variable */
2348static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350 struct sample *smp)
2351{
2352 struct spoe_config *conf = FLT_CONF(ctx->filter);
2353 struct spoe_agent *agent = conf->agent;
2354 char varname[64];
2355
2356 memset(varname, 0, sizeof(varname));
2357 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2358 scope, agent->var_pfx, len, name);
2359 vars_unset_by_name_ifexist(varname, len, smp);
2360}
2361
2362
Christopher Faulet8ef75252017-02-20 22:56:03 +01002363static inline int
2364spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2365 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002366{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367 char *str, *scope, *p = *buf;
2368 struct sample smp;
2369 uint64_t sz;
2370 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 if (p + 2 >= end)
2373 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 /* SET-VAR requires 3 arguments */
2376 if (*p++ != 3)
2377 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378
Christopher Faulet8ef75252017-02-20 22:56:03 +01002379 switch (*p++) {
2380 case SPOE_SCOPE_PROC: scope = "proc"; break;
2381 case SPOE_SCOPE_SESS: scope = "sess"; break;
2382 case SPOE_SCOPE_TXN : scope = "txn"; break;
2383 case SPOE_SCOPE_REQ : scope = "req"; break;
2384 case SPOE_SCOPE_RES : scope = "res"; break;
2385 default: goto skip;
2386 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2389 goto skip;
2390 memset(&smp, 0, sizeof(smp));
2391 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002392
Christopher Faulet8ef75252017-02-20 22:56:03 +01002393 if (spoe_decode_data(&p, end, &smp) == -1)
2394 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002395
Christopher Faulet8ef75252017-02-20 22:56:03 +01002396 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2397 " - set-var '%s.%s.%.*s'\n",
2398 (int)now.tv_sec, (int)now.tv_usec,
2399 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2400 __FUNCTION__, s, scope,
2401 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2402 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 ret = (p - *buf);
2407 *buf = p;
2408 return ret;
2409 skip:
2410 return 0;
2411}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413static inline int
2414spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2415 char **buf, char *end, int dir)
2416{
2417 char *str, *scope, *p = *buf;
2418 struct sample smp;
2419 uint64_t sz;
2420 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 if (p + 2 >= end)
2423 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002424
Christopher Faulet8ef75252017-02-20 22:56:03 +01002425 /* UNSET-VAR requires 2 arguments */
2426 if (*p++ != 2)
2427 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet8ef75252017-02-20 22:56:03 +01002429 switch (*p++) {
2430 case SPOE_SCOPE_PROC: scope = "proc"; break;
2431 case SPOE_SCOPE_SESS: scope = "sess"; break;
2432 case SPOE_SCOPE_TXN : scope = "txn"; break;
2433 case SPOE_SCOPE_REQ : scope = "req"; break;
2434 case SPOE_SCOPE_RES : scope = "res"; break;
2435 default: goto skip;
2436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2439 goto skip;
2440 memset(&smp, 0, sizeof(smp));
2441 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2444 " - unset-var '%s.%s.%.*s'\n",
2445 (int)now.tv_sec, (int)now.tv_usec,
2446 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2447 __FUNCTION__, s, scope,
2448 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2449 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451 spoe_unset_var(ctx, scope, str, sz, &smp);
2452
2453 ret = (p - *buf);
2454 *buf = p;
2455 return ret;
2456 skip:
2457 return 0;
2458}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460/* Process SPOE actions for a specific event. It returns 1 on success. If an
2461 * error occurred, 0 is returned. */
2462static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002463spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464{
2465 char *p, *end;
2466 int ret;
2467
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002468 p = b_head(&ctx->buffer);
2469 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002470
2471 while (p < end) {
2472 enum spoe_action_type type;
2473
2474 type = *p++;
2475 switch (type) {
2476 case SPOE_ACT_T_SET_VAR:
2477 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2478 if (!ret)
2479 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002480 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Faulet8ef75252017-02-20 22:56:03 +01002482 case SPOE_ACT_T_UNSET_VAR:
2483 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2484 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002485 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002486 break;
2487
2488 default:
2489 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002490 }
2491 }
2492
2493 return 1;
2494 skip:
2495 return 0;
2496}
2497
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002498/***************************************************************************
2499 * Functions that process SPOE events
2500 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002501static void
2502spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2503 struct spoe_context *ctx, int dir)
2504{
2505 if (!tv_iszero(&ctx->stats.tv_start)) {
2506 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2507 ctx->stats.t_total += ctx->stats.t_process;
2508 tv_zero(&ctx->stats.tv_request);
2509 tv_zero(&ctx->stats.tv_queue);
2510 tv_zero(&ctx->stats.tv_wait);
2511 tv_zero(&ctx->stats.tv_response);
2512 }
2513
2514 if (agent->var_t_process) {
2515 struct sample smp;
2516
2517 memset(&smp, 0, sizeof(smp));
2518 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2519 smp.data.u.sint = ctx->stats.t_process;
2520 smp.data.type = SMP_T_SINT;
2521
2522 spoe_set_var(ctx, "txn", agent->var_t_process,
2523 strlen(agent->var_t_process), &smp);
2524 }
2525
2526 if (agent->var_t_total) {
2527 struct sample smp;
2528
2529 memset(&smp, 0, sizeof(smp));
2530 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2531 smp.data.u.sint = ctx->stats.t_total;
2532 smp.data.type = SMP_T_SINT;
2533
2534 spoe_set_var(ctx, "txn", agent->var_t_total,
2535 strlen(agent->var_t_total), &smp);
2536 }
2537}
2538
2539static void
2540spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2541 struct spoe_context *ctx, int dir)
2542{
2543 if (agent->eps_max > 0)
2544 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2545
2546 if (agent->var_on_error) {
2547 struct sample smp;
2548
2549 memset(&smp, 0, sizeof(smp));
2550 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2551 smp.data.u.sint = ctx->status_code;
2552 smp.data.type = SMP_T_BOOL;
2553
2554 spoe_set_var(ctx, "txn", agent->var_on_error,
2555 strlen(agent->var_on_error), &smp);
2556 }
2557
2558 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2559 ? SPOE_CTX_ST_READY
2560 : SPOE_CTX_ST_NONE);
2561}
2562
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002563static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002564spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566 /* If a process is already started for this SPOE context, retry
2567 * later. */
2568 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002571 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002572 ctx->stats.tv_start = now;
2573 ctx->stats.tv_request = now;
2574 ctx->stats.t_request = -1;
2575 ctx->stats.t_queue = -1;
2576 ctx->stats.t_waiting = -1;
2577 ctx->stats.t_response = -1;
2578 ctx->stats.t_process = -1;
2579
2580 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002581
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 /* Set the right flag to prevent request and response processing
2583 * in same time. */
2584 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2585 ? SPOE_CTX_FL_REQ_PROCESS
2586 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588}
2589
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002590static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002591spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002593 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002595 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2596 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002597 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002598 if (sa) {
2599 if (sa->frag_ctx.ctx == ctx) {
2600 sa->frag_ctx.ctx = NULL;
2601 spoe_wakeup_appctx(sa->owner);
2602 }
2603 else
2604 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605 }
2606
Christopher Fauleta1cda022016-12-21 08:58:06 +01002607 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002608 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002609 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610
2611 /* Reset processing timer */
2612 ctx->process_exp = TICK_ETERNITY;
2613
Christopher Faulet8ef75252017-02-20 22:56:03 +01002614 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615
Christopher Fauletfce747b2018-01-24 15:59:32 +01002616 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002617 ctx->frag_ctx.curmsg = NULL;
2618 ctx->frag_ctx.curarg = NULL;
2619 ctx->frag_ctx.curoff = 0;
2620 ctx->frag_ctx.flags = 0;
2621
Christopher Fauleta1cda022016-12-21 08:58:06 +01002622 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002623 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002624 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002625 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002626 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002627
Christopher Fauleta1cda022016-12-21 08:58:06 +01002628 LIST_DEL(&ctx->list);
2629 LIST_INIT(&ctx->list);
2630 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002631}
2632
Christopher Faulet58d03682017-09-21 16:57:24 +02002633/* Process a list of SPOE messages. First, this functions will process messages
2634 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2635 * to process corresponding actions. During all the processing, it returns 0
2636 * and it returns 1 when the processing is finished. If an error occurred, -1
2637 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002638static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002639spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2640 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002641{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002642 struct spoe_config *conf = FLT_CONF(ctx->filter);
2643 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002645
2646 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002647 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002648
2649 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2650 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002651 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002652 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002653 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002654 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002655 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002656 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002657
2658 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002660 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002662 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002663 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 goto skip;
2666 }
2667 }
2668
Christopher Fauletf7a30922016-11-10 15:04:51 +01002669 if (!tick_isset(ctx->process_exp)) {
2670 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2671 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2672 ctx->process_exp);
2673 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002674 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002675 if (!ret)
2676 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002677
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002678 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002679 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002680 }
2681
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002682 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002683 if (tv_iszero(&ctx->stats.tv_request))
2684 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002685 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002686 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002687 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002688 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002689 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002690 if (!ret)
2691 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002692 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002693 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002694 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2695 }
2696
2697 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002698 if (ctx->spoe_appctx)
2699 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 ret = 0;
2701 goto out;
2702 }
2703
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002704 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2705 ret = 0;
2706 goto out;
2707 }
2708
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002709 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002710 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002711 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002712 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002714 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002715 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002716 }
2717
2718 out:
2719 return ret;
2720
Christopher Fauleta1cda022016-12-21 08:58:06 +01002721 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002722 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002723 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002724 spoe_stop_processing(agent, ctx);
2725 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002726
Christopher Fauleta1cda022016-12-21 08:58:06 +01002727 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002728 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002729 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002730 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002731 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002732 spoe_handle_processing_error(s, agent, ctx, dir);
2733 ret = 1;
2734 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002735 return ret;
2736}
2737
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002738/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2739 * See spoe_process_message for details. */
2740static int
2741spoe_process_group(struct stream *s, struct spoe_context *ctx,
2742 struct spoe_group *group, int dir)
2743{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 struct spoe_config *conf = FLT_CONF(ctx->filter);
2745 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002746 int ret;
2747
2748 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2749 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002750 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002751 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2752 group->id);
2753
2754 if (LIST_ISEMPTY(&group->messages))
2755 return 1;
2756
2757 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002758 if (ret && ctx->stats.t_process != -1) {
2759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002760 " - <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 +01002761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002762 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002763 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002764 ctx->stats.t_response, ctx->stats.t_process,
2765 agent->counters.idles, agent->counters.applets,
2766 agent->counters.nb_sending, agent->counters.nb_waiting,
2767 agent->counters.nb_errors, agent->counters.nb_processed,
2768 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2769 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2770 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2771 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2772 agent->id, group->id, s->uniq_id, ctx->status_code,
2773 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2774 ctx->stats.t_response, ctx->stats.t_process,
2775 agent->counters.idles, agent->counters.applets,
2776 agent->counters.nb_sending, agent->counters.nb_waiting,
2777 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002778 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002779 return ret;
2780}
2781
Christopher Faulet58d03682017-09-21 16:57:24 +02002782/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2783 * See spoe_process_message for details. */
2784static int
2785spoe_process_event(struct stream *s, struct spoe_context *ctx,
2786 enum spoe_event ev)
2787{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002788 struct spoe_config *conf = FLT_CONF(ctx->filter);
2789 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002790 int dir, ret;
2791
2792 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002793 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002794 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002795 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2796 spoe_event_str[ev]);
2797
2798 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2799
2800 if (LIST_ISEMPTY(&(ctx->events[ev])))
2801 return 1;
2802
2803 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002804 if (ret && ctx->stats.t_process != -1) {
2805 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002806 " - <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 +01002807 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2808 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2809 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002810 ctx->stats.t_response, ctx->stats.t_process,
2811 agent->counters.idles, agent->counters.applets,
2812 agent->counters.nb_sending, agent->counters.nb_waiting,
2813 agent->counters.nb_errors, agent->counters.nb_processed,
2814 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2815 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2816 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2817 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2818 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2819 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2820 ctx->stats.t_response, ctx->stats.t_process,
2821 agent->counters.idles, agent->counters.applets,
2822 agent->counters.nb_sending, agent->counters.nb_waiting,
2823 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002824 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002825 return ret;
2826}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002827
2828/***************************************************************************
2829 * Functions that create/destroy SPOE contexts
2830 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002831static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002833{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002835 return 1;
2836
Christopher Faulet4596fb72017-01-11 14:05:19 +01002837 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002838 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002839 LIST_DEL(&buffer_wait->list);
2840 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002841 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002842 }
2843
Christopher Faulet4596fb72017-01-11 14:05:19 +01002844 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002845 return 1;
2846
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002847 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002848 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002849 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002850 return 0;
2851}
2852
2853static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002854spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002855{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002856 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002857 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002858 LIST_DEL(&buffer_wait->list);
2859 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002860 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002861 }
2862
2863 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002864 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002865 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002866 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002867 }
2868}
2869
Christopher Faulet4596fb72017-01-11 14:05:19 +01002870static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002871spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002872{
2873 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2874 return 1;
2875}
2876
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002877static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002878spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002879{
2880 struct spoe_config *conf = FLT_CONF(filter);
2881 struct spoe_context *ctx;
2882
Willy Tarreaubafbe012017-11-24 17:34:44 +01002883 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002884 if (ctx == NULL) {
2885 return NULL;
2886 }
2887 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002888 ctx->filter = filter;
2889 ctx->state = SPOE_CTX_ST_NONE;
2890 ctx->status_code = SPOE_CTX_ERR_NONE;
2891 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002892 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002893 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002894 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002895 LIST_INIT(&ctx->buffer_wait.list);
2896 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002897 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002898 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002899
Christopher Fauletf7a30922016-11-10 15:04:51 +01002900 ctx->stream_id = 0;
2901 ctx->frame_id = 1;
2902 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002903
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002904 tv_zero(&ctx->stats.tv_start);
2905 tv_zero(&ctx->stats.tv_request);
2906 tv_zero(&ctx->stats.tv_queue);
2907 tv_zero(&ctx->stats.tv_wait);
2908 tv_zero(&ctx->stats.tv_response);
2909 ctx->stats.t_request = -1;
2910 ctx->stats.t_queue = -1;
2911 ctx->stats.t_waiting = -1;
2912 ctx->stats.t_response = -1;
2913 ctx->stats.t_process = -1;
2914 ctx->stats.t_total = 0;
2915
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002916 ctx->strm = s;
2917 ctx->state = SPOE_CTX_ST_READY;
2918 filter->ctx = ctx;
2919
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002920 return ctx;
2921}
2922
2923static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002924spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002925{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002926 struct spoe_config *conf = FLT_CONF(filter);
2927 struct spoe_context *ctx = filter->ctx;
2928
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002929 if (!ctx)
2930 return;
2931
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002932 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002933 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002934 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935}
2936
2937static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002938spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002939{
2940 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002941 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002942
2943 tv_zero(&ctx->stats.tv_start);
2944 tv_zero(&ctx->stats.tv_request);
2945 tv_zero(&ctx->stats.tv_queue);
2946 tv_zero(&ctx->stats.tv_wait);
2947 tv_zero(&ctx->stats.tv_response);
2948 ctx->stats.t_request = -1;
2949 ctx->stats.t_queue = -1;
2950 ctx->stats.t_waiting = -1;
2951 ctx->stats.t_response = -1;
2952 ctx->stats.t_process = -1;
2953 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002954}
2955
2956
2957/***************************************************************************
2958 * Hooks that manage the filter lifecycle (init/check/deinit)
2959 **************************************************************************/
2960/* Signal handler: Do a soft stop, wakeup SPOE applet */
2961static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002962spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963{
2964 struct proxy *p;
2965
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002966 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967 while (p) {
2968 struct flt_conf *fconf;
2969
2970 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002971 struct spoe_config *conf;
2972 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002973 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002974 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002975
Christopher Faulet3b386a32017-02-23 10:17:15 +01002976 if (fconf->id != spoe_filter_id)
2977 continue;
2978
2979 conf = fconf->conf;
2980 agent = conf->agent;
2981
Christopher Faulet24289f22017-09-25 14:48:02 +02002982 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002983 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002984 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2985 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002986 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002987 }
2988 }
2989 p = p->next;
2990 }
2991}
2992
2993
2994/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2995static int
2996spoe_init(struct proxy *px, struct flt_conf *fconf)
2997{
2998 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002999
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003000 /* conf->agent_fe was already initialized during the config
3001 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003003 conf->agent_fe.cap = PR_CAP_FE;
3004 conf->agent_fe.mode = PR_MODE_TCP;
3005 conf->agent_fe.maxconn = 0;
3006 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3007 conf->agent_fe.conn_retries = CONN_RETRIES;
3008 conf->agent_fe.accept = frontend_accept;
3009 conf->agent_fe.srv = NULL;
3010 conf->agent_fe.timeout.client = TICK_ETERNITY;
3011 conf->agent_fe.default_target = &spoe_applet.obj_type;
3012 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3013
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003015 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003016 sighandler_registered = 1;
3017 }
3018
Christopher Faulet00292352019-01-09 15:05:10 +01003019 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003020 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021}
3022
3023/* Free ressources allocated by the SPOE filter. */
3024static void
3025spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3026{
3027 struct spoe_config *conf = fconf->conf;
3028
3029 if (conf) {
3030 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031
Christopher Faulet8ef75252017-02-20 22:56:03 +01003032 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003033 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 free(conf);
3035 }
3036 fconf->conf = NULL;
3037}
3038
3039/* Check configuration of a SPOE filter for a specified proxy.
3040 * Return 1 on error, else 0. */
3041static int
3042spoe_check(struct proxy *px, struct flt_conf *fconf)
3043{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003044 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 struct spoe_config *conf = fconf->conf;
3046 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003047 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048
Christopher Faulet7ee86672017-09-19 11:08:28 +02003049 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3050 * are uniq */
3051 list_for_each_entry(f, &px->filter_configs, list) {
3052 struct spoe_config *c = f->conf;
3053
3054 /* This is not an SPOE filter */
3055 if (f->id != spoe_filter_id)
3056 continue;
3057 /* This is the current SPOE filter */
3058 if (f == fconf)
3059 continue;
3060
3061 /* Check engine Id. It should be uniq */
3062 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003063 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3064 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003065 return 1;
3066 }
3067 }
3068
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003069 target = proxy_be_by_name(conf->agent->b.name);
3070 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003071 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3072 " declared at %s:%d.\n",
3073 px->id, conf->agent->b.name, conf->agent->id,
3074 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003075 return 1;
3076 }
3077 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003078 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3079 " at %s:%d does not support HTTP mode.\n",
3080 px->id, target->id, conf->agent->id,
3081 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082 return 1;
3083 }
3084
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003085 if (px->bind_proc & ~target->bind_proc) {
3086 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3087 " at %s:%d does not cover all of its processes.\n",
3088 px->id, target->id, conf->agent->id,
3089 conf->agent->conf.file, conf->agent->conf.line);
3090 return 1;
3091 }
3092
Christopher Fauletfe261552019-03-18 13:57:42 +01003093 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003094 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3095 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3096 return 1;
3097 }
3098 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003099 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003100 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3101 conf->agent->rt[i].processing = 0;
3102 LIST_INIT(&conf->agent->rt[i].applets);
3103 LIST_INIT(&conf->agent->rt[i].sending_queue);
3104 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3105 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003106 }
3107
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003108 free(conf->agent->b.name);
3109 conf->agent->b.name = NULL;
3110 conf->agent->b.be = target;
3111 return 0;
3112}
3113
Kevin Zhud87b1a52019-09-17 15:05:45 +02003114/* Initializes the SPOE filter for a proxy for a specific thread.
3115 * Returns a negative value if an error occurs. */
3116static int
3117spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3118{
3119 struct spoe_config *conf = fconf->conf;
3120 struct spoe_agent *agent = conf->agent;
3121
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003122 /* Use a != seed per process */
3123 if (relative_pid > 1 && tid == 0)
Christopher Faulet09bd9aa2019-09-17 15:07:02 +02003124 srandom(now_ms * pid);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003125
3126 agent->rt[tid].engine_id = generate_pseudo_uuid();
3127 if (agent->rt[tid].engine_id == NULL)
3128 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003129 return 0;
3130}
3131
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132/**************************************************************************
3133 * Hooks attached to a stream
3134 *************************************************************************/
3135/* Called when a filter instance is created and attach to a stream. It creates
3136 * the context that will be used to process this stream. */
3137static int
3138spoe_start(struct stream *s, struct filter *filter)
3139{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003140 struct spoe_config *conf = FLT_CONF(filter);
3141 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 struct spoe_context *ctx;
3143
3144 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003145 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146 __FUNCTION__, s);
3147
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003148 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003149 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3150 " - failed to create SPOE context\n",
3151 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003152 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003153 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003154 "SPOE: [%s] failed to create SPOE context\n",
3155 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003156 return 0;
3157 }
3158
Christopher Faulet11610f32017-09-21 10:23:10 +02003159 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3161
Christopher Faulet11610f32017-09-21 10:23:10 +02003162 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3164
Christopher Faulet11610f32017-09-21 10:23:10 +02003165 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003166 filter->pre_analyzers |= AN_RES_INSPECT;
3167
Christopher Faulet11610f32017-09-21 10:23:10 +02003168 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3170
Christopher Faulet11610f32017-09-21 10:23:10 +02003171 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003172 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3173
Christopher Faulet11610f32017-09-21 10:23:10 +02003174 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003175 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3176
3177 return 1;
3178}
3179
3180/* Called when a filter instance is detached from a stream. It release the
3181 * attached SPOE context. */
3182static void
3183spoe_stop(struct stream *s, struct filter *filter)
3184{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3186 (int)now.tv_sec, (int)now.tv_usec,
3187 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3188 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003189 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003190}
3191
Christopher Fauletf7a30922016-11-10 15:04:51 +01003192
3193/*
3194 * Called when the stream is woken up because of expired timer.
3195 */
3196static void
3197spoe_check_timeouts(struct stream *s, struct filter *filter)
3198{
3199 struct spoe_context *ctx = filter->ctx;
3200
Christopher Fauletac580602018-03-20 16:09:20 +01003201 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003202 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003203}
3204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205/* Called when we are ready to filter data on a channel */
3206static int
3207spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3208{
3209 struct spoe_context *ctx = filter->ctx;
3210 int ret = 1;
3211
3212 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3213 " - ctx-flags=0x%08x\n",
3214 (int)now.tv_sec, (int)now.tv_usec,
3215 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3216 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3217
Christopher Fauletb067b062017-01-04 16:39:11 +01003218 if (ctx->state == SPOE_CTX_ST_NONE)
3219 goto out;
3220
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 if (!(chn->flags & CF_ISRESP)) {
3222 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3223 chn->analysers |= AN_REQ_INSPECT_FE;
3224 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3225 chn->analysers |= AN_REQ_INSPECT_BE;
3226
3227 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3228 goto out;
3229
3230 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003231 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003232 if (!ret)
3233 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003234 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3235 }
3236 else {
3237 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3238 chn->analysers |= AN_RES_INSPECT;
3239
3240 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3241 goto out;
3242
Christopher Faulet8ef75252017-02-20 22:56:03 +01003243 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003244 if (!ret) {
3245 channel_dont_read(chn);
3246 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003247 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003248 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003249 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 }
3251
3252 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003253 return ret;
3254}
3255
3256/* Called before a processing happens on a given channel */
3257static int
3258spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3259 struct channel *chn, unsigned an_bit)
3260{
3261 struct spoe_context *ctx = filter->ctx;
3262 int ret = 1;
3263
3264 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3265 " - ctx-flags=0x%08x - ana=0x%08x\n",
3266 (int)now.tv_sec, (int)now.tv_usec,
3267 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3268 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3269 ctx->flags, an_bit);
3270
Christopher Fauletb067b062017-01-04 16:39:11 +01003271 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003272 goto out;
3273
3274 switch (an_bit) {
3275 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003276 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003277 break;
3278 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003279 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003280 break;
3281 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003282 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003283 break;
3284 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003285 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003286 break;
3287 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003288 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 break;
3290 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003291 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 break;
3293 }
3294
3295 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003296 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 channel_dont_read(chn);
3298 channel_dont_close(chn);
3299 }
3300 return ret;
3301}
3302
3303/* Called when the filtering on the channel ends. */
3304static int
3305spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3306{
3307 struct spoe_context *ctx = filter->ctx;
3308
3309 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3310 " - ctx-flags=0x%08x\n",
3311 (int)now.tv_sec, (int)now.tv_usec,
3312 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3313 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3314
3315 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003316 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 }
3318
3319 return 1;
3320}
3321
3322/********************************************************************
3323 * Functions that manage the filter initialization
3324 ********************************************************************/
3325struct flt_ops spoe_ops = {
3326 /* Manage SPOE filter, called for each filter declaration */
3327 .init = spoe_init,
3328 .deinit = spoe_deinit,
3329 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003330 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331
3332 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003333 .attach = spoe_start,
3334 .detach = spoe_stop,
3335 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003336
3337 /* Handle channels activity */
3338 .channel_start_analyze = spoe_start_analyze,
3339 .channel_pre_analyze = spoe_chn_pre_analyze,
3340 .channel_end_analyze = spoe_end_analyze,
3341};
3342
3343
3344static int
3345cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3346{
3347 const char *err;
3348 int i, err_code = 0;
3349
3350 if ((cfg_scope == NULL && curengine != NULL) ||
3351 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003352 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353 goto out;
3354
3355 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3356 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003357 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3358 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003359 err_code |= ERR_ALERT | ERR_ABORT;
3360 goto out;
3361 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003362 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3363 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003364 goto out;
3365 }
3366
3367 err = invalid_char(args[1]);
3368 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003369 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3370 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 err_code |= ERR_ALERT | ERR_ABORT;
3372 goto out;
3373 }
3374
3375 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003376 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3377 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 err_code |= ERR_ALERT | ERR_ABORT;
3379 goto out;
3380 }
3381 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003382 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003383 err_code |= ERR_ALERT | ERR_ABORT;
3384 goto out;
3385 }
3386
3387 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003388
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 curagent->conf.file = strdup(file);
3390 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003391
3392 curagent->timeout.hello = TICK_ETERNITY;
3393 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003394 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003395
Christopher Fauleta1cda022016-12-21 08:58:06 +01003396 curagent->var_pfx = NULL;
3397 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003398 curagent->var_t_process = NULL;
3399 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003400 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003401 curagent->cps_max = 0;
3402 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003403 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003404 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405
3406 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003407 LIST_INIT(&curagent->events[i]);
3408 LIST_INIT(&curagent->groups);
3409 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 }
3411 else if (!strcmp(args[0], "use-backend")) {
3412 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3414 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003415 err_code |= ERR_ALERT | ERR_FATAL;
3416 goto out;
3417 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003418 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003419 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003420 free(curagent->b.name);
3421 curagent->b.name = strdup(args[1]);
3422 }
3423 else if (!strcmp(args[0], "messages")) {
3424 int cur_arg = 1;
3425 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003426 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003427
Christopher Faulet11610f32017-09-21 10:23:10 +02003428 list_for_each_entry(ph, &curmphs, list) {
3429 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003430 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3431 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 err_code |= ERR_ALERT | ERR_FATAL;
3433 goto out;
3434 }
3435 }
3436
Christopher Faulet11610f32017-09-21 10:23:10 +02003437 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003438 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003439 err_code |= ERR_ALERT | ERR_ABORT;
3440 goto out;
3441 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003442 ph->id = strdup(args[cur_arg]);
3443 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444 cur_arg++;
3445 }
3446 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003447 else if (!strcmp(args[0], "groups")) {
3448 int cur_arg = 1;
3449 while (*args[cur_arg]) {
3450 struct spoe_placeholder *ph = NULL;
3451
3452 list_for_each_entry(ph, &curgphs, list) {
3453 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003454 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3455 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003456 err_code |= ERR_ALERT | ERR_FATAL;
3457 goto out;
3458 }
3459 }
3460
3461 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003462 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003463 err_code |= ERR_ALERT | ERR_ABORT;
3464 goto out;
3465 }
3466 ph->id = strdup(args[cur_arg]);
3467 LIST_ADDQ(&curgphs, &ph->list);
3468 cur_arg++;
3469 }
3470 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003471 else if (!strcmp(args[0], "timeout")) {
3472 unsigned int *tv = NULL;
3473 const char *res;
3474 unsigned timeout;
3475
3476 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3478 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003479 err_code |= ERR_ALERT | ERR_FATAL;
3480 goto out;
3481 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003482 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3483 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003484 if (!strcmp(args[1], "hello"))
3485 tv = &curagent->timeout.hello;
3486 else if (!strcmp(args[1], "idle"))
3487 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003488 else if (!strcmp(args[1], "processing"))
3489 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003490 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003491 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3492 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003493 err_code |= ERR_ALERT | ERR_FATAL;
3494 goto out;
3495 }
3496 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003497 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3498 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003499 err_code |= ERR_ALERT | ERR_FATAL;
3500 goto out;
3501 }
3502 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003503 if (res == PARSE_TIME_OVER) {
3504 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3505 file, linenum, args[2], args[0], args[1]);
3506 err_code |= ERR_ALERT | ERR_FATAL;
3507 goto out;
3508 }
3509 else if (res == PARSE_TIME_UNDER) {
3510 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3511 file, linenum, args[2], args[0], args[1]);
3512 err_code |= ERR_ALERT | ERR_FATAL;
3513 goto out;
3514 }
3515 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3517 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003518 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003519 goto out;
3520 }
3521 *tv = MS_TO_TICKS(timeout);
3522 }
3523 else if (!strcmp(args[0], "option")) {
3524 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003525 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3526 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003527 err_code |= ERR_ALERT | ERR_FATAL;
3528 goto out;
3529 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003530
Christopher Faulet305c6072017-02-23 16:17:53 +01003531 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003532 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003533 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003534 if (kwm == 1)
3535 curagent->flags &= ~SPOE_FL_PIPELINING;
3536 else
3537 curagent->flags |= SPOE_FL_PIPELINING;
3538 goto out;
3539 }
3540 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003541 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003542 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003543 if (kwm == 1)
3544 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003545 else
3546 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003547 goto out;
3548 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003549 else if (!strcmp(args[1], "send-frag-payload")) {
3550 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3551 goto out;
3552 if (kwm == 1)
3553 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3554 else
3555 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3556 goto out;
3557 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003558 else if (!strcmp(args[1], "dontlog-normal")) {
3559 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3560 goto out;
3561 if (kwm == 1)
3562 curpxopts2 &= ~PR_O2_NOLOGNORM;
3563 else
3564 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003565 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003566 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003567
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003568 /* Following options does not support negation */
3569 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003570 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3571 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003572 err_code |= ERR_ALERT | ERR_FATAL;
3573 goto out;
3574 }
3575
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003576 if (!strcmp(args[1], "var-prefix")) {
3577 char *tmp;
3578
3579 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003580 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3581 file, linenum, args[0],
3582 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003583 err_code |= ERR_ALERT | ERR_FATAL;
3584 goto out;
3585 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003586 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3587 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003588 tmp = args[2];
3589 while (*tmp) {
3590 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003591 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003592 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003593 err_code |= ERR_ALERT | ERR_FATAL;
3594 goto out;
3595 }
3596 tmp++;
3597 }
3598 curagent->var_pfx = strdup(args[2]);
3599 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003600 else if (!strcmp(args[1], "force-set-var")) {
3601 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3602 goto out;
3603 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3604 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003605 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003606 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003607 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003608 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3609 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003610 else if (!strcmp(args[1], "set-on-error")) {
3611 char *tmp;
3612
3613 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003614 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3615 file, linenum, args[0],
3616 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003617 err_code |= ERR_ALERT | ERR_FATAL;
3618 goto out;
3619 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003620 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3621 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003622 tmp = args[2];
3623 while (*tmp) {
3624 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003625 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003626 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003627 err_code |= ERR_ALERT | ERR_FATAL;
3628 goto out;
3629 }
3630 tmp++;
3631 }
3632 curagent->var_on_error = strdup(args[2]);
3633 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003634 else if (!strcmp(args[1], "set-process-time")) {
3635 char *tmp;
3636
3637 if (!*args[2]) {
3638 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3639 file, linenum, args[0],
3640 args[1]);
3641 err_code |= ERR_ALERT | ERR_FATAL;
3642 goto out;
3643 }
3644 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3645 goto out;
3646 tmp = args[2];
3647 while (*tmp) {
3648 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003649 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003650 file, linenum, args[0], args[1]);
3651 err_code |= ERR_ALERT | ERR_FATAL;
3652 goto out;
3653 }
3654 tmp++;
3655 }
3656 curagent->var_t_process = strdup(args[2]);
3657 }
3658 else if (!strcmp(args[1], "set-total-time")) {
3659 char *tmp;
3660
3661 if (!*args[2]) {
3662 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3663 file, linenum, args[0],
3664 args[1]);
3665 err_code |= ERR_ALERT | ERR_FATAL;
3666 goto out;
3667 }
3668 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3669 goto out;
3670 tmp = args[2];
3671 while (*tmp) {
3672 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003673 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003674 file, linenum, args[0], args[1]);
3675 err_code |= ERR_ALERT | ERR_FATAL;
3676 goto out;
3677 }
3678 tmp++;
3679 }
3680 curagent->var_t_total = strdup(args[2]);
3681 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003682 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003683 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3684 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003685 err_code |= ERR_ALERT | ERR_FATAL;
3686 goto out;
3687 }
Christopher Faulet48026722016-11-16 15:01:12 +01003688 }
3689 else if (!strcmp(args[0], "maxconnrate")) {
3690 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003691 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3692 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003693 err_code |= ERR_ALERT | ERR_FATAL;
3694 goto out;
3695 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003696 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003697 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003698 curagent->cps_max = atol(args[1]);
3699 }
3700 else if (!strcmp(args[0], "maxerrrate")) {
3701 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003702 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3703 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003704 err_code |= ERR_ALERT | ERR_FATAL;
3705 goto out;
3706 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003707 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003708 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003709 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003710 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003711 else if (!strcmp(args[0], "max-frame-size")) {
3712 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003713 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3714 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003715 err_code |= ERR_ALERT | ERR_FATAL;
3716 goto out;
3717 }
3718 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3719 goto out;
3720 curagent->max_frame_size = atol(args[1]);
3721 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3722 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003723 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3724 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003725 err_code |= ERR_ALERT | ERR_FATAL;
3726 goto out;
3727 }
3728 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003729 else if (!strcmp(args[0], "max-waiting-frames")) {
3730 if (!*args[1]) {
3731 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3732 file, linenum, args[0]);
3733 err_code |= ERR_ALERT | ERR_FATAL;
3734 goto out;
3735 }
3736 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3737 goto out;
3738 curagent->max_fpa = atol(args[1]);
3739 if (curagent->max_fpa < 1) {
3740 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3741 file, linenum, args[0]);
3742 err_code |= ERR_ALERT | ERR_FATAL;
3743 goto out;
3744 }
3745 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003746 else if (!strcmp(args[0], "register-var-names")) {
3747 int cur_arg;
3748
3749 if (!*args[1]) {
3750 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3751 file, linenum, args[0]);
3752 err_code |= ERR_ALERT | ERR_FATAL;
3753 goto out;
3754 }
3755 cur_arg = 1;
3756 while (*args[cur_arg]) {
3757 struct spoe_var_placeholder *vph;
3758
3759 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3760 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3761 err_code |= ERR_ALERT | ERR_ABORT;
3762 goto out;
3763 }
3764 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003765 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003766 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3767 err_code |= ERR_ALERT | ERR_ABORT;
3768 goto out;
3769 }
3770 LIST_ADDQ(&curvars, &vph->list);
3771 cur_arg++;
3772 }
3773 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003774 else if (!strcmp(args[0], "log")) {
3775 char *errmsg = NULL;
3776
3777 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3778 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3779 err_code |= ERR_ALERT | ERR_FATAL;
3780 goto out;
3781 }
3782 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003783 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003784 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3785 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003786 err_code |= ERR_ALERT | ERR_FATAL;
3787 goto out;
3788 }
3789 out:
3790 return err_code;
3791}
Christopher Faulet11610f32017-09-21 10:23:10 +02003792static int
3793cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3794{
3795 struct spoe_group *grp;
3796 const char *err;
3797 int err_code = 0;
3798
3799 if ((cfg_scope == NULL && curengine != NULL) ||
3800 (cfg_scope != NULL && curengine == NULL) ||
3801 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3802 goto out;
3803
3804 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3805 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003806 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3807 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003808 err_code |= ERR_ALERT | ERR_ABORT;
3809 goto out;
3810 }
3811 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3812 err_code |= ERR_ABORT;
3813 goto out;
3814 }
3815
3816 err = invalid_char(args[1]);
3817 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003818 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3819 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003820 err_code |= ERR_ALERT | ERR_ABORT;
3821 goto out;
3822 }
3823
3824 list_for_each_entry(grp, &curgrps, list) {
3825 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003826 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3827 " name as another one declared at %s:%d.\n",
3828 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003829 err_code |= ERR_ALERT | ERR_FATAL;
3830 goto out;
3831 }
3832 }
3833
3834 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003835 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003836 err_code |= ERR_ALERT | ERR_ABORT;
3837 goto out;
3838 }
3839
3840 curgrp->id = strdup(args[1]);
3841 curgrp->conf.file = strdup(file);
3842 curgrp->conf.line = linenum;
3843 LIST_INIT(&curgrp->phs);
3844 LIST_INIT(&curgrp->messages);
3845 LIST_ADDQ(&curgrps, &curgrp->list);
3846 }
3847 else if (!strcmp(args[0], "messages")) {
3848 int cur_arg = 1;
3849 while (*args[cur_arg]) {
3850 struct spoe_placeholder *ph = NULL;
3851
3852 list_for_each_entry(ph, &curgrp->phs, list) {
3853 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003854 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3855 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003856 err_code |= ERR_ALERT | ERR_FATAL;
3857 goto out;
3858 }
3859 }
3860
3861 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003863 err_code |= ERR_ALERT | ERR_ABORT;
3864 goto out;
3865 }
3866 ph->id = strdup(args[cur_arg]);
3867 LIST_ADDQ(&curgrp->phs, &ph->list);
3868 cur_arg++;
3869 }
3870 }
3871 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003872 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3873 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003874 err_code |= ERR_ALERT | ERR_FATAL;
3875 goto out;
3876 }
3877 out:
3878 return err_code;
3879}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003880
3881static int
3882cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3883{
3884 struct spoe_message *msg;
3885 struct spoe_arg *arg;
3886 const char *err;
3887 char *errmsg = NULL;
3888 int err_code = 0;
3889
3890 if ((cfg_scope == NULL && curengine != NULL) ||
3891 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003892 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003893 goto out;
3894
3895 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3896 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003897 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3898 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 err_code |= ERR_ALERT | ERR_ABORT;
3900 goto out;
3901 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003902 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3903 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003904 goto out;
3905 }
3906
3907 err = invalid_char(args[1]);
3908 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003909 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3910 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003911 err_code |= ERR_ALERT | ERR_ABORT;
3912 goto out;
3913 }
3914
3915 list_for_each_entry(msg, &curmsgs, list) {
3916 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003917 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3918 " name as another one declared at %s:%d.\n",
3919 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003920 err_code |= ERR_ALERT | ERR_FATAL;
3921 goto out;
3922 }
3923 }
3924
3925 if ((curmsg = calloc(1, sizeof(*curmsg))) == 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 curmsg->id = strdup(args[1]);
3932 curmsg->id_len = strlen(curmsg->id);
3933 curmsg->event = SPOE_EV_NONE;
3934 curmsg->conf.file = strdup(file);
3935 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003936 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003937 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003938 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003939 LIST_INIT(&curmsg->by_evt);
3940 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003941 LIST_ADDQ(&curmsgs, &curmsg->list);
3942 }
3943 else if (!strcmp(args[0], "args")) {
3944 int cur_arg = 1;
3945
3946 curproxy->conf.args.ctx = ARGC_SPOE;
3947 curproxy->conf.args.file = file;
3948 curproxy->conf.args.line = linenum;
3949 while (*args[cur_arg]) {
3950 char *delim = strchr(args[cur_arg], '=');
3951 int idx = 0;
3952
3953 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003954 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003955 err_code |= ERR_ALERT | ERR_ABORT;
3956 goto out;
3957 }
3958
3959 if (!delim) {
3960 arg->name = NULL;
3961 arg->name_len = 0;
3962 delim = args[cur_arg];
3963 }
3964 else {
3965 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3966 arg->name_len = delim - args[cur_arg];
3967 delim++;
3968 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003969 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3970 &idx, file, linenum, &errmsg,
3971 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003972 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003973 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003974 err_code |= ERR_ALERT | ERR_FATAL;
3975 free(arg->name);
3976 free(arg);
3977 goto out;
3978 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003979 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 LIST_ADDQ(&curmsg->args, &arg->list);
3981 cur_arg++;
3982 }
3983 curproxy->conf.args.file = NULL;
3984 curproxy->conf.args.line = 0;
3985 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003986 else if (!strcmp(args[0], "acl")) {
3987 err = invalid_char(args[1]);
3988 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003989 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3990 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003991 err_code |= ERR_ALERT | ERR_FATAL;
3992 goto out;
3993 }
3994 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003995 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3996 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003997 err_code |= ERR_ALERT | ERR_FATAL;
3998 goto out;
3999 }
4000 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004001 else if (!strcmp(args[0], "event")) {
4002 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004003 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004004 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004005 goto out;
4006 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004007 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4008 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004009
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004010 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
4011 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
4012 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
4013 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4014
4015 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
4016 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
4017 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
4018 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
4019 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
4020 curmsg->event = SPOE_EV_ON_TCP_RSP;
4021
4022 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
4023 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
4024 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
4025 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
4026 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
4027 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4028 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004029 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004030 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004031 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004032 goto out;
4033 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004034
4035 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4036 struct acl_cond *cond;
4037
4038 cond = build_acl_cond(file, linenum, &curmsg->acls,
4039 curproxy, (const char **)args+2,
4040 &errmsg);
4041 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004042 ha_alert("parsing [%s:%d] : error detected while "
4043 "parsing an 'event %s' condition : %s.\n",
4044 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004045 err_code |= ERR_ALERT | ERR_FATAL;
4046 goto out;
4047 }
4048 curmsg->cond = cond;
4049 }
4050 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004051 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4052 "or 'unless' followed by a condition but found '%s'.\n",
4053 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004054 err_code |= ERR_ALERT | ERR_FATAL;
4055 goto out;
4056 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 }
4058 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004059 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4060 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004061 err_code |= ERR_ALERT | ERR_FATAL;
4062 goto out;
4063 }
4064 out:
4065 free(errmsg);
4066 return err_code;
4067}
4068
4069/* Return -1 on error, else 0 */
4070static int
4071parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4072 struct flt_conf *fconf, char **err, void *private)
4073{
4074 struct list backup_sections;
4075 struct spoe_config *conf;
4076 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004077 struct spoe_group *grp, *grpback;
4078 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004079 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004080 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004081 char *file = NULL, *engine = NULL;
4082 int ret, pos = *cur_arg + 1;
4083
Christopher Faulet84c844e2018-03-23 14:37:14 +01004084 LIST_INIT(&curmsgs);
4085 LIST_INIT(&curgrps);
4086 LIST_INIT(&curmphs);
4087 LIST_INIT(&curgphs);
4088 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004089 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004090 curpxopts = 0;
4091 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004092
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004093 conf = calloc(1, sizeof(*conf));
4094 if (conf == NULL) {
4095 memprintf(err, "%s: out of memory", args[*cur_arg]);
4096 goto error;
4097 }
4098 conf->proxy = px;
4099
4100 while (*args[pos]) {
4101 if (!strcmp(args[pos], "config")) {
4102 if (!*args[pos+1]) {
4103 memprintf(err, "'%s' : '%s' option without value",
4104 args[*cur_arg], args[pos]);
4105 goto error;
4106 }
4107 file = args[pos+1];
4108 pos += 2;
4109 }
4110 else if (!strcmp(args[pos], "engine")) {
4111 if (!*args[pos+1]) {
4112 memprintf(err, "'%s' : '%s' option without value",
4113 args[*cur_arg], args[pos]);
4114 goto error;
4115 }
4116 engine = args[pos+1];
4117 pos += 2;
4118 }
4119 else {
4120 memprintf(err, "unknown keyword '%s'", args[pos]);
4121 goto error;
4122 }
4123 }
4124 if (file == NULL) {
4125 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4126 goto error;
4127 }
4128
4129 /* backup sections and register SPOE sections */
4130 LIST_INIT(&backup_sections);
4131 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004132 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4133 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004134 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004135
4136 /* Parse SPOE filter configuration file */
4137 curengine = engine;
4138 curproxy = px;
4139 curagent = NULL;
4140 curmsg = NULL;
4141 ret = readcfgfile(file);
4142 curproxy = NULL;
4143
4144 /* unregister SPOE sections and restore previous sections */
4145 cfg_unregister_sections();
4146 cfg_restore_sections(&backup_sections);
4147
4148 if (ret == -1) {
4149 memprintf(err, "Could not open configuration file %s : %s",
4150 file, strerror(errno));
4151 goto error;
4152 }
4153 if (ret & (ERR_ABORT|ERR_FATAL)) {
4154 memprintf(err, "Error(s) found in configuration file %s", file);
4155 goto error;
4156 }
4157
4158 /* Check SPOE agent */
4159 if (curagent == NULL) {
4160 memprintf(err, "No SPOE agent found in file %s", file);
4161 goto error;
4162 }
4163 if (curagent->b.name == NULL) {
4164 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4165 curagent->id, curagent->conf.file, curagent->conf.line);
4166 goto error;
4167 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004168 if (curagent->timeout.hello == TICK_ETERNITY ||
4169 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004170 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004171 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4172 " | While not properly invalid, you will certainly encounter various problems\n"
4173 " | with such a configuration. To fix this, please ensure that all following\n"
4174 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4175 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004176 }
4177 if (curagent->var_pfx == NULL) {
4178 char *tmp = curagent->id;
4179
4180 while (*tmp) {
4181 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4182 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4183 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4184 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4185 goto error;
4186 }
4187 tmp++;
4188 }
4189 curagent->var_pfx = strdup(curagent->id);
4190 }
4191
Christopher Fauletb7426d12018-03-21 14:12:17 +01004192 if (curagent->var_on_error) {
4193 struct arg arg;
4194
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004195 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004196 curagent->var_pfx, curagent->var_on_error);
4197
4198 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004199 arg.data.str.area = trash.area;
4200 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004201 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004202 if (!vars_check_arg(&arg, err)) {
4203 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4204 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4205 goto error;
4206 }
4207 }
4208
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004209 if (curagent->var_t_process) {
4210 struct arg arg;
4211
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004212 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004213 curagent->var_pfx, curagent->var_t_process);
4214
4215 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004216 arg.data.str.area = trash.area;
4217 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004218 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004219 if (!vars_check_arg(&arg, err)) {
4220 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4221 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4222 goto error;
4223 }
4224 }
4225
4226 if (curagent->var_t_total) {
4227 struct arg arg;
4228
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004229 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004230 curagent->var_pfx, curagent->var_t_total);
4231
4232 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004233 arg.data.str.area = trash.area;
4234 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004235 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004236 if (!vars_check_arg(&arg, err)) {
4237 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4238 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4239 goto error;
4240 }
4241 }
4242
Christopher Faulet11610f32017-09-21 10:23:10 +02004243 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004244 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4245 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004246 goto finish;
4247 }
4248
Christopher Faulet11610f32017-09-21 10:23:10 +02004249 /* Replace placeholders by the corresponding messages for the SPOE
4250 * agent */
4251 list_for_each_entry(ph, &curmphs, list) {
4252 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004253 struct spoe_arg *arg;
4254 unsigned int where;
4255
Christopher Faulet11610f32017-09-21 10:23:10 +02004256 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004257 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4258 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4259 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4260 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4261 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4262 }
4263 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4264 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4265 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004266 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4267 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004268 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004269 }
4270 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004271 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4272 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004273 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004274 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004275
4276 where = 0;
4277 switch (msg->event) {
4278 case SPOE_EV_ON_CLIENT_SESS:
4279 where |= SMP_VAL_FE_CON_ACC;
4280 break;
4281
4282 case SPOE_EV_ON_TCP_REQ_FE:
4283 where |= SMP_VAL_FE_REQ_CNT;
4284 break;
4285
4286 case SPOE_EV_ON_HTTP_REQ_FE:
4287 where |= SMP_VAL_FE_HRQ_HDR;
4288 break;
4289
4290 case SPOE_EV_ON_TCP_REQ_BE:
4291 if (px->cap & PR_CAP_FE)
4292 where |= SMP_VAL_FE_REQ_CNT;
4293 if (px->cap & PR_CAP_BE)
4294 where |= SMP_VAL_BE_REQ_CNT;
4295 break;
4296
4297 case SPOE_EV_ON_HTTP_REQ_BE:
4298 if (px->cap & PR_CAP_FE)
4299 where |= SMP_VAL_FE_HRQ_HDR;
4300 if (px->cap & PR_CAP_BE)
4301 where |= SMP_VAL_BE_HRQ_HDR;
4302 break;
4303
4304 case SPOE_EV_ON_SERVER_SESS:
4305 where |= SMP_VAL_BE_SRV_CON;
4306 break;
4307
4308 case SPOE_EV_ON_TCP_RSP:
4309 if (px->cap & PR_CAP_FE)
4310 where |= SMP_VAL_FE_RES_CNT;
4311 if (px->cap & PR_CAP_BE)
4312 where |= SMP_VAL_BE_RES_CNT;
4313 break;
4314
4315 case SPOE_EV_ON_HTTP_RSP:
4316 if (px->cap & PR_CAP_FE)
4317 where |= SMP_VAL_FE_HRS_HDR;
4318 if (px->cap & PR_CAP_BE)
4319 where |= SMP_VAL_BE_HRS_HDR;
4320 break;
4321
4322 default:
4323 break;
4324 }
4325
4326 list_for_each_entry(arg, &msg->args, list) {
4327 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004328 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004329 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004330 "none of which is available here ('%s')",
4331 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004332 sample_ckp_names(arg->expr->fetch->use),
4333 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004334 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004335 }
4336 }
4337
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004338 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004339 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4340 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004341 }
4342 }
4343 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004344 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004345 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004346 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004347 continue;
4348 }
4349
Christopher Faulet11610f32017-09-21 10:23:10 +02004350 /* Replace placeholders by the corresponding groups for the SPOE
4351 * agent */
4352 list_for_each_entry(ph, &curgphs, list) {
4353 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4354 if (!strcmp(grp->id, ph->id)) {
4355 grp->agent = curagent;
4356 LIST_DEL(&grp->list);
4357 LIST_ADDQ(&curagent->groups, &grp->list);
4358 goto next_aph;
4359 }
4360 }
4361 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4362 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4363 goto error;
4364 next_aph:
4365 continue;
4366 }
4367
4368 /* Replace placeholders by the corresponding message for each SPOE
4369 * group of the SPOE agent */
4370 list_for_each_entry(grp, &curagent->groups, list) {
4371 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4372 list_for_each_entry(msg, &curmsgs, list) {
4373 if (!strcmp(msg->id, ph->id)) {
4374 if (msg->group != NULL) {
4375 memprintf(err, "SPOE message '%s' already belongs to "
4376 "the SPOE group '%s' declare at %s:%d",
4377 msg->id, msg->group->id,
4378 msg->group->conf.file,
4379 msg->group->conf.line);
4380 goto error;
4381 }
4382
4383 /* Scope for arguments are not checked for now. We will check
4384 * them only if a rule use the corresponding SPOE group. */
4385 msg->agent = curagent;
4386 msg->group = grp;
4387 LIST_DEL(&ph->list);
4388 LIST_ADDQ(&grp->messages, &msg->by_grp);
4389 goto next_mph_grp;
4390 }
4391 }
4392 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4393 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4394 goto error;
4395 next_mph_grp:
4396 continue;
4397 }
4398 }
4399
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004400 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004401 /* move curmsgs to the agent message list */
4402 curmsgs.n->p = &curagent->messages;
4403 curmsgs.p->n = &curagent->messages;
4404 curagent->messages = curmsgs;
4405 LIST_INIT(&curmsgs);
4406
Christopher Faulet7ee86672017-09-19 11:08:28 +02004407 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004408 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004409
4410 /* Start agent's proxy initialization here. It will be finished during
4411 * the filter init. */
4412 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4413 init_new_proxy(&conf->agent_fe);
4414 conf->agent_fe.id = conf->agent->id;
4415 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004416 conf->agent_fe.options |= curpxopts;
4417 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004418
4419 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4420 LIST_DEL(&logsrv->list);
4421 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4422 }
4423
Christopher Faulet11610f32017-09-21 10:23:10 +02004424 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4425 LIST_DEL(&ph->list);
4426 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004427 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004428 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4429 LIST_DEL(&ph->list);
4430 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004431 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004432 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4433 struct arg arg;
4434
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004435 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004436 curagent->var_pfx, vph->name);
4437
4438 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004439 arg.data.str.area = trash.area;
4440 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004441 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004442 if (!vars_check_arg(&arg, err)) {
4443 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4444 curagent->id, curagent->var_pfx, vph->name, *err);
4445 goto error;
4446 }
4447
4448 LIST_DEL(&vph->list);
4449 free(vph->name);
4450 free(vph);
4451 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004452 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4453 LIST_DEL(&grp->list);
4454 spoe_release_group(grp);
4455 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004456 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004457 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004458 fconf->ops = &spoe_ops;
4459 fconf->conf = conf;
4460 return 0;
4461
4462 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004463 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004464 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4465 LIST_DEL(&ph->list);
4466 spoe_release_placeholder(ph);
4467 }
4468 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4469 LIST_DEL(&ph->list);
4470 spoe_release_placeholder(ph);
4471 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004472 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4473 LIST_DEL(&vph->list);
4474 free(vph->name);
4475 free(vph);
4476 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004477 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4478 LIST_DEL(&grp->list);
4479 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004480 }
4481 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4482 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004483 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004484 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004485 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4486 LIST_DEL(&logsrv->list);
4487 free(logsrv);
4488 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004489 free(conf);
4490 return -1;
4491}
4492
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004493/* Send message of a SPOE group. This is the action_ptr callback of a rule
4494 * associated to a "send-spoe-group" action.
4495 *
4496 * It returns ACT_RET_CONT is processing is finished without error, it returns
4497 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4498 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004499static enum act_return
4500spoe_send_group(struct act_rule *rule, struct proxy *px,
4501 struct session *sess, struct stream *s, int flags)
4502{
4503 struct filter *filter;
4504 struct spoe_agent *agent = NULL;
4505 struct spoe_group *group = NULL;
4506 struct spoe_context *ctx = NULL;
4507 int ret, dir;
4508
4509 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4510 if (filter->config == rule->arg.act.p[0]) {
4511 agent = rule->arg.act.p[2];
4512 group = rule->arg.act.p[3];
4513 ctx = filter->ctx;
4514 break;
4515 }
4516 }
4517 if (agent == NULL || group == NULL || ctx == NULL)
4518 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004519 if (ctx->state == SPOE_CTX_ST_NONE)
4520 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004521
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004522 switch (rule->from) {
4523 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4524 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4525 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4526 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4527 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4528 default:
4529 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4530 " - internal error while execute spoe-send-group\n",
4531 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4532 __FUNCTION__, s);
4533 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4534 agent->id);
4535 return ACT_RET_CONT;
4536 }
4537
4538 ret = spoe_process_group(s, ctx, group, dir);
4539 if (ret == 1)
4540 return ACT_RET_CONT;
4541 else if (ret == 0) {
4542 if (flags & ACT_FLAG_FINAL) {
4543 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4544 " - failed to process group '%s': interrupted by caller\n",
4545 (int)now.tv_sec, (int)now.tv_usec,
4546 agent->id, __FUNCTION__, s, group->id);
4547 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004548 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004549 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004550 return ACT_RET_CONT;
4551 }
4552 return ACT_RET_YIELD;
4553 }
4554 else
4555 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004556}
4557
4558/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4559 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4560 * action should be:
4561 *
4562 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4563 *
4564 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4565 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4566 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4567 * group.
4568 *
4569 * The function returns 1 in success case, otherwise, it returns 0 and err is
4570 * filled.
4571 */
4572static int
4573check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4574{
4575 struct flt_conf *fconf;
4576 struct spoe_config *conf;
4577 struct spoe_agent *agent = NULL;
4578 struct spoe_group *group;
4579 struct spoe_message *msg;
4580 char *engine_id = rule->arg.act.p[0];
4581 char *group_id = rule->arg.act.p[1];
4582 unsigned int where = 0;
4583
4584 switch (rule->from) {
4585 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4586 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4587 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4588 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4589 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4590 default:
4591 memprintf(err,
4592 "internal error, unexpected rule->from=%d, please report this bug!",
4593 rule->from);
4594 goto error;
4595 }
4596
4597 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4598 * <px> */
4599 list_for_each_entry(fconf, &px->filter_configs, list) {
4600 conf = fconf->conf;
4601
4602 /* This is not an SPOE filter */
4603 if (fconf->id != spoe_filter_id)
4604 continue;
4605
4606 /* This is the good engine */
4607 if (!strcmp(conf->id, engine_id)) {
4608 agent = conf->agent;
4609 break;
4610 }
4611 }
4612 if (agent == NULL) {
4613 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4614 engine_id, group_id);
4615 goto error;
4616 }
4617
4618 /* Try to find the right group */
4619 list_for_each_entry(group, &agent->groups, list) {
4620 /* This is the good group */
4621 if (!strcmp(group->id, group_id))
4622 break;
4623 }
4624 if (&group->list == &agent->groups) {
4625 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4626 group_id, engine_id);
4627 goto error;
4628 }
4629
4630 /* Ok, we found the group, we need to check messages and their
4631 * arguments */
4632 list_for_each_entry(msg, &group->messages, by_grp) {
4633 struct spoe_arg *arg;
4634
4635 list_for_each_entry(arg, &msg->args, list) {
4636 if (!(arg->expr->fetch->val & where)) {
4637 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4638 "some args extract information from '%s',"
4639 "none of which is available here ('%s')",
4640 msg->id, group->id, msg->conf.file, msg->conf.line,
4641 sample_ckp_names(arg->expr->fetch->use),
4642 sample_ckp_names(where));
4643 goto error;
4644 }
4645 }
4646 }
4647
4648 free(engine_id);
4649 free(group_id);
4650 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4651 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4652 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4653 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4654 return 1;
4655
4656 error:
4657 free(engine_id);
4658 free(group_id);
4659 return 0;
4660}
4661
4662/* Parse 'send-spoe-group' action following the format:
4663 *
4664 * ... send-spoe-group <engine-id> <group-id>
4665 *
4666 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4667 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4668 * ids are saved and used later, when the rule will be checked.
4669 */
4670static enum act_parse_ret
4671parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4672 struct act_rule *rule, char **err)
4673{
4674 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4675 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4676 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4677 return ACT_RET_PRS_ERR;
4678 }
4679 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4680 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4681
4682 (*orig_arg) += 2;
4683
4684 rule->action = ACT_CUSTOM;
4685 rule->action_ptr = spoe_send_group;
4686 rule->check_ptr = check_send_spoe_group;
4687 return ACT_RET_PRS_OK;
4688}
4689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004690
4691/* Declare the filter parser for "spoe" keyword */
4692static struct flt_kw_list flt_kws = { "SPOE", { }, {
4693 { "spoe", parse_spoe_flt, NULL },
4694 { NULL, NULL, NULL },
4695 }
4696};
4697
Willy Tarreau0108d902018-11-25 19:14:37 +01004698INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4699
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004700/* Delcate the action parser for "spoe-action" keyword */
4701static struct action_kw_list tcp_req_action_kws = { { }, {
4702 { "send-spoe-group", parse_send_spoe_group },
4703 { /* END */ },
4704 }
4705};
Willy Tarreau0108d902018-11-25 19:14:37 +01004706
4707INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4708
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004709static struct action_kw_list tcp_res_action_kws = { { }, {
4710 { "send-spoe-group", parse_send_spoe_group },
4711 { /* END */ },
4712 }
4713};
Willy Tarreau0108d902018-11-25 19:14:37 +01004714
4715INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4716
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004717static struct action_kw_list http_req_action_kws = { { }, {
4718 { "send-spoe-group", parse_send_spoe_group },
4719 { /* END */ },
4720 }
4721};
Willy Tarreau0108d902018-11-25 19:14:37 +01004722
4723INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4724
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004725static struct action_kw_list http_res_action_kws = { { }, {
4726 { "send-spoe-group", parse_send_spoe_group },
4727 { /* END */ },
4728 }
4729};
4730
Willy Tarreau0108d902018-11-25 19:14:37 +01004731INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);