blob: 42e83313541394dd0209ac6d79d342e0f66ca964 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/hathreads.h>
20#include <common/initcall.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020021#include <common/memory.h>
22#include <common/time.h>
23
24#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010026#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020027
Christopher Faulet57583e42017-09-04 15:41:09 +020028#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020029#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030#include <proto/arg.h>
31#include <proto/backend.h>
32#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010033#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020034#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020035#include <proto/http_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020036#include <proto/log.h>
37#include <proto/proto_http.h>
38#include <proto/proxy.h>
39#include <proto/sample.h>
40#include <proto/session.h>
41#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010042#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043#include <proto/stream.h>
44#include <proto/stream_interface.h>
45#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020046#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020047#include <proto/vars.h>
48
49#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
50#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#else
53#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010054#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020055#endif
56
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010057/* Reserved 4 bytes to the frame size. So a frame and its size can be written
58 * together in a buffer */
59#define MAX_FRAME_SIZE global.tune.bufsize - 4
60
61/* The minimum size for a frame */
62#define MIN_FRAME_SIZE 256
63
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Reserved for the metadata and the frame type.
65 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010066#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010068/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010069#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
70
Christopher Faulet3b386a32017-02-23 10:17:15 +010071/* SPOE filter id. Used to identify SPOE filters */
72const char *spoe_filter_id = "SPOE filter";
73
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020074/* Set if the handle on SIGUSR1 is registered */
75static int sighandler_registered = 0;
76
77/* proxy used during the parsing */
78struct proxy *curproxy = NULL;
79
80/* The name of the SPOE engine, used during the parsing */
81char *curengine = NULL;
82
83/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020084/* SPOE agent/group/message used during the parsing */
85struct spoe_agent *curagent = NULL;
86struct spoe_group *curgrp = NULL;
87struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
89/* list of SPOE messages and placeholders used during the parsing */
90struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020091struct list curgrps;
92struct list curmphs;
93struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010094struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Faulet7250b8f2018-03-26 17:19:01 +020096/* list of log servers used during the parsing */
97struct list curlogsrvs;
98
Christopher Faulet0e0f0852018-03-26 17:20:36 +020099/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
100int curpxopts;
101int curpxopts2;
102
Christopher Faulet42bfa462017-01-04 14:14:19 +0100103/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100104DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
105DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107struct flt_ops spoe_ops;
108
Christopher Faulet8ef75252017-02-20 22:56:03 +0100109static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200110static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
111static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112
113/********************************************************************
114 * helper functions/globals
115 ********************************************************************/
116static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200117spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118{
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200121 free(ph->id);
122 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123}
124
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100126spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127{
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 struct spoe_arg *arg, *argback;
129 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200130
131 if (!msg)
132 return;
133 free(msg->id);
134 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200135 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200136 release_sample_expr(arg->expr);
137 free(arg->name);
138 LIST_DEL(&arg->list);
139 free(arg);
140 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200141 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
142 LIST_DEL(&acl->list);
143 prune_acl(acl);
144 free(acl);
145 }
146 if (msg->cond) {
147 prune_acl_cond(msg->cond);
148 free(msg->cond);
149 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200150 free(msg);
151}
152
153static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200154spoe_release_group(struct spoe_group *grp)
155{
156 if (!grp)
157 return;
158 free(grp->id);
159 free(grp->conf.file);
160 free(grp);
161}
162
163static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100164spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200165{
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 struct spoe_message *msg, *msgback;
167 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200168 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200169
170 if (!agent)
171 return;
172 free(agent->id);
173 free(agent->conf.file);
174 free(agent->var_pfx);
Christopher 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 Faulet46c76822019-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 Faulet46c76822019-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 Fauleteaf11912019-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 Fauleteaf11912019-09-17 15:07:02 +0200267 if ((uuid = calloc(1, 37)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100268 return NULL;
269
Christopher Fauleteaf11912019-09-17 15:07:02 +0200270 while (byte < 4) {
271 while (bits < 32) {
Willy Tarreau861c4ef2020-03-08 00:42:37 +0100272 last |= (uint64_t)ha_random() << bits;
Christopher Fauleteaf11912019-09-17 15:07:02 +0200273 bits += rand_max_bits;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100274 }
Christopher Fauleteaf11912019-09-17 15:07:02 +0200275 rnd[byte++] = last;
276 last >>= 32u;
277 bits -= 32;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100278 }
Willy Tarreau079ec122019-10-29 10:25:49 +0100279 snprintf(uuid, 37, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
Christopher Fauleteaf11912019-09-17 15:07:02 +0200280 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 Faulet46c76822019-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 Faulet46c76822019-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 */
Willy Tarreau249346d2020-06-16 17:58:14 +0200718 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
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 */
Willy Tarreau249346d2020-06-16 17:58:14 +0200747 else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) {
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 */
Willy Tarreau249346d2020-06-16 17:58:14 +0200770 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
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 */
Willy Tarreau249346d2020-06-16 17:58:14 +0200904 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
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 */
Willy Tarreau249346d2020-06-16 17:58:14 +0200923 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924 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 Faulet746326a2020-11-10 18:45:34 +01001289 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001290 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293 }
1294
Christopher Faulet8ef75252017-02-20 22:56:03 +01001295 /* If the applet was processing a fragmented frame, notify the
1296 * corresponding stream. */
1297 if (spoe_appctx->frag_ctx.ctx) {
1298 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001299 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001300 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001302 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1303 }
1304
Christopher Faulet746326a2020-11-10 18:45:34 +01001305 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1306 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1307 if (ctx->spoe_appctx == spoe_appctx)
1308 ctx->spoe_appctx = NULL;
1309 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001310 goto end;
Christopher Faulet746326a2020-11-10 18:45:34 +01001311 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312
Christopher Faulet8ef75252017-02-20 22:56:03 +01001313 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001314 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 LIST_DEL(&ctx->list);
1316 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001317 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001318 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Faulet746326a2020-11-10 18:45:34 +01001319 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001321 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001323 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001324 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001325 LIST_DEL(&ctx->list);
1326 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001327 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001328 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Faulet746326a2020-11-10 18:45:34 +01001329 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001330 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001331 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001332 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1333 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001334
1335 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001336 /* Release allocated memory */
1337 spoe_release_buffer(&spoe_appctx->buffer,
1338 &spoe_appctx->buffer_wait);
1339 pool_free(pool_head_spoe_appctx, spoe_appctx);
1340
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001341 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001342 agent->rt[tid].frame_size = agent->max_frame_size;
1343 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1344 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001345}
1346
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001347static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001349{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001350 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001351 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001352 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001354
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001355 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1356 /* closed */
1357 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1358 goto exit;
1359 }
1360
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001361 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001362 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001363 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1365 goto stop;
1366 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001367
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001369 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1370 " - Connection timed out\n",
1371 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1372 __FUNCTION__, appctx);
1373 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001375 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001376
Christopher Faulet42bfa462017-01-04 14:14:19 +01001377 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001378 SPOE_APPCTX(appctx)->task->expire =
1379 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1382 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001383 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001384 ret = spoe_prepare_hahello_frame(appctx, frame,
1385 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001386 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001387 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388
1389 switch (ret) {
1390 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 case 0: /* ignore => an error, cannot be ignored */
1392 goto exit;
1393
1394 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001395 goto stop;
1396
Christopher Faulet8ef75252017-02-20 22:56:03 +01001397 default:
1398 /* HELLO frame successfully sent, now wait for the
1399 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1401 goto next;
1402 }
1403
1404 next:
1405 return 0;
1406 stop:
1407 return 1;
1408 exit:
1409 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1410 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411}
1412
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001417 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001418 char *frame;
1419 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001420
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001421
Christopher Fauletb067b062017-01-04 16:39:11 +01001422 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001424 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001425 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001426
Christopher Fauleta1cda022016-12-21 08:58:06 +01001427 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001428 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1429 " - Connection timed out\n",
1430 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1431 __FUNCTION__, appctx);
1432 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001433 goto exit;
1434 }
1435
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001436 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001437 ret = spoe_recv_frame(appctx, frame,
1438 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 if (ret > 1) {
1440 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1441 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1442 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001443 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001444 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001445 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001447
Christopher Fauleta1cda022016-12-21 08:58:06 +01001448 switch (ret) {
1449 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001450 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001451 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1452 goto next;
1453
1454 case 1: /* retry later */
1455 goto stop;
1456
1457 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 /* HELLO handshake is finished, set the idle timeout and
1459 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001460 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001461 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001462 SPOE_APPCTX(appctx)->node.key = 0;
1463 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001464
1465 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001466 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001467 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001468 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001469
Christopher Fauleta1cda022016-12-21 08:58:06 +01001470 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001471 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001472 if (trash.data)
1473 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474
1475 SPOE_APPCTX(appctx)->task->expire =
1476 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001477 return 0;
1478 stop:
1479 return 1;
1480 exit:
1481 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1482 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001483}
1484
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485
Christopher Fauleta1cda022016-12-21 08:58:06 +01001486static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001488{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001489 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1490 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 char *frame, *buf;
1492 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001493
Christopher Faulet8ef75252017-02-20 22:56:03 +01001494 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1495 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001496 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001497
1498 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1499 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1500 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1501 SPOE_APPCTX(appctx)->max_frame_size);
1502 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001503 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001504 *skip = 1;
1505 ret = 1;
1506 goto end;
1507 }
1508 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001509 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001510 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1511 SPOE_APPCTX(appctx)->max_frame_size);
1512
1513 }
1514
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 if (ret > 1)
1516 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001517
Christopher Faulet8ef75252017-02-20 22:56:03 +01001518 switch (ret) {
1519 case -1: /* error */
1520 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1521 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001522
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 case 0: /* ignore */
1524 if (ctx == NULL)
1525 goto abort_frag_frame;
1526
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001527 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001528 LIST_DEL(&ctx->list);
1529 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001530 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001531 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001532 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 ctx->state = SPOE_CTX_ST_ERROR;
1534 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1535 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001536 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001537 break;
1538
1539 case 1: /* retry */
1540 *skip = 1;
1541 break;
1542
1543 default:
1544 if (ctx == NULL)
1545 goto abort_frag_frame;
1546
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001547 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001548 LIST_DEL(&ctx->list);
1549 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001550 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001551 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001552 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001553 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1554 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1555 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001556 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001557 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558 }
1559 goto end;
1560
1561 frag_frame_sent:
1562 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001563 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1565 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1566 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1568 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1569 goto end;
1570
1571 no_frag_frame_sent:
1572 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1573 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001574 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 }
1576 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1577 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1578 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1579 }
1580 else {
1581 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001582 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001583 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1584 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001585 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001586 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1588 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1589 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001590 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591
Christopher Faulet8ef75252017-02-20 22:56:03 +01001592 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1593 goto end;
1594
1595 abort_frag_frame:
1596 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1597 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1598 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1599 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1600 goto end;
1601
1602 end:
1603 return ret;
1604}
1605
1606static int
1607spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1608{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001609 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 struct spoe_context *ctx = NULL;
1611 char *frame;
1612 int ret;
1613
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001614 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001615 ret = spoe_recv_frame(appctx, frame,
1616 SPOE_APPCTX(appctx)->max_frame_size);
1617 if (ret > 1) {
1618 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1619 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001620 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001621 goto end;
1622 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001623 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1625 }
1626 switch (ret) {
1627 case -1: /* error */
1628 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1629 break;
1630
1631 case 0: /* ignore */
1632 break;
1633
1634 case 1: /* retry */
1635 *skip = 1;
1636 break;
1637
1638 default:
1639 LIST_DEL(&ctx->list);
1640 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001641 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001642 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1643 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001644 if (ctx->spoe_appctx) {
1645 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001646 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001647 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001648 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1649 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1650 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1651 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1652 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1653 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1654 }
1655 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1656 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001657 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1658 break;
1659 }
1660
1661 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001662 if (trash.data)
1663 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001664 end:
1665 return ret;
1666}
1667
1668static int
1669spoe_handle_processing_appctx(struct appctx *appctx)
1670{
1671 struct stream_interface *si = appctx->owner;
1672 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001673 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001674
1675 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1676 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1677 goto exit;
1678 }
1679
1680 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1681 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1682 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1683 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1684 goto next;
1685 }
1686
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001687 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001688 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001689 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001690 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1691 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001692 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001693
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1695 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 /* receiving_frame loop */
1698 while (!skip_receiving) {
1699 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1700 switch (ret) {
1701 case -1: /* error */
1702 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001703
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 case 0: /* ignore */
1705 active_r = 1;
1706 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 case 1: /* retry */
1709 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001710
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 default:
1712 active_r = 1;
1713 break;
1714 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001716
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 /* send_frame loop */
1718 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1719 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1720 switch (ret) {
1721 case -1: /* error */
1722 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001725 if (SPOE_APPCTX(appctx)->node.key)
1726 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 active_s++;
1728 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001729
Christopher Faulet8f82b202018-01-24 16:23:03 +01001730 case 1: /* retry */
1731 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732
Christopher Faulet8f82b202018-01-24 16:23:03 +01001733 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001734 if (SPOE_APPCTX(appctx)->node.key)
1735 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001736 active_s++;
1737 break;
1738 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001740
Christopher Faulet8f82b202018-01-24 16:23:03 +01001741 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001742 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1743 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1744 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001745
Christopher Faulet8f82b202018-01-24 16:23:03 +01001746 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001747 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001748 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001749 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750 }
1751 return 1;
1752
Christopher Faulet8f82b202018-01-24 16:23:03 +01001753 next:
1754 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1755 return 0;
1756
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757 exit:
1758 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1759 return 0;
1760}
1761
1762static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764{
1765 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001766 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001767 char *frame, *buf;
1768 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001769
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1771 goto exit;
1772
1773 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1774 goto exit;
1775
Christopher Faulet8ef75252017-02-20 22:56:03 +01001776 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1777 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001778 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1780 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783
1784 switch (ret) {
1785 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 goto exit;
1788
1789 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790 goto stop;
1791
1792 default:
1793 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1794 " - disconnected by HAProxy (%d): %s\n",
1795 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796 __FUNCTION__, appctx,
1797 SPOE_APPCTX(appctx)->status_code,
1798 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799
1800 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1801 goto next;
1802 }
1803
1804 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 SPOE_APPCTX(appctx)->task->expire =
1806 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 return 0;
1808 stop:
1809 return 1;
1810 exit:
1811 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1812 return 0;
1813}
1814
1815static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001816spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817{
1818 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001819 char *frame;
1820 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821
Christopher Fauletb067b062017-01-04 16:39:11 +01001822 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001823 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001825 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826
Christopher Fauletb067b062017-01-04 16:39:11 +01001827 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001830 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001832 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 ret = spoe_recv_frame(appctx, frame,
1834 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001836 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 }
1839
1840 switch (ret) {
1841 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1843 " - error on frame (%s)\n",
1844 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001845 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 goto exit;
1849
1850 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 goto next;
1852
1853 case 1: /* retry */
1854 goto stop;
1855
1856 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001857 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001858 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001860 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001861 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1862 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 goto exit;
1864 }
1865
1866 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001868 if (trash.data)
1869 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001870
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871 return 0;
1872 stop:
1873 return 1;
1874 exit:
1875 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1876 return 0;
1877}
1878
1879/* I/O Handler processing messages exchanged with the agent */
1880static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001881spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 struct stream_interface *si = appctx->owner;
1884 struct spoe_agent *agent;
1885
1886 if (SPOE_APPCTX(appctx) == NULL)
1887 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1890 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001891
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 switchstate:
1893 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1894 " - appctx-state=%s\n",
1895 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1896 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1897
1898 switch (appctx->st0) {
1899 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001900 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001901 goto out;
1902 goto switchstate;
1903
1904 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001906 goto out;
1907 goto switchstate;
1908
1909 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001910 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001911 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001912 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001913 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001914 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915 SPOE_APPCTX(appctx)->task->expire =
1916 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001918 goto switchstate;
1919 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1921 /* fall through */
1922
1923 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001924 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1925 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001926 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001927 goto out;
1928 goto switchstate;
1929
1930 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001931 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001932 goto out;
1933 goto switchstate;
1934
1935 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001936 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001937 goto out;
1938 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939
1940 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001941 appctx->st0 = SPOE_APPCTX_ST_END;
1942 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1943
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 si_shutw(si);
1945 si_shutr(si);
1946 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001947 /* fall through */
1948
1949 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001950 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001952 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001953 if (stopping)
1954 spoe_wakeup_appctx(appctx);
1955
Christopher Faulet42bfa462017-01-04 14:14:19 +01001956 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1957 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958}
1959
1960struct applet spoe_applet = {
1961 .obj_type = OBJ_TYPE_APPLET,
1962 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001963 .fct = spoe_handle_appctx,
1964 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965};
1966
1967/* Create a SPOE applet. On success, the created applet is returned, else
1968 * NULL. */
1969static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001970spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971{
1972 struct appctx *appctx;
1973 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001974 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001975
Emeric Brun1138fd02017-06-19 12:38:55 +02001976 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977 goto out_error;
1978
Willy Tarreaubafbe012017-11-24 17:34:44 +01001979 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001980 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001981 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001982 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001983
Christopher Faulet42bfa462017-01-04 14:14:19 +01001984 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001985 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001986 goto out_free_spoe_appctx;
1987
1988 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001989 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001990 SPOE_APPCTX(appctx)->task->context = appctx;
1991 SPOE_APPCTX(appctx)->agent = conf->agent;
1992 SPOE_APPCTX(appctx)->version = 0;
1993 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1994 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001995 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001996 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001997 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001998
1999 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
2000 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002001 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002002
2003 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2004 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005
Willy Tarreau5820a362016-12-22 15:59:02 +01002006 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002007 if (!sess)
2008 goto out_free_spoe;
2009
Willy Tarreau87787ac2017-08-28 16:22:54 +02002010 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011 goto out_free_sess;
2012
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013 stream_set_backend(strm, conf->agent->b.be);
2014
2015 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002016 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 appctx_wakeup(appctx);
2018
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002019 strm->do_log = NULL;
2020 strm->res.flags |= CF_READ_DONTWAIT;
2021
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002022 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002023 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002024 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002025 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002026
Emeric Brunc60def82017-09-27 14:59:38 +02002027 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002028 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029 return appctx;
2030
2031 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002032 out_free_sess:
2033 session_free(sess);
2034 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002035 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002036 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002037 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038 out_free_appctx:
2039 appctx_free(appctx);
2040 out_error:
2041 return NULL;
2042}
2043
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002044static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002045spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002046{
2047 struct spoe_config *conf = FLT_CONF(ctx->filter);
2048 struct spoe_agent *agent = conf->agent;
2049 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002050 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002053 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauletd6cd2b32020-06-22 15:32:14 +02002054 (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002056
2057 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002059 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2060 ctx->strm);
2061
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 /* Do not try to create a new applet if there is no server up for the
2063 * agent's backend. */
2064 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2065 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2066 " - cannot create SPOE appctx: no server up\n",
2067 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2068 __FUNCTION__, ctx->strm);
2069 goto end;
2070 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071
Christopher Fauleta1cda022016-12-21 08:58:06 +01002072 /* Do not try to create a new applet if we have reached the maximum of
2073 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002074 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002075 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002076 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2077 " - cannot create SPOE appctx: max CPS reached\n",
2078 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2079 __FUNCTION__, ctx->strm);
2080 goto end;
2081 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002082 }
2083
Christopher Faulet8ef75252017-02-20 22:56:03 +01002084 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002085 if (appctx == NULL) {
2086 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2087 " - failed to create SPOE appctx\n",
2088 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2089 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002090 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002091 "SPOE: [%s] failed to create SPOE applet\n",
2092 agent->id);
2093
Christopher Fauleta1cda022016-12-21 08:58:06 +01002094 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002095 }
2096
Christopher Fauleta1cda022016-12-21 08:58:06 +01002097 /* Increase the per-process number of cumulated connections */
2098 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002099 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002100
Christopher Fauleta1cda022016-12-21 08:58:06 +01002101 end:
2102 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002103 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002104 ctx->status_code = SPOE_CTX_ERR_RES;
2105 return -1;
2106 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002108 /* Add the SPOE context in the sending queue if the stream has no applet
2109 * already assigned and wakeup all idle applets. Otherwise, don't queue
2110 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002111 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002112 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2113 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002114 if (ctx->spoe_appctx)
2115 return 1;
2116 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002117
2118 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002119 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002120 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002121 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002122 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002123 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002124
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002125 /* Finally try to wakeup an IDLE applet. */
2126 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2127 struct eb32_node *node;
2128
2129 node = eb32_first(&agent->rt[tid].idle_applets);
2130 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2131 if (node && spoe_appctx) {
2132 eb32_delete(&spoe_appctx->node);
2133 spoe_appctx->node.key++;
2134 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2135 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002136 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002137 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002138 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002139}
2140
2141/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002142 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002143 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002144/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2145 * fragmented_content. If the next message can be processed, it returns 0. If
2146 * the message is too big, it returns -1.*/
2147static int
2148spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2149 struct spoe_message *msg, int dir,
2150 char **buf, char *end)
2151{
2152 struct sample *smp;
2153 struct spoe_arg *arg;
2154 int ret;
2155
2156 if (msg->cond) {
2157 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2158 ret = acl_pass(ret);
2159 if (msg->cond->pol == ACL_COND_UNLESS)
2160 ret = !ret;
2161
2162 /* the rule does not match */
2163 if (!ret)
2164 goto next;
2165 }
2166
2167 /* Resume encoding of a SPOE argument */
2168 if (ctx->frag_ctx.curarg != NULL) {
2169 arg = ctx->frag_ctx.curarg;
2170 goto encode_argument;
2171 }
2172
2173 if (ctx->frag_ctx.curoff != UINT_MAX)
2174 goto encode_msg_payload;
2175
2176 /* Check if there is enough space for the message name and the
2177 * number of arguments. It implies <msg->id_len> is encoded on 2
2178 * bytes, at most (< 2288). */
2179 if (*buf + 2 + msg->id_len + 1 > end)
2180 goto too_big;
2181
2182 /* Encode the message name */
2183 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2184 goto too_big;
2185
2186 /* Set the number of arguments for this message */
2187 **buf = msg->nargs;
2188 (*buf)++;
2189
2190 ctx->frag_ctx.curoff = 0;
2191 encode_msg_payload:
2192
2193 /* Loop on arguments */
2194 list_for_each_entry(arg, &msg->args, list) {
2195 ctx->frag_ctx.curarg = arg;
2196 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002197 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002198
2199 encode_argument:
2200 if (ctx->frag_ctx.curoff != UINT_MAX)
2201 goto encode_arg_value;
2202
Joseph Herlantf7f60312018-11-15 13:46:49 -08002203 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002204 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2205 goto too_big;
2206
2207 ctx->frag_ctx.curoff = 0;
2208 encode_arg_value:
2209
Joseph Herlantf7f60312018-11-15 13:46:49 -08002210 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002211 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002212 if (smp) {
2213 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2214 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2215 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002216 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002217 if (ret == -1 || ctx->frag_ctx.curoff)
2218 goto too_big;
2219 }
2220
2221 next:
2222 return 0;
2223
2224 too_big:
2225 return -1;
2226}
2227
Christopher Fauletc718b822017-09-21 16:50:56 +02002228/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2229 * handle fragmented content. On success it returns 1. If an error occurred, -1
2230 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2231 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002233spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002234 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002235{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002236 struct spoe_config *conf = FLT_CONF(ctx->filter);
2237 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002238 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002239 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002240
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002241 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002242 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002243
Christopher Fauletc718b822017-09-21 16:50:56 +02002244 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2245 /* Resume encoding of a SPOE message */
2246 if (ctx->frag_ctx.curmsg != NULL) {
2247 msg = ctx->frag_ctx.curmsg;
2248 goto encode_evt_message;
2249 }
2250
2251 list_for_each_entry(msg, messages, by_evt) {
2252 ctx->frag_ctx.curmsg = msg;
2253 ctx->frag_ctx.curarg = NULL;
2254 ctx->frag_ctx.curoff = UINT_MAX;
2255
2256 encode_evt_message:
2257 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2258 goto too_big;
2259 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002260 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002261 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2262 /* Resume encoding of a SPOE message */
2263 if (ctx->frag_ctx.curmsg != NULL) {
2264 msg = ctx->frag_ctx.curmsg;
2265 goto encode_grp_message;
2266 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267
Christopher Fauletc718b822017-09-21 16:50:56 +02002268 list_for_each_entry(msg, messages, by_grp) {
2269 ctx->frag_ctx.curmsg = msg;
2270 ctx->frag_ctx.curarg = NULL;
2271 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002272
Christopher Fauletc718b822017-09-21 16:50:56 +02002273 encode_grp_message:
2274 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2275 goto too_big;
2276 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002277 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002278 else
2279 goto skip;
2280
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281
Christopher Faulet57583e42017-09-04 15:41:09 +02002282 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002283 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002284 goto skip;
2285
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002287 " - encode %s messages - spoe_appctx=%p"
2288 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289 (int)now.tv_sec, (int)now.tv_usec,
2290 agent->id, __FUNCTION__, s,
2291 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002292 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002293 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002295 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002296 ctx->frag_ctx.curmsg = NULL;
2297 ctx->frag_ctx.curarg = NULL;
2298 ctx->frag_ctx.curoff = 0;
2299 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002300
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301 return 1;
2302
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002303 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002304 /* Return an error if fragmentation is unsupported or if nothing has
2305 * been encoded because its too big and not splittable. */
2306 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002307 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2308 return -1;
2309 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002310
2311 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002312 " - encode fragmented messages - spoe_appctx=%p"
2313 " - curmsg=%p - curarg=%p - curoff=%u"
2314 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002315 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002316 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002317 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002318 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002319
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002320 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002321 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2322 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2323 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002324
2325 skip:
2326 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2327 " - skip the frame because nothing has been encoded\n",
2328 (int)now.tv_sec, (int)now.tv_usec,
2329 agent->id, __FUNCTION__, s);
2330 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002331}
2332
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002333
2334/***************************************************************************
2335 * Functions that handle SPOE actions
2336 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002337/* Helper function to set a variable */
2338static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002339spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002340 struct sample *smp)
2341{
2342 struct spoe_config *conf = FLT_CONF(ctx->filter);
2343 struct spoe_agent *agent = conf->agent;
2344 char varname[64];
2345
2346 memset(varname, 0, sizeof(varname));
2347 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2348 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002349 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2350 vars_set_by_name(varname, len, smp);
2351 else
2352 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353}
2354
2355/* Helper function to unset a variable */
2356static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002358 struct sample *smp)
2359{
2360 struct spoe_config *conf = FLT_CONF(ctx->filter);
2361 struct spoe_agent *agent = conf->agent;
2362 char varname[64];
2363
2364 memset(varname, 0, sizeof(varname));
2365 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2366 scope, agent->var_pfx, len, name);
2367 vars_unset_by_name_ifexist(varname, len, smp);
2368}
2369
2370
Christopher Faulet8ef75252017-02-20 22:56:03 +01002371static inline int
2372spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2373 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 char *str, *scope, *p = *buf;
2376 struct sample smp;
2377 uint64_t sz;
2378 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002379
Christopher Faulet8ef75252017-02-20 22:56:03 +01002380 if (p + 2 >= end)
2381 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 /* SET-VAR requires 3 arguments */
2384 if (*p++ != 3)
2385 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002386
Christopher Faulet8ef75252017-02-20 22:56:03 +01002387 switch (*p++) {
2388 case SPOE_SCOPE_PROC: scope = "proc"; break;
2389 case SPOE_SCOPE_SESS: scope = "sess"; break;
2390 case SPOE_SCOPE_TXN : scope = "txn"; break;
2391 case SPOE_SCOPE_REQ : scope = "req"; break;
2392 case SPOE_SCOPE_RES : scope = "res"; break;
2393 default: goto skip;
2394 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002395
Christopher Faulet8ef75252017-02-20 22:56:03 +01002396 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2397 goto skip;
2398 memset(&smp, 0, sizeof(smp));
2399 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401 if (spoe_decode_data(&p, end, &smp) == -1)
2402 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2405 " - set-var '%s.%s.%.*s'\n",
2406 (int)now.tv_sec, (int)now.tv_usec,
2407 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2408 __FUNCTION__, s, scope,
2409 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2410 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Fauletb135abc2020-10-15 16:08:30 +02002412 if (smp.data.type == SMP_T_ANY)
2413 spoe_unset_var(ctx, scope, str, sz, &smp);
2414 else
2415 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002416
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417 ret = (p - *buf);
2418 *buf = p;
2419 return ret;
2420 skip:
2421 return 0;
2422}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002423
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424static inline int
2425spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2426 char **buf, char *end, int dir)
2427{
2428 char *str, *scope, *p = *buf;
2429 struct sample smp;
2430 uint64_t sz;
2431 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002432
Christopher Faulet8ef75252017-02-20 22:56:03 +01002433 if (p + 2 >= end)
2434 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002435
Christopher Faulet8ef75252017-02-20 22:56:03 +01002436 /* UNSET-VAR requires 2 arguments */
2437 if (*p++ != 2)
2438 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002439
Christopher Faulet8ef75252017-02-20 22:56:03 +01002440 switch (*p++) {
2441 case SPOE_SCOPE_PROC: scope = "proc"; break;
2442 case SPOE_SCOPE_SESS: scope = "sess"; break;
2443 case SPOE_SCOPE_TXN : scope = "txn"; break;
2444 case SPOE_SCOPE_REQ : scope = "req"; break;
2445 case SPOE_SCOPE_RES : scope = "res"; break;
2446 default: goto skip;
2447 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002448
Christopher Faulet8ef75252017-02-20 22:56:03 +01002449 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2450 goto skip;
2451 memset(&smp, 0, sizeof(smp));
2452 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002453
Christopher Faulet8ef75252017-02-20 22:56:03 +01002454 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2455 " - unset-var '%s.%s.%.*s'\n",
2456 (int)now.tv_sec, (int)now.tv_usec,
2457 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2458 __FUNCTION__, s, scope,
2459 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2460 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002461
Christopher Faulet8ef75252017-02-20 22:56:03 +01002462 spoe_unset_var(ctx, scope, str, sz, &smp);
2463
2464 ret = (p - *buf);
2465 *buf = p;
2466 return ret;
2467 skip:
2468 return 0;
2469}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002470
Christopher Faulet8ef75252017-02-20 22:56:03 +01002471/* Process SPOE actions for a specific event. It returns 1 on success. If an
2472 * error occurred, 0 is returned. */
2473static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002474spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002475{
2476 char *p, *end;
2477 int ret;
2478
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002479 p = b_head(&ctx->buffer);
2480 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002481
2482 while (p < end) {
2483 enum spoe_action_type type;
2484
2485 type = *p++;
2486 switch (type) {
2487 case SPOE_ACT_T_SET_VAR:
2488 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2489 if (!ret)
2490 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002491 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002492
Christopher Faulet8ef75252017-02-20 22:56:03 +01002493 case SPOE_ACT_T_UNSET_VAR:
2494 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2495 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002496 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002497 break;
2498
2499 default:
2500 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002501 }
2502 }
2503
2504 return 1;
2505 skip:
2506 return 0;
2507}
2508
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002509/***************************************************************************
2510 * Functions that process SPOE events
2511 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002512static void
2513spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2514 struct spoe_context *ctx, int dir)
2515{
2516 if (!tv_iszero(&ctx->stats.tv_start)) {
2517 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2518 ctx->stats.t_total += ctx->stats.t_process;
2519 tv_zero(&ctx->stats.tv_request);
2520 tv_zero(&ctx->stats.tv_queue);
2521 tv_zero(&ctx->stats.tv_wait);
2522 tv_zero(&ctx->stats.tv_response);
2523 }
2524
2525 if (agent->var_t_process) {
2526 struct sample smp;
2527
2528 memset(&smp, 0, sizeof(smp));
2529 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2530 smp.data.u.sint = ctx->stats.t_process;
2531 smp.data.type = SMP_T_SINT;
2532
2533 spoe_set_var(ctx, "txn", agent->var_t_process,
2534 strlen(agent->var_t_process), &smp);
2535 }
2536
2537 if (agent->var_t_total) {
2538 struct sample smp;
2539
2540 memset(&smp, 0, sizeof(smp));
2541 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2542 smp.data.u.sint = ctx->stats.t_total;
2543 smp.data.type = SMP_T_SINT;
2544
2545 spoe_set_var(ctx, "txn", agent->var_t_total,
2546 strlen(agent->var_t_total), &smp);
2547 }
2548}
2549
2550static void
2551spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2552 struct spoe_context *ctx, int dir)
2553{
2554 if (agent->eps_max > 0)
2555 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2556
2557 if (agent->var_on_error) {
2558 struct sample smp;
2559
2560 memset(&smp, 0, sizeof(smp));
2561 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2562 smp.data.u.sint = ctx->status_code;
2563 smp.data.type = SMP_T_BOOL;
2564
2565 spoe_set_var(ctx, "txn", agent->var_on_error,
2566 strlen(agent->var_on_error), &smp);
2567 }
2568
2569 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2570 ? SPOE_CTX_ST_READY
2571 : SPOE_CTX_ST_NONE);
2572}
2573
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002574static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002575spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002577 /* If a process is already started for this SPOE context, retry
2578 * later. */
2579 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002580 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002582 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002583 ctx->stats.tv_start = now;
2584 ctx->stats.tv_request = now;
2585 ctx->stats.t_request = -1;
2586 ctx->stats.t_queue = -1;
2587 ctx->stats.t_waiting = -1;
2588 ctx->stats.t_response = -1;
2589 ctx->stats.t_process = -1;
2590
2591 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002592
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593 /* Set the right flag to prevent request and response processing
2594 * in same time. */
2595 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2596 ? SPOE_CTX_FL_REQ_PROCESS
2597 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599}
2600
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002601static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002602spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002603{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002604 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002606 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2607 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002608 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002609 if (sa) {
2610 if (sa->frag_ctx.ctx == ctx) {
2611 sa->frag_ctx.ctx = NULL;
2612 spoe_wakeup_appctx(sa->owner);
2613 }
2614 else
2615 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002616 }
2617
Christopher Fauleta1cda022016-12-21 08:58:06 +01002618 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002619 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002620 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002621
2622 /* Reset processing timer */
2623 ctx->process_exp = TICK_ETERNITY;
2624
Christopher Faulet8ef75252017-02-20 22:56:03 +01002625 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002626
Christopher Fauletfce747b2018-01-24 15:59:32 +01002627 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002628 ctx->frag_ctx.curmsg = NULL;
2629 ctx->frag_ctx.curarg = NULL;
2630 ctx->frag_ctx.curoff = 0;
2631 ctx->frag_ctx.flags = 0;
2632
Christopher Fauleta1cda022016-12-21 08:58:06 +01002633 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002634 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002635 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002636 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002637 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002638
Christopher Fauleta1cda022016-12-21 08:58:06 +01002639 LIST_DEL(&ctx->list);
2640 LIST_INIT(&ctx->list);
2641 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002642}
2643
Christopher Faulet58d03682017-09-21 16:57:24 +02002644/* Process a list of SPOE messages. First, this functions will process messages
2645 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2646 * to process corresponding actions. During all the processing, it returns 0
2647 * and it returns 1 when the processing is finished. If an error occurred, -1
2648 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002649static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002650spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2651 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002652{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002653 struct spoe_config *conf = FLT_CONF(ctx->filter);
2654 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002655 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002656
2657 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002658 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002659
2660 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2661 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002662 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002663 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002665 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002666 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002667 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002668
2669 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002670 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002671 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002672 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002673 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002674 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002675 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002676 goto skip;
2677 }
2678 }
2679
Christopher Fauletf7a30922016-11-10 15:04:51 +01002680 if (!tick_isset(ctx->process_exp)) {
2681 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2682 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2683 ctx->process_exp);
2684 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002685 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002686 if (!ret)
2687 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002688
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002689 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002690 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691 }
2692
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002693 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002694 if (tv_iszero(&ctx->stats.tv_request))
2695 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002696 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002697 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002698 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002699 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002700 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002701 if (!ret)
2702 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002703 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002704 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002705 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2706 }
2707
2708 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002709 if (ctx->spoe_appctx)
2710 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002711 ret = 0;
2712 goto out;
2713 }
2714
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002715 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2716 ret = 0;
2717 goto out;
2718 }
2719
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002720 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002721 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002722 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002723 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002724 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002725 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002726 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002727 }
2728
2729 out:
2730 return ret;
2731
Christopher Fauleta1cda022016-12-21 08:58:06 +01002732 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002733 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002734 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002735 spoe_stop_processing(agent, ctx);
2736 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002737
Christopher Fauleta1cda022016-12-21 08:58:06 +01002738 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002739 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002740 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002741 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002742 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002743 spoe_handle_processing_error(s, agent, ctx, dir);
2744 ret = 1;
2745 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002746 return ret;
2747}
2748
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002749/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2750 * See spoe_process_message for details. */
2751static int
2752spoe_process_group(struct stream *s, struct spoe_context *ctx,
2753 struct spoe_group *group, int dir)
2754{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002755 struct spoe_config *conf = FLT_CONF(ctx->filter);
2756 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002757 int ret;
2758
2759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2760 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002762 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2763 group->id);
2764
2765 if (LIST_ISEMPTY(&group->messages))
2766 return 1;
2767
2768 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002769 if (ret && ctx->stats.t_process != -1) {
2770 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002771 " - <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 +01002772 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002773 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002774 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002775 ctx->stats.t_response, ctx->stats.t_process,
2776 agent->counters.idles, agent->counters.applets,
2777 agent->counters.nb_sending, agent->counters.nb_waiting,
2778 agent->counters.nb_errors, agent->counters.nb_processed,
2779 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2780 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2781 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2782 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2783 agent->id, group->id, s->uniq_id, ctx->status_code,
2784 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2785 ctx->stats.t_response, ctx->stats.t_process,
2786 agent->counters.idles, agent->counters.applets,
2787 agent->counters.nb_sending, agent->counters.nb_waiting,
2788 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002789 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002790 return ret;
2791}
2792
Christopher Faulet58d03682017-09-21 16:57:24 +02002793/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2794 * See spoe_process_message for details. */
2795static int
2796spoe_process_event(struct stream *s, struct spoe_context *ctx,
2797 enum spoe_event ev)
2798{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002799 struct spoe_config *conf = FLT_CONF(ctx->filter);
2800 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002801 int dir, ret;
2802
2803 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002804 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002805 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002806 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2807 spoe_event_str[ev]);
2808
2809 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2810
2811 if (LIST_ISEMPTY(&(ctx->events[ev])))
2812 return 1;
2813
2814 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002815 if (ret && ctx->stats.t_process != -1) {
2816 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002817 " - <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 +01002818 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2819 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2820 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002821 ctx->stats.t_response, ctx->stats.t_process,
2822 agent->counters.idles, agent->counters.applets,
2823 agent->counters.nb_sending, agent->counters.nb_waiting,
2824 agent->counters.nb_errors, agent->counters.nb_processed,
2825 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2826 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2827 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2828 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2829 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2830 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2831 ctx->stats.t_response, ctx->stats.t_process,
2832 agent->counters.idles, agent->counters.applets,
2833 agent->counters.nb_sending, agent->counters.nb_waiting,
2834 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002835 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002836 return ret;
2837}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002838
2839/***************************************************************************
2840 * Functions that create/destroy SPOE contexts
2841 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002842static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002843spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002846 return 1;
2847
Christopher Faulet4596fb72017-01-11 14:05:19 +01002848 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002849 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002850 LIST_DEL(&buffer_wait->list);
2851 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002852 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002853 }
2854
Christopher Faulet4596fb72017-01-11 14:05:19 +01002855 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002856 return 1;
2857
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002858 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002859 LIST_ADDQ(&buffer_wq, &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 return 0;
2862}
2863
2864static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002865spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002866{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002867 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002868 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002869 LIST_DEL(&buffer_wait->list);
2870 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002871 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002872 }
2873
2874 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002875 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002876 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002877 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002878 }
2879}
2880
Christopher Faulet4596fb72017-01-11 14:05:19 +01002881static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002882spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002883{
2884 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2885 return 1;
2886}
2887
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002888static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002889spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002890{
2891 struct spoe_config *conf = FLT_CONF(filter);
2892 struct spoe_context *ctx;
2893
Willy Tarreaubafbe012017-11-24 17:34:44 +01002894 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002895 if (ctx == NULL) {
2896 return NULL;
2897 }
2898 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002899 ctx->filter = filter;
2900 ctx->state = SPOE_CTX_ST_NONE;
2901 ctx->status_code = SPOE_CTX_ERR_NONE;
2902 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002903 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002904 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002906 LIST_INIT(&ctx->buffer_wait.list);
2907 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002908 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002909 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002910
Christopher Fauletf7a30922016-11-10 15:04:51 +01002911 ctx->stream_id = 0;
2912 ctx->frame_id = 1;
2913 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002915 tv_zero(&ctx->stats.tv_start);
2916 tv_zero(&ctx->stats.tv_request);
2917 tv_zero(&ctx->stats.tv_queue);
2918 tv_zero(&ctx->stats.tv_wait);
2919 tv_zero(&ctx->stats.tv_response);
2920 ctx->stats.t_request = -1;
2921 ctx->stats.t_queue = -1;
2922 ctx->stats.t_waiting = -1;
2923 ctx->stats.t_response = -1;
2924 ctx->stats.t_process = -1;
2925 ctx->stats.t_total = 0;
2926
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002927 ctx->strm = s;
2928 ctx->state = SPOE_CTX_ST_READY;
2929 filter->ctx = ctx;
2930
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931 return ctx;
2932}
2933
2934static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002935spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002937 struct spoe_config *conf = FLT_CONF(filter);
2938 struct spoe_context *ctx = filter->ctx;
2939
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940 if (!ctx)
2941 return;
2942
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002943 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002944 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002945 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946}
2947
2948static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002949spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002950{
2951 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002952 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002953
2954 tv_zero(&ctx->stats.tv_start);
2955 tv_zero(&ctx->stats.tv_request);
2956 tv_zero(&ctx->stats.tv_queue);
2957 tv_zero(&ctx->stats.tv_wait);
2958 tv_zero(&ctx->stats.tv_response);
2959 ctx->stats.t_request = -1;
2960 ctx->stats.t_queue = -1;
2961 ctx->stats.t_waiting = -1;
2962 ctx->stats.t_response = -1;
2963 ctx->stats.t_process = -1;
2964 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965}
2966
2967
2968/***************************************************************************
2969 * Hooks that manage the filter lifecycle (init/check/deinit)
2970 **************************************************************************/
2971/* Signal handler: Do a soft stop, wakeup SPOE applet */
2972static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002973spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002974{
2975 struct proxy *p;
2976
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002977 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978 while (p) {
2979 struct flt_conf *fconf;
2980
2981 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002982 struct spoe_config *conf;
2983 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002984 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002985 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986
Christopher Faulet3b386a32017-02-23 10:17:15 +01002987 if (fconf->id != spoe_filter_id)
2988 continue;
2989
2990 conf = fconf->conf;
2991 agent = conf->agent;
2992
Christopher Faulet24289f22017-09-25 14:48:02 +02002993 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002994 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002995 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2996 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002997 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002998 }
2999 }
3000 p = p->next;
3001 }
3002}
3003
3004
3005/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3006static int
3007spoe_init(struct proxy *px, struct flt_conf *fconf)
3008{
3009 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003011 /* conf->agent_fe was already initialized during the config
3012 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014 conf->agent_fe.cap = PR_CAP_FE;
3015 conf->agent_fe.mode = PR_MODE_TCP;
3016 conf->agent_fe.maxconn = 0;
3017 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3018 conf->agent_fe.conn_retries = CONN_RETRIES;
3019 conf->agent_fe.accept = frontend_accept;
3020 conf->agent_fe.srv = NULL;
3021 conf->agent_fe.timeout.client = TICK_ETERNITY;
3022 conf->agent_fe.default_target = &spoe_applet.obj_type;
3023 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3024
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003025 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003026 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003027 sighandler_registered = 1;
3028 }
3029
Christopher Faulet00292352019-01-09 15:05:10 +01003030 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003032}
3033
3034/* Free ressources allocated by the SPOE filter. */
3035static void
3036spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3037{
3038 struct spoe_config *conf = fconf->conf;
3039
3040 if (conf) {
3041 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042
Christopher Faulet8ef75252017-02-20 22:56:03 +01003043 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003044 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 free(conf);
3046 }
3047 fconf->conf = NULL;
3048}
3049
3050/* Check configuration of a SPOE filter for a specified proxy.
3051 * Return 1 on error, else 0. */
3052static int
3053spoe_check(struct proxy *px, struct flt_conf *fconf)
3054{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003055 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056 struct spoe_config *conf = fconf->conf;
3057 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003058 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059
Christopher Faulet7ee86672017-09-19 11:08:28 +02003060 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3061 * are uniq */
3062 list_for_each_entry(f, &px->filter_configs, list) {
3063 struct spoe_config *c = f->conf;
3064
3065 /* This is not an SPOE filter */
3066 if (f->id != spoe_filter_id)
3067 continue;
3068 /* This is the current SPOE filter */
3069 if (f == fconf)
3070 continue;
3071
3072 /* Check engine Id. It should be uniq */
3073 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003074 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3075 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003076 return 1;
3077 }
3078 }
3079
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003080 target = proxy_be_by_name(conf->agent->b.name);
3081 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003082 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3083 " declared at %s:%d.\n",
3084 px->id, conf->agent->b.name, conf->agent->id,
3085 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003086 return 1;
3087 }
3088 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003089 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3090 " at %s:%d does not support HTTP mode.\n",
3091 px->id, target->id, conf->agent->id,
3092 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003093 return 1;
3094 }
3095
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003096 if (px->bind_proc & ~target->bind_proc) {
3097 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3098 " at %s:%d does not cover all of its processes.\n",
3099 px->id, target->id, conf->agent->id,
3100 conf->agent->conf.file, conf->agent->conf.line);
3101 return 1;
3102 }
3103
Christopher Fauletfe261552019-03-18 13:57:42 +01003104 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003105 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3106 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3107 return 1;
3108 }
3109 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet46c76822019-09-17 11:55:52 +02003110 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003111 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3112 conf->agent->rt[i].processing = 0;
3113 LIST_INIT(&conf->agent->rt[i].applets);
3114 LIST_INIT(&conf->agent->rt[i].sending_queue);
3115 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3116 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003117 }
3118
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003119 free(conf->agent->b.name);
3120 conf->agent->b.name = NULL;
3121 conf->agent->b.be = target;
3122 return 0;
3123}
3124
Kevin Zhu652c8e22019-09-17 15:05:45 +02003125/* Initializes the SPOE filter for a proxy for a specific thread.
3126 * Returns a negative value if an error occurs. */
3127static int
3128spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3129{
3130 struct spoe_config *conf = fconf->conf;
3131 struct spoe_agent *agent = conf->agent;
3132
Christopher Faulet46c76822019-09-17 11:55:52 +02003133 agent->rt[tid].engine_id = generate_pseudo_uuid();
3134 if (agent->rt[tid].engine_id == NULL)
3135 return -1;
Kevin Zhu652c8e22019-09-17 15:05:45 +02003136 return 0;
3137}
3138
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139/**************************************************************************
3140 * Hooks attached to a stream
3141 *************************************************************************/
3142/* Called when a filter instance is created and attach to a stream. It creates
3143 * the context that will be used to process this stream. */
3144static int
3145spoe_start(struct stream *s, struct filter *filter)
3146{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003147 struct spoe_config *conf = FLT_CONF(filter);
3148 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003149 struct spoe_context *ctx;
3150
3151 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003152 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003153 __FUNCTION__, s);
3154
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003155 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003156 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3157 " - failed to create SPOE context\n",
3158 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003159 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003160 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003161 "SPOE: [%s] failed to create SPOE context\n",
3162 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 return 0;
3164 }
3165
Christopher Faulet11610f32017-09-21 10:23:10 +02003166 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003167 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3168
Christopher Faulet11610f32017-09-21 10:23:10 +02003169 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003170 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3171
Christopher Faulet11610f32017-09-21 10:23:10 +02003172 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003173 filter->pre_analyzers |= AN_RES_INSPECT;
3174
Christopher Faulet11610f32017-09-21 10:23:10 +02003175 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003176 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3177
Christopher Faulet11610f32017-09-21 10:23:10 +02003178 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003179 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3180
Christopher Faulet11610f32017-09-21 10:23:10 +02003181 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003182 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3183
3184 return 1;
3185}
3186
3187/* Called when a filter instance is detached from a stream. It release the
3188 * attached SPOE context. */
3189static void
3190spoe_stop(struct stream *s, struct filter *filter)
3191{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003192 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3193 (int)now.tv_sec, (int)now.tv_usec,
3194 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3195 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003196 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003197}
3198
Christopher Fauletf7a30922016-11-10 15:04:51 +01003199
3200/*
3201 * Called when the stream is woken up because of expired timer.
3202 */
3203static void
3204spoe_check_timeouts(struct stream *s, struct filter *filter)
3205{
3206 struct spoe_context *ctx = filter->ctx;
3207
Christopher Fauletac580602018-03-20 16:09:20 +01003208 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003209 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003210}
3211
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003212/* Called when we are ready to filter data on a channel */
3213static int
3214spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3215{
3216 struct spoe_context *ctx = filter->ctx;
3217 int ret = 1;
3218
3219 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3220 " - ctx-flags=0x%08x\n",
3221 (int)now.tv_sec, (int)now.tv_usec,
3222 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3223 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3224
Christopher Fauletb067b062017-01-04 16:39:11 +01003225 if (ctx->state == SPOE_CTX_ST_NONE)
3226 goto out;
3227
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 if (!(chn->flags & CF_ISRESP)) {
3229 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3230 chn->analysers |= AN_REQ_INSPECT_FE;
3231 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3232 chn->analysers |= AN_REQ_INSPECT_BE;
3233
3234 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3235 goto out;
3236
3237 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003238 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003239 if (!ret)
3240 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003241 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3242 }
3243 else {
Miroslav Zagoracd995d5f2020-06-19 22:17:17 +02003244 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003245 chn->analysers |= AN_RES_INSPECT;
3246
3247 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3248 goto out;
3249
Christopher Faulet8ef75252017-02-20 22:56:03 +01003250 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003251 if (!ret) {
3252 channel_dont_read(chn);
3253 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003254 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003255 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003256 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003257 }
3258
3259 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 return ret;
3261}
3262
3263/* Called before a processing happens on a given channel */
3264static int
3265spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3266 struct channel *chn, unsigned an_bit)
3267{
3268 struct spoe_context *ctx = filter->ctx;
3269 int ret = 1;
3270
3271 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3272 " - ctx-flags=0x%08x - ana=0x%08x\n",
3273 (int)now.tv_sec, (int)now.tv_usec,
3274 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3275 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3276 ctx->flags, an_bit);
3277
Christopher Fauletb067b062017-01-04 16:39:11 +01003278 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 goto out;
3280
3281 switch (an_bit) {
3282 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003283 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003284 break;
3285 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003286 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003287 break;
3288 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003289 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003290 break;
3291 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003292 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003293 break;
3294 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003295 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003296 break;
3297 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003298 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 break;
3300 }
3301
3302 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003303 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 channel_dont_read(chn);
3305 channel_dont_close(chn);
3306 }
3307 return ret;
3308}
3309
3310/* Called when the filtering on the channel ends. */
3311static int
3312spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3313{
3314 struct spoe_context *ctx = filter->ctx;
3315
3316 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3317 " - ctx-flags=0x%08x\n",
3318 (int)now.tv_sec, (int)now.tv_usec,
3319 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3320 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3321
3322 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003323 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003324 }
3325
3326 return 1;
3327}
3328
3329/********************************************************************
3330 * Functions that manage the filter initialization
3331 ********************************************************************/
3332struct flt_ops spoe_ops = {
3333 /* Manage SPOE filter, called for each filter declaration */
3334 .init = spoe_init,
3335 .deinit = spoe_deinit,
3336 .check = spoe_check,
Kevin Zhu652c8e22019-09-17 15:05:45 +02003337 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003338
3339 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003340 .attach = spoe_start,
3341 .detach = spoe_stop,
3342 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003343
3344 /* Handle channels activity */
3345 .channel_start_analyze = spoe_start_analyze,
3346 .channel_pre_analyze = spoe_chn_pre_analyze,
3347 .channel_end_analyze = spoe_end_analyze,
3348};
3349
3350
3351static int
3352cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3353{
3354 const char *err;
3355 int i, err_code = 0;
3356
3357 if ((cfg_scope == NULL && curengine != NULL) ||
3358 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003359 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003360 goto out;
3361
3362 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3363 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003364 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3365 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366 err_code |= ERR_ALERT | ERR_ABORT;
3367 goto out;
3368 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003369 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3370 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 goto out;
3372 }
3373
3374 err = invalid_char(args[1]);
3375 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003376 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3377 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 err_code |= ERR_ALERT | ERR_ABORT;
3379 goto out;
3380 }
3381
3382 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003383 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3384 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385 err_code |= ERR_ALERT | ERR_ABORT;
3386 goto out;
3387 }
3388 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003389 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003390 err_code |= ERR_ALERT | ERR_ABORT;
3391 goto out;
3392 }
3393
3394 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003395
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003396 curagent->conf.file = strdup(file);
3397 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003398
3399 curagent->timeout.hello = TICK_ETERNITY;
3400 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003401 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003402
Christopher Fauleta1cda022016-12-21 08:58:06 +01003403 curagent->var_pfx = NULL;
3404 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003405 curagent->var_t_process = NULL;
3406 curagent->var_t_total = NULL;
Christopher Faulet46c76822019-09-17 11:55:52 +02003407 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003408 curagent->cps_max = 0;
3409 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003410 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003411 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003412
3413 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003414 LIST_INIT(&curagent->events[i]);
3415 LIST_INIT(&curagent->groups);
3416 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 }
3418 else if (!strcmp(args[0], "use-backend")) {
3419 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003420 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3421 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 err_code |= ERR_ALERT | ERR_FATAL;
3423 goto out;
3424 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003425 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003426 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003427 free(curagent->b.name);
3428 curagent->b.name = strdup(args[1]);
3429 }
3430 else if (!strcmp(args[0], "messages")) {
3431 int cur_arg = 1;
3432 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003433 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434
Christopher Faulet11610f32017-09-21 10:23:10 +02003435 list_for_each_entry(ph, &curmphs, list) {
3436 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003437 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3438 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003439 err_code |= ERR_ALERT | ERR_FATAL;
3440 goto out;
3441 }
3442 }
3443
Christopher Faulet11610f32017-09-21 10:23:10 +02003444 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003445 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003446 err_code |= ERR_ALERT | ERR_ABORT;
3447 goto out;
3448 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003449 ph->id = strdup(args[cur_arg]);
3450 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 cur_arg++;
3452 }
3453 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003454 else if (!strcmp(args[0], "groups")) {
3455 int cur_arg = 1;
3456 while (*args[cur_arg]) {
3457 struct spoe_placeholder *ph = NULL;
3458
3459 list_for_each_entry(ph, &curgphs, list) {
3460 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003461 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3462 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003463 err_code |= ERR_ALERT | ERR_FATAL;
3464 goto out;
3465 }
3466 }
3467
3468 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003469 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003470 err_code |= ERR_ALERT | ERR_ABORT;
3471 goto out;
3472 }
3473 ph->id = strdup(args[cur_arg]);
3474 LIST_ADDQ(&curgphs, &ph->list);
3475 cur_arg++;
3476 }
3477 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 else if (!strcmp(args[0], "timeout")) {
3479 unsigned int *tv = NULL;
3480 const char *res;
3481 unsigned timeout;
3482
3483 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003484 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3485 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003486 err_code |= ERR_ALERT | ERR_FATAL;
3487 goto out;
3488 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003489 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3490 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003491 if (!strcmp(args[1], "hello"))
3492 tv = &curagent->timeout.hello;
3493 else if (!strcmp(args[1], "idle"))
3494 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003495 else if (!strcmp(args[1], "processing"))
3496 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003497 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003498 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3499 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003500 err_code |= ERR_ALERT | ERR_FATAL;
3501 goto out;
3502 }
3503 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003504 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3505 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003506 err_code |= ERR_ALERT | ERR_FATAL;
3507 goto out;
3508 }
3509 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003510 if (res == PARSE_TIME_OVER) {
3511 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3512 file, linenum, args[2], args[0], args[1]);
3513 err_code |= ERR_ALERT | ERR_FATAL;
3514 goto out;
3515 }
3516 else if (res == PARSE_TIME_UNDER) {
3517 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3518 file, linenum, args[2], args[0], args[1]);
3519 err_code |= ERR_ALERT | ERR_FATAL;
3520 goto out;
3521 }
3522 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003523 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3524 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003525 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003526 goto out;
3527 }
3528 *tv = MS_TO_TICKS(timeout);
3529 }
3530 else if (!strcmp(args[0], "option")) {
3531 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003532 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3533 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003534 err_code |= ERR_ALERT | ERR_FATAL;
3535 goto out;
3536 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003537
Christopher Faulet305c6072017-02-23 16:17:53 +01003538 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003539 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003540 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003541 if (kwm == 1)
3542 curagent->flags &= ~SPOE_FL_PIPELINING;
3543 else
3544 curagent->flags |= SPOE_FL_PIPELINING;
3545 goto out;
3546 }
3547 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003548 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003549 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003550 if (kwm == 1)
3551 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet46c76822019-09-17 11:55:52 +02003552 else
3553 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003554 goto out;
3555 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003556 else if (!strcmp(args[1], "send-frag-payload")) {
3557 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3558 goto out;
3559 if (kwm == 1)
3560 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3561 else
3562 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3563 goto out;
3564 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003565 else if (!strcmp(args[1], "dontlog-normal")) {
3566 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3567 goto out;
3568 if (kwm == 1)
3569 curpxopts2 &= ~PR_O2_NOLOGNORM;
3570 else
3571 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003572 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003573 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003574
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003575 /* Following options does not support negation */
3576 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003577 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3578 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003579 err_code |= ERR_ALERT | ERR_FATAL;
3580 goto out;
3581 }
3582
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003583 if (!strcmp(args[1], "var-prefix")) {
3584 char *tmp;
3585
3586 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003587 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3588 file, linenum, args[0],
3589 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003590 err_code |= ERR_ALERT | ERR_FATAL;
3591 goto out;
3592 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003593 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3594 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003595 tmp = args[2];
3596 while (*tmp) {
3597 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003598 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003599 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003600 err_code |= ERR_ALERT | ERR_FATAL;
3601 goto out;
3602 }
3603 tmp++;
3604 }
3605 curagent->var_pfx = strdup(args[2]);
3606 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003607 else if (!strcmp(args[1], "force-set-var")) {
3608 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3609 goto out;
3610 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3611 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003612 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003613 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003614 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003615 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3616 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003617 else if (!strcmp(args[1], "set-on-error")) {
3618 char *tmp;
3619
3620 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003621 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3622 file, linenum, args[0],
3623 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003624 err_code |= ERR_ALERT | ERR_FATAL;
3625 goto out;
3626 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003627 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3628 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003629 tmp = args[2];
3630 while (*tmp) {
3631 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003632 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003633 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003634 err_code |= ERR_ALERT | ERR_FATAL;
3635 goto out;
3636 }
3637 tmp++;
3638 }
3639 curagent->var_on_error = strdup(args[2]);
3640 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003641 else if (!strcmp(args[1], "set-process-time")) {
3642 char *tmp;
3643
3644 if (!*args[2]) {
3645 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3646 file, linenum, args[0],
3647 args[1]);
3648 err_code |= ERR_ALERT | ERR_FATAL;
3649 goto out;
3650 }
3651 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3652 goto out;
3653 tmp = args[2];
3654 while (*tmp) {
3655 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003656 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003657 file, linenum, args[0], args[1]);
3658 err_code |= ERR_ALERT | ERR_FATAL;
3659 goto out;
3660 }
3661 tmp++;
3662 }
3663 curagent->var_t_process = strdup(args[2]);
3664 }
3665 else if (!strcmp(args[1], "set-total-time")) {
3666 char *tmp;
3667
3668 if (!*args[2]) {
3669 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3670 file, linenum, args[0],
3671 args[1]);
3672 err_code |= ERR_ALERT | ERR_FATAL;
3673 goto out;
3674 }
3675 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3676 goto out;
3677 tmp = args[2];
3678 while (*tmp) {
3679 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003680 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003681 file, linenum, args[0], args[1]);
3682 err_code |= ERR_ALERT | ERR_FATAL;
3683 goto out;
3684 }
3685 tmp++;
3686 }
3687 curagent->var_t_total = strdup(args[2]);
3688 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003689 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003690 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3691 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003692 err_code |= ERR_ALERT | ERR_FATAL;
3693 goto out;
3694 }
Christopher Faulet48026722016-11-16 15:01:12 +01003695 }
3696 else if (!strcmp(args[0], "maxconnrate")) {
3697 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003698 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3699 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003703 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003704 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003705 curagent->cps_max = atol(args[1]);
3706 }
3707 else if (!strcmp(args[0], "maxerrrate")) {
3708 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003709 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3710 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003711 err_code |= ERR_ALERT | ERR_FATAL;
3712 goto out;
3713 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003714 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003715 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003716 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003717 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003718 else if (!strcmp(args[0], "max-frame-size")) {
3719 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003720 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3721 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003722 err_code |= ERR_ALERT | ERR_FATAL;
3723 goto out;
3724 }
3725 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3726 goto out;
3727 curagent->max_frame_size = atol(args[1]);
3728 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3729 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003730 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3731 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
3735 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003736 else if (!strcmp(args[0], "max-waiting-frames")) {
3737 if (!*args[1]) {
3738 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3739 file, linenum, args[0]);
3740 err_code |= ERR_ALERT | ERR_FATAL;
3741 goto out;
3742 }
3743 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3744 goto out;
3745 curagent->max_fpa = atol(args[1]);
3746 if (curagent->max_fpa < 1) {
3747 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3748 file, linenum, args[0]);
3749 err_code |= ERR_ALERT | ERR_FATAL;
3750 goto out;
3751 }
3752 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003753 else if (!strcmp(args[0], "register-var-names")) {
3754 int cur_arg;
3755
3756 if (!*args[1]) {
3757 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3758 file, linenum, args[0]);
3759 err_code |= ERR_ALERT | ERR_FATAL;
3760 goto out;
3761 }
3762 cur_arg = 1;
3763 while (*args[cur_arg]) {
3764 struct spoe_var_placeholder *vph;
3765
3766 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3767 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3768 err_code |= ERR_ALERT | ERR_ABORT;
3769 goto out;
3770 }
3771 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusf818ea52019-06-23 22:10:13 +02003772 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003773 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3774 err_code |= ERR_ALERT | ERR_ABORT;
3775 goto out;
3776 }
3777 LIST_ADDQ(&curvars, &vph->list);
3778 cur_arg++;
3779 }
3780 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003781 else if (!strcmp(args[0], "log")) {
3782 char *errmsg = NULL;
3783
3784 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3785 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3786 err_code |= ERR_ALERT | ERR_FATAL;
3787 goto out;
3788 }
3789 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003790 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003791 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3792 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003793 err_code |= ERR_ALERT | ERR_FATAL;
3794 goto out;
3795 }
3796 out:
3797 return err_code;
3798}
Christopher Faulet11610f32017-09-21 10:23:10 +02003799static int
3800cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3801{
3802 struct spoe_group *grp;
3803 const char *err;
3804 int err_code = 0;
3805
3806 if ((cfg_scope == NULL && curengine != NULL) ||
3807 (cfg_scope != NULL && curengine == NULL) ||
3808 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3809 goto out;
3810
3811 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3812 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003813 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3814 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003815 err_code |= ERR_ALERT | ERR_ABORT;
3816 goto out;
3817 }
3818 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3819 err_code |= ERR_ABORT;
3820 goto out;
3821 }
3822
3823 err = invalid_char(args[1]);
3824 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003825 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3826 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 err_code |= ERR_ALERT | ERR_ABORT;
3828 goto out;
3829 }
3830
3831 list_for_each_entry(grp, &curgrps, list) {
3832 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003833 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3834 " name as another one declared at %s:%d.\n",
3835 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003836 err_code |= ERR_ALERT | ERR_FATAL;
3837 goto out;
3838 }
3839 }
3840
3841 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003842 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003843 err_code |= ERR_ALERT | ERR_ABORT;
3844 goto out;
3845 }
3846
3847 curgrp->id = strdup(args[1]);
3848 curgrp->conf.file = strdup(file);
3849 curgrp->conf.line = linenum;
3850 LIST_INIT(&curgrp->phs);
3851 LIST_INIT(&curgrp->messages);
3852 LIST_ADDQ(&curgrps, &curgrp->list);
3853 }
3854 else if (!strcmp(args[0], "messages")) {
3855 int cur_arg = 1;
3856 while (*args[cur_arg]) {
3857 struct spoe_placeholder *ph = NULL;
3858
3859 list_for_each_entry(ph, &curgrp->phs, list) {
3860 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3862 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003863 err_code |= ERR_ALERT | ERR_FATAL;
3864 goto out;
3865 }
3866 }
3867
3868 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003869 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003870 err_code |= ERR_ALERT | ERR_ABORT;
3871 goto out;
3872 }
3873 ph->id = strdup(args[cur_arg]);
3874 LIST_ADDQ(&curgrp->phs, &ph->list);
3875 cur_arg++;
3876 }
3877 }
3878 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003879 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3880 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003881 err_code |= ERR_ALERT | ERR_FATAL;
3882 goto out;
3883 }
3884 out:
3885 return err_code;
3886}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003887
3888static int
3889cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3890{
3891 struct spoe_message *msg;
3892 struct spoe_arg *arg;
3893 const char *err;
3894 char *errmsg = NULL;
3895 int err_code = 0;
3896
3897 if ((cfg_scope == NULL && curengine != NULL) ||
3898 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003899 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003900 goto out;
3901
3902 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3903 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003904 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3905 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003906 err_code |= ERR_ALERT | ERR_ABORT;
3907 goto out;
3908 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003909 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3910 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003911 goto out;
3912 }
3913
3914 err = invalid_char(args[1]);
3915 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003916 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3917 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 err_code |= ERR_ALERT | ERR_ABORT;
3919 goto out;
3920 }
3921
3922 list_for_each_entry(msg, &curmsgs, list) {
3923 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003924 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3925 " name as another one declared at %s:%d.\n",
3926 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 err_code |= ERR_ALERT | ERR_FATAL;
3928 goto out;
3929 }
3930 }
3931
3932 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003933 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934 err_code |= ERR_ALERT | ERR_ABORT;
3935 goto out;
3936 }
3937
3938 curmsg->id = strdup(args[1]);
3939 curmsg->id_len = strlen(curmsg->id);
3940 curmsg->event = SPOE_EV_NONE;
3941 curmsg->conf.file = strdup(file);
3942 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003943 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003944 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003945 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003946 LIST_INIT(&curmsg->by_evt);
3947 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003948 LIST_ADDQ(&curmsgs, &curmsg->list);
3949 }
3950 else if (!strcmp(args[0], "args")) {
3951 int cur_arg = 1;
3952
3953 curproxy->conf.args.ctx = ARGC_SPOE;
3954 curproxy->conf.args.file = file;
3955 curproxy->conf.args.line = linenum;
3956 while (*args[cur_arg]) {
3957 char *delim = strchr(args[cur_arg], '=');
3958 int idx = 0;
3959
3960 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003961 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003962 err_code |= ERR_ALERT | ERR_ABORT;
3963 goto out;
3964 }
3965
3966 if (!delim) {
3967 arg->name = NULL;
3968 arg->name_len = 0;
3969 delim = args[cur_arg];
3970 }
3971 else {
3972 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3973 arg->name_len = delim - args[cur_arg];
3974 delim++;
3975 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003976 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3977 &idx, file, linenum, &errmsg,
3978 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003979 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003980 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 err_code |= ERR_ALERT | ERR_FATAL;
3982 free(arg->name);
3983 free(arg);
3984 goto out;
3985 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003986 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003987 LIST_ADDQ(&curmsg->args, &arg->list);
3988 cur_arg++;
3989 }
3990 curproxy->conf.args.file = NULL;
3991 curproxy->conf.args.line = 0;
3992 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003993 else if (!strcmp(args[0], "acl")) {
3994 err = invalid_char(args[1]);
3995 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003996 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3997 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003998 err_code |= ERR_ALERT | ERR_FATAL;
3999 goto out;
4000 }
Tim Duesterhus6b7ce1e2020-02-05 21:00:50 +01004001 if (strcasecmp(args[1], "or") == 0) {
4002 ha_warning("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
4003 "logical disjunction within a condition.\n",
4004 file, linenum, args[1]);
4005 err_code |= ERR_WARN;
4006 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004007 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004008 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4009 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004010 err_code |= ERR_ALERT | ERR_FATAL;
4011 goto out;
4012 }
4013 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004014 else if (!strcmp(args[0], "event")) {
4015 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004016 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004017 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004018 goto out;
4019 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004020 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4021 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004022
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004023 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
4024 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
4025 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
4026 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4027
4028 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
4029 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
4030 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
4031 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
4032 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
4033 curmsg->event = SPOE_EV_ON_TCP_RSP;
4034
4035 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
4036 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
4037 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
4038 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
4039 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
4040 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4041 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004042 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004043 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004044 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004045 goto out;
4046 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004047
4048 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4049 struct acl_cond *cond;
4050
4051 cond = build_acl_cond(file, linenum, &curmsg->acls,
4052 curproxy, (const char **)args+2,
4053 &errmsg);
4054 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004055 ha_alert("parsing [%s:%d] : error detected while "
4056 "parsing an 'event %s' condition : %s.\n",
4057 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004058 err_code |= ERR_ALERT | ERR_FATAL;
4059 goto out;
4060 }
4061 curmsg->cond = cond;
4062 }
4063 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004064 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4065 "or 'unless' followed by a condition but found '%s'.\n",
4066 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004067 err_code |= ERR_ALERT | ERR_FATAL;
4068 goto out;
4069 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004070 }
4071 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004072 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4073 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004074 err_code |= ERR_ALERT | ERR_FATAL;
4075 goto out;
4076 }
4077 out:
4078 free(errmsg);
4079 return err_code;
4080}
4081
4082/* Return -1 on error, else 0 */
4083static int
4084parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4085 struct flt_conf *fconf, char **err, void *private)
4086{
4087 struct list backup_sections;
4088 struct spoe_config *conf;
4089 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004090 struct spoe_group *grp, *grpback;
4091 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004092 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004093 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004094 char *file = NULL, *engine = NULL;
4095 int ret, pos = *cur_arg + 1;
4096
Christopher Faulet84c844e2018-03-23 14:37:14 +01004097 LIST_INIT(&curmsgs);
4098 LIST_INIT(&curgrps);
4099 LIST_INIT(&curmphs);
4100 LIST_INIT(&curgphs);
4101 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004102 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004103 curpxopts = 0;
4104 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004105
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004106 conf = calloc(1, sizeof(*conf));
4107 if (conf == NULL) {
4108 memprintf(err, "%s: out of memory", args[*cur_arg]);
4109 goto error;
4110 }
4111 conf->proxy = px;
4112
4113 while (*args[pos]) {
4114 if (!strcmp(args[pos], "config")) {
4115 if (!*args[pos+1]) {
4116 memprintf(err, "'%s' : '%s' option without value",
4117 args[*cur_arg], args[pos]);
4118 goto error;
4119 }
4120 file = args[pos+1];
4121 pos += 2;
4122 }
4123 else if (!strcmp(args[pos], "engine")) {
4124 if (!*args[pos+1]) {
4125 memprintf(err, "'%s' : '%s' option without value",
4126 args[*cur_arg], args[pos]);
4127 goto error;
4128 }
4129 engine = args[pos+1];
4130 pos += 2;
4131 }
4132 else {
4133 memprintf(err, "unknown keyword '%s'", args[pos]);
4134 goto error;
4135 }
4136 }
4137 if (file == NULL) {
4138 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4139 goto error;
4140 }
4141
4142 /* backup sections and register SPOE sections */
4143 LIST_INIT(&backup_sections);
4144 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004145 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4146 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004147 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004148
4149 /* Parse SPOE filter configuration file */
4150 curengine = engine;
4151 curproxy = px;
4152 curagent = NULL;
4153 curmsg = NULL;
4154 ret = readcfgfile(file);
4155 curproxy = NULL;
4156
4157 /* unregister SPOE sections and restore previous sections */
4158 cfg_unregister_sections();
4159 cfg_restore_sections(&backup_sections);
4160
4161 if (ret == -1) {
4162 memprintf(err, "Could not open configuration file %s : %s",
4163 file, strerror(errno));
4164 goto error;
4165 }
4166 if (ret & (ERR_ABORT|ERR_FATAL)) {
4167 memprintf(err, "Error(s) found in configuration file %s", file);
4168 goto error;
4169 }
4170
4171 /* Check SPOE agent */
4172 if (curagent == NULL) {
4173 memprintf(err, "No SPOE agent found in file %s", file);
4174 goto error;
4175 }
4176 if (curagent->b.name == NULL) {
4177 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4178 curagent->id, curagent->conf.file, curagent->conf.line);
4179 goto error;
4180 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004181 if (curagent->timeout.hello == TICK_ETERNITY ||
4182 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004183 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004184 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4185 " | While not properly invalid, you will certainly encounter various problems\n"
4186 " | with such a configuration. To fix this, please ensure that all following\n"
4187 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4188 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004189 }
4190 if (curagent->var_pfx == NULL) {
4191 char *tmp = curagent->id;
4192
4193 while (*tmp) {
4194 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4195 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4196 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4197 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4198 goto error;
4199 }
4200 tmp++;
4201 }
4202 curagent->var_pfx = strdup(curagent->id);
4203 }
4204
Christopher Fauletb7426d12018-03-21 14:12:17 +01004205 if (curagent->var_on_error) {
4206 struct arg arg;
4207
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004208 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004209 curagent->var_pfx, curagent->var_on_error);
4210
4211 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004212 arg.data.str.area = trash.area;
4213 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004214 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004215 if (!vars_check_arg(&arg, err)) {
4216 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4217 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4218 goto error;
4219 }
4220 }
4221
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004222 if (curagent->var_t_process) {
4223 struct arg arg;
4224
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004225 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004226 curagent->var_pfx, curagent->var_t_process);
4227
4228 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004229 arg.data.str.area = trash.area;
4230 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004231 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004232 if (!vars_check_arg(&arg, err)) {
4233 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4234 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4235 goto error;
4236 }
4237 }
4238
4239 if (curagent->var_t_total) {
4240 struct arg arg;
4241
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004242 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004243 curagent->var_pfx, curagent->var_t_total);
4244
4245 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004246 arg.data.str.area = trash.area;
4247 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004248 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004249 if (!vars_check_arg(&arg, err)) {
4250 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4251 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4252 goto error;
4253 }
4254 }
4255
Christopher Faulet11610f32017-09-21 10:23:10 +02004256 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004257 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4258 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004259 goto finish;
4260 }
4261
Christopher Faulet11610f32017-09-21 10:23:10 +02004262 /* Replace placeholders by the corresponding messages for the SPOE
4263 * agent */
4264 list_for_each_entry(ph, &curmphs, list) {
4265 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004266 struct spoe_arg *arg;
4267 unsigned int where;
4268
Christopher Faulet11610f32017-09-21 10:23:10 +02004269 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004270 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4271 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4272 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4273 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4274 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4275 }
4276 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4277 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4278 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004279 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4280 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004281 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004282 }
4283 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004284 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4285 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004286 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004287 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004288
4289 where = 0;
4290 switch (msg->event) {
4291 case SPOE_EV_ON_CLIENT_SESS:
4292 where |= SMP_VAL_FE_CON_ACC;
4293 break;
4294
4295 case SPOE_EV_ON_TCP_REQ_FE:
4296 where |= SMP_VAL_FE_REQ_CNT;
4297 break;
4298
4299 case SPOE_EV_ON_HTTP_REQ_FE:
4300 where |= SMP_VAL_FE_HRQ_HDR;
4301 break;
4302
4303 case SPOE_EV_ON_TCP_REQ_BE:
4304 if (px->cap & PR_CAP_FE)
4305 where |= SMP_VAL_FE_REQ_CNT;
4306 if (px->cap & PR_CAP_BE)
4307 where |= SMP_VAL_BE_REQ_CNT;
4308 break;
4309
4310 case SPOE_EV_ON_HTTP_REQ_BE:
4311 if (px->cap & PR_CAP_FE)
4312 where |= SMP_VAL_FE_HRQ_HDR;
4313 if (px->cap & PR_CAP_BE)
4314 where |= SMP_VAL_BE_HRQ_HDR;
4315 break;
4316
4317 case SPOE_EV_ON_SERVER_SESS:
4318 where |= SMP_VAL_BE_SRV_CON;
4319 break;
4320
4321 case SPOE_EV_ON_TCP_RSP:
4322 if (px->cap & PR_CAP_FE)
4323 where |= SMP_VAL_FE_RES_CNT;
4324 if (px->cap & PR_CAP_BE)
4325 where |= SMP_VAL_BE_RES_CNT;
4326 break;
4327
4328 case SPOE_EV_ON_HTTP_RSP:
4329 if (px->cap & PR_CAP_FE)
4330 where |= SMP_VAL_FE_HRS_HDR;
4331 if (px->cap & PR_CAP_BE)
4332 where |= SMP_VAL_BE_HRS_HDR;
4333 break;
4334
4335 default:
4336 break;
4337 }
4338
4339 list_for_each_entry(arg, &msg->args, list) {
4340 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004341 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004342 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004343 "none of which is available here ('%s')",
4344 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004345 sample_ckp_names(arg->expr->fetch->use),
4346 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004347 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004348 }
4349 }
4350
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004351 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004352 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4353 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004354 }
4355 }
4356 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004357 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004358 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004359 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004360 continue;
4361 }
4362
Christopher Faulet11610f32017-09-21 10:23:10 +02004363 /* Replace placeholders by the corresponding groups for the SPOE
4364 * agent */
4365 list_for_each_entry(ph, &curgphs, list) {
4366 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4367 if (!strcmp(grp->id, ph->id)) {
4368 grp->agent = curagent;
4369 LIST_DEL(&grp->list);
4370 LIST_ADDQ(&curagent->groups, &grp->list);
4371 goto next_aph;
4372 }
4373 }
4374 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4375 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4376 goto error;
4377 next_aph:
4378 continue;
4379 }
4380
4381 /* Replace placeholders by the corresponding message for each SPOE
4382 * group of the SPOE agent */
4383 list_for_each_entry(grp, &curagent->groups, list) {
4384 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4385 list_for_each_entry(msg, &curmsgs, list) {
4386 if (!strcmp(msg->id, ph->id)) {
4387 if (msg->group != NULL) {
4388 memprintf(err, "SPOE message '%s' already belongs to "
4389 "the SPOE group '%s' declare at %s:%d",
4390 msg->id, msg->group->id,
4391 msg->group->conf.file,
4392 msg->group->conf.line);
4393 goto error;
4394 }
4395
4396 /* Scope for arguments are not checked for now. We will check
4397 * them only if a rule use the corresponding SPOE group. */
4398 msg->agent = curagent;
4399 msg->group = grp;
4400 LIST_DEL(&ph->list);
4401 LIST_ADDQ(&grp->messages, &msg->by_grp);
4402 goto next_mph_grp;
4403 }
4404 }
4405 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4406 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4407 goto error;
4408 next_mph_grp:
4409 continue;
4410 }
4411 }
4412
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004413 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004414 /* move curmsgs to the agent message list */
4415 curmsgs.n->p = &curagent->messages;
4416 curmsgs.p->n = &curagent->messages;
4417 curagent->messages = curmsgs;
4418 LIST_INIT(&curmsgs);
4419
Christopher Faulet7ee86672017-09-19 11:08:28 +02004420 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004421 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004422
4423 /* Start agent's proxy initialization here. It will be finished during
4424 * the filter init. */
4425 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4426 init_new_proxy(&conf->agent_fe);
4427 conf->agent_fe.id = conf->agent->id;
4428 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004429 conf->agent_fe.options |= curpxopts;
4430 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004431
4432 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4433 LIST_DEL(&logsrv->list);
4434 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4435 }
4436
Christopher Faulet11610f32017-09-21 10:23:10 +02004437 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4438 LIST_DEL(&ph->list);
4439 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004440 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004441 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4442 LIST_DEL(&ph->list);
4443 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004444 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004445 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4446 struct arg arg;
4447
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004448 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004449 curagent->var_pfx, vph->name);
4450
4451 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004452 arg.data.str.area = trash.area;
4453 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004454 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004455 if (!vars_check_arg(&arg, err)) {
4456 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4457 curagent->id, curagent->var_pfx, vph->name, *err);
4458 goto error;
4459 }
4460
4461 LIST_DEL(&vph->list);
4462 free(vph->name);
4463 free(vph);
4464 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004465 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4466 LIST_DEL(&grp->list);
4467 spoe_release_group(grp);
4468 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004469 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004470 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004471 fconf->ops = &spoe_ops;
4472 fconf->conf = conf;
4473 return 0;
4474
4475 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004476 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004477 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4478 LIST_DEL(&ph->list);
4479 spoe_release_placeholder(ph);
4480 }
4481 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4482 LIST_DEL(&ph->list);
4483 spoe_release_placeholder(ph);
4484 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004485 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4486 LIST_DEL(&vph->list);
4487 free(vph->name);
4488 free(vph);
4489 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004490 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4491 LIST_DEL(&grp->list);
4492 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004493 }
4494 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4495 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004496 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004497 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004498 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4499 LIST_DEL(&logsrv->list);
4500 free(logsrv);
4501 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004502 free(conf);
4503 return -1;
4504}
4505
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004506/* Send message of a SPOE group. This is the action_ptr callback of a rule
4507 * associated to a "send-spoe-group" action.
4508 *
4509 * It returns ACT_RET_CONT is processing is finished without error, it returns
4510 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4511 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004512static enum act_return
4513spoe_send_group(struct act_rule *rule, struct proxy *px,
4514 struct session *sess, struct stream *s, int flags)
4515{
4516 struct filter *filter;
4517 struct spoe_agent *agent = NULL;
4518 struct spoe_group *group = NULL;
4519 struct spoe_context *ctx = NULL;
4520 int ret, dir;
4521
4522 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4523 if (filter->config == rule->arg.act.p[0]) {
4524 agent = rule->arg.act.p[2];
4525 group = rule->arg.act.p[3];
4526 ctx = filter->ctx;
4527 break;
4528 }
4529 }
4530 if (agent == NULL || group == NULL || ctx == NULL)
4531 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004532 if (ctx->state == SPOE_CTX_ST_NONE)
4533 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004534
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004535 switch (rule->from) {
4536 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4537 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4538 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4539 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4540 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4541 default:
4542 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4543 " - internal error while execute spoe-send-group\n",
4544 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4545 __FUNCTION__, s);
4546 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4547 agent->id);
4548 return ACT_RET_CONT;
4549 }
4550
4551 ret = spoe_process_group(s, ctx, group, dir);
4552 if (ret == 1)
4553 return ACT_RET_CONT;
4554 else if (ret == 0) {
4555 if (flags & ACT_FLAG_FINAL) {
4556 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4557 " - failed to process group '%s': interrupted by caller\n",
4558 (int)now.tv_sec, (int)now.tv_usec,
4559 agent->id, __FUNCTION__, s, group->id);
4560 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004561 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004562 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004563 return ACT_RET_CONT;
4564 }
4565 return ACT_RET_YIELD;
4566 }
4567 else
4568 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004569}
4570
4571/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4572 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4573 * action should be:
4574 *
4575 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4576 *
4577 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4578 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4579 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4580 * group.
4581 *
4582 * The function returns 1 in success case, otherwise, it returns 0 and err is
4583 * filled.
4584 */
4585static int
4586check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4587{
4588 struct flt_conf *fconf;
4589 struct spoe_config *conf;
4590 struct spoe_agent *agent = NULL;
4591 struct spoe_group *group;
4592 struct spoe_message *msg;
4593 char *engine_id = rule->arg.act.p[0];
4594 char *group_id = rule->arg.act.p[1];
4595 unsigned int where = 0;
4596
4597 switch (rule->from) {
4598 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4599 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4600 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4601 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4602 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4603 default:
4604 memprintf(err,
4605 "internal error, unexpected rule->from=%d, please report this bug!",
4606 rule->from);
4607 goto error;
4608 }
4609
4610 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4611 * <px> */
4612 list_for_each_entry(fconf, &px->filter_configs, list) {
4613 conf = fconf->conf;
4614
4615 /* This is not an SPOE filter */
4616 if (fconf->id != spoe_filter_id)
4617 continue;
4618
4619 /* This is the good engine */
4620 if (!strcmp(conf->id, engine_id)) {
4621 agent = conf->agent;
4622 break;
4623 }
4624 }
4625 if (agent == NULL) {
4626 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4627 engine_id, group_id);
4628 goto error;
4629 }
4630
4631 /* Try to find the right group */
4632 list_for_each_entry(group, &agent->groups, list) {
4633 /* This is the good group */
4634 if (!strcmp(group->id, group_id))
4635 break;
4636 }
4637 if (&group->list == &agent->groups) {
4638 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4639 group_id, engine_id);
4640 goto error;
4641 }
4642
4643 /* Ok, we found the group, we need to check messages and their
4644 * arguments */
4645 list_for_each_entry(msg, &group->messages, by_grp) {
4646 struct spoe_arg *arg;
4647
4648 list_for_each_entry(arg, &msg->args, list) {
4649 if (!(arg->expr->fetch->val & where)) {
4650 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4651 "some args extract information from '%s',"
4652 "none of which is available here ('%s')",
4653 msg->id, group->id, msg->conf.file, msg->conf.line,
4654 sample_ckp_names(arg->expr->fetch->use),
4655 sample_ckp_names(where));
4656 goto error;
4657 }
4658 }
4659 }
4660
4661 free(engine_id);
4662 free(group_id);
4663 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4664 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4665 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4666 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4667 return 1;
4668
4669 error:
4670 free(engine_id);
4671 free(group_id);
4672 return 0;
4673}
4674
4675/* Parse 'send-spoe-group' action following the format:
4676 *
4677 * ... send-spoe-group <engine-id> <group-id>
4678 *
4679 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4680 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4681 * ids are saved and used later, when the rule will be checked.
4682 */
4683static enum act_parse_ret
4684parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4685 struct act_rule *rule, char **err)
4686{
4687 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4688 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4689 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4690 return ACT_RET_PRS_ERR;
4691 }
4692 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4693 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4694
4695 (*orig_arg) += 2;
4696
4697 rule->action = ACT_CUSTOM;
4698 rule->action_ptr = spoe_send_group;
4699 rule->check_ptr = check_send_spoe_group;
4700 return ACT_RET_PRS_OK;
4701}
4702
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004703
4704/* Declare the filter parser for "spoe" keyword */
4705static struct flt_kw_list flt_kws = { "SPOE", { }, {
4706 { "spoe", parse_spoe_flt, NULL },
4707 { NULL, NULL, NULL },
4708 }
4709};
4710
Willy Tarreau0108d902018-11-25 19:14:37 +01004711INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4712
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004713/* Delcate the action parser for "spoe-action" keyword */
4714static struct action_kw_list tcp_req_action_kws = { { }, {
4715 { "send-spoe-group", parse_send_spoe_group },
4716 { /* END */ },
4717 }
4718};
Willy Tarreau0108d902018-11-25 19:14:37 +01004719
4720INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4721
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004722static struct action_kw_list tcp_res_action_kws = { { }, {
4723 { "send-spoe-group", parse_send_spoe_group },
4724 { /* END */ },
4725 }
4726};
Willy Tarreau0108d902018-11-25 19:14:37 +01004727
4728INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4729
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004730static struct action_kw_list http_req_action_kws = { { }, {
4731 { "send-spoe-group", parse_send_spoe_group },
4732 { /* END */ },
4733 }
4734};
Willy Tarreau0108d902018-11-25 19:14:37 +01004735
4736INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4737
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004738static struct action_kw_list http_res_action_kws = { { }, {
4739 { "send-spoe-group", parse_send_spoe_group },
4740 { /* END */ },
4741 }
4742};
4743
Willy Tarreau0108d902018-11-25 19:14:37 +01004744INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);