blob: 6840ec8d65248d90428256d97364875796d3aee6 [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 Fauletd6e54662021-08-02 17:53:56 +0200112static struct appctx *spoe_create_appctx(struct spoe_config *conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113
114/********************************************************************
115 * helper functions/globals
116 ********************************************************************/
117static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200118spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119{
Christopher Faulet11610f32017-09-21 10:23:10 +0200120 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200122 free(ph->id);
123 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124}
125
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200126static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100127spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128{
Christopher Faulet57583e42017-09-04 15:41:09 +0200129 struct spoe_arg *arg, *argback;
130 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200131
132 if (!msg)
133 return;
134 free(msg->id);
135 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200136 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200137 release_sample_expr(arg->expr);
138 free(arg->name);
139 LIST_DEL(&arg->list);
140 free(arg);
141 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200142 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
143 LIST_DEL(&acl->list);
144 prune_acl(acl);
145 free(acl);
146 }
147 if (msg->cond) {
148 prune_acl_cond(msg->cond);
149 free(msg->cond);
150 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200151 free(msg);
152}
153
154static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200155spoe_release_group(struct spoe_group *grp)
156{
157 if (!grp)
158 return;
159 free(grp->id);
160 free(grp->conf.file);
161 free(grp);
162}
163
164static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100165spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200166{
Christopher Faulet11610f32017-09-21 10:23:10 +0200167 struct spoe_message *msg, *msgback;
168 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200169 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200170
171 if (!agent)
172 return;
173 free(agent->id);
174 free(agent->conf.file);
175 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100176 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100177 free(agent->var_t_process);
178 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200179 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
180 LIST_DEL(&msg->list);
181 spoe_release_message(msg);
182 }
183 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
184 LIST_DEL(&grp->list);
185 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200186 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 if (agent->rt) {
Christopher Faulet46c76822019-09-17 11:55:52 +0200188 for (i = 0; i < global.nbthread; ++i) {
189 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100190 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet46c76822019-09-17 11:55:52 +0200191 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100192 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200193 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200194 free(agent);
195}
196
197static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100198 [SPOE_FRM_ERR_NONE] = "normal",
199 [SPOE_FRM_ERR_IO] = "I/O error",
200 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
201 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
202 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
203 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
204 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
205 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
206 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
207 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
208 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
209 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100210 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100211 [SPOE_FRM_ERR_RES] = "resource allocation error",
212 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200213};
214
215static const char *spoe_event_str[SPOE_EV_EVENTS] = {
216 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
217 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
218 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
219 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
220 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
221
222 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
223 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
224 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
225};
226
227
228#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
229
230static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100231 [SPOE_CTX_ST_NONE] = "NONE",
232 [SPOE_CTX_ST_READY] = "READY",
233 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
234 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
235 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
236 [SPOE_CTX_ST_DONE] = "DONE",
237 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200238};
239
240static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100241 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
242 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
243 [SPOE_APPCTX_ST_IDLE] = "IDLE",
244 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
245 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
246 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
247 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
248 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
249 [SPOE_APPCTX_ST_EXIT] = "EXIT",
250 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200251};
252
253#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254
Christopher Faulet8ef75252017-02-20 22:56:03 +0100255/* Used to generates a unique id for an engine. On success, it returns a
256 * allocated string. So it is the caller's reponsibility to release it. If the
257 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100258static char *
259generate_pseudo_uuid()
260{
Christopher Fauleta1cda022016-12-21 08:58:06 +0100261 char *uuid;
Christopher Fauleteaf11912019-09-17 15:07:02 +0200262 uint32_t rnd[4] = { 0, 0, 0, 0 };
263 uint64_t last = 0;
264 int byte = 0;
265 uint8_t bits = 0;
266 unsigned int rand_max_bits = my_flsl(RAND_MAX);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100267
Christopher Fauleteaf11912019-09-17 15:07:02 +0200268 if ((uuid = calloc(1, 37)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100269 return NULL;
270
Christopher Fauleteaf11912019-09-17 15:07:02 +0200271 while (byte < 4) {
272 while (bits < 32) {
Willy Tarreau861c4ef2020-03-08 00:42:37 +0100273 last |= (uint64_t)ha_random() << bits;
Christopher Fauleteaf11912019-09-17 15:07:02 +0200274 bits += rand_max_bits;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100275 }
Christopher Fauleteaf11912019-09-17 15:07:02 +0200276 rnd[byte++] = last;
277 last >>= 32u;
278 bits -= 32;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279 }
Willy Tarreau079ec122019-10-29 10:25:49 +0100280 snprintf(uuid, 37, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
Christopher Fauleteaf11912019-09-17 15:07:02 +0200281 rnd[0],
282 rnd[1] & 0xFFFF,
283 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
284 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
285 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
286 );
Christopher Fauleta1cda022016-12-21 08:58:06 +0100287 return uuid;
288}
289
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100290
291static inline void
292spoe_update_stat_time(struct timeval *tv, long *t)
293{
294 if (*t == -1)
295 *t = tv_ms_elapsed(tv, &now);
296 else
297 *t += tv_ms_elapsed(tv, &now);
298 tv_zero(tv);
299}
300
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301/********************************************************************
302 * Functions that encode/decode SPOE frames
303 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304/* Helper to get static string length, excluding the terminating null byte */
305#define SLEN(str) (sizeof(str)-1)
306
307/* Predefined key used in HELLO/DISCONNECT frames */
308#define SUPPORTED_VERSIONS_KEY "supported-versions"
309#define VERSION_KEY "version"
310#define MAX_FRAME_SIZE_KEY "max-frame-size"
311#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100312#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100313#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200314#define STATUS_CODE_KEY "status-code"
315#define MSG_KEY "message"
316
317struct spoe_version {
318 char *str;
319 int min;
320 int max;
321};
322
323/* All supported versions */
324static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200325 /* 1.0 is now unsupported because of a bug about frame's flags*/
326 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200327 {NULL, 0, 0}
328};
329
330/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200331#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333/* Convert a string to a SPOE version value. The string must follow the format
334 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
335 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200336static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100337spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200338{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100339 const char *p, *end;
340 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342 p = str;
343 end = str+len;
344 maj = min = 0;
345 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347 /* skip leading spaces */
348 while (p < end && isspace(*p))
349 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200350
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351 /* parse Major number, until the '.' */
352 while (*p != '.') {
353 if (p >= end || *p < '0' || *p > '9')
354 goto out;
355 maj *= 10;
356 maj += (*p - '0');
357 p++;
358 }
359
360 /* check Major version */
361 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362 goto out;
363
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364 p++; /* skip the '.' */
365 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
366 goto out;
367
368 /* Parse Minor number */
369 while (p < end) {
370 if (*p < '0' || *p > '9')
371 break;
372 min *= 10;
373 min += (*p - '0');
374 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376
377 /* check Minor number */
378 if (min > 999)
379 goto out;
380
381 /* skip trailing spaces */
382 while (p < end && isspace(*p))
383 p++;
384 if (p != end)
385 goto out;
386
387 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388 out:
389 return vsn;
390}
391
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800393 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100396spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397{
Willy Tarreau83061a82018-07-13 11:56:34 +0200398 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100399 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 char *p, *end;
401 unsigned int flags = SPOE_FRM_FL_FIN;
402 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200403
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 p = frame;
405 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200406
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 /* Set Frame type */
408 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100410 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200411 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 memcpy(p, (char *)&flags, 4);
413 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200414
415 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100416 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200417
418 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
419 * and "capabilities" */
420
421 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100422 sz = SLEN(SUPPORTED_VERSIONS_KEY);
423 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
424 goto too_big;
425
426 *p++ = SPOE_DATA_T_STR;
427 sz = SLEN(SUPPORTED_VERSIONS_VAL);
428 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
431 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 sz = SLEN(MAX_FRAME_SIZE_KEY);
433 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
434 goto too_big;
435
436 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200437 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200439
440 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 sz = SLEN(CAPABILITIES_KEY);
442 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
443 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200444
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100446 chk = get_trash_chunk();
447 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200448 memcpy(chk->area, "pipelining", 10);
449 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100450 }
451 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 if (chk->data) chk->area[chk->data++] = ',';
453 memcpy(chk->area+chk->data, "async", 5);
454 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100455 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100456 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 if (chk->data) chk->area[chk->data++] = ',';
458 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100459 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100460 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 goto too_big;
463
464 /* (optionnal) "engine-id" K/V item, if present */
Christopher Faulet46c76822019-09-17 11:55:52 +0200465 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 sz = SLEN(ENGINE_ID_KEY);
467 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
468 goto too_big;
469
470 *p++ = SPOE_DATA_T_STR;
Christopher Faulet46c76822019-09-17 11:55:52 +0200471 sz = strlen(agent->rt[tid].engine_id);
472 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100474 }
475
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 return (p - frame);
477
478 too_big:
479 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
480 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200481}
482
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
484 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
485 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 const char *reason;
490 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100491 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 p = frame;
495 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 /* Set Frame type */
498 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200499
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100500 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200501 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 memcpy(p, (char *)&flags, 4);
503 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200504
505 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100506 *p++ = 0; *p++ = 0;
507
508 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
509 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200510
511 /* There are 2 mandatory items: "status-code" and "message" */
512
513 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514 sz = SLEN(STATUS_CODE_KEY);
515 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
516 goto too_big;
517
518 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200519 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200521
522 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 sz = SLEN(MSG_KEY);
524 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
525 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200526
Christopher Faulet8ef75252017-02-20 22:56:03 +0100527 /*Get the message corresponding to the status code */
528 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
529
530 *p++ = SPOE_DATA_T_STR;
531 sz = strlen(reason);
532 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
533 goto too_big;
534
535 return (p - frame);
536
537 too_big:
538 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
539 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200540}
541
Christopher Faulet8ef75252017-02-20 22:56:03 +0100542/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
543 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
544 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200545static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100546spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100547 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200548{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100549 char *p, *end;
550 unsigned int stream_id, frame_id;
551 unsigned int flags = SPOE_FRM_FL_FIN;
552 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200553
Christopher Faulet8ef75252017-02-20 22:56:03 +0100554 p = frame;
555 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200556
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 stream_id = ctx->stream_id;
558 frame_id = ctx->frame_id;
559
560 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
561 /* The fragmentation is not supported by the applet */
562 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
563 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
564 return -1;
565 }
566 flags = ctx->frag_ctx.flags;
567 }
568
569 /* Set Frame type */
570 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
571
572 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200573 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100574 memcpy(p, (char *)&flags, 4);
575 p += 4;
576
577 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200578 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100579 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200580 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100581 goto too_big;
582
583 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200584 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100585 if (p + sz >= end)
586 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200587 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 p += sz;
589
590 return (p - frame);
591
592 too_big:
593 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
594 return 0;
595}
596
597/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
598 * returns the number of encoded bytes in the frame on success, 0 if an encoding
599 * error occurred and -1 if a fatal error occurred. */
600static int
601spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
602 char *frame, size_t size)
603{
604 char *p, *end;
605 unsigned int stream_id, frame_id;
606 unsigned int flags;
607 size_t sz;
608
609 p = frame;
610 end = frame+size;
611
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 /* <ctx> is null when the stream has aborted the processing of a
613 * fragmented frame. In this case, we must notify the corresponding
614 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100615 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100616 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100617 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
618 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
619 }
620 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100621 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100622 stream_id = ctx->stream_id;
623 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100624 }
625
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100629 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200630 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631 memcpy(p, (char *)&flags, 4);
632 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633
634 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200635 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200637 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100640 if (ctx == NULL)
641 goto end;
642
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200644 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (p + sz >= end)
646 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200647 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100649
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100650 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 return (p - frame);
652
653 too_big:
654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
655 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200656}
657
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658/* Decode and process the HELLO frame sent by an agent. It returns the number of
659 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
660 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663{
Christopher Faulet305c6072017-02-23 16:17:53 +0100664 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
665 char *p, *end;
666 int vsn, max_frame_size;
667 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668
669 p = frame;
670 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671
672 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
674 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200675 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 if (size < 7 /* TYPE + METADATA */) {
679 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
680 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200681 }
682
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100683 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200685 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100688 /* Fragmentation is not supported for HELLO frame */
689 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100691 return -1;
692 }
693
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 if (*p != 0 || *(p+1) != 0) {
696 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
697 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200698 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700
701 /* There are 3 mandatory items: "version", "max-frame-size" and
702 * "capabilities" */
703
704 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100705 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 while (p < end) {
707 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200708 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710
711 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 ret = spoe_decode_buffer(&p, end, &str, &sz);
713 if (ret == -1 || !sz) {
714 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
715 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 /* Check "version" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200719 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 int i, type = *p++;
721
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
729 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 }
731
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200733 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 for (i = 0; supported_versions[i].str != NULL; ++i) {
738 if (vsn >= supported_versions[i].min &&
739 vsn <= supported_versions[i].max)
740 break;
741 }
742 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744 return -1;
745 }
746 }
747 /* Check "max-frame-size" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200748 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 +0100749 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200750
751 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200752 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
753 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
754 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
755 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
757 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200758 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200759 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100760 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
761 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200762 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 if (sz < MIN_FRAME_SIZE ||
764 sz > SPOE_APPCTX(appctx)->max_frame_size) {
765 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200766 return -1;
767 }
768 max_frame_size = sz;
769 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 /* Check "capabilities" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200771 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100773
774 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
776 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
777 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
780 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
781 return 0;
782 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100783
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785 char *delim;
786
787 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789
Christopher Faulet8ef75252017-02-20 22:56:03 +0100790 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
791 str += 10; sz -= 10;
792 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100793 flags |= SPOE_APPCTX_FL_PIPELINING;
794 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 else if (sz >= 5 && !strncmp(str, "async", 5)) {
796 str += 5; sz -= 5;
797 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100798 flags |= SPOE_APPCTX_FL_ASYNC;
799 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
801 str += 13; sz -= 13;
802 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100803 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
804 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100805
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806 /* Get the next comma or break */
807 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100808 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100809 delim++;
810 sz -= (delim - str);
811 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100812 }
813 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200814 else {
815 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816 if (spoe_skip_data(&p, end) == -1) {
817 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
818 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820 }
821 }
822
823 /* Final checks */
824 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 return -1;
827 }
828 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830 return -1;
831 }
Christopher Faulet11389012019-02-07 16:13:26 +0100832 if (!agent)
833 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
834 else {
835 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
836 flags &= ~SPOE_APPCTX_FL_PIPELINING;
837 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
838 flags &= ~SPOE_APPCTX_FL_ASYNC;
839 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840
Christopher Faulet42bfa462017-01-04 14:14:19 +0100841 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
842 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
843 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844
845 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200846}
847
848/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
849 * bytes on success, 0 if the frame can be ignored and -1 if an error
850 * occurred. */
851static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100855 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856
857 p = frame;
858 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859
860 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
862 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 if (size < 7 /* TYPE + METADATA */) {
867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
868 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 }
870
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100871 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200873 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100876 /* Fragmentation is not supported for DISCONNECT frame */
877 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100879 return -1;
880 }
881
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 if (*p != 0 || *(p+1) != 0) {
884 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
885 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888
889 /* There are 2 mandatory items: "status-code" and "message" */
890
891 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 while (p < end) {
893 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200894 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896
897 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 ret = spoe_decode_buffer(&p, end, &str, &sz);
899 if (ret == -1 || !sz) {
900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
903
904 /* Check "status-code" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200905 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907
908 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
910 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
911 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
912 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
914 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200916 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921 }
922
923 /* Check "message" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200924 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 int type = *p++;
926
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
929 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
930 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200931 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 ret = spoe_decode_buffer(&p, end, &str, &sz);
933 if (ret == -1 || sz > 255) {
934 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
935 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200936 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100937#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
938 SPOE_APPCTX(appctx)->reason = str;
939 SPOE_APPCTX(appctx)->rlen = sz;
940#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941 }
942 else {
943 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (spoe_skip_data(&p, end) == -1) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
946 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 }
949 }
950
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952}
953
954
Christopher Fauleta1cda022016-12-21 08:58:06 +0100955/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200956 * success, 0 if the frame can be ignored and -1 if an error occurred. */
957static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100965 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966
967 p = frame;
968 end = frame + size;
969 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970
971 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
973 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200974 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 if (size < 7 /* TYPE + METADATA */) {
978 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
979 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 }
981
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200984 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200986
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100987 /* Fragmentation is not supported for now */
988 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100989 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100990 return -1;
991 }
992
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200993 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200994 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100995 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100996 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100997 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200998 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100999 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001000 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001001 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001002
Christopher Faulet8ef75252017-02-20 22:56:03 +01001003 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +01001004 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +02001005 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001006 if ((*ctx)->stream_id == (unsigned int)stream_id &&
1007 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001008 goto found;
1009 }
1010 }
1011 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1013 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001014 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001015 goto found;
1016 }
1017 }
1018
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001019 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1020 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1021 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1022
1023 /* ABRT bit is set for an unfinished fragmented frame */
1024 if (flags & SPOE_FRM_FL_ABRT) {
1025 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001026 (*ctx)->state = SPOE_CTX_ST_ERROR;
1027 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1028 /* Ignore the payload */
1029 goto end;
1030 }
1031 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1032 /* For now, we ignore the ack */
1033 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1034 return 0;
1035 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036
Christopher Fauleta1cda022016-12-21 08:58:06 +01001037 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1039 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 " - stream-id=%u - frame-id=%u\n",
1041 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1042 __FUNCTION__, appctx,
1043 (unsigned int)stream_id, (unsigned int)frame_id);
1044
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001045 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletf4e52512020-11-10 14:31:39 +01001046 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1047 /* Report an error if we are waiting the ack for another frame,
1048 * but not if there is no longer frame waiting for a ack
1049 * (timeout)
1050 */
1051 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1052 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1053 return -1;
1054 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1055 SPOE_APPCTX(appctx)->cur_fpa = 0;
1056 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001057 return 0;
1058
1059 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1061 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001062 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001063 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001064 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001065
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001066 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001068 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1069 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001071
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001072 /* Transfer the buffer ownership to the SPOE context */
1073 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001074 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 (*ctx)->state = SPOE_CTX_ST_DONE;
1077
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001078 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001079 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 " - ACK frame received"
1081 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001082 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1084 (*ctx)->frame_id, flags);
1085 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001086}
1087
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088/* This function is used in cfgparse.c and declared in proto/checks.h. It
1089 * prepare the request to send to agents during a healthcheck. It returns 0 on
1090 * success and -1 if an error occurred. */
1091int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 struct appctx appctx;
1095 struct spoe_appctx spoe_appctx;
1096 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1097 size_t sz;
1098 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099
Christopher Faulet42bfa462017-01-04 14:14:19 +01001100 memset(&appctx, 0, sizeof(appctx));
1101 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001103
1104 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001105 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1108 end = frame + MAX_FRAME_SIZE;
1109
1110 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1111 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 /* Add "healthcheck" K/V item */
1116 sz = SLEN(HEALTHCHECK_KEY);
1117 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1118 return -1;
1119 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120
Christopher Faulet8ef75252017-02-20 22:56:03 +01001121 *len = frame - buf;
1122 sz = htonl(*len - 4);
1123 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return 0;
1129}
1130
1131/* This function is used in checks.c and declared in proto/checks.h. It decode
1132 * the response received from an agent during a healthcheck. It returns 0 on
1133 * success and -1 if an error occurred. */
1134int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001136{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001137 struct appctx appctx;
1138 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001139
Christopher Faulet42bfa462017-01-04 14:14:19 +01001140 memset(&appctx, 0, sizeof(appctx));
1141 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001142
Christopher Faulet42bfa462017-01-04 14:14:19 +01001143 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001144 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001145
Christopher Faulet8ef75252017-02-20 22:56:03 +01001146 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1147 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001148 goto error;
1149 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1151 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001152
1153 return 0;
1154
1155 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1157 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1158 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001159 return -1;
1160}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001161
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1163 * the frame can be ignored, 1 to retry later, and the frame legnth on
1164 * success. */
1165static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001166spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167{
1168 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001169 int ret;
1170 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1173 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 netint = htonl(framesz);
1175 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001176 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001178 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001179 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181 }
1182 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001183 return -1; /* error */
1184 }
1185 return framesz;
1186}
1187
1188/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1189 * when the frame can be ignored, 1 to retry later and the frame length on
1190 * success. */
1191static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001192spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001193{
1194 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001195 int ret;
1196 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001197
Willy Tarreau06d80a92017-10-19 14:32:15 +02001198 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001199 if (ret > 0) {
1200 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001201 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001202 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001203 return -1;
1204 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001205 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001206 }
1207 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001209 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 }
1211 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001212 return -1; /* error */
1213 }
1214 return framesz;
1215}
1216
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001217/********************************************************************
1218 * Functions that manage the SPOE applet
1219 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001220static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001222{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001223 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001224 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001225 appctx_wakeup(appctx);
1226 return 1;
1227}
1228
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001229/* Callback function that catches applet timeouts. If a timeout occurred, we set
1230 * <appctx->st1> flag and the SPOE applet is woken up. */
1231static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001232spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001234 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001235
1236 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1237 if (tick_is_expired(task->expire, now_ms)) {
1238 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242 return task;
1243}
1244
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245/* Callback function that releases a SPOE applet. This happens when the
1246 * connection with the agent is closed. */
1247static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001248spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 struct stream_interface *si = appctx->owner;
1251 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1252 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001253 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254
1255 if (spoe_appctx == NULL)
1256 return;
1257
1258 appctx->ctx.spoe.ptr = NULL;
1259 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260
1261 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1262 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1263 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001266 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001267 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1269 LIST_DEL(&spoe_appctx->list);
1270 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001271 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001272 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001273
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001276 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1277 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001278 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001279 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001280
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001281 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1283 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001284
1285 si_shutw(si);
1286 si_shutr(si);
1287 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288 }
1289
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001291 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* Notify all waiting streams */
1294 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 LIST_DEL(&ctx->list);
1296 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001297 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001298 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Faulet746326a2020-11-10 18:45:34 +01001299 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001302 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001303 }
1304
Christopher Faulet8ef75252017-02-20 22:56:03 +01001305 /* If the applet was processing a fragmented frame, notify the
1306 * corresponding stream. */
1307 if (spoe_appctx->frag_ctx.ctx) {
1308 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001309 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001310 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001312 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1313 }
1314
Christopher Faulet746326a2020-11-10 18:45:34 +01001315 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1316 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1317 if (ctx->spoe_appctx == spoe_appctx)
1318 ctx->spoe_appctx = NULL;
1319 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001320 goto end;
Christopher Faulet746326a2020-11-10 18:45:34 +01001321 }
Christopher Fauletd6e54662021-08-02 17:53:56 +02001322 else {
1323 /* It is the last running applet and the sending and waiting
1324 * queues are not empty. Try to start a new one if HAproxy is
1325 * not stopping.
1326 */
1327 if (!stopping &&
1328 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
1329 spoe_create_appctx(agent->spoe_conf))
1330 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001331
Christopher Fauletd6e54662021-08-02 17:53:56 +02001332 /* otherwise, notify all waiting streams */
1333 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1334 LIST_DEL(&ctx->list);
1335 LIST_INIT(&ctx->list);
1336 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
1337 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1338 ctx->spoe_appctx = NULL;
1339 ctx->state = SPOE_CTX_ST_ERROR;
1340 ctx->status_code = (spoe_appctx->status_code + 0x100);
1341 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1342 }
1343 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1344 LIST_DEL(&ctx->list);
1345 LIST_INIT(&ctx->list);
1346 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
1347 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1348 ctx->spoe_appctx = NULL;
1349 ctx->state = SPOE_CTX_ST_ERROR;
1350 ctx->status_code = (spoe_appctx->status_code + 0x100);
1351 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1352 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001354
1355 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001356 /* Release allocated memory */
1357 spoe_release_buffer(&spoe_appctx->buffer,
1358 &spoe_appctx->buffer_wait);
1359 pool_free(pool_head_spoe_appctx, spoe_appctx);
1360
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001361 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001362 agent->rt[tid].frame_size = agent->max_frame_size;
1363 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1364 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001365}
1366
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001367static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001368spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001369{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001371 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001372 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001375 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1376 /* closed */
1377 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1378 goto exit;
1379 }
1380
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001381 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001382 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001383 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1385 goto stop;
1386 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001387
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1390 " - Connection timed out\n",
1391 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1392 __FUNCTION__, appctx);
1393 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001395 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001396
Christopher Faulet42bfa462017-01-04 14:14:19 +01001397 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001398 SPOE_APPCTX(appctx)->task->expire =
1399 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1402 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001403 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 ret = spoe_prepare_hahello_frame(appctx, frame,
1405 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001407 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001408
1409 switch (ret) {
1410 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 case 0: /* ignore => an error, cannot be ignored */
1412 goto exit;
1413
1414 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 goto stop;
1416
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 default:
1418 /* HELLO frame successfully sent, now wait for the
1419 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001420 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1421 goto next;
1422 }
1423
1424 next:
1425 return 0;
1426 stop:
1427 return 1;
1428 exit:
1429 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1430 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431}
1432
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001434spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001437 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 char *frame;
1439 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001440
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001441
Christopher Fauletb067b062017-01-04 16:39:11 +01001442 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001443 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001445 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001448 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1449 " - Connection timed out\n",
1450 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1451 __FUNCTION__, appctx);
1452 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 goto exit;
1454 }
1455
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001456 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001457 ret = spoe_recv_frame(appctx, frame,
1458 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001459 if (ret > 1) {
1460 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1461 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1462 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001463 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001464 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001465 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001467
Christopher Fauleta1cda022016-12-21 08:58:06 +01001468 switch (ret) {
1469 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001470 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001471 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1472 goto next;
1473
1474 case 1: /* retry later */
1475 goto stop;
1476
1477 default:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001478 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001479 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001480 SPOE_APPCTX(appctx)->node.key = 0;
1481 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001482
1483 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001484 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001485 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001487
Christopher Fauleta1cda022016-12-21 08:58:06 +01001488 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001489 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 if (trash.data)
1491 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492
1493 SPOE_APPCTX(appctx)->task->expire =
1494 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001495 return 0;
1496 stop:
1497 return 1;
1498 exit:
1499 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1500 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001501}
1502
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001503
Christopher Fauleta1cda022016-12-21 08:58:06 +01001504static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001505spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001506{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001507 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1508 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509 char *frame, *buf;
1510 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001511
Christopher Faulet8ef75252017-02-20 22:56:03 +01001512 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1513 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001514 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001515
1516 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1517 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1518 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1519 SPOE_APPCTX(appctx)->max_frame_size);
1520 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001521 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001522 *skip = 1;
1523 ret = 1;
1524 goto end;
1525 }
1526 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001527 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001528 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1529 SPOE_APPCTX(appctx)->max_frame_size);
1530
1531 }
1532
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 if (ret > 1)
1534 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001535
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 switch (ret) {
1537 case -1: /* error */
1538 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1539 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001540
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 case 0: /* ignore */
1542 if (ctx == NULL)
1543 goto abort_frag_frame;
1544
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001545 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 LIST_DEL(&ctx->list);
1547 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001548 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001549 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001550 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001551 ctx->state = SPOE_CTX_ST_ERROR;
1552 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1553 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001554 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 break;
1556
1557 case 1: /* retry */
1558 *skip = 1;
1559 break;
1560
1561 default:
1562 if (ctx == NULL)
1563 goto abort_frag_frame;
1564
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001565 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566 LIST_DEL(&ctx->list);
1567 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001568 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001569 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001570 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001571 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1572 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1573 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001574 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001576 }
1577 goto end;
1578
1579 frag_frame_sent:
1580 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001581 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001582 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1583 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1584 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001585 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1586 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1587 goto end;
1588
1589 no_frag_frame_sent:
1590 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1591 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001592 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001593 }
1594 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1595 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1596 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1597 }
1598 else {
1599 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001600 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1602 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001603 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001604 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001605 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1606 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1607 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001608 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001609
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1611 goto end;
1612
1613 abort_frag_frame:
1614 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1615 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1616 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1617 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1618 goto end;
1619
1620 end:
1621 return ret;
1622}
1623
1624static int
1625spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1626{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001627 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 struct spoe_context *ctx = NULL;
1629 char *frame;
1630 int ret;
1631
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001632 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001633 ret = spoe_recv_frame(appctx, frame,
1634 SPOE_APPCTX(appctx)->max_frame_size);
1635 if (ret > 1) {
1636 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1637 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001638 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001639 goto end;
1640 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001641 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001642 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1643 }
1644 switch (ret) {
1645 case -1: /* error */
1646 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1647 break;
1648
1649 case 0: /* ignore */
1650 break;
1651
1652 case 1: /* retry */
1653 *skip = 1;
1654 break;
1655
1656 default:
1657 LIST_DEL(&ctx->list);
1658 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001659 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001660 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1661 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001662 if (ctx->spoe_appctx) {
1663 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001664 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001665 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001666 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1667 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1668 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1669 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1670 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1671 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1672 }
1673 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1674 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001675 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1676 break;
1677 }
1678
1679 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001680 if (trash.data)
1681 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001682 end:
1683 return ret;
1684}
1685
1686static int
1687spoe_handle_processing_appctx(struct appctx *appctx)
1688{
1689 struct stream_interface *si = appctx->owner;
1690 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001692
1693 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1694 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1695 goto exit;
1696 }
1697
1698 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1699 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1700 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1701 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1702 goto next;
1703 }
1704
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001705 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001706 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001707 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1709 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001710 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001711
Christopher Faulet8f82b202018-01-24 16:23:03 +01001712 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1713 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001714
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 /* receiving_frame loop */
1716 while (!skip_receiving) {
1717 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1718 switch (ret) {
1719 case -1: /* error */
1720 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001721
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 case 0: /* ignore */
1723 active_r = 1;
1724 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001725
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 case 1: /* retry */
1727 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001728
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 default:
1730 active_r = 1;
1731 break;
1732 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001733 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001734
Christopher Faulet8f82b202018-01-24 16:23:03 +01001735 /* send_frame loop */
1736 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1737 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1738 switch (ret) {
1739 case -1: /* error */
1740 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001741
Christopher Faulet8f82b202018-01-24 16:23:03 +01001742 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001743 if (SPOE_APPCTX(appctx)->node.key)
1744 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001745 active_s++;
1746 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747
Christopher Faulet8f82b202018-01-24 16:23:03 +01001748 case 1: /* retry */
1749 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750
Christopher Faulet8f82b202018-01-24 16:23:03 +01001751 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001752 if (SPOE_APPCTX(appctx)->node.key)
1753 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001754 active_s++;
1755 break;
1756 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758
Christopher Faulet8f82b202018-01-24 16:23:03 +01001759 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001760 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1761 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1762 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001763
Christopher Faulet8f82b202018-01-24 16:23:03 +01001764 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Faulete7c50f12021-03-01 15:01:14 +01001765 struct server *srv = objt_server(si_strm(si)->target);
1766
1767 /* With several threads, close the applet if there are pending
1768 * connections or if the server is full. Otherwise, add the
1769 * applet in the idle list.
1770 */
1771 if (global.nbthread > 1 &&
1772 (agent->b.be->nbpend ||
1773 (srv && (srv->nbpend || (srv->maxconn && srv->served >=srv_dynamic_maxconn(srv)))))) {
1774 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1775 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1776 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1777 goto next;
1778 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001779 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001780 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001781 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782 }
1783 return 1;
1784
Christopher Faulet8f82b202018-01-24 16:23:03 +01001785 next:
1786 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1787 return 0;
1788
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 exit:
1790 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1791 return 0;
1792}
1793
1794static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796{
1797 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001798 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 char *frame, *buf;
1800 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001801
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1803 goto exit;
1804
1805 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1806 goto exit;
1807
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1809 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001810 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1812 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815
1816 switch (ret) {
1817 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001818 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 goto exit;
1820
1821 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001822 goto stop;
1823
1824 default:
1825 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1826 " - disconnected by HAProxy (%d): %s\n",
1827 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 __FUNCTION__, appctx,
1829 SPOE_APPCTX(appctx)->status_code,
1830 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831
1832 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1833 goto next;
1834 }
1835
1836 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837 SPOE_APPCTX(appctx)->task->expire =
1838 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001839 return 0;
1840 stop:
1841 return 1;
1842 exit:
1843 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1844 return 0;
1845}
1846
1847static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849{
1850 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001851 char *frame;
1852 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853
Christopher Fauletb067b062017-01-04 16:39:11 +01001854 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001856 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001857 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858
Christopher Fauletb067b062017-01-04 16:39:11 +01001859 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001860 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001861 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001862 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001864 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001865 ret = spoe_recv_frame(appctx, frame,
1866 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001868 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001869 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870 }
1871
1872 switch (ret) {
1873 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1875 " - error on frame (%s)\n",
1876 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001877 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001879 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880 goto exit;
1881
1882 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 goto next;
1884
1885 case 1: /* retry */
1886 goto stop;
1887
1888 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001892 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1894 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto exit;
1896 }
1897
1898 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001900 if (trash.data)
1901 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001902
Christopher Fauleta1cda022016-12-21 08:58:06 +01001903 return 0;
1904 stop:
1905 return 1;
1906 exit:
1907 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1908 return 0;
1909}
1910
1911/* I/O Handler processing messages exchanged with the agent */
1912static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001913spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001914{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915 struct stream_interface *si = appctx->owner;
1916 struct spoe_agent *agent;
1917
1918 if (SPOE_APPCTX(appctx) == NULL)
1919 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920
Christopher Faulet8ef75252017-02-20 22:56:03 +01001921 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1922 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001923
Christopher Fauleta1cda022016-12-21 08:58:06 +01001924 switchstate:
1925 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1926 " - appctx-state=%s\n",
1927 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1928 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1929
1930 switch (appctx->st0) {
1931 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001932 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001933 goto out;
1934 goto switchstate;
1935
1936 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001937 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001938 goto out;
1939 goto switchstate;
1940
1941 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001942 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001943 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001944 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001945 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001946 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947 SPOE_APPCTX(appctx)->task->expire =
1948 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001949 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001950 goto switchstate;
1951 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001952 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1953 /* fall through */
1954
1955 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001956 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1957 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001958 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001959 goto out;
1960 goto switchstate;
1961
1962 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001963 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001964 goto out;
1965 goto switchstate;
1966
1967 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001968 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001969 goto out;
1970 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971
1972 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001973 appctx->st0 = SPOE_APPCTX_ST_END;
1974 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1975
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001976 si_shutw(si);
1977 si_shutr(si);
1978 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979 /* fall through */
1980
1981 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001982 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001983 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001984 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001985 if (stopping)
1986 spoe_wakeup_appctx(appctx);
1987
Christopher Faulet42bfa462017-01-04 14:14:19 +01001988 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1989 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990}
1991
1992struct applet spoe_applet = {
1993 .obj_type = OBJ_TYPE_APPLET,
1994 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001995 .fct = spoe_handle_appctx,
1996 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997};
1998
1999/* Create a SPOE applet. On success, the created applet is returned, else
2000 * NULL. */
2001static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002002spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003{
2004 struct appctx *appctx;
2005 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002007
Emeric Brun1138fd02017-06-19 12:38:55 +02002008 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009 goto out_error;
2010
Willy Tarreaubafbe012017-11-24 17:34:44 +01002011 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002012 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01002014 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002015
Christopher Faulet42bfa462017-01-04 14:14:19 +01002016 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002017 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002018 goto out_free_spoe_appctx;
2019
2020 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002021 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002022 SPOE_APPCTX(appctx)->task->context = appctx;
2023 SPOE_APPCTX(appctx)->agent = conf->agent;
2024 SPOE_APPCTX(appctx)->version = 0;
2025 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
2026 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01002027 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002028 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01002029 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01002030
2031 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
2032 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002033 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002034
2035 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2036 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002037
Willy Tarreau5820a362016-12-22 15:59:02 +01002038 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002039 if (!sess)
2040 goto out_free_spoe;
2041
Willy Tarreau87787ac2017-08-28 16:22:54 +02002042 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002043 goto out_free_sess;
2044
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045 stream_set_backend(strm, conf->agent->b.be);
2046
2047 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002048 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 appctx_wakeup(appctx);
2050
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051 strm->do_log = NULL;
2052 strm->res.flags |= CF_READ_DONTWAIT;
2053
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002054 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002055 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002056 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002057 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002058
Emeric Brunc60def82017-09-27 14:59:38 +02002059 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002060 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002061 return appctx;
2062
2063 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064 out_free_sess:
2065 session_free(sess);
2066 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002067 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002068 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002069 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002070 out_free_appctx:
2071 appctx_free(appctx);
2072 out_error:
2073 return NULL;
2074}
2075
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002076static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002077spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002078{
2079 struct spoe_config *conf = FLT_CONF(ctx->filter);
2080 struct spoe_agent *agent = conf->agent;
2081 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002082 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002083
Christopher Fauleta1cda022016-12-21 08:58:06 +01002084 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002085 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauletd6cd2b32020-06-22 15:32:14 +02002086 (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 +01002087 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002088
2089 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002090 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002091 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2092 ctx->strm);
2093
Christopher Fauleta1cda022016-12-21 08:58:06 +01002094 /* Do not try to create a new applet if there is no server up for the
2095 * agent's backend. */
2096 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2097 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2098 " - cannot create SPOE appctx: no server up\n",
2099 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2100 __FUNCTION__, ctx->strm);
2101 goto end;
2102 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002103
Christopher Fauleta1cda022016-12-21 08:58:06 +01002104 /* Do not try to create a new applet if we have reached the maximum of
2105 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002106 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002107 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002108 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2109 " - cannot create SPOE appctx: max CPS reached\n",
2110 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2111 __FUNCTION__, ctx->strm);
2112 goto end;
2113 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002114 }
2115
Christopher Faulet8ef75252017-02-20 22:56:03 +01002116 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002117 if (appctx == NULL) {
2118 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2119 " - failed to create SPOE appctx\n",
2120 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2121 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002122 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002123 "SPOE: [%s] failed to create SPOE applet\n",
2124 agent->id);
2125
Christopher Fauleta1cda022016-12-21 08:58:06 +01002126 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002127 }
2128
Christopher Fauleta1cda022016-12-21 08:58:06 +01002129 /* Increase the per-process number of cumulated connections */
2130 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002131 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002132
Christopher Fauleta1cda022016-12-21 08:58:06 +01002133 end:
2134 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002135 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002136 ctx->status_code = SPOE_CTX_ERR_RES;
2137 return -1;
2138 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002139
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002140 /* Add the SPOE context in the sending queue if the stream has no applet
2141 * already assigned and wakeup all idle applets. Otherwise, don't queue
2142 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002143 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002144 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2145 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002146 if (ctx->spoe_appctx)
2147 return 1;
2148 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002149
2150 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002151 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002152 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002153 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002154 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002155 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002156
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002157 /* Finally try to wakeup an IDLE applet. */
2158 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2159 struct eb32_node *node;
2160
2161 node = eb32_first(&agent->rt[tid].idle_applets);
2162 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2163 if (node && spoe_appctx) {
2164 eb32_delete(&spoe_appctx->node);
2165 spoe_appctx->node.key++;
2166 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2167 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002168 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002169 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002170 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002171}
2172
2173/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002174 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002175 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002176/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2177 * fragmented_content. If the next message can be processed, it returns 0. If
2178 * the message is too big, it returns -1.*/
2179static int
2180spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2181 struct spoe_message *msg, int dir,
2182 char **buf, char *end)
2183{
2184 struct sample *smp;
2185 struct spoe_arg *arg;
2186 int ret;
2187
2188 if (msg->cond) {
2189 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2190 ret = acl_pass(ret);
2191 if (msg->cond->pol == ACL_COND_UNLESS)
2192 ret = !ret;
2193
2194 /* the rule does not match */
2195 if (!ret)
2196 goto next;
2197 }
2198
2199 /* Resume encoding of a SPOE argument */
2200 if (ctx->frag_ctx.curarg != NULL) {
2201 arg = ctx->frag_ctx.curarg;
2202 goto encode_argument;
2203 }
2204
2205 if (ctx->frag_ctx.curoff != UINT_MAX)
2206 goto encode_msg_payload;
2207
2208 /* Check if there is enough space for the message name and the
2209 * number of arguments. It implies <msg->id_len> is encoded on 2
2210 * bytes, at most (< 2288). */
2211 if (*buf + 2 + msg->id_len + 1 > end)
2212 goto too_big;
2213
2214 /* Encode the message name */
2215 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2216 goto too_big;
2217
2218 /* Set the number of arguments for this message */
2219 **buf = msg->nargs;
2220 (*buf)++;
2221
2222 ctx->frag_ctx.curoff = 0;
2223 encode_msg_payload:
2224
2225 /* Loop on arguments */
2226 list_for_each_entry(arg, &msg->args, list) {
2227 ctx->frag_ctx.curarg = arg;
2228 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002229 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002230
2231 encode_argument:
2232 if (ctx->frag_ctx.curoff != UINT_MAX)
2233 goto encode_arg_value;
2234
Joseph Herlantf7f60312018-11-15 13:46:49 -08002235 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002236 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2237 goto too_big;
2238
2239 ctx->frag_ctx.curoff = 0;
2240 encode_arg_value:
2241
Joseph Herlantf7f60312018-11-15 13:46:49 -08002242 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002243 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002244 if (smp) {
2245 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2246 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2247 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002248 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002249 if (ret == -1 || ctx->frag_ctx.curoff)
2250 goto too_big;
2251 }
2252
2253 next:
2254 return 0;
2255
2256 too_big:
2257 return -1;
2258}
2259
Christopher Fauletc718b822017-09-21 16:50:56 +02002260/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2261 * handle fragmented content. On success it returns 1. If an error occurred, -1
2262 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2263 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002266 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002267{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002268 struct spoe_config *conf = FLT_CONF(ctx->filter);
2269 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002270 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002271 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002272
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002273 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002274 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002275
Christopher Fauletc718b822017-09-21 16:50:56 +02002276 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2277 /* Resume encoding of a SPOE message */
2278 if (ctx->frag_ctx.curmsg != NULL) {
2279 msg = ctx->frag_ctx.curmsg;
2280 goto encode_evt_message;
2281 }
2282
2283 list_for_each_entry(msg, messages, by_evt) {
2284 ctx->frag_ctx.curmsg = msg;
2285 ctx->frag_ctx.curarg = NULL;
2286 ctx->frag_ctx.curoff = UINT_MAX;
2287
2288 encode_evt_message:
2289 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2290 goto too_big;
2291 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002293 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2294 /* Resume encoding of a SPOE message */
2295 if (ctx->frag_ctx.curmsg != NULL) {
2296 msg = ctx->frag_ctx.curmsg;
2297 goto encode_grp_message;
2298 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002299
Christopher Fauletc718b822017-09-21 16:50:56 +02002300 list_for_each_entry(msg, messages, by_grp) {
2301 ctx->frag_ctx.curmsg = msg;
2302 ctx->frag_ctx.curarg = NULL;
2303 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002304
Christopher Fauletc718b822017-09-21 16:50:56 +02002305 encode_grp_message:
2306 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2307 goto too_big;
2308 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002309 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002310 else
2311 goto skip;
2312
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002313
Christopher Faulet57583e42017-09-04 15:41:09 +02002314 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002315 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002316 goto skip;
2317
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002318 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319 " - encode %s messages - spoe_appctx=%p"
2320 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002321 (int)now.tv_sec, (int)now.tv_usec,
2322 agent->id, __FUNCTION__, s,
2323 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002324 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002325 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002326
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002327 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002328 ctx->frag_ctx.curmsg = NULL;
2329 ctx->frag_ctx.curarg = NULL;
2330 ctx->frag_ctx.curoff = 0;
2331 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002332
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002333 return 1;
2334
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002335 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002336 /* Return an error if fragmentation is unsupported or if nothing has
2337 * been encoded because its too big and not splittable. */
2338 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002339 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2340 return -1;
2341 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002342
2343 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 " - encode fragmented messages - spoe_appctx=%p"
2345 " - curmsg=%p - curarg=%p - curoff=%u"
2346 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002347 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002348 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002349 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002350 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002351
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002352 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002353 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2354 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2355 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002356
2357 skip:
2358 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2359 " - skip the frame because nothing has been encoded\n",
2360 (int)now.tv_sec, (int)now.tv_usec,
2361 agent->id, __FUNCTION__, s);
2362 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002363}
2364
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002365
2366/***************************************************************************
2367 * Functions that handle SPOE actions
2368 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369/* Helper function to set a variable */
2370static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002371spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372 struct sample *smp)
2373{
2374 struct spoe_config *conf = FLT_CONF(ctx->filter);
2375 struct spoe_agent *agent = conf->agent;
2376 char varname[64];
2377
2378 memset(varname, 0, sizeof(varname));
2379 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2380 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002381 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2382 vars_set_by_name(varname, len, smp);
2383 else
2384 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002385}
2386
2387/* Helper function to unset a variable */
2388static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002389spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002390 struct sample *smp)
2391{
2392 struct spoe_config *conf = FLT_CONF(ctx->filter);
2393 struct spoe_agent *agent = conf->agent;
2394 char varname[64];
2395
2396 memset(varname, 0, sizeof(varname));
2397 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2398 scope, agent->var_pfx, len, name);
2399 vars_unset_by_name_ifexist(varname, len, smp);
2400}
2401
2402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403static inline int
2404spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2405 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002406{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002407 char *str, *scope, *p = *buf;
2408 struct sample smp;
2409 uint64_t sz;
2410 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412 if (p + 2 >= end)
2413 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
Christopher Faulet8ef75252017-02-20 22:56:03 +01002415 /* SET-VAR requires 3 arguments */
2416 if (*p++ != 3)
2417 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 switch (*p++) {
2420 case SPOE_SCOPE_PROC: scope = "proc"; break;
2421 case SPOE_SCOPE_SESS: scope = "sess"; break;
2422 case SPOE_SCOPE_TXN : scope = "txn"; break;
2423 case SPOE_SCOPE_REQ : scope = "req"; break;
2424 case SPOE_SCOPE_RES : scope = "res"; break;
2425 default: goto skip;
2426 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002427
Christopher Faulet8ef75252017-02-20 22:56:03 +01002428 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2429 goto skip;
2430 memset(&smp, 0, sizeof(smp));
2431 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002432
Christopher Faulet8ef75252017-02-20 22:56:03 +01002433 if (spoe_decode_data(&p, end, &smp) == -1)
2434 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002435
Christopher Faulet8ef75252017-02-20 22:56:03 +01002436 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2437 " - set-var '%s.%s.%.*s'\n",
2438 (int)now.tv_sec, (int)now.tv_usec,
2439 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2440 __FUNCTION__, s, scope,
2441 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2442 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443
Christopher Fauletb135abc2020-10-15 16:08:30 +02002444 if (smp.data.type == SMP_T_ANY)
2445 spoe_unset_var(ctx, scope, str, sz, &smp);
2446 else
2447 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002448
Christopher Faulet8ef75252017-02-20 22:56:03 +01002449 ret = (p - *buf);
2450 *buf = p;
2451 return ret;
2452 skip:
2453 return 0;
2454}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002455
Christopher Faulet8ef75252017-02-20 22:56:03 +01002456static inline int
2457spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2458 char **buf, char *end, int dir)
2459{
2460 char *str, *scope, *p = *buf;
2461 struct sample smp;
2462 uint64_t sz;
2463 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002464
Christopher Faulet8ef75252017-02-20 22:56:03 +01002465 if (p + 2 >= end)
2466 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002467
Christopher Faulet8ef75252017-02-20 22:56:03 +01002468 /* UNSET-VAR requires 2 arguments */
2469 if (*p++ != 2)
2470 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002471
Christopher Faulet8ef75252017-02-20 22:56:03 +01002472 switch (*p++) {
2473 case SPOE_SCOPE_PROC: scope = "proc"; break;
2474 case SPOE_SCOPE_SESS: scope = "sess"; break;
2475 case SPOE_SCOPE_TXN : scope = "txn"; break;
2476 case SPOE_SCOPE_REQ : scope = "req"; break;
2477 case SPOE_SCOPE_RES : scope = "res"; break;
2478 default: goto skip;
2479 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002480
Christopher Faulet8ef75252017-02-20 22:56:03 +01002481 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2482 goto skip;
2483 memset(&smp, 0, sizeof(smp));
2484 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002485
Christopher Faulet8ef75252017-02-20 22:56:03 +01002486 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2487 " - unset-var '%s.%s.%.*s'\n",
2488 (int)now.tv_sec, (int)now.tv_usec,
2489 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2490 __FUNCTION__, s, scope,
2491 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2492 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002493
Christopher Faulet8ef75252017-02-20 22:56:03 +01002494 spoe_unset_var(ctx, scope, str, sz, &smp);
2495
2496 ret = (p - *buf);
2497 *buf = p;
2498 return ret;
2499 skip:
2500 return 0;
2501}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002502
Christopher Faulet8ef75252017-02-20 22:56:03 +01002503/* Process SPOE actions for a specific event. It returns 1 on success. If an
2504 * error occurred, 0 is returned. */
2505static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002506spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002507{
2508 char *p, *end;
2509 int ret;
2510
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002511 p = b_head(&ctx->buffer);
2512 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002513
2514 while (p < end) {
2515 enum spoe_action_type type;
2516
2517 type = *p++;
2518 switch (type) {
2519 case SPOE_ACT_T_SET_VAR:
2520 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2521 if (!ret)
2522 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002523 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002524
Christopher Faulet8ef75252017-02-20 22:56:03 +01002525 case SPOE_ACT_T_UNSET_VAR:
2526 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2527 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002528 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002529 break;
2530
2531 default:
2532 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002533 }
2534 }
2535
2536 return 1;
2537 skip:
2538 return 0;
2539}
2540
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002541/***************************************************************************
2542 * Functions that process SPOE events
2543 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002544static void
2545spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2546 struct spoe_context *ctx, int dir)
2547{
2548 if (!tv_iszero(&ctx->stats.tv_start)) {
2549 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2550 ctx->stats.t_total += ctx->stats.t_process;
2551 tv_zero(&ctx->stats.tv_request);
2552 tv_zero(&ctx->stats.tv_queue);
2553 tv_zero(&ctx->stats.tv_wait);
2554 tv_zero(&ctx->stats.tv_response);
2555 }
2556
2557 if (agent->var_t_process) {
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->stats.t_process;
2563 smp.data.type = SMP_T_SINT;
2564
2565 spoe_set_var(ctx, "txn", agent->var_t_process,
2566 strlen(agent->var_t_process), &smp);
2567 }
2568
2569 if (agent->var_t_total) {
2570 struct sample smp;
2571
2572 memset(&smp, 0, sizeof(smp));
2573 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2574 smp.data.u.sint = ctx->stats.t_total;
2575 smp.data.type = SMP_T_SINT;
2576
2577 spoe_set_var(ctx, "txn", agent->var_t_total,
2578 strlen(agent->var_t_total), &smp);
2579 }
2580}
2581
2582static void
2583spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2584 struct spoe_context *ctx, int dir)
2585{
2586 if (agent->eps_max > 0)
2587 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2588
2589 if (agent->var_on_error) {
2590 struct sample smp;
2591
2592 memset(&smp, 0, sizeof(smp));
2593 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2594 smp.data.u.sint = ctx->status_code;
2595 smp.data.type = SMP_T_BOOL;
2596
2597 spoe_set_var(ctx, "txn", agent->var_on_error,
2598 strlen(agent->var_on_error), &smp);
2599 }
2600
2601 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2602 ? SPOE_CTX_ST_READY
2603 : SPOE_CTX_ST_NONE);
2604}
2605
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002606static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002607spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002608{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002609 /* If a process is already started for this SPOE context, retry
2610 * later. */
2611 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002612 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002613
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002614 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002615 ctx->stats.tv_start = now;
2616 ctx->stats.tv_request = now;
2617 ctx->stats.t_request = -1;
2618 ctx->stats.t_queue = -1;
2619 ctx->stats.t_waiting = -1;
2620 ctx->stats.t_response = -1;
2621 ctx->stats.t_process = -1;
2622
2623 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002624
Christopher Fauleta1cda022016-12-21 08:58:06 +01002625 /* Set the right flag to prevent request and response processing
2626 * in same time. */
2627 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2628 ? SPOE_CTX_FL_REQ_PROCESS
2629 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002630 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002631}
2632
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002633static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002634spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002636 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002637
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002638 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2639 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002640 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002641 if (sa) {
2642 if (sa->frag_ctx.ctx == ctx) {
2643 sa->frag_ctx.ctx = NULL;
2644 spoe_wakeup_appctx(sa->owner);
2645 }
2646 else
2647 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002648 }
2649
Christopher Fauleta1cda022016-12-21 08:58:06 +01002650 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002651 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002652 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002653
2654 /* Reset processing timer */
2655 ctx->process_exp = TICK_ETERNITY;
2656
Christopher Faulet8ef75252017-02-20 22:56:03 +01002657 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002658
Christopher Fauletfce747b2018-01-24 15:59:32 +01002659 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002660 ctx->frag_ctx.curmsg = NULL;
2661 ctx->frag_ctx.curarg = NULL;
2662 ctx->frag_ctx.curoff = 0;
2663 ctx->frag_ctx.flags = 0;
2664
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002666 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002667 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002668 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002669 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002670
Christopher Fauleta1cda022016-12-21 08:58:06 +01002671 LIST_DEL(&ctx->list);
2672 LIST_INIT(&ctx->list);
2673 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002674}
2675
Christopher Faulet58d03682017-09-21 16:57:24 +02002676/* Process a list of SPOE messages. First, this functions will process messages
2677 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2678 * to process corresponding actions. During all the processing, it returns 0
2679 * and it returns 1 when the processing is finished. If an error occurred, -1
2680 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002681static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002682spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2683 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002685 struct spoe_config *conf = FLT_CONF(ctx->filter);
2686 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002687 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688
2689 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002690 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002691
2692 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2693 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002694 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002695 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002696 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002697 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002698 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002699 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700
2701 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002703 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002704 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002705 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002706 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002707 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002708 goto skip;
2709 }
2710 }
2711
Christopher Fauletf7a30922016-11-10 15:04:51 +01002712 if (!tick_isset(ctx->process_exp)) {
2713 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2714 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2715 ctx->process_exp);
2716 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002717 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002718 if (!ret)
2719 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002720
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002721 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002722 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002723 }
2724
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002725 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002726 if (tv_iszero(&ctx->stats.tv_request))
2727 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002728 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002729 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002730 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002731 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002732 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002733 if (!ret)
2734 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002735 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002736 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002737 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2738 }
2739
2740 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002741 if (ctx->spoe_appctx)
2742 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002743 ret = 0;
2744 goto out;
2745 }
2746
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002747 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2748 ret = 0;
2749 goto out;
2750 }
2751
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002752 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002753 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002754 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002755 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002756 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002757 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002758 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002759 }
2760
2761 out:
2762 return ret;
2763
Christopher Fauleta1cda022016-12-21 08:58:06 +01002764 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002765 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002766 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002767 spoe_stop_processing(agent, ctx);
2768 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002769
Christopher Fauleta1cda022016-12-21 08:58:06 +01002770 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002771 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002772 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002773 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002774 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002775 spoe_handle_processing_error(s, agent, ctx, dir);
2776 ret = 1;
2777 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002778 return ret;
2779}
2780
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002781/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2782 * See spoe_process_message for details. */
2783static int
2784spoe_process_group(struct stream *s, struct spoe_context *ctx,
2785 struct spoe_group *group, int dir)
2786{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002787 struct spoe_config *conf = FLT_CONF(ctx->filter);
2788 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002789 int ret;
2790
2791 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2792 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002793 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002794 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2795 group->id);
2796
2797 if (LIST_ISEMPTY(&group->messages))
2798 return 1;
2799
2800 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002801 if (ret && ctx->stats.t_process != -1) {
2802 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002803 " - <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 +01002804 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002805 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002806 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002807 ctx->stats.t_response, ctx->stats.t_process,
2808 agent->counters.idles, agent->counters.applets,
2809 agent->counters.nb_sending, agent->counters.nb_waiting,
2810 agent->counters.nb_errors, agent->counters.nb_processed,
2811 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2812 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2813 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2814 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2815 agent->id, group->id, s->uniq_id, ctx->status_code,
2816 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2817 ctx->stats.t_response, ctx->stats.t_process,
2818 agent->counters.idles, agent->counters.applets,
2819 agent->counters.nb_sending, agent->counters.nb_waiting,
2820 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002821 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002822 return ret;
2823}
2824
Christopher Faulet58d03682017-09-21 16:57:24 +02002825/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2826 * See spoe_process_message for details. */
2827static int
2828spoe_process_event(struct stream *s, struct spoe_context *ctx,
2829 enum spoe_event ev)
2830{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002831 struct spoe_config *conf = FLT_CONF(ctx->filter);
2832 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002833 int dir, ret;
2834
2835 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002836 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002837 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002838 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2839 spoe_event_str[ev]);
2840
2841 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2842
2843 if (LIST_ISEMPTY(&(ctx->events[ev])))
2844 return 1;
2845
2846 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002847 if (ret && ctx->stats.t_process != -1) {
2848 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002849 " - <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 +01002850 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2851 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2852 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002853 ctx->stats.t_response, ctx->stats.t_process,
2854 agent->counters.idles, agent->counters.applets,
2855 agent->counters.nb_sending, agent->counters.nb_waiting,
2856 agent->counters.nb_errors, agent->counters.nb_processed,
2857 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2858 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2859 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2860 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2861 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2862 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2863 ctx->stats.t_response, ctx->stats.t_process,
2864 agent->counters.idles, agent->counters.applets,
2865 agent->counters.nb_sending, agent->counters.nb_waiting,
2866 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002867 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002868 return ret;
2869}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002870
2871/***************************************************************************
2872 * Functions that create/destroy SPOE contexts
2873 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002874static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002875spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002876{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002877 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002878 return 1;
2879
Christopher Faulet4596fb72017-01-11 14:05:19 +01002880 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002881 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002882 LIST_DEL(&buffer_wait->list);
2883 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002884 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002885 }
2886
Christopher Faulet4596fb72017-01-11 14:05:19 +01002887 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002888 return 1;
2889
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002890 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002891 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002892 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002893 return 0;
2894}
2895
2896static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002897spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002898{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002899 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002900 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002901 LIST_DEL(&buffer_wait->list);
2902 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002903 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002904 }
2905
2906 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002907 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002908 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002909 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002910 }
2911}
2912
Christopher Faulet4596fb72017-01-11 14:05:19 +01002913static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002914spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002915{
2916 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2917 return 1;
2918}
2919
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002920static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002921spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002922{
2923 struct spoe_config *conf = FLT_CONF(filter);
2924 struct spoe_context *ctx;
2925
Willy Tarreaubafbe012017-11-24 17:34:44 +01002926 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002927 if (ctx == NULL) {
2928 return NULL;
2929 }
2930 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002931 ctx->filter = filter;
2932 ctx->state = SPOE_CTX_ST_NONE;
2933 ctx->status_code = SPOE_CTX_ERR_NONE;
2934 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002935 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002936 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002937 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002938 LIST_INIT(&ctx->buffer_wait.list);
2939 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002941 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942
Christopher Fauletf7a30922016-11-10 15:04:51 +01002943 ctx->stream_id = 0;
2944 ctx->frame_id = 1;
2945 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002947 tv_zero(&ctx->stats.tv_start);
2948 tv_zero(&ctx->stats.tv_request);
2949 tv_zero(&ctx->stats.tv_queue);
2950 tv_zero(&ctx->stats.tv_wait);
2951 tv_zero(&ctx->stats.tv_response);
2952 ctx->stats.t_request = -1;
2953 ctx->stats.t_queue = -1;
2954 ctx->stats.t_waiting = -1;
2955 ctx->stats.t_response = -1;
2956 ctx->stats.t_process = -1;
2957 ctx->stats.t_total = 0;
2958
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002959 ctx->strm = s;
2960 ctx->state = SPOE_CTX_ST_READY;
2961 filter->ctx = ctx;
2962
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963 return ctx;
2964}
2965
2966static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002967spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002968{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002969 struct spoe_config *conf = FLT_CONF(filter);
2970 struct spoe_context *ctx = filter->ctx;
2971
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002972 if (!ctx)
2973 return;
2974
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002975 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002976 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002977 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978}
2979
2980static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002981spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982{
2983 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002984 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002985
2986 tv_zero(&ctx->stats.tv_start);
2987 tv_zero(&ctx->stats.tv_request);
2988 tv_zero(&ctx->stats.tv_queue);
2989 tv_zero(&ctx->stats.tv_wait);
2990 tv_zero(&ctx->stats.tv_response);
2991 ctx->stats.t_request = -1;
2992 ctx->stats.t_queue = -1;
2993 ctx->stats.t_waiting = -1;
2994 ctx->stats.t_response = -1;
2995 ctx->stats.t_process = -1;
2996 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997}
2998
2999
3000/***************************************************************************
3001 * Hooks that manage the filter lifecycle (init/check/deinit)
3002 **************************************************************************/
3003/* Signal handler: Do a soft stop, wakeup SPOE applet */
3004static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01003005spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003006{
3007 struct proxy *p;
3008
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003009 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 while (p) {
3011 struct flt_conf *fconf;
3012
3013 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01003014 struct spoe_config *conf;
3015 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01003016 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02003017 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018
Christopher Faulet3b386a32017-02-23 10:17:15 +01003019 if (fconf->id != spoe_filter_id)
3020 continue;
3021
3022 conf = fconf->conf;
3023 agent = conf->agent;
3024
Christopher Faulet24289f22017-09-25 14:48:02 +02003025 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003026 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003027 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3028 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003029 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003030 }
3031 }
3032 p = p->next;
3033 }
3034}
3035
3036
3037/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3038static int
3039spoe_init(struct proxy *px, struct flt_conf *fconf)
3040{
3041 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003043 /* conf->agent_fe was already initialized during the config
3044 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046 conf->agent_fe.cap = PR_CAP_FE;
3047 conf->agent_fe.mode = PR_MODE_TCP;
3048 conf->agent_fe.maxconn = 0;
3049 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3050 conf->agent_fe.conn_retries = CONN_RETRIES;
3051 conf->agent_fe.accept = frontend_accept;
3052 conf->agent_fe.srv = NULL;
3053 conf->agent_fe.timeout.client = TICK_ETERNITY;
3054 conf->agent_fe.default_target = &spoe_applet.obj_type;
3055 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3056
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003058 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059 sighandler_registered = 1;
3060 }
3061
Christopher Faulet00292352019-01-09 15:05:10 +01003062 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003063 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003064}
3065
3066/* Free ressources allocated by the SPOE filter. */
3067static void
3068spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3069{
3070 struct spoe_config *conf = fconf->conf;
3071
3072 if (conf) {
3073 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003074
Christopher Faulet8ef75252017-02-20 22:56:03 +01003075 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003076 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003077 free(conf);
3078 }
3079 fconf->conf = NULL;
3080}
3081
3082/* Check configuration of a SPOE filter for a specified proxy.
3083 * Return 1 on error, else 0. */
3084static int
3085spoe_check(struct proxy *px, struct flt_conf *fconf)
3086{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003087 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003088 struct spoe_config *conf = fconf->conf;
3089 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003090 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091
Christopher Faulet7ee86672017-09-19 11:08:28 +02003092 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3093 * are uniq */
3094 list_for_each_entry(f, &px->filter_configs, list) {
3095 struct spoe_config *c = f->conf;
3096
3097 /* This is not an SPOE filter */
3098 if (f->id != spoe_filter_id)
3099 continue;
3100 /* This is the current SPOE filter */
3101 if (f == fconf)
3102 continue;
3103
3104 /* Check engine Id. It should be uniq */
3105 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003106 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3107 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003108 return 1;
3109 }
3110 }
3111
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003112 target = proxy_be_by_name(conf->agent->b.name);
3113 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003114 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3115 " declared at %s:%d.\n",
3116 px->id, conf->agent->b.name, conf->agent->id,
3117 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003118 return 1;
3119 }
3120 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003121 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3122 " at %s:%d does not support HTTP mode.\n",
3123 px->id, target->id, conf->agent->id,
3124 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003125 return 1;
3126 }
3127
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003128 if (px->bind_proc & ~target->bind_proc) {
3129 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3130 " at %s:%d does not cover all of its processes.\n",
3131 px->id, target->id, conf->agent->id,
3132 conf->agent->conf.file, conf->agent->conf.line);
3133 return 1;
3134 }
3135
Christopher Fauletfe261552019-03-18 13:57:42 +01003136 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003137 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3138 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3139 return 1;
3140 }
3141 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet46c76822019-09-17 11:55:52 +02003142 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003143 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3144 conf->agent->rt[i].processing = 0;
3145 LIST_INIT(&conf->agent->rt[i].applets);
3146 LIST_INIT(&conf->agent->rt[i].sending_queue);
3147 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3148 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003149 }
3150
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003151 free(conf->agent->b.name);
3152 conf->agent->b.name = NULL;
3153 conf->agent->b.be = target;
3154 return 0;
3155}
3156
Kevin Zhu652c8e22019-09-17 15:05:45 +02003157/* Initializes the SPOE filter for a proxy for a specific thread.
3158 * Returns a negative value if an error occurs. */
3159static int
3160spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3161{
3162 struct spoe_config *conf = fconf->conf;
3163 struct spoe_agent *agent = conf->agent;
3164
Christopher Faulet46c76822019-09-17 11:55:52 +02003165 agent->rt[tid].engine_id = generate_pseudo_uuid();
3166 if (agent->rt[tid].engine_id == NULL)
3167 return -1;
Kevin Zhu652c8e22019-09-17 15:05:45 +02003168 return 0;
3169}
3170
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003171/**************************************************************************
3172 * Hooks attached to a stream
3173 *************************************************************************/
3174/* Called when a filter instance is created and attach to a stream. It creates
3175 * the context that will be used to process this stream. */
3176static int
3177spoe_start(struct stream *s, struct filter *filter)
3178{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003179 struct spoe_config *conf = FLT_CONF(filter);
3180 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003181 struct spoe_context *ctx;
3182
3183 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003184 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 __FUNCTION__, s);
3186
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003187 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003188 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3189 " - failed to create SPOE context\n",
3190 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003191 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003192 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003193 "SPOE: [%s] failed to create SPOE context\n",
3194 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 return 0;
3196 }
3197
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003198 return 1;
3199}
3200
3201/* Called when a filter instance is detached from a stream. It release the
3202 * attached SPOE context. */
3203static void
3204spoe_stop(struct stream *s, struct filter *filter)
3205{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003206 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3207 (int)now.tv_sec, (int)now.tv_usec,
3208 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3209 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003210 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211}
3212
Christopher Fauletf7a30922016-11-10 15:04:51 +01003213
3214/*
3215 * Called when the stream is woken up because of expired timer.
3216 */
3217static void
3218spoe_check_timeouts(struct stream *s, struct filter *filter)
3219{
3220 struct spoe_context *ctx = filter->ctx;
3221
Christopher Fauletac580602018-03-20 16:09:20 +01003222 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003223 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003224}
3225
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003226/* Called when we are ready to filter data on a channel */
3227static int
3228spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3229{
3230 struct spoe_context *ctx = filter->ctx;
3231 int ret = 1;
3232
3233 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3234 " - ctx-flags=0x%08x\n",
3235 (int)now.tv_sec, (int)now.tv_usec,
3236 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3237 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3238
Christopher Fauletb067b062017-01-04 16:39:11 +01003239 if (ctx->state == SPOE_CTX_ST_NONE)
3240 goto out;
3241
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003242 if (!(chn->flags & CF_ISRESP)) {
Christopher Fauletbebe2552021-06-25 19:11:49 +02003243 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
3244 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3245
3246 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
3247 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3248
3249 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
3250 filter->pre_analyzers |= AN_RES_INSPECT;
3251
3252 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
3253 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3254
3255 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
3256 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3257
3258 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
3259 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3260
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003261 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3262 chn->analysers |= AN_REQ_INSPECT_FE;
3263 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3264 chn->analysers |= AN_REQ_INSPECT_BE;
3265
3266 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3267 goto out;
3268
3269 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003270 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003271 if (!ret)
3272 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3274 }
3275 else {
Miroslav Zagoracd995d5f2020-06-19 22:17:17 +02003276 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003277 chn->analysers |= AN_RES_INSPECT;
3278
3279 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3280 goto out;
3281
Christopher Faulet8ef75252017-02-20 22:56:03 +01003282 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003283 if (!ret) {
3284 channel_dont_read(chn);
3285 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003286 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003287 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003288 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 }
3290
3291 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 return ret;
3293}
3294
3295/* Called before a processing happens on a given channel */
3296static int
3297spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3298 struct channel *chn, unsigned an_bit)
3299{
3300 struct spoe_context *ctx = filter->ctx;
3301 int ret = 1;
3302
3303 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3304 " - ctx-flags=0x%08x - ana=0x%08x\n",
3305 (int)now.tv_sec, (int)now.tv_usec,
3306 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3307 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3308 ctx->flags, an_bit);
3309
Christopher Fauletb067b062017-01-04 16:39:11 +01003310 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 goto out;
3312
3313 switch (an_bit) {
3314 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003315 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003316 break;
3317 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003318 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003319 break;
3320 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003321 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 break;
3323 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003324 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 break;
3326 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003327 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003328 break;
3329 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003330 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331 break;
3332 }
3333
3334 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003335 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003336 channel_dont_read(chn);
3337 channel_dont_close(chn);
3338 }
3339 return ret;
3340}
3341
3342/* Called when the filtering on the channel ends. */
3343static int
3344spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3345{
3346 struct spoe_context *ctx = filter->ctx;
3347
3348 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3349 " - ctx-flags=0x%08x\n",
3350 (int)now.tv_sec, (int)now.tv_usec,
3351 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3352 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3353
3354 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003355 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003356 }
3357
3358 return 1;
3359}
3360
3361/********************************************************************
3362 * Functions that manage the filter initialization
3363 ********************************************************************/
3364struct flt_ops spoe_ops = {
3365 /* Manage SPOE filter, called for each filter declaration */
3366 .init = spoe_init,
3367 .deinit = spoe_deinit,
3368 .check = spoe_check,
Kevin Zhu652c8e22019-09-17 15:05:45 +02003369 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003370
3371 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003372 .attach = spoe_start,
3373 .detach = spoe_stop,
3374 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003375
3376 /* Handle channels activity */
3377 .channel_start_analyze = spoe_start_analyze,
3378 .channel_pre_analyze = spoe_chn_pre_analyze,
3379 .channel_end_analyze = spoe_end_analyze,
3380};
3381
3382
3383static int
3384cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3385{
3386 const char *err;
3387 int i, err_code = 0;
3388
3389 if ((cfg_scope == NULL && curengine != NULL) ||
3390 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003391 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003392 goto out;
3393
3394 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3395 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3397 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 err_code |= ERR_ALERT | ERR_ABORT;
3399 goto out;
3400 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003401 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3402 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003403 goto out;
3404 }
3405
3406 err = invalid_char(args[1]);
3407 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003408 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3409 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 err_code |= ERR_ALERT | ERR_ABORT;
3411 goto out;
3412 }
3413
3414 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3416 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 err_code |= ERR_ALERT | ERR_ABORT;
3418 goto out;
3419 }
3420 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003421 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 err_code |= ERR_ALERT | ERR_ABORT;
3423 goto out;
3424 }
3425
3426 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003427
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003428 curagent->conf.file = strdup(file);
3429 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003430
3431 curagent->timeout.hello = TICK_ETERNITY;
3432 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003433 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003434
Christopher Fauleta1cda022016-12-21 08:58:06 +01003435 curagent->var_pfx = NULL;
3436 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003437 curagent->var_t_process = NULL;
3438 curagent->var_t_total = NULL;
Christopher Faulet46c76822019-09-17 11:55:52 +02003439 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003440 curagent->cps_max = 0;
3441 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003442 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003443 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444
3445 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003446 LIST_INIT(&curagent->events[i]);
3447 LIST_INIT(&curagent->groups);
3448 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449 }
3450 else if (!strcmp(args[0], "use-backend")) {
3451 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3453 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 err_code |= ERR_ALERT | ERR_FATAL;
3455 goto out;
3456 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003457 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003458 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 free(curagent->b.name);
3460 curagent->b.name = strdup(args[1]);
3461 }
3462 else if (!strcmp(args[0], "messages")) {
3463 int cur_arg = 1;
3464 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003465 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003466
Christopher Faulet11610f32017-09-21 10:23:10 +02003467 list_for_each_entry(ph, &curmphs, list) {
3468 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003469 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3470 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003471 err_code |= ERR_ALERT | ERR_FATAL;
3472 goto out;
3473 }
3474 }
3475
Christopher Faulet11610f32017-09-21 10:23:10 +02003476 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 err_code |= ERR_ALERT | ERR_ABORT;
3479 goto out;
3480 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003481 ph->id = strdup(args[cur_arg]);
3482 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003483 cur_arg++;
3484 }
3485 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003486 else if (!strcmp(args[0], "groups")) {
3487 int cur_arg = 1;
3488 while (*args[cur_arg]) {
3489 struct spoe_placeholder *ph = NULL;
3490
3491 list_for_each_entry(ph, &curgphs, list) {
3492 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003493 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3494 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003495 err_code |= ERR_ALERT | ERR_FATAL;
3496 goto out;
3497 }
3498 }
3499
3500 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003501 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003502 err_code |= ERR_ALERT | ERR_ABORT;
3503 goto out;
3504 }
3505 ph->id = strdup(args[cur_arg]);
3506 LIST_ADDQ(&curgphs, &ph->list);
3507 cur_arg++;
3508 }
3509 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003510 else if (!strcmp(args[0], "timeout")) {
3511 unsigned int *tv = NULL;
3512 const char *res;
3513 unsigned timeout;
3514
3515 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3517 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003518 err_code |= ERR_ALERT | ERR_FATAL;
3519 goto out;
3520 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003521 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3522 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003523 if (!strcmp(args[1], "hello"))
3524 tv = &curagent->timeout.hello;
3525 else if (!strcmp(args[1], "idle"))
3526 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003527 else if (!strcmp(args[1], "processing"))
3528 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003529 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003530 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3531 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003532 err_code |= ERR_ALERT | ERR_FATAL;
3533 goto out;
3534 }
3535 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003536 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3537 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003538 err_code |= ERR_ALERT | ERR_FATAL;
3539 goto out;
3540 }
3541 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003542 if (res == PARSE_TIME_OVER) {
3543 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3544 file, linenum, args[2], args[0], args[1]);
3545 err_code |= ERR_ALERT | ERR_FATAL;
3546 goto out;
3547 }
3548 else if (res == PARSE_TIME_UNDER) {
3549 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3550 file, linenum, args[2], args[0], args[1]);
3551 err_code |= ERR_ALERT | ERR_FATAL;
3552 goto out;
3553 }
3554 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003555 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3556 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003557 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003558 goto out;
3559 }
3560 *tv = MS_TO_TICKS(timeout);
3561 }
3562 else if (!strcmp(args[0], "option")) {
3563 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003564 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3565 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003566 err_code |= ERR_ALERT | ERR_FATAL;
3567 goto out;
3568 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003569
Christopher Faulet305c6072017-02-23 16:17:53 +01003570 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003571 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003572 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003573 if (kwm == 1)
3574 curagent->flags &= ~SPOE_FL_PIPELINING;
3575 else
3576 curagent->flags |= SPOE_FL_PIPELINING;
3577 goto out;
3578 }
3579 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003580 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003581 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003582 if (kwm == 1)
3583 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet46c76822019-09-17 11:55:52 +02003584 else
3585 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003586 goto out;
3587 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003588 else if (!strcmp(args[1], "send-frag-payload")) {
3589 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3590 goto out;
3591 if (kwm == 1)
3592 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3593 else
3594 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3595 goto out;
3596 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003597 else if (!strcmp(args[1], "dontlog-normal")) {
3598 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3599 goto out;
3600 if (kwm == 1)
3601 curpxopts2 &= ~PR_O2_NOLOGNORM;
3602 else
3603 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003604 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003605 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003606
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003607 /* Following options does not support negation */
3608 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003609 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3610 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003611 err_code |= ERR_ALERT | ERR_FATAL;
3612 goto out;
3613 }
3614
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003615 if (!strcmp(args[1], "var-prefix")) {
3616 char *tmp;
3617
3618 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003619 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3620 file, linenum, args[0],
3621 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003622 err_code |= ERR_ALERT | ERR_FATAL;
3623 goto out;
3624 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003625 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3626 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003627 tmp = args[2];
3628 while (*tmp) {
3629 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003630 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003631 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003632 err_code |= ERR_ALERT | ERR_FATAL;
3633 goto out;
3634 }
3635 tmp++;
3636 }
3637 curagent->var_pfx = strdup(args[2]);
3638 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003639 else if (!strcmp(args[1], "force-set-var")) {
3640 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3641 goto out;
3642 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3643 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003644 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003645 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003646 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003647 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3648 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003649 else if (!strcmp(args[1], "set-on-error")) {
3650 char *tmp;
3651
3652 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003653 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3654 file, linenum, args[0],
3655 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003656 err_code |= ERR_ALERT | ERR_FATAL;
3657 goto out;
3658 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003659 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3660 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003661 tmp = args[2];
3662 while (*tmp) {
3663 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003664 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003665 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003666 err_code |= ERR_ALERT | ERR_FATAL;
3667 goto out;
3668 }
3669 tmp++;
3670 }
3671 curagent->var_on_error = strdup(args[2]);
3672 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003673 else if (!strcmp(args[1], "set-process-time")) {
3674 char *tmp;
3675
3676 if (!*args[2]) {
3677 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3678 file, linenum, args[0],
3679 args[1]);
3680 err_code |= ERR_ALERT | ERR_FATAL;
3681 goto out;
3682 }
3683 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3684 goto out;
3685 tmp = args[2];
3686 while (*tmp) {
3687 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003688 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003689 file, linenum, args[0], args[1]);
3690 err_code |= ERR_ALERT | ERR_FATAL;
3691 goto out;
3692 }
3693 tmp++;
3694 }
3695 curagent->var_t_process = strdup(args[2]);
3696 }
3697 else if (!strcmp(args[1], "set-total-time")) {
3698 char *tmp;
3699
3700 if (!*args[2]) {
3701 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3702 file, linenum, args[0],
3703 args[1]);
3704 err_code |= ERR_ALERT | ERR_FATAL;
3705 goto out;
3706 }
3707 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3708 goto out;
3709 tmp = args[2];
3710 while (*tmp) {
3711 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003712 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003713 file, linenum, args[0], args[1]);
3714 err_code |= ERR_ALERT | ERR_FATAL;
3715 goto out;
3716 }
3717 tmp++;
3718 }
3719 curagent->var_t_total = strdup(args[2]);
3720 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003721 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003722 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3723 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003724 err_code |= ERR_ALERT | ERR_FATAL;
3725 goto out;
3726 }
Christopher Faulet48026722016-11-16 15:01:12 +01003727 }
3728 else if (!strcmp(args[0], "maxconnrate")) {
3729 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003730 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3731 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003735 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003736 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003737 curagent->cps_max = atol(args[1]);
3738 }
3739 else if (!strcmp(args[0], "maxerrrate")) {
3740 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003741 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3742 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003743 err_code |= ERR_ALERT | ERR_FATAL;
3744 goto out;
3745 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003746 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003747 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003748 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003749 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003750 else if (!strcmp(args[0], "max-frame-size")) {
3751 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003752 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3753 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003754 err_code |= ERR_ALERT | ERR_FATAL;
3755 goto out;
3756 }
3757 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3758 goto out;
3759 curagent->max_frame_size = atol(args[1]);
3760 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3761 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003762 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3763 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003764 err_code |= ERR_ALERT | ERR_FATAL;
3765 goto out;
3766 }
3767 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003768 else if (!strcmp(args[0], "max-waiting-frames")) {
3769 if (!*args[1]) {
3770 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3771 file, linenum, args[0]);
3772 err_code |= ERR_ALERT | ERR_FATAL;
3773 goto out;
3774 }
3775 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3776 goto out;
3777 curagent->max_fpa = atol(args[1]);
3778 if (curagent->max_fpa < 1) {
3779 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3780 file, linenum, args[0]);
3781 err_code |= ERR_ALERT | ERR_FATAL;
3782 goto out;
3783 }
3784 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003785 else if (!strcmp(args[0], "register-var-names")) {
3786 int cur_arg;
3787
3788 if (!*args[1]) {
3789 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3790 file, linenum, args[0]);
3791 err_code |= ERR_ALERT | ERR_FATAL;
3792 goto out;
3793 }
3794 cur_arg = 1;
3795 while (*args[cur_arg]) {
3796 struct spoe_var_placeholder *vph;
3797
3798 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3799 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3800 err_code |= ERR_ALERT | ERR_ABORT;
3801 goto out;
3802 }
3803 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusf818ea52019-06-23 22:10:13 +02003804 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003805 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3806 err_code |= ERR_ALERT | ERR_ABORT;
3807 goto out;
3808 }
3809 LIST_ADDQ(&curvars, &vph->list);
3810 cur_arg++;
3811 }
3812 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003813 else if (!strcmp(args[0], "log")) {
3814 char *errmsg = NULL;
3815
3816 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3817 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3818 err_code |= ERR_ALERT | ERR_FATAL;
3819 goto out;
3820 }
3821 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003822 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003823 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3824 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003825 err_code |= ERR_ALERT | ERR_FATAL;
3826 goto out;
3827 }
3828 out:
3829 return err_code;
3830}
Christopher Faulet11610f32017-09-21 10:23:10 +02003831static int
3832cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3833{
3834 struct spoe_group *grp;
3835 const char *err;
3836 int err_code = 0;
3837
3838 if ((cfg_scope == NULL && curengine != NULL) ||
3839 (cfg_scope != NULL && curengine == NULL) ||
3840 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3841 goto out;
3842
3843 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3844 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003845 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3846 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003847 err_code |= ERR_ALERT | ERR_ABORT;
3848 goto out;
3849 }
3850 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3851 err_code |= ERR_ABORT;
3852 goto out;
3853 }
3854
3855 err = invalid_char(args[1]);
3856 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003857 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3858 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003859 err_code |= ERR_ALERT | ERR_ABORT;
3860 goto out;
3861 }
3862
3863 list_for_each_entry(grp, &curgrps, list) {
3864 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003865 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3866 " name as another one declared at %s:%d.\n",
3867 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003868 err_code |= ERR_ALERT | ERR_FATAL;
3869 goto out;
3870 }
3871 }
3872
3873 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003874 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003875 err_code |= ERR_ALERT | ERR_ABORT;
3876 goto out;
3877 }
3878
3879 curgrp->id = strdup(args[1]);
3880 curgrp->conf.file = strdup(file);
3881 curgrp->conf.line = linenum;
3882 LIST_INIT(&curgrp->phs);
3883 LIST_INIT(&curgrp->messages);
3884 LIST_ADDQ(&curgrps, &curgrp->list);
3885 }
3886 else if (!strcmp(args[0], "messages")) {
3887 int cur_arg = 1;
3888 while (*args[cur_arg]) {
3889 struct spoe_placeholder *ph = NULL;
3890
3891 list_for_each_entry(ph, &curgrp->phs, list) {
3892 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003893 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3894 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003895 err_code |= ERR_ALERT | ERR_FATAL;
3896 goto out;
3897 }
3898 }
3899
3900 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003901 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003902 err_code |= ERR_ALERT | ERR_ABORT;
3903 goto out;
3904 }
3905 ph->id = strdup(args[cur_arg]);
3906 LIST_ADDQ(&curgrp->phs, &ph->list);
3907 cur_arg++;
3908 }
3909 }
3910 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003911 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3912 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003913 err_code |= ERR_ALERT | ERR_FATAL;
3914 goto out;
3915 }
3916 out:
3917 return err_code;
3918}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919
3920static int
3921cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3922{
3923 struct spoe_message *msg;
3924 struct spoe_arg *arg;
3925 const char *err;
3926 char *errmsg = NULL;
3927 int err_code = 0;
3928
3929 if ((cfg_scope == NULL && curengine != NULL) ||
3930 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003931 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003932 goto out;
3933
3934 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3935 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003936 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3937 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003938 err_code |= ERR_ALERT | ERR_ABORT;
3939 goto out;
3940 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003941 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3942 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003943 goto out;
3944 }
3945
3946 err = invalid_char(args[1]);
3947 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003948 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3949 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 err_code |= ERR_ALERT | ERR_ABORT;
3951 goto out;
3952 }
3953
3954 list_for_each_entry(msg, &curmsgs, list) {
3955 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003956 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3957 " name as another one declared at %s:%d.\n",
3958 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003959 err_code |= ERR_ALERT | ERR_FATAL;
3960 goto out;
3961 }
3962 }
3963
3964 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003965 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003966 err_code |= ERR_ALERT | ERR_ABORT;
3967 goto out;
3968 }
3969
3970 curmsg->id = strdup(args[1]);
3971 curmsg->id_len = strlen(curmsg->id);
3972 curmsg->event = SPOE_EV_NONE;
3973 curmsg->conf.file = strdup(file);
3974 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003975 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003976 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003977 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003978 LIST_INIT(&curmsg->by_evt);
3979 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 LIST_ADDQ(&curmsgs, &curmsg->list);
3981 }
3982 else if (!strcmp(args[0], "args")) {
3983 int cur_arg = 1;
3984
3985 curproxy->conf.args.ctx = ARGC_SPOE;
3986 curproxy->conf.args.file = file;
3987 curproxy->conf.args.line = linenum;
3988 while (*args[cur_arg]) {
3989 char *delim = strchr(args[cur_arg], '=');
3990 int idx = 0;
3991
3992 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003993 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003994 err_code |= ERR_ALERT | ERR_ABORT;
3995 goto out;
3996 }
3997
3998 if (!delim) {
3999 arg->name = NULL;
4000 arg->name_len = 0;
4001 delim = args[cur_arg];
4002 }
4003 else {
4004 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
4005 arg->name_len = delim - args[cur_arg];
4006 delim++;
4007 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01004008 arg->expr = sample_parse_expr((char*[]){delim, NULL},
4009 &idx, file, linenum, &errmsg,
4010 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004011 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004012 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004013 err_code |= ERR_ALERT | ERR_FATAL;
4014 free(arg->name);
4015 free(arg);
4016 goto out;
4017 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004018 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004019 LIST_ADDQ(&curmsg->args, &arg->list);
4020 cur_arg++;
4021 }
4022 curproxy->conf.args.file = NULL;
4023 curproxy->conf.args.line = 0;
4024 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004025 else if (!strcmp(args[0], "acl")) {
4026 err = invalid_char(args[1]);
4027 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004028 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4029 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004030 err_code |= ERR_ALERT | ERR_FATAL;
4031 goto out;
4032 }
Tim Duesterhus6b7ce1e2020-02-05 21:00:50 +01004033 if (strcasecmp(args[1], "or") == 0) {
4034 ha_warning("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
4035 "logical disjunction within a condition.\n",
4036 file, linenum, args[1]);
4037 err_code |= ERR_WARN;
4038 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004039 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004040 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4041 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004042 err_code |= ERR_ALERT | ERR_FATAL;
4043 goto out;
4044 }
4045 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004046 else if (!strcmp(args[0], "event")) {
4047 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004048 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004049 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004050 goto out;
4051 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004052 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4053 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004054
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
4056 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
4057 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
4058 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4059
4060 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
4061 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
4062 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
4063 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
4064 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
4065 curmsg->event = SPOE_EV_ON_TCP_RSP;
4066
4067 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
4068 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
4069 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
4070 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
4071 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
4072 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4073 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004074 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004075 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004076 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004077 goto out;
4078 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004079
4080 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4081 struct acl_cond *cond;
4082
4083 cond = build_acl_cond(file, linenum, &curmsg->acls,
4084 curproxy, (const char **)args+2,
4085 &errmsg);
4086 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004087 ha_alert("parsing [%s:%d] : error detected while "
4088 "parsing an 'event %s' condition : %s.\n",
4089 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004090 err_code |= ERR_ALERT | ERR_FATAL;
4091 goto out;
4092 }
4093 curmsg->cond = cond;
4094 }
4095 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004096 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4097 "or 'unless' followed by a condition but found '%s'.\n",
4098 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004099 err_code |= ERR_ALERT | ERR_FATAL;
4100 goto out;
4101 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004102 }
4103 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004104 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4105 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004106 err_code |= ERR_ALERT | ERR_FATAL;
4107 goto out;
4108 }
4109 out:
4110 free(errmsg);
4111 return err_code;
4112}
4113
4114/* Return -1 on error, else 0 */
4115static int
4116parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4117 struct flt_conf *fconf, char **err, void *private)
4118{
4119 struct list backup_sections;
4120 struct spoe_config *conf;
4121 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004122 struct spoe_group *grp, *grpback;
4123 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004124 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004125 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004126 char *file = NULL, *engine = NULL;
4127 int ret, pos = *cur_arg + 1;
4128
Christopher Faulet84c844e2018-03-23 14:37:14 +01004129 LIST_INIT(&curmsgs);
4130 LIST_INIT(&curgrps);
4131 LIST_INIT(&curmphs);
4132 LIST_INIT(&curgphs);
4133 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004134 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004135 curpxopts = 0;
4136 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004137
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004138 conf = calloc(1, sizeof(*conf));
4139 if (conf == NULL) {
4140 memprintf(err, "%s: out of memory", args[*cur_arg]);
4141 goto error;
4142 }
4143 conf->proxy = px;
4144
4145 while (*args[pos]) {
4146 if (!strcmp(args[pos], "config")) {
4147 if (!*args[pos+1]) {
4148 memprintf(err, "'%s' : '%s' option without value",
4149 args[*cur_arg], args[pos]);
4150 goto error;
4151 }
4152 file = args[pos+1];
4153 pos += 2;
4154 }
4155 else if (!strcmp(args[pos], "engine")) {
4156 if (!*args[pos+1]) {
4157 memprintf(err, "'%s' : '%s' option without value",
4158 args[*cur_arg], args[pos]);
4159 goto error;
4160 }
4161 engine = args[pos+1];
4162 pos += 2;
4163 }
4164 else {
4165 memprintf(err, "unknown keyword '%s'", args[pos]);
4166 goto error;
4167 }
4168 }
4169 if (file == NULL) {
4170 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4171 goto error;
4172 }
4173
4174 /* backup sections and register SPOE sections */
4175 LIST_INIT(&backup_sections);
4176 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004177 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4178 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004179 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004180
4181 /* Parse SPOE filter configuration file */
4182 curengine = engine;
4183 curproxy = px;
4184 curagent = NULL;
4185 curmsg = NULL;
4186 ret = readcfgfile(file);
4187 curproxy = NULL;
4188
4189 /* unregister SPOE sections and restore previous sections */
4190 cfg_unregister_sections();
4191 cfg_restore_sections(&backup_sections);
4192
4193 if (ret == -1) {
4194 memprintf(err, "Could not open configuration file %s : %s",
4195 file, strerror(errno));
4196 goto error;
4197 }
4198 if (ret & (ERR_ABORT|ERR_FATAL)) {
4199 memprintf(err, "Error(s) found in configuration file %s", file);
4200 goto error;
4201 }
4202
4203 /* Check SPOE agent */
4204 if (curagent == NULL) {
4205 memprintf(err, "No SPOE agent found in file %s", file);
4206 goto error;
4207 }
4208 if (curagent->b.name == NULL) {
4209 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4210 curagent->id, curagent->conf.file, curagent->conf.line);
4211 goto error;
4212 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004213 if (curagent->timeout.hello == TICK_ETERNITY ||
4214 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004215 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004216 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4217 " | While not properly invalid, you will certainly encounter various problems\n"
4218 " | with such a configuration. To fix this, please ensure that all following\n"
4219 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4220 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004221 }
4222 if (curagent->var_pfx == NULL) {
4223 char *tmp = curagent->id;
4224
4225 while (*tmp) {
4226 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4227 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4228 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4229 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4230 goto error;
4231 }
4232 tmp++;
4233 }
4234 curagent->var_pfx = strdup(curagent->id);
4235 }
4236
Christopher Fauletb7426d12018-03-21 14:12:17 +01004237 if (curagent->var_on_error) {
4238 struct arg arg;
4239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004240 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004241 curagent->var_pfx, curagent->var_on_error);
4242
4243 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004244 arg.data.str.area = trash.area;
4245 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004246 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004247 if (!vars_check_arg(&arg, err)) {
4248 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4249 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4250 goto error;
4251 }
4252 }
4253
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004254 if (curagent->var_t_process) {
4255 struct arg arg;
4256
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004257 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004258 curagent->var_pfx, curagent->var_t_process);
4259
4260 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004261 arg.data.str.area = trash.area;
4262 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004263 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004264 if (!vars_check_arg(&arg, err)) {
4265 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4266 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4267 goto error;
4268 }
4269 }
4270
4271 if (curagent->var_t_total) {
4272 struct arg arg;
4273
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004274 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004275 curagent->var_pfx, curagent->var_t_total);
4276
4277 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004278 arg.data.str.area = trash.area;
4279 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004280 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004281 if (!vars_check_arg(&arg, err)) {
4282 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4283 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4284 goto error;
4285 }
4286 }
4287
Christopher Faulet11610f32017-09-21 10:23:10 +02004288 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004289 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4290 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004291 goto finish;
4292 }
4293
Christopher Faulet11610f32017-09-21 10:23:10 +02004294 /* Replace placeholders by the corresponding messages for the SPOE
4295 * agent */
4296 list_for_each_entry(ph, &curmphs, list) {
4297 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004298 struct spoe_arg *arg;
4299 unsigned int where;
4300
Christopher Faulet11610f32017-09-21 10:23:10 +02004301 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004302 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4303 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4304 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4305 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4306 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4307 }
4308 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4309 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4310 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004311 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4312 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004313 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004314 }
4315 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004316 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4317 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004318 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004319 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004320
4321 where = 0;
4322 switch (msg->event) {
4323 case SPOE_EV_ON_CLIENT_SESS:
4324 where |= SMP_VAL_FE_CON_ACC;
4325 break;
4326
4327 case SPOE_EV_ON_TCP_REQ_FE:
4328 where |= SMP_VAL_FE_REQ_CNT;
4329 break;
4330
4331 case SPOE_EV_ON_HTTP_REQ_FE:
4332 where |= SMP_VAL_FE_HRQ_HDR;
4333 break;
4334
4335 case SPOE_EV_ON_TCP_REQ_BE:
4336 if (px->cap & PR_CAP_FE)
4337 where |= SMP_VAL_FE_REQ_CNT;
4338 if (px->cap & PR_CAP_BE)
4339 where |= SMP_VAL_BE_REQ_CNT;
4340 break;
4341
4342 case SPOE_EV_ON_HTTP_REQ_BE:
4343 if (px->cap & PR_CAP_FE)
4344 where |= SMP_VAL_FE_HRQ_HDR;
4345 if (px->cap & PR_CAP_BE)
4346 where |= SMP_VAL_BE_HRQ_HDR;
4347 break;
4348
4349 case SPOE_EV_ON_SERVER_SESS:
4350 where |= SMP_VAL_BE_SRV_CON;
4351 break;
4352
4353 case SPOE_EV_ON_TCP_RSP:
4354 if (px->cap & PR_CAP_FE)
4355 where |= SMP_VAL_FE_RES_CNT;
4356 if (px->cap & PR_CAP_BE)
4357 where |= SMP_VAL_BE_RES_CNT;
4358 break;
4359
4360 case SPOE_EV_ON_HTTP_RSP:
4361 if (px->cap & PR_CAP_FE)
4362 where |= SMP_VAL_FE_HRS_HDR;
4363 if (px->cap & PR_CAP_BE)
4364 where |= SMP_VAL_BE_HRS_HDR;
4365 break;
4366
4367 default:
4368 break;
4369 }
4370
4371 list_for_each_entry(arg, &msg->args, list) {
4372 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004373 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004374 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004375 "none of which is available here ('%s')",
4376 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004377 sample_ckp_names(arg->expr->fetch->use),
4378 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004379 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004380 }
4381 }
4382
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004383 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004384 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4385 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004386 }
4387 }
4388 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004389 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004390 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004391 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004392 continue;
4393 }
4394
Christopher Faulet11610f32017-09-21 10:23:10 +02004395 /* Replace placeholders by the corresponding groups for the SPOE
4396 * agent */
4397 list_for_each_entry(ph, &curgphs, list) {
4398 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4399 if (!strcmp(grp->id, ph->id)) {
4400 grp->agent = curagent;
4401 LIST_DEL(&grp->list);
4402 LIST_ADDQ(&curagent->groups, &grp->list);
4403 goto next_aph;
4404 }
4405 }
4406 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4407 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4408 goto error;
4409 next_aph:
4410 continue;
4411 }
4412
4413 /* Replace placeholders by the corresponding message for each SPOE
4414 * group of the SPOE agent */
4415 list_for_each_entry(grp, &curagent->groups, list) {
4416 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4417 list_for_each_entry(msg, &curmsgs, list) {
4418 if (!strcmp(msg->id, ph->id)) {
4419 if (msg->group != NULL) {
4420 memprintf(err, "SPOE message '%s' already belongs to "
4421 "the SPOE group '%s' declare at %s:%d",
4422 msg->id, msg->group->id,
4423 msg->group->conf.file,
4424 msg->group->conf.line);
4425 goto error;
4426 }
4427
4428 /* Scope for arguments are not checked for now. We will check
4429 * them only if a rule use the corresponding SPOE group. */
4430 msg->agent = curagent;
4431 msg->group = grp;
4432 LIST_DEL(&ph->list);
4433 LIST_ADDQ(&grp->messages, &msg->by_grp);
4434 goto next_mph_grp;
4435 }
4436 }
4437 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4438 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4439 goto error;
4440 next_mph_grp:
4441 continue;
4442 }
4443 }
4444
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004445 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004446 /* move curmsgs to the agent message list */
4447 curmsgs.n->p = &curagent->messages;
4448 curmsgs.p->n = &curagent->messages;
4449 curagent->messages = curmsgs;
4450 LIST_INIT(&curmsgs);
4451
Christopher Faulet7ee86672017-09-19 11:08:28 +02004452 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004453 conf->agent = curagent;
Christopher Faulet75fc5f32021-08-02 17:51:01 +02004454 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004455
4456 /* Start agent's proxy initialization here. It will be finished during
4457 * the filter init. */
4458 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4459 init_new_proxy(&conf->agent_fe);
4460 conf->agent_fe.id = conf->agent->id;
4461 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004462 conf->agent_fe.options |= curpxopts;
4463 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004464
4465 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4466 LIST_DEL(&logsrv->list);
4467 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4468 }
4469
Christopher Faulet11610f32017-09-21 10:23:10 +02004470 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4471 LIST_DEL(&ph->list);
4472 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004473 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004474 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4475 LIST_DEL(&ph->list);
4476 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004477 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004478 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4479 struct arg arg;
4480
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004481 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004482 curagent->var_pfx, vph->name);
4483
4484 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004485 arg.data.str.area = trash.area;
4486 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004487 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004488 if (!vars_check_arg(&arg, err)) {
4489 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4490 curagent->id, curagent->var_pfx, vph->name, *err);
4491 goto error;
4492 }
4493
4494 LIST_DEL(&vph->list);
4495 free(vph->name);
4496 free(vph);
4497 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004498 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4499 LIST_DEL(&grp->list);
4500 spoe_release_group(grp);
4501 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004502 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004503 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004504 fconf->ops = &spoe_ops;
4505 fconf->conf = conf;
4506 return 0;
4507
4508 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004509 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004510 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4511 LIST_DEL(&ph->list);
4512 spoe_release_placeholder(ph);
4513 }
4514 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4515 LIST_DEL(&ph->list);
4516 spoe_release_placeholder(ph);
4517 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004518 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4519 LIST_DEL(&vph->list);
4520 free(vph->name);
4521 free(vph);
4522 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004523 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4524 LIST_DEL(&grp->list);
4525 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004526 }
4527 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4528 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004529 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004530 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004531 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4532 LIST_DEL(&logsrv->list);
4533 free(logsrv);
4534 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004535 free(conf);
4536 return -1;
4537}
4538
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004539/* Send message of a SPOE group. This is the action_ptr callback of a rule
4540 * associated to a "send-spoe-group" action.
4541 *
4542 * It returns ACT_RET_CONT is processing is finished without error, it returns
4543 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4544 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004545static enum act_return
4546spoe_send_group(struct act_rule *rule, struct proxy *px,
4547 struct session *sess, struct stream *s, int flags)
4548{
4549 struct filter *filter;
4550 struct spoe_agent *agent = NULL;
4551 struct spoe_group *group = NULL;
4552 struct spoe_context *ctx = NULL;
4553 int ret, dir;
4554
4555 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4556 if (filter->config == rule->arg.act.p[0]) {
4557 agent = rule->arg.act.p[2];
4558 group = rule->arg.act.p[3];
4559 ctx = filter->ctx;
4560 break;
4561 }
4562 }
4563 if (agent == NULL || group == NULL || ctx == NULL)
4564 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004565 if (ctx->state == SPOE_CTX_ST_NONE)
4566 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004567
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004568 switch (rule->from) {
4569 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4570 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4571 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4572 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4573 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4574 default:
4575 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4576 " - internal error while execute spoe-send-group\n",
4577 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4578 __FUNCTION__, s);
4579 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4580 agent->id);
4581 return ACT_RET_CONT;
4582 }
4583
4584 ret = spoe_process_group(s, ctx, group, dir);
4585 if (ret == 1)
4586 return ACT_RET_CONT;
4587 else if (ret == 0) {
4588 if (flags & ACT_FLAG_FINAL) {
4589 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4590 " - failed to process group '%s': interrupted by caller\n",
4591 (int)now.tv_sec, (int)now.tv_usec,
4592 agent->id, __FUNCTION__, s, group->id);
4593 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004594 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004595 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004596 return ACT_RET_CONT;
4597 }
4598 return ACT_RET_YIELD;
4599 }
4600 else
4601 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004602}
4603
4604/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4605 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4606 * action should be:
4607 *
4608 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4609 *
4610 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4611 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4612 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4613 * group.
4614 *
4615 * The function returns 1 in success case, otherwise, it returns 0 and err is
4616 * filled.
4617 */
4618static int
4619check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4620{
4621 struct flt_conf *fconf;
4622 struct spoe_config *conf;
4623 struct spoe_agent *agent = NULL;
4624 struct spoe_group *group;
4625 struct spoe_message *msg;
4626 char *engine_id = rule->arg.act.p[0];
4627 char *group_id = rule->arg.act.p[1];
4628 unsigned int where = 0;
4629
4630 switch (rule->from) {
4631 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4632 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4633 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4634 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4635 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4636 default:
4637 memprintf(err,
4638 "internal error, unexpected rule->from=%d, please report this bug!",
4639 rule->from);
4640 goto error;
4641 }
4642
4643 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4644 * <px> */
4645 list_for_each_entry(fconf, &px->filter_configs, list) {
4646 conf = fconf->conf;
4647
4648 /* This is not an SPOE filter */
4649 if (fconf->id != spoe_filter_id)
4650 continue;
4651
4652 /* This is the good engine */
4653 if (!strcmp(conf->id, engine_id)) {
4654 agent = conf->agent;
4655 break;
4656 }
4657 }
4658 if (agent == NULL) {
4659 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4660 engine_id, group_id);
4661 goto error;
4662 }
4663
4664 /* Try to find the right group */
4665 list_for_each_entry(group, &agent->groups, list) {
4666 /* This is the good group */
4667 if (!strcmp(group->id, group_id))
4668 break;
4669 }
4670 if (&group->list == &agent->groups) {
4671 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4672 group_id, engine_id);
4673 goto error;
4674 }
4675
4676 /* Ok, we found the group, we need to check messages and their
4677 * arguments */
4678 list_for_each_entry(msg, &group->messages, by_grp) {
4679 struct spoe_arg *arg;
4680
4681 list_for_each_entry(arg, &msg->args, list) {
4682 if (!(arg->expr->fetch->val & where)) {
4683 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4684 "some args extract information from '%s',"
4685 "none of which is available here ('%s')",
4686 msg->id, group->id, msg->conf.file, msg->conf.line,
4687 sample_ckp_names(arg->expr->fetch->use),
4688 sample_ckp_names(where));
4689 goto error;
4690 }
4691 }
4692 }
4693
4694 free(engine_id);
4695 free(group_id);
4696 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4697 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4698 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4699 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4700 return 1;
4701
4702 error:
4703 free(engine_id);
4704 free(group_id);
4705 return 0;
4706}
4707
4708/* Parse 'send-spoe-group' action following the format:
4709 *
4710 * ... send-spoe-group <engine-id> <group-id>
4711 *
4712 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4713 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4714 * ids are saved and used later, when the rule will be checked.
4715 */
4716static enum act_parse_ret
4717parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4718 struct act_rule *rule, char **err)
4719{
4720 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4721 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4722 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4723 return ACT_RET_PRS_ERR;
4724 }
4725 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4726 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4727
4728 (*orig_arg) += 2;
4729
4730 rule->action = ACT_CUSTOM;
4731 rule->action_ptr = spoe_send_group;
4732 rule->check_ptr = check_send_spoe_group;
4733 return ACT_RET_PRS_OK;
4734}
4735
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004736
4737/* Declare the filter parser for "spoe" keyword */
4738static struct flt_kw_list flt_kws = { "SPOE", { }, {
4739 { "spoe", parse_spoe_flt, NULL },
4740 { NULL, NULL, NULL },
4741 }
4742};
4743
Willy Tarreau0108d902018-11-25 19:14:37 +01004744INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4745
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004746/* Delcate the action parser for "spoe-action" keyword */
4747static struct action_kw_list tcp_req_action_kws = { { }, {
4748 { "send-spoe-group", parse_send_spoe_group },
4749 { /* END */ },
4750 }
4751};
Willy Tarreau0108d902018-11-25 19:14:37 +01004752
4753INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4754
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004755static struct action_kw_list tcp_res_action_kws = { { }, {
4756 { "send-spoe-group", parse_send_spoe_group },
4757 { /* END */ },
4758 }
4759};
Willy Tarreau0108d902018-11-25 19:14:37 +01004760
4761INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4762
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004763static struct action_kw_list http_req_action_kws = { { }, {
4764 { "send-spoe-group", parse_send_spoe_group },
4765 { /* END */ },
4766 }
4767};
Willy Tarreau0108d902018-11-25 19:14:37 +01004768
4769INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4770
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004771static struct action_kw_list http_res_action_kws = { { }, {
4772 { "send-spoe-group", parse_send_spoe_group },
4773 { /* END */ },
4774 }
4775};
4776
Willy Tarreau0108d902018-11-25 19:14:37 +01004777INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);