blob: 47537d770c7fbacbee809f903a83d138b375eab6 [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
Willy Tarreaudcc048a2020-06-04 19:11:43 +020015#include <haproxy/acl.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020016#include <haproxy/applet.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020017#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Tim Duesterhus75e2f8d2021-09-16 17:38:26 +020021#include <haproxy/check.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010022#include <haproxy/conn_stream.h>
23#include <haproxy/cs_utils.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020026#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020028#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020030#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020033#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010035#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020036#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020039#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020041#include <haproxy/time.h>
Willy Tarreaue16ada12021-05-08 12:57:17 +020042#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020043#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020044
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045
46#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
47#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010048#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020049#else
50#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#endif
53
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010054/* Reserved 4 bytes to the frame size. So a frame and its size can be written
55 * together in a buffer */
56#define MAX_FRAME_SIZE global.tune.bufsize - 4
57
58/* The minimum size for a frame */
59#define MIN_FRAME_SIZE 256
60
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010061/* Reserved for the metadata and the frame type.
62 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010063#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020064
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010065/* Helper to get SPOE ctx inside an appctx */
Willy Tarreau23a24072022-05-05 20:18:44 +020066#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->svcctx))
Christopher Faulet42bfa462017-01-04 14:14:19 +010067
Christopher Faulet3b386a32017-02-23 10:17:15 +010068/* SPOE filter id. Used to identify SPOE filters */
69const char *spoe_filter_id = "SPOE filter";
70
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020071/* Set if the handle on SIGUSR1 is registered */
72static int sighandler_registered = 0;
73
74/* proxy used during the parsing */
75struct proxy *curproxy = NULL;
76
77/* The name of the SPOE engine, used during the parsing */
78char *curengine = NULL;
79
80/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020081/* SPOE agent/group/message used during the parsing */
82struct spoe_agent *curagent = NULL;
83struct spoe_group *curgrp = NULL;
84struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020085
86/* list of SPOE messages and placeholders used during the parsing */
87struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020088struct list curgrps;
89struct list curmphs;
90struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010091struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020092
Christopher Faulet7250b8f2018-03-26 17:19:01 +020093/* list of log servers used during the parsing */
94struct list curlogsrvs;
95
Christopher Faulet0e0f0852018-03-26 17:20:36 +020096/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
97int curpxopts;
98int curpxopts2;
99
Christopher Faulet42bfa462017-01-04 14:14:19 +0100100/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100101DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
102DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200103
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104struct flt_ops spoe_ops;
105
Christopher Faulet8ef75252017-02-20 22:56:03 +0100106static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
108static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Faulet6f1296b2021-08-02 17:53:56 +0200109static struct appctx *spoe_create_appctx(struct spoe_config *conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110
111/********************************************************************
112 * helper functions/globals
113 ********************************************************************/
114static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200115spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 free(ph->id);
120 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121}
122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100124spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125{
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 struct spoe_arg *arg, *argback;
127 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128
129 if (!msg)
130 return;
131 free(msg->id);
132 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 release_sample_expr(arg->expr);
135 free(arg->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200136 LIST_DELETE(&arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200137 free(arg);
138 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200140 LIST_DELETE(&acl->list);
Christopher Faulet57583e42017-09-04 15:41:09 +0200141 prune_acl(acl);
142 free(acl);
143 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100173 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100174 free(agent->var_t_process);
175 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200176 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200177 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 spoe_release_message(msg);
179 }
180 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200181 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200182 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200183 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200185 for (i = 0; i < global.nbthread; ++i) {
186 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200188 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100189 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200190 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200191 free(agent);
192}
193
194static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100195 [SPOE_FRM_ERR_NONE] = "normal",
196 [SPOE_FRM_ERR_IO] = "I/O error",
197 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
198 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
199 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
200 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
201 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
202 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
203 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
204 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
205 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
206 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100207 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100208 [SPOE_FRM_ERR_RES] = "resource allocation error",
209 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200210};
211
212static const char *spoe_event_str[SPOE_EV_EVENTS] = {
213 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
214 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
215 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
216 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
217 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
218
219 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
220 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
221 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
222};
223
224
225#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
226
227static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100228 [SPOE_CTX_ST_NONE] = "NONE",
229 [SPOE_CTX_ST_READY] = "READY",
230 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
231 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
232 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
233 [SPOE_CTX_ST_DONE] = "DONE",
234 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200235};
236
237static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100238 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
239 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
240 [SPOE_APPCTX_ST_IDLE] = "IDLE",
241 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
242 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
243 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
244 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
245 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
246 [SPOE_APPCTX_ST_EXIT] = "EXIT",
247 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200248};
249
250#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100251
Christopher Faulet8ef75252017-02-20 22:56:03 +0100252/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500253 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100254 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100255static char *
256generate_pseudo_uuid()
257{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100258 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800259 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100260}
261
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100262
263static inline void
264spoe_update_stat_time(struct timeval *tv, long *t)
265{
266 if (*t == -1)
267 *t = tv_ms_elapsed(tv, &now);
268 else
269 *t += tv_ms_elapsed(tv, &now);
270 tv_zero(tv);
271}
272
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/********************************************************************
274 * Functions that encode/decode SPOE frames
275 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200276/* Helper to get static string length, excluding the terminating null byte */
277#define SLEN(str) (sizeof(str)-1)
278
279/* Predefined key used in HELLO/DISCONNECT frames */
280#define SUPPORTED_VERSIONS_KEY "supported-versions"
281#define VERSION_KEY "version"
282#define MAX_FRAME_SIZE_KEY "max-frame-size"
283#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100284#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100285#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200286#define STATUS_CODE_KEY "status-code"
287#define MSG_KEY "message"
288
289struct spoe_version {
290 char *str;
291 int min;
292 int max;
293};
294
295/* All supported versions */
296static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200297 /* 1.0 is now unsupported because of a bug about frame's flags*/
298 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299 {NULL, 0, 0}
300};
301
302/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200303#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305/* Convert a string to a SPOE version value. The string must follow the format
306 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
307 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 const char *p, *end;
312 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200313
Christopher Faulet8ef75252017-02-20 22:56:03 +0100314 p = str;
315 end = str+len;
316 maj = min = 0;
317 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200318
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100320 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200322
Christopher Faulet8ef75252017-02-20 22:56:03 +0100323 /* parse Major number, until the '.' */
324 while (*p != '.') {
325 if (p >= end || *p < '0' || *p > '9')
326 goto out;
327 maj *= 10;
328 maj += (*p - '0');
329 p++;
330 }
331
332 /* check Major version */
333 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334 goto out;
335
Christopher Faulet8ef75252017-02-20 22:56:03 +0100336 p++; /* skip the '.' */
337 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
338 goto out;
339
340 /* Parse Minor number */
341 while (p < end) {
342 if (*p < '0' || *p > '9')
343 break;
344 min *= 10;
345 min += (*p - '0');
346 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200347 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100348
349 /* check Minor number */
350 if (min > 999)
351 goto out;
352
353 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100354 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++;
356 if (p != end)
357 goto out;
358
359 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200360 out:
361 return vsn;
362}
363
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800365 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200367static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369{
Willy Tarreau83061a82018-07-13 11:56:34 +0200370 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100371 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372 char *p, *end;
373 unsigned int flags = SPOE_FRM_FL_FIN;
374 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 p = frame;
377 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379 /* Set Frame type */
380 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100382 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200383 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 memcpy(p, (char *)&flags, 4);
385 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200389
390 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
391 * and "capabilities" */
392
393 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 sz = SLEN(SUPPORTED_VERSIONS_KEY);
395 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
396 goto too_big;
397
398 *p++ = SPOE_DATA_T_STR;
399 sz = SLEN(SUPPORTED_VERSIONS_VAL);
400 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
401 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
403 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 sz = SLEN(MAX_FRAME_SIZE_KEY);
405 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
406 goto too_big;
407
408 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200409 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
412 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(CAPABILITIES_KEY);
414 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
415 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200416
Christopher Faulet8ef75252017-02-20 22:56:03 +0100417 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100418 chk = get_trash_chunk();
419 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200420 memcpy(chk->area, "pipelining", 10);
421 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100422 }
423 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 if (chk->data) chk->area[chk->data++] = ',';
425 memcpy(chk->area+chk->data, "async", 5);
426 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100427 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100428 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 if (chk->data) chk->area[chk->data++] = ',';
430 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100431 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100432 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200433 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 goto too_big;
435
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500436 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200437 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 sz = SLEN(ENGINE_ID_KEY);
439 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
440 goto too_big;
441
442 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200443 sz = strlen(agent->rt[tid].engine_id);
444 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100446 }
447
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448 return (p - frame);
449
450 too_big:
451 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
452 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453}
454
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
456 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
457 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200460{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 const char *reason;
462 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100463 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 p = frame;
467 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 /* Set Frame type */
470 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100472 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200473 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474 memcpy(p, (char *)&flags, 4);
475 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476
477 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 *p++ = 0; *p++ = 0;
479
480 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
481 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482
483 /* There are 2 mandatory items: "status-code" and "message" */
484
485 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 sz = SLEN(STATUS_CODE_KEY);
487 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
488 goto too_big;
489
490 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200491 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
494 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 sz = SLEN(MSG_KEY);
496 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
497 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200498
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499 /*Get the message corresponding to the status code */
500 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
501
502 *p++ = SPOE_DATA_T_STR;
503 sz = strlen(reason);
504 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
505 goto too_big;
506
507 return (p - frame);
508
509 too_big:
510 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
511 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512}
513
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
515 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
516 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100519 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 char *p, *end;
522 unsigned int stream_id, frame_id;
523 unsigned int flags = SPOE_FRM_FL_FIN;
524 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Faulet8ef75252017-02-20 22:56:03 +0100526 p = frame;
527 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200528
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100529 stream_id = ctx->stream_id;
530 frame_id = ctx->frame_id;
531
532 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
533 /* The fragmentation is not supported by the applet */
534 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
535 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
536 return -1;
537 }
538 flags = ctx->frag_ctx.flags;
539 }
540
541 /* Set Frame type */
542 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
543
544 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200545 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 memcpy(p, (char *)&flags, 4);
547 p += 4;
548
549 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200550 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200552 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 goto too_big;
554
555 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 if (p + sz >= end)
558 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200559 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100560 p += sz;
561
562 return (p - frame);
563
564 too_big:
565 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
566 return 0;
567}
568
569/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
570 * returns the number of encoded bytes in the frame on success, 0 if an encoding
571 * error occurred and -1 if a fatal error occurred. */
572static int
573spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
574 char *frame, size_t size)
575{
576 char *p, *end;
577 unsigned int stream_id, frame_id;
578 unsigned int flags;
579 size_t sz;
580
581 p = frame;
582 end = frame+size;
583
Christopher Faulet8ef75252017-02-20 22:56:03 +0100584 /* <ctx> is null when the stream has aborted the processing of a
585 * fragmented frame. In this case, we must notify the corresponding
586 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100587 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100589 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
590 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
591 }
592 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 stream_id = ctx->stream_id;
595 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 }
597
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100599 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200602 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 memcpy(p, (char *)&flags, 4);
604 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200605
606 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200607 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200609 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200611
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 if (ctx == NULL)
613 goto end;
614
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200616 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 if (p + sz >= end)
618 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200619 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100621
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100622 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 return (p - frame);
624
625 too_big:
626 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
627 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628}
629
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630/* Decode and process the HELLO frame sent by an agent. It returns the number of
631 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
632 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635{
Christopher Faulet305c6072017-02-23 16:17:53 +0100636 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
637 char *p, *end;
638 int vsn, max_frame_size;
639 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640
641 p = frame;
642 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643
644 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 if (size < 7 /* TYPE + METADATA */) {
651 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
652 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 }
654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200657 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100660 /* Fragmentation is not supported for HELLO frame */
661 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100663 return -1;
664 }
665
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 if (*p != 0 || *(p+1) != 0) {
668 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
669 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672
673 /* There are 3 mandatory items: "version", "max-frame-size" and
674 * "capabilities" */
675
676 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100677 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 while (p < end) {
679 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200680 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682
683 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 ret = spoe_decode_buffer(&p, end, &str, &sz);
685 if (ret == -1 || !sz) {
686 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
687 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200691 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 int i, type = *p++;
693
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
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 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
701 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 }
703
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 return -1;
708 }
709 for (i = 0; supported_versions[i].str != NULL; ++i) {
710 if (vsn >= supported_versions[i].min &&
711 vsn <= supported_versions[i].max)
712 break;
713 }
714 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 return -1;
717 }
718 }
719 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200720 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 +0100721 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722
723 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
725 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
726 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
727 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
729 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200731 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
733 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 if (sz < MIN_FRAME_SIZE ||
736 sz > SPOE_APPCTX(appctx)->max_frame_size) {
737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738 return -1;
739 }
740 max_frame_size = sz;
741 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200743 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100744 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100745
746 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
748 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
749 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
752 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
753 return 0;
754 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 char *delim;
758
759 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100760 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
763 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100764 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765 flags |= SPOE_APPCTX_FL_PIPELINING;
766 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 else if (sz >= 5 && !strncmp(str, "async", 5)) {
768 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100769 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 flags |= SPOE_APPCTX_FL_ASYNC;
771 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
773 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100774 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100775 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
776 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 /* Get the next comma or break */
779 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 delim++;
782 sz -= (delim - str);
783 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 }
785 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 else {
787 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 if (spoe_skip_data(&p, end) == -1) {
789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
790 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792 }
793 }
794
795 /* Final checks */
796 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200798 return -1;
799 }
800 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100801 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802 return -1;
803 }
Christopher Faulet11389012019-02-07 16:13:26 +0100804 if (!agent)
805 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
806 else {
807 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
808 flags &= ~SPOE_APPCTX_FL_PIPELINING;
809 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
810 flags &= ~SPOE_APPCTX_FL_ASYNC;
811 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812
Christopher Faulet42bfa462017-01-04 14:14:19 +0100813 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
814 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
815 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816
817 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818}
819
820/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
821 * bytes on success, 0 if the frame can be ignored and -1 if an error
822 * occurred. */
823static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100827 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828
829 p = frame;
830 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831
832 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
834 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838 if (size < 7 /* TYPE + METADATA */) {
839 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
840 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 }
842
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200845 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 /* Fragmentation is not supported for DISCONNECT frame */
849 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 return -1;
852 }
853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (*p != 0 || *(p+1) != 0) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860
861 /* There are 2 mandatory items: "status-code" and "message" */
862
863 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 while (p < end) {
865 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200866 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868
869 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 ret = spoe_decode_buffer(&p, end, &str, &sz);
871 if (ret == -1 || !sz) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
875
876 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200877 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879
880 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
883 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200888 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
890 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
894
895 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200896 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 int type = *p++;
898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 ret = spoe_decode_buffer(&p, end, &str, &sz);
905 if (ret == -1 || sz > 255) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
910 SPOE_APPCTX(appctx)->reason = str;
911 SPOE_APPCTX(appctx)->rlen = sz;
912#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
914 else {
915 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 if (spoe_skip_data(&p, end) == -1) {
917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921 }
922
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924}
925
926
Christopher Fauleta1cda022016-12-21 08:58:06 +0100927/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 * success, 0 if the frame can be ignored and -1 if an error occurred. */
929static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938
939 p = frame;
940 end = frame + size;
941 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942
943 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (size < 7 /* TYPE + METADATA */) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
951 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 }
953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200956 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 /* Fragmentation is not supported for now */
960 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100962 return -1;
963 }
964
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200965 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200966 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100968 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200970 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200972 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100974
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100976 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200977 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 if ((*ctx)->stream_id == (unsigned int)stream_id &&
979 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100980 goto found;
981 }
982 }
983 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100984 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
985 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100987 goto found;
988 }
989 }
990
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100991 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
992 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
993 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
994
995 /* ABRT bit is set for an unfinished fragmented frame */
996 if (flags & SPOE_FRM_FL_ABRT) {
997 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100998 (*ctx)->state = SPOE_CTX_ST_ERROR;
999 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1000 /* Ignore the payload */
1001 goto end;
1002 }
1003 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1004 /* For now, we ignore the ack */
1005 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1006 return 0;
1007 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008
Christopher Fauleta1cda022016-12-21 08:58:06 +01001009 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1011 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 " - stream-id=%u - frame-id=%u\n",
1013 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1014 __FUNCTION__, appctx,
1015 (unsigned int)stream_id, (unsigned int)frame_id);
1016
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001017 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001018 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1019 /* Report an error if we are waiting the ack for another frame,
1020 * but not if there is no longer frame waiting for a ack
1021 * (timeout)
1022 */
1023 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1024 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1025 return -1;
1026 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1027 SPOE_APPCTX(appctx)->cur_fpa = 0;
1028 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029 return 0;
1030
1031 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1033 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001034 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001035 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001037
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001038 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001040 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1041 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001043
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 /* Transfer the buffer ownership to the SPOE context */
1045 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001046 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001047
Christopher Faulet8ef75252017-02-20 22:56:03 +01001048 (*ctx)->state = SPOE_CTX_ST_DONE;
1049
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001050 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 " - ACK frame received"
1053 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001054 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1056 (*ctx)->frame_id, flags);
1057 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001058}
1059
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060/* This function is used in cfgparse.c and declared in proto/checks.h. It
1061 * prepare the request to send to agents during a healthcheck. It returns 0 on
1062 * success and -1 if an error occurred. */
1063int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001066 struct appctx appctx;
1067 struct spoe_appctx spoe_appctx;
1068 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1069 size_t sz;
1070 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072 memset(&appctx, 0, sizeof(appctx));
1073 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001075
Willy Tarreau23a24072022-05-05 20:18:44 +02001076 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001077 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1080 end = frame + MAX_FRAME_SIZE;
1081
1082 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1083 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 /* Add "healthcheck" K/V item */
1088 sz = SLEN(HEALTHCHECK_KEY);
1089 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1090 return -1;
1091 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 *len = frame - buf;
1094 sz = htonl(*len - 4);
1095 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001099 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100 return 0;
1101}
1102
1103/* This function is used in checks.c and declared in proto/checks.h. It decode
1104 * the response received from an agent during a healthcheck. It returns 0 on
1105 * success and -1 if an error occurred. */
1106int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 struct appctx appctx;
1110 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112 memset(&appctx, 0, sizeof(appctx));
1113 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
Willy Tarreau23a24072022-05-05 20:18:44 +02001115 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001116 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001117
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1119 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120 goto error;
1121 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1123 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124
1125 return 0;
1126
1127 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1129 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1130 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001131 return -1;
1132}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001133
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001135 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136 * success. */
1137static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001140 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 int ret;
1142 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1145 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 netint = htonl(framesz);
1147 memcpy(buf, (char *)&netint, 4);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001148 ret = applet_putblk(appctx, buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 if (ret <= 0) {
Willy Tarreau40a9c322022-05-18 15:55:18 +02001150 if ((ret == -3 && b_is_null(&sc_ic(cs)->buf)) || ret == -1) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001151 /* WT: is this still needed for the case ret==-3 ? */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001152 cs_rx_room_blk(cs);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001154 }
1155 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 return -1; /* error */
1157 }
1158 return framesz;
1159}
1160
1161/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1162 * when the frame can be ignored, 1 to retry later and the frame length on
1163 * success. */
1164static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001167 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168 int ret;
1169 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170
Willy Tarreau40a9c322022-05-18 15:55:18 +02001171 ret = co_getblk(sc_oc(cs), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 if (ret > 0) {
1173 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001174 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176 return -1;
1177 }
Willy Tarreau40a9c322022-05-18 15:55:18 +02001178 ret = co_getblk(sc_oc(cs), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 }
1180 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001183 }
1184 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 return -1; /* error */
1186 }
1187 return framesz;
1188}
1189
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001190/********************************************************************
1191 * Functions that manage the SPOE applet
1192 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001193static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001194spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001195{
Willy Tarreau0698c802022-05-11 14:09:57 +02001196 cs_want_get(appctx_cs(appctx));
1197 cs_rx_endp_more(appctx_cs(appctx));
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198 appctx_wakeup(appctx);
1199 return 1;
1200}
1201
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202/* Callback function that catches applet timeouts. If a timeout occurred, we set
1203 * <appctx->st1> flag and the SPOE applet is woken up. */
1204static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001205spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001206{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001207 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208
1209 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1210 if (tick_is_expired(task->expire, now_ms)) {
1211 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001212 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215 return task;
1216}
1217
Christopher Faulet69ebc302022-05-12 15:28:51 +02001218static int
1219spoe_init_appctx(struct appctx *appctx)
1220{
1221 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1222 struct spoe_agent *agent = spoe_appctx->agent;
1223 struct task *task;
1224 struct stream *s;
1225
1226 if ((task = task_new_here()) == NULL)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001227 goto out_error;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001228 task->process = spoe_process_appctx;
1229 task->context = appctx;
1230
1231 if (appctx_finalize_startup(appctx, &agent->spoe_conf->agent_fe, &BUF_NULL) == -1)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001232 goto out_free_task;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001233
1234 spoe_appctx->owner = appctx;
1235 spoe_appctx->task = task;
1236
1237 LIST_INIT(&spoe_appctx->buffer_wait.list);
1238 spoe_appctx->buffer_wait.target = appctx;
1239 spoe_appctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
1240
1241 s = appctx_strm(appctx);
1242 stream_set_backend(s, agent->b.be);
1243
1244 /* applet is waiting for data */
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001245 cs_cant_get(s->scf);
Christopher Faulet69ebc302022-05-12 15:28:51 +02001246
1247 s->do_log = NULL;
1248 s->res.flags |= CF_READ_DONTWAIT;
1249
1250 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1251 LIST_APPEND(&agent->rt[tid].applets, &spoe_appctx->list);
1252 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1253 _HA_ATOMIC_INC(&agent->counters.applets);
1254
1255 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1256 task_wakeup(spoe_appctx->task, TASK_WOKEN_INIT);
1257 return 0;
1258 out_free_task:
1259 task_destroy(task);
1260 out_error:
1261 return -1;
1262}
1263
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264/* Callback function that releases a SPOE applet. This happens when the
1265 * connection with the agent is closed. */
1266static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001267spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001268{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001269 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001270 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1271 struct spoe_agent *agent;
1272 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273
1274 if (spoe_appctx == NULL)
1275 return;
1276
Willy Tarreau23a24072022-05-05 20:18:44 +02001277 appctx->svcctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001279
1280 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1281 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1282 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283
Christopher Faulet8ef75252017-02-20 22:56:03 +01001284 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001285 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001286 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001287 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001288 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001290 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001291 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001295 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1296 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001297 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001298 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1302 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001303
Christopher Fauletda098e62022-03-31 17:44:45 +02001304 cs_shutw(cs);
1305 cs_shutr(cs);
Willy Tarreau40a9c322022-05-18 15:55:18 +02001306 sc_ic(cs)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001307 }
1308
Christopher Faulet8ef75252017-02-20 22:56:03 +01001309 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001310 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001311
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 /* Notify all waiting streams */
1313 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001314 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001316 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001317 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001318 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001320 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001321 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001322 }
1323
Christopher Faulet8ef75252017-02-20 22:56:03 +01001324 /* If the applet was processing a fragmented frame, notify the
1325 * corresponding stream. */
1326 if (spoe_appctx->frag_ctx.ctx) {
1327 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001328 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001329 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001330 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001331 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1332 }
1333
Willy Tarreaub042e4f2022-02-15 16:49:37 +01001334 if (!LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) {
Christopher Fauletcf181c72020-11-10 18:45:34 +01001335 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1336 if (ctx->spoe_appctx == spoe_appctx)
1337 ctx->spoe_appctx = NULL;
1338 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001339 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001340 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001341 else {
1342 /* It is the last running applet and the sending and waiting
1343 * queues are not empty. Try to start a new one if HAproxy is
1344 * not stopping.
1345 */
1346 if (!stopping &&
1347 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
1348 spoe_create_appctx(agent->spoe_conf))
1349 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001350
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001351 /* otherwise, notify all waiting streams */
1352 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1353 LIST_DELETE(&ctx->list);
1354 LIST_INIT(&ctx->list);
1355 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
1356 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1357 ctx->spoe_appctx = NULL;
1358 ctx->state = SPOE_CTX_ST_ERROR;
1359 ctx->status_code = (spoe_appctx->status_code + 0x100);
1360 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1361 }
1362 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1363 LIST_DELETE(&ctx->list);
1364 LIST_INIT(&ctx->list);
1365 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
1366 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1367 ctx->spoe_appctx = NULL;
1368 ctx->state = SPOE_CTX_ST_ERROR;
1369 ctx->status_code = (spoe_appctx->status_code + 0x100);
1370 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1371 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001373
1374 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001375 /* Release allocated memory */
1376 spoe_release_buffer(&spoe_appctx->buffer,
1377 &spoe_appctx->buffer_wait);
1378 pool_free(pool_head_spoe_appctx, spoe_appctx);
1379
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001380 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001381 agent->rt[tid].frame_size = agent->max_frame_size;
1382 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1383 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001384}
1385
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001386static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001387spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001389 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001390 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001394 if (cs_state_in(cs->state, SC_SB_CER|SC_SB_DIS|SC_SB_CLO)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001395 /* closed */
1396 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1397 goto exit;
1398 }
1399
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001400 if (!cs_state_in(cs->state, SC_SB_RDY|SC_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001401 /* not connected yet */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001402 cs_rx_endp_more(cs);
Willy Tarreauea27f482022-05-18 16:10:52 +02001403 task_wakeup(__sc_strm(cs)->task, TASK_WOKEN_MSG);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 goto stop;
1405 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001406
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1409 " - Connection timed out\n",
1410 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1411 __FUNCTION__, appctx);
1412 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001413 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415
Christopher Faulet42bfa462017-01-04 14:14:19 +01001416 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 SPOE_APPCTX(appctx)->task->expire =
1418 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1421 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001422 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 ret = spoe_prepare_hahello_frame(appctx, frame,
1424 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001427
1428 switch (ret) {
1429 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001430 case 0: /* ignore => an error, cannot be ignored */
1431 goto exit;
1432
1433 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 goto stop;
1435
Christopher Faulet8ef75252017-02-20 22:56:03 +01001436 default:
1437 /* HELLO frame successfully sent, now wait for the
1438 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1440 goto next;
1441 }
1442
1443 next:
1444 return 0;
1445 stop:
1446 return 1;
1447 exit:
1448 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1449 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001450}
1451
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001453spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001454{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001455 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001456 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001457 char *frame;
1458 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001459
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001460
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001461 if (cs->state == SC_ST_CLO || cs_opposite(cs)->state == SC_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001462 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001463 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001464 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001465
Christopher Fauleta1cda022016-12-21 08:58:06 +01001466 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001467 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1468 " - Connection timed out\n",
1469 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1470 __FUNCTION__, appctx);
1471 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001472 goto exit;
1473 }
1474
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001475 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 ret = spoe_recv_frame(appctx, frame,
1477 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001478 if (ret > 1) {
1479 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1480 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1481 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001482 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001483 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001484 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001485 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486
Christopher Fauleta1cda022016-12-21 08:58:06 +01001487 switch (ret) {
1488 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001489 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001490 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1491 goto next;
1492
1493 case 1: /* retry later */
1494 goto stop;
1495
1496 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001497 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001498 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001499 SPOE_APPCTX(appctx)->node.key = 0;
1500 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001501
1502 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001503 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001504 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001505 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001506
Christopher Fauleta1cda022016-12-21 08:58:06 +01001507 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001509 if (trash.data)
Willy Tarreau40a9c322022-05-18 15:55:18 +02001510 co_skip(sc_oc(cs), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511
1512 SPOE_APPCTX(appctx)->task->expire =
1513 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001514 return 0;
1515 stop:
1516 return 1;
1517 exit:
1518 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1519 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001520}
1521
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001522
Christopher Fauleta1cda022016-12-21 08:58:06 +01001523static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001524spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001525{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001526 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1527 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001528 char *frame, *buf;
1529 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001530
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1532 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001533 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001534
1535 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1536 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1537 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1538 SPOE_APPCTX(appctx)->max_frame_size);
1539 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001540 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001541 *skip = 1;
1542 ret = 1;
1543 goto end;
1544 }
1545 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001546 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001547 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1548 SPOE_APPCTX(appctx)->max_frame_size);
1549
1550 }
1551
Christopher Faulet8ef75252017-02-20 22:56:03 +01001552 if (ret > 1)
1553 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001554
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 switch (ret) {
1556 case -1: /* error */
1557 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1558 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001559
Christopher Faulet8ef75252017-02-20 22:56:03 +01001560 case 0: /* ignore */
1561 if (ctx == NULL)
1562 goto abort_frag_frame;
1563
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001564 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001565 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001567 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001568 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001569 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001570 ctx->state = SPOE_CTX_ST_ERROR;
1571 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1572 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001573 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001574 break;
1575
1576 case 1: /* retry */
1577 *skip = 1;
1578 break;
1579
1580 default:
1581 if (ctx == NULL)
1582 goto abort_frag_frame;
1583
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001584 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001585 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001586 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001587 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001588 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001589 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001590 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1591 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1592 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001593 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001594 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001595 }
1596 goto end;
1597
1598 frag_frame_sent:
1599 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001600 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1602 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1603 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001604 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1605 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1606 goto end;
1607
1608 no_frag_frame_sent:
1609 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1610 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001611 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001612 }
1613 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1614 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001615 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001616 }
1617 else {
1618 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001619 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001620 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001621 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001622 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001623 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1625 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1626 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001627 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628
Christopher Faulet8ef75252017-02-20 22:56:03 +01001629 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1630 goto end;
1631
1632 abort_frag_frame:
1633 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1634 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1635 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1636 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1637 goto end;
1638
1639 end:
1640 return ret;
1641}
1642
1643static int
1644spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1645{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001646 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001647 struct spoe_context *ctx = NULL;
1648 char *frame;
1649 int ret;
1650
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001651 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652 ret = spoe_recv_frame(appctx, frame,
1653 SPOE_APPCTX(appctx)->max_frame_size);
1654 if (ret > 1) {
1655 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1656 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001657 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001658 goto end;
1659 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001660 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001661 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1662 }
1663 switch (ret) {
1664 case -1: /* error */
1665 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1666 break;
1667
1668 case 0: /* ignore */
1669 break;
1670
1671 case 1: /* retry */
1672 *skip = 1;
1673 break;
1674
1675 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001676 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001677 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001678 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001679 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1680 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001681 if (ctx->spoe_appctx) {
1682 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001683 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001685 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1686 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1687 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1688 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1689 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1690 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1691 }
1692 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1693 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001694 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1695 break;
1696 }
1697
1698 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001699 if (trash.data)
Willy Tarreau40a9c322022-05-18 15:55:18 +02001700 co_skip(sc_oc(appctx_cs(appctx)), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001701 end:
1702 return ret;
1703}
1704
1705static int
1706spoe_handle_processing_appctx(struct appctx *appctx)
1707{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001708 struct stconn *cs = appctx_cs(appctx);
Willy Tarreauea27f482022-05-18 16:10:52 +02001709 struct server *srv = objt_server(__sc_strm(cs)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001710 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001711 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001712
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001713 if (cs->state == SC_ST_CLO || cs_opposite(cs)->state == SC_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001714 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1715 goto exit;
1716 }
1717
1718 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1719 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1720 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1721 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1722 goto next;
1723 }
1724
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001725 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001726 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001727 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001728 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1729 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001730 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001731
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001732
1733 /* Close the applet ASAP because some sessions are waiting for a free
1734 * connection slot. It is only an issue in multithreaded mode.
1735 */
1736 close_asap = (global.nbthread > 1 &&
1737 (agent->b.be->queue.length ||
1738 (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1739
1740 /* Don"t try to send new frame we are waiting for at lease a ack, in
1741 * sync mode or if applet must be closed ASAP
1742 */
1743 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001744 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001745
Christopher Faulet8f82b202018-01-24 16:23:03 +01001746 /* receiving_frame loop */
1747 while (!skip_receiving) {
1748 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1749 switch (ret) {
1750 case -1: /* error */
1751 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001752
Christopher Faulet8f82b202018-01-24 16:23:03 +01001753 case 0: /* ignore */
1754 active_r = 1;
1755 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001756
Christopher Faulet8f82b202018-01-24 16:23:03 +01001757 case 1: /* retry */
1758 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001759
Christopher Faulet8f82b202018-01-24 16:23:03 +01001760 default:
1761 active_r = 1;
1762 break;
1763 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765
Christopher Faulet8f82b202018-01-24 16:23:03 +01001766 /* send_frame loop */
1767 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1768 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1769 switch (ret) {
1770 case -1: /* error */
1771 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772
Christopher Faulet8f82b202018-01-24 16:23:03 +01001773 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001774 if (SPOE_APPCTX(appctx)->node.key)
1775 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001776 active_s++;
1777 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778
Christopher Faulet8f82b202018-01-24 16:23:03 +01001779 case 1: /* retry */
1780 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781
Christopher Faulet8f82b202018-01-24 16:23:03 +01001782 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001783 if (SPOE_APPCTX(appctx)->node.key)
1784 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001785 active_s++;
1786 break;
1787 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001788
1789 /* if applet must be close ASAP, don't send more than a frame */
1790 if (close_asap)
1791 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793
Christopher Faulet8f82b202018-01-24 16:23:03 +01001794 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001795 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1796 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1797 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001798
Christopher Faulet8f82b202018-01-24 16:23:03 +01001799 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001800 /* If applet must be closed, don't switch it in IDLE state and
1801 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001802 */
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001803 if (close_asap) {
1804 if (SPOE_APPCTX(appctx)->cur_fpa)
1805 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1807 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1808 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1809 goto next;
1810 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001811 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001812 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001813 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001815
1816 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 return 1;
1818
Christopher Faulet8f82b202018-01-24 16:23:03 +01001819 next:
1820 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1821 return 0;
1822
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 exit:
1824 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1825 return 0;
1826}
1827
1828static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001831 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001832 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 char *frame, *buf;
1834 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001835
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001836 if (cs->state == SC_ST_CLO || cs_opposite(cs)->state == SC_ST_CLO)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 goto exit;
1838
1839 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1840 goto exit;
1841
Christopher Faulet8ef75252017-02-20 22:56:03 +01001842 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1843 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001844 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1846 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849
1850 switch (ret) {
1851 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 goto exit;
1854
1855 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001856 goto stop;
1857
1858 default:
1859 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1860 " - disconnected by HAProxy (%d): %s\n",
1861 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862 __FUNCTION__, appctx,
1863 SPOE_APPCTX(appctx)->status_code,
1864 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865
1866 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1867 goto next;
1868 }
1869
1870 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 SPOE_APPCTX(appctx)->task->expire =
1872 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 return 0;
1874 stop:
1875 return 1;
1876 exit:
1877 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1878 return 0;
1879}
1880
1881static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001884 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001885 char *frame;
1886 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001887
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001888 if (cs->state == SC_ST_CLO || cs_opposite(cs)->state == SC_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001891 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892
Christopher Fauletb067b062017-01-04 16:39:11 +01001893 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001896 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001898 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899 ret = spoe_recv_frame(appctx, frame,
1900 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001901 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001902 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 }
1905
1906 switch (ret) {
1907 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001908 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1909 " - error on frame (%s)\n",
1910 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001911 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001912 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001913 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001914 goto exit;
1915
1916 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 goto next;
1918
1919 case 1: /* retry */
1920 goto stop;
1921
1922 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001923 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001924 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001925 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001926 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001927 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1928 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929 goto exit;
1930 }
1931
1932 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001933 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001934 if (trash.data)
Willy Tarreau40a9c322022-05-18 15:55:18 +02001935 co_skip(sc_oc(cs), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001936
Christopher Fauleta1cda022016-12-21 08:58:06 +01001937 return 0;
1938 stop:
1939 return 1;
1940 exit:
1941 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1942 return 0;
1943}
1944
1945/* I/O Handler processing messages exchanged with the agent */
1946static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001948{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001949 struct stconn *cs = appctx_cs(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001950 struct spoe_agent *agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001951
1952 if (SPOE_APPCTX(appctx) == NULL)
1953 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001954
Christopher Faulet8ef75252017-02-20 22:56:03 +01001955 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1956 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001957
Christopher Fauleta1cda022016-12-21 08:58:06 +01001958 switchstate:
1959 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1960 " - appctx-state=%s\n",
1961 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1962 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1963
1964 switch (appctx->st0) {
1965 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001966 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001967 goto out;
1968 goto switchstate;
1969
1970 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001971 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001972 goto out;
1973 goto switchstate;
1974
1975 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001976 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001977 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001978 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001979 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001980 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001981 SPOE_APPCTX(appctx)->task->expire =
1982 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001983 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984 goto switchstate;
1985 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001986 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1987 /* fall through */
1988
1989 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001990 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1991 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001992 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 goto out;
1994 goto switchstate;
1995
1996 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001997 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001998 goto out;
1999 goto switchstate;
2000
2001 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002002 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002003 goto out;
2004 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005
2006 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002007 appctx->st0 = SPOE_APPCTX_ST_END;
2008 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
2009
Christopher Fauletda098e62022-03-31 17:44:45 +02002010 cs_shutw(cs);
2011 cs_shutr(cs);
Willy Tarreau40a9c322022-05-18 15:55:18 +02002012 sc_ic(cs)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013 /* fall through */
2014
2015 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002016 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002018 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02002019 if (stopping)
2020 spoe_wakeup_appctx(appctx);
2021
Christopher Faulet42bfa462017-01-04 14:14:19 +01002022 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
2023 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024}
2025
2026struct applet spoe_applet = {
2027 .obj_type = OBJ_TYPE_APPLET,
2028 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002029 .fct = spoe_handle_appctx,
Christopher Faulet69ebc302022-05-12 15:28:51 +02002030 .init = spoe_init_appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002031 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002032};
2033
2034/* Create a SPOE applet. On success, the created applet is returned, else
2035 * NULL. */
2036static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002037spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038{
Christopher Faulet69ebc302022-05-12 15:28:51 +02002039 struct spoe_appctx *spoe_appctx;
2040 struct appctx *appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
Christopher Faulet69ebc302022-05-12 15:28:51 +02002042 spoe_appctx = pool_zalloc(pool_head_spoe_appctx);
2043 if (spoe_appctx == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002044 goto out_error;
2045
Christopher Faulet69ebc302022-05-12 15:28:51 +02002046 spoe_appctx->agent = conf->agent;
2047 spoe_appctx->version = 0;
2048 spoe_appctx->max_frame_size = conf->agent->max_frame_size;
2049 spoe_appctx->flags = 0;
2050 spoe_appctx->status_code = SPOE_FRM_ERR_NONE;
2051 spoe_appctx->buffer = BUF_NULL;
2052 spoe_appctx->cur_fpa = 0;
2053 LIST_INIT(&spoe_appctx->list);
2054 LIST_INIT(&spoe_appctx->waiting_queue);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002055
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002056
Christopher Faulet6095d572022-05-16 17:09:48 +02002057 if ((appctx = appctx_new_here(&spoe_applet, NULL)) == NULL)
Christopher Faulet69ebc302022-05-12 15:28:51 +02002058 goto out_free_spoe_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002059
Christopher Faulet69ebc302022-05-12 15:28:51 +02002060 appctx->svcctx = spoe_appctx;
2061 if (appctx_init(appctx) == -1)
2062 goto out_free_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064 appctx_wakeup(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065 return appctx;
2066
2067 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068 out_free_appctx:
Christopher Faulet69ebc302022-05-12 15:28:51 +02002069 appctx_free_on_early_error(appctx);
2070 out_free_spoe_appctx:
2071 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072 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 Fauleteccfa612020-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. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002143 _HA_ATOMIC_INC(&agent->counters.nb_sending);
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;
Willy Tarreau2b718102021-04-21 07:32:39 +02002148 LIST_APPEND(&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 Faulet2469eba2020-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;
Willy Tarreau4781b152021-04-06 13:53:36 +02002640 _HA_ATOMIC_INC(&agent->counters.nb_processed);
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)
Willy Tarreau4781b152021-04-06 13:53:36 +02002667 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002668 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002669 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002670
Willy Tarreau2b718102021-04-21 07:32:39 +02002671 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002672 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) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002774 _HA_ATOMIC_INC(&agent->counters.nb_errors);
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
Willy Tarreau2b718102021-04-21 07:32:39 +02002880 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002881 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002882
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002883 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002884 return 1;
2885
Willy Tarreaub4e34762021-09-30 19:02:18 +02002886 LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002887 return 0;
2888}
2889
2890static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002891spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002892{
Willy Tarreau2b718102021-04-21 07:32:39 +02002893 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002894 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002895
2896 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002897 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002898 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002899 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002900 }
2901}
2902
Christopher Faulet4596fb72017-01-11 14:05:19 +01002903static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002904spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002905{
2906 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2907 return 1;
2908}
2909
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002910static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002911spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002912{
2913 struct spoe_config *conf = FLT_CONF(filter);
2914 struct spoe_context *ctx;
2915
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002916 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917 if (ctx == NULL) {
2918 return NULL;
2919 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002920 ctx->filter = filter;
2921 ctx->state = SPOE_CTX_ST_NONE;
2922 ctx->status_code = SPOE_CTX_ERR_NONE;
2923 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002924 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002925 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002926 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002927 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002928 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002929 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002930 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931
Christopher Fauletf7a30922016-11-10 15:04:51 +01002932 ctx->stream_id = 0;
2933 ctx->frame_id = 1;
2934 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002936 tv_zero(&ctx->stats.tv_start);
2937 tv_zero(&ctx->stats.tv_request);
2938 tv_zero(&ctx->stats.tv_queue);
2939 tv_zero(&ctx->stats.tv_wait);
2940 tv_zero(&ctx->stats.tv_response);
2941 ctx->stats.t_request = -1;
2942 ctx->stats.t_queue = -1;
2943 ctx->stats.t_waiting = -1;
2944 ctx->stats.t_response = -1;
2945 ctx->stats.t_process = -1;
2946 ctx->stats.t_total = 0;
2947
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002948 ctx->strm = s;
2949 ctx->state = SPOE_CTX_ST_READY;
2950 filter->ctx = ctx;
2951
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952 return ctx;
2953}
2954
2955static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002956spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002958 struct spoe_config *conf = FLT_CONF(filter);
2959 struct spoe_context *ctx = filter->ctx;
2960
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961 if (!ctx)
2962 return;
2963
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002964 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002965 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002966 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967}
2968
2969static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002970spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002971{
2972 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002973 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002974
2975 tv_zero(&ctx->stats.tv_start);
2976 tv_zero(&ctx->stats.tv_request);
2977 tv_zero(&ctx->stats.tv_queue);
2978 tv_zero(&ctx->stats.tv_wait);
2979 tv_zero(&ctx->stats.tv_response);
2980 ctx->stats.t_request = -1;
2981 ctx->stats.t_queue = -1;
2982 ctx->stats.t_waiting = -1;
2983 ctx->stats.t_response = -1;
2984 ctx->stats.t_process = -1;
2985 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986}
2987
2988
2989/***************************************************************************
2990 * Hooks that manage the filter lifecycle (init/check/deinit)
2991 **************************************************************************/
2992/* Signal handler: Do a soft stop, wakeup SPOE applet */
2993static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002994spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995{
2996 struct proxy *p;
2997
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002998 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002999 while (p) {
3000 struct flt_conf *fconf;
3001
3002 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01003003 struct spoe_config *conf;
3004 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01003005 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02003006 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007
Christopher Faulet3b386a32017-02-23 10:17:15 +01003008 if (fconf->id != spoe_filter_id)
3009 continue;
3010
3011 conf = fconf->conf;
3012 agent = conf->agent;
3013
Christopher Faulet24289f22017-09-25 14:48:02 +02003014 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003015 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003016 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3017 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003018 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003019 }
3020 }
3021 p = p->next;
3022 }
3023}
3024
3025
3026/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3027static int
3028spoe_init(struct proxy *px, struct flt_conf *fconf)
3029{
3030 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003032 /* conf->agent_fe was already initialized during the config
3033 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003035 conf->agent_fe.cap = PR_CAP_FE;
3036 conf->agent_fe.mode = PR_MODE_TCP;
3037 conf->agent_fe.maxconn = 0;
3038 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3039 conf->agent_fe.conn_retries = CONN_RETRIES;
3040 conf->agent_fe.accept = frontend_accept;
3041 conf->agent_fe.srv = NULL;
3042 conf->agent_fe.timeout.client = TICK_ETERNITY;
3043 conf->agent_fe.default_target = &spoe_applet.obj_type;
3044 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3045
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003047 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 sighandler_registered = 1;
3049 }
3050
Christopher Faulet00292352019-01-09 15:05:10 +01003051 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053}
3054
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003055/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056static void
3057spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3058{
3059 struct spoe_config *conf = fconf->conf;
3060
3061 if (conf) {
3062 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003063
Christopher Faulet8ef75252017-02-20 22:56:03 +01003064 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003065 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003066 free(conf);
3067 }
3068 fconf->conf = NULL;
3069}
3070
3071/* Check configuration of a SPOE filter for a specified proxy.
3072 * Return 1 on error, else 0. */
3073static int
3074spoe_check(struct proxy *px, struct flt_conf *fconf)
3075{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003076 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003077 struct spoe_config *conf = fconf->conf;
3078 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003079 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003080 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003081
Christopher Faulet7ee86672017-09-19 11:08:28 +02003082 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3083 * are uniq */
3084 list_for_each_entry(f, &px->filter_configs, list) {
3085 struct spoe_config *c = f->conf;
3086
3087 /* This is not an SPOE filter */
3088 if (f->id != spoe_filter_id)
3089 continue;
3090 /* This is the current SPOE filter */
3091 if (f == fconf)
3092 continue;
3093
3094 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003095 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003096 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3097 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003098 return 1;
3099 }
3100 }
3101
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003102 target = proxy_be_by_name(conf->agent->b.name);
3103 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003104 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3105 " declared at %s:%d.\n",
3106 px->id, conf->agent->b.name, conf->agent->id,
3107 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003108 return 1;
3109 }
3110 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003111 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3112 " at %s:%d does not support HTTP mode.\n",
3113 px->id, target->id, conf->agent->id,
3114 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003115 return 1;
3116 }
3117
Christopher Fauletfe261552019-03-18 13:57:42 +01003118 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003119 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3120 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3121 return 1;
3122 }
3123 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003124 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003125 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3126 conf->agent->rt[i].processing = 0;
3127 LIST_INIT(&conf->agent->rt[i].applets);
3128 LIST_INIT(&conf->agent->rt[i].sending_queue);
3129 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3130 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003131 }
3132
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003133 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3134 if (logsrv->type == LOG_TARGET_BUFFER) {
3135 struct sink *sink = sink_find(logsrv->ring_name);
3136
3137 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3138 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003139 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003140 px->id, conf->agent->id, conf->agent->conf.file,
3141 conf->agent->conf.line, logsrv->ring_name);
3142 return 1;
3143 }
3144 logsrv->sink = sink;
3145 }
3146 }
3147
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003148 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003149 conf->agent->b.be = target;
3150 return 0;
3151}
3152
Kevin Zhud87b1a52019-09-17 15:05:45 +02003153/* Initializes the SPOE filter for a proxy for a specific thread.
3154 * Returns a negative value if an error occurs. */
3155static int
3156spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3157{
3158 struct spoe_config *conf = fconf->conf;
3159 struct spoe_agent *agent = conf->agent;
3160
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003161 agent->rt[tid].engine_id = generate_pseudo_uuid();
3162 if (agent->rt[tid].engine_id == NULL)
3163 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003164 return 0;
3165}
3166
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003167/**************************************************************************
3168 * Hooks attached to a stream
3169 *************************************************************************/
3170/* Called when a filter instance is created and attach to a stream. It creates
3171 * the context that will be used to process this stream. */
3172static int
3173spoe_start(struct stream *s, struct filter *filter)
3174{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003175 struct spoe_config *conf = FLT_CONF(filter);
3176 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003177 struct spoe_context *ctx;
3178
3179 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003180 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003181 __FUNCTION__, s);
3182
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003183 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003184 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3185 " - failed to create SPOE context\n",
3186 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003187 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003188 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003189 "SPOE: [%s] failed to create SPOE context\n",
3190 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 return 0;
3192 }
3193
Christopher Faulet11610f32017-09-21 10:23:10 +02003194 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3196
Christopher Faulet11610f32017-09-21 10:23:10 +02003197 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003198 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3199
Christopher Faulet11610f32017-09-21 10:23:10 +02003200 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003201 filter->pre_analyzers |= AN_RES_INSPECT;
3202
Christopher Faulet11610f32017-09-21 10:23:10 +02003203 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003204 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3205
Christopher Faulet11610f32017-09-21 10:23:10 +02003206 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003207 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3208
Christopher Faulet11610f32017-09-21 10:23:10 +02003209 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003210 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3211
3212 return 1;
3213}
3214
3215/* Called when a filter instance is detached from a stream. It release the
3216 * attached SPOE context. */
3217static void
3218spoe_stop(struct stream *s, struct filter *filter)
3219{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003220 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3221 (int)now.tv_sec, (int)now.tv_usec,
3222 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3223 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003224 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003225}
3226
Christopher Fauletf7a30922016-11-10 15:04:51 +01003227
3228/*
3229 * Called when the stream is woken up because of expired timer.
3230 */
3231static void
3232spoe_check_timeouts(struct stream *s, struct filter *filter)
3233{
3234 struct spoe_context *ctx = filter->ctx;
3235
Christopher Fauletac580602018-03-20 16:09:20 +01003236 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003237 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003238}
3239
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003240/* Called when we are ready to filter data on a channel */
3241static int
3242spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3243{
3244 struct spoe_context *ctx = filter->ctx;
3245 int ret = 1;
3246
3247 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3248 " - ctx-flags=0x%08x\n",
3249 (int)now.tv_sec, (int)now.tv_usec,
3250 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3251 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3252
Christopher Fauletb067b062017-01-04 16:39:11 +01003253 if (ctx->state == SPOE_CTX_ST_NONE)
3254 goto out;
3255
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 if (!(chn->flags & CF_ISRESP)) {
3257 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3258 chn->analysers |= AN_REQ_INSPECT_FE;
3259 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3260 chn->analysers |= AN_REQ_INSPECT_BE;
3261
3262 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3263 goto out;
3264
3265 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003266 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003267 if (!ret)
3268 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003269 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3270 }
3271 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003272 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 chn->analysers |= AN_RES_INSPECT;
3274
3275 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3276 goto out;
3277
Christopher Faulet8ef75252017-02-20 22:56:03 +01003278 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003279 if (!ret) {
3280 channel_dont_read(chn);
3281 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003282 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003283 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003284 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003285 }
3286
3287 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003288 return ret;
3289}
3290
3291/* Called before a processing happens on a given channel */
3292static int
3293spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3294 struct channel *chn, unsigned an_bit)
3295{
3296 struct spoe_context *ctx = filter->ctx;
3297 int ret = 1;
3298
3299 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3300 " - ctx-flags=0x%08x - ana=0x%08x\n",
3301 (int)now.tv_sec, (int)now.tv_usec,
3302 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3303 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3304 ctx->flags, an_bit);
3305
Christopher Fauletb067b062017-01-04 16:39:11 +01003306 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003307 goto out;
3308
3309 switch (an_bit) {
3310 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003311 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003312 break;
3313 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003314 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003315 break;
3316 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003317 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003318 break;
3319 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003320 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003321 break;
3322 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003323 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003324 break;
3325 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003326 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003327 break;
3328 }
3329
3330 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003331 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003332 channel_dont_read(chn);
3333 channel_dont_close(chn);
3334 }
3335 return ret;
3336}
3337
3338/* Called when the filtering on the channel ends. */
3339static int
3340spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3341{
3342 struct spoe_context *ctx = filter->ctx;
3343
3344 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3345 " - ctx-flags=0x%08x\n",
3346 (int)now.tv_sec, (int)now.tv_usec,
3347 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3348 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3349
3350 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003351 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003352 }
3353
3354 return 1;
3355}
3356
3357/********************************************************************
3358 * Functions that manage the filter initialization
3359 ********************************************************************/
3360struct flt_ops spoe_ops = {
3361 /* Manage SPOE filter, called for each filter declaration */
3362 .init = spoe_init,
3363 .deinit = spoe_deinit,
3364 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003365 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366
3367 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003368 .attach = spoe_start,
3369 .detach = spoe_stop,
3370 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371
3372 /* Handle channels activity */
3373 .channel_start_analyze = spoe_start_analyze,
3374 .channel_pre_analyze = spoe_chn_pre_analyze,
3375 .channel_end_analyze = spoe_end_analyze,
3376};
3377
3378
3379static int
3380cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3381{
3382 const char *err;
3383 int i, err_code = 0;
3384
3385 if ((cfg_scope == NULL && curengine != NULL) ||
3386 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003387 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003388 goto out;
3389
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003390 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003392 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3393 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 err_code |= ERR_ALERT | ERR_ABORT;
3395 goto out;
3396 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003397 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3398 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 goto out;
3400 }
3401
3402 err = invalid_char(args[1]);
3403 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003404 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3405 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003406 err_code |= ERR_ALERT | ERR_ABORT;
3407 goto out;
3408 }
3409
3410 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003411 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3412 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003413 err_code |= ERR_ALERT | ERR_ABORT;
3414 goto out;
3415 }
3416 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003417 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003418 err_code |= ERR_ALERT | ERR_ABORT;
3419 goto out;
3420 }
3421
3422 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003423
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003424 curagent->conf.file = strdup(file);
3425 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003426
3427 curagent->timeout.hello = TICK_ETERNITY;
3428 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003429 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003430
Christopher Fauleta1cda022016-12-21 08:58:06 +01003431 curagent->var_pfx = NULL;
3432 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003433 curagent->var_t_process = NULL;
3434 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003435 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003436 curagent->cps_max = 0;
3437 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003438 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003439 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440
3441 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003442 LIST_INIT(&curagent->events[i]);
3443 LIST_INIT(&curagent->groups);
3444 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003446 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003447 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3449 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450 err_code |= ERR_ALERT | ERR_FATAL;
3451 goto out;
3452 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003453 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003455 free(curagent->b.name);
3456 curagent->b.name = strdup(args[1]);
3457 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003458 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 int cur_arg = 1;
3460 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003461 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003462
Christopher Faulet11610f32017-09-21 10:23:10 +02003463 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003464 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003465 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3466 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003467 err_code |= ERR_ALERT | ERR_FATAL;
3468 goto out;
3469 }
3470 }
3471
Christopher Faulet11610f32017-09-21 10:23:10 +02003472 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003473 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003474 err_code |= ERR_ALERT | ERR_ABORT;
3475 goto out;
3476 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003477 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003478 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003479 cur_arg++;
3480 }
3481 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003482 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003483 int cur_arg = 1;
3484 while (*args[cur_arg]) {
3485 struct spoe_placeholder *ph = NULL;
3486
3487 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003488 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003489 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3490 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003491 err_code |= ERR_ALERT | ERR_FATAL;
3492 goto out;
3493 }
3494 }
3495
3496 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003497 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003498 err_code |= ERR_ALERT | ERR_ABORT;
3499 goto out;
3500 }
3501 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003502 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003503 cur_arg++;
3504 }
3505 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003506 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003507 unsigned int *tv = NULL;
3508 const char *res;
3509 unsigned timeout;
3510
3511 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003512 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3513 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003514 err_code |= ERR_ALERT | ERR_FATAL;
3515 goto out;
3516 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003517 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3518 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003519 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003520 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003521 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003522 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003523 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003524 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003525 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003526 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3527 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003528 err_code |= ERR_ALERT | ERR_FATAL;
3529 goto out;
3530 }
3531 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003532 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3533 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003534 err_code |= ERR_ALERT | ERR_FATAL;
3535 goto out;
3536 }
3537 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003538 if (res == PARSE_TIME_OVER) {
3539 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3540 file, linenum, args[2], args[0], args[1]);
3541 err_code |= ERR_ALERT | ERR_FATAL;
3542 goto out;
3543 }
3544 else if (res == PARSE_TIME_UNDER) {
3545 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3546 file, linenum, args[2], args[0], args[1]);
3547 err_code |= ERR_ALERT | ERR_FATAL;
3548 goto out;
3549 }
3550 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003551 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3552 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003553 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003554 goto out;
3555 }
3556 *tv = MS_TO_TICKS(timeout);
3557 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003558 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003559 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003560 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3561 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003562 err_code |= ERR_ALERT | ERR_FATAL;
3563 goto out;
3564 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003565
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003566 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003567 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003568 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003569 if (kwm == 1)
3570 curagent->flags &= ~SPOE_FL_PIPELINING;
3571 else
3572 curagent->flags |= SPOE_FL_PIPELINING;
3573 goto out;
3574 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003575 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003576 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003577 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003578 if (kwm == 1)
3579 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003580 else
3581 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003582 goto out;
3583 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003584 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003585 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3586 goto out;
3587 if (kwm == 1)
3588 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3589 else
3590 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3591 goto out;
3592 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003593 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003594 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3595 goto out;
3596 if (kwm == 1)
3597 curpxopts2 &= ~PR_O2_NOLOGNORM;
3598 else
3599 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003600 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003601 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003602
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003603 /* Following options does not support negation */
3604 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003605 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3606 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003607 err_code |= ERR_ALERT | ERR_FATAL;
3608 goto out;
3609 }
3610
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003611 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003612 char *tmp;
3613
3614 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003615 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3616 file, linenum, args[0],
3617 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003618 err_code |= ERR_ALERT | ERR_FATAL;
3619 goto out;
3620 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003621 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3622 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003623 tmp = args[2];
3624 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003625 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003626 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003627 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003628 err_code |= ERR_ALERT | ERR_FATAL;
3629 goto out;
3630 }
3631 tmp++;
3632 }
3633 curagent->var_pfx = strdup(args[2]);
3634 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003635 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003636 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3637 goto out;
3638 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3639 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003640 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003641 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003642 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003643 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3644 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003645 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003646 char *tmp;
3647
3648 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003649 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3650 file, linenum, args[0],
3651 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003652 err_code |= ERR_ALERT | ERR_FATAL;
3653 goto out;
3654 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003655 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3656 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003657 tmp = args[2];
3658 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003659 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003660 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003661 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003662 err_code |= ERR_ALERT | ERR_FATAL;
3663 goto out;
3664 }
3665 tmp++;
3666 }
3667 curagent->var_on_error = strdup(args[2]);
3668 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003669 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003670 char *tmp;
3671
3672 if (!*args[2]) {
3673 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3674 file, linenum, args[0],
3675 args[1]);
3676 err_code |= ERR_ALERT | ERR_FATAL;
3677 goto out;
3678 }
3679 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3680 goto out;
3681 tmp = args[2];
3682 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003683 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003684 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003685 file, linenum, args[0], args[1]);
3686 err_code |= ERR_ALERT | ERR_FATAL;
3687 goto out;
3688 }
3689 tmp++;
3690 }
3691 curagent->var_t_process = strdup(args[2]);
3692 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003693 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003694 char *tmp;
3695
3696 if (!*args[2]) {
3697 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3698 file, linenum, args[0],
3699 args[1]);
3700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
3703 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3704 goto out;
3705 tmp = args[2];
3706 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003707 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003708 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003709 file, linenum, args[0], args[1]);
3710 err_code |= ERR_ALERT | ERR_FATAL;
3711 goto out;
3712 }
3713 tmp++;
3714 }
3715 curagent->var_t_total = strdup(args[2]);
3716 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003717 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003718 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3719 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003720 err_code |= ERR_ALERT | ERR_FATAL;
3721 goto out;
3722 }
Christopher Faulet48026722016-11-16 15:01:12 +01003723 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003724 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003725 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003726 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3727 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003728 err_code |= ERR_ALERT | ERR_FATAL;
3729 goto out;
3730 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003731 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003732 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003733 curagent->cps_max = atol(args[1]);
3734 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003735 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003736 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003737 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3738 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003739 err_code |= ERR_ALERT | ERR_FATAL;
3740 goto out;
3741 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003742 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003743 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003744 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003745 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003746 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003747 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003748 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3749 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003750 err_code |= ERR_ALERT | ERR_FATAL;
3751 goto out;
3752 }
3753 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3754 goto out;
3755 curagent->max_frame_size = atol(args[1]);
3756 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3757 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003758 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3759 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003760 err_code |= ERR_ALERT | ERR_FATAL;
3761 goto out;
3762 }
3763 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003764 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003765 if (!*args[1]) {
3766 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3767 file, linenum, args[0]);
3768 err_code |= ERR_ALERT | ERR_FATAL;
3769 goto out;
3770 }
3771 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3772 goto out;
3773 curagent->max_fpa = atol(args[1]);
3774 if (curagent->max_fpa < 1) {
3775 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3776 file, linenum, args[0]);
3777 err_code |= ERR_ALERT | ERR_FATAL;
3778 goto out;
3779 }
3780 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003781 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003782 int cur_arg;
3783
3784 if (!*args[1]) {
3785 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3786 file, linenum, args[0]);
3787 err_code |= ERR_ALERT | ERR_FATAL;
3788 goto out;
3789 }
3790 cur_arg = 1;
3791 while (*args[cur_arg]) {
3792 struct spoe_var_placeholder *vph;
3793
3794 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3795 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3796 err_code |= ERR_ALERT | ERR_ABORT;
3797 goto out;
3798 }
3799 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003800 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003801 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3802 err_code |= ERR_ALERT | ERR_ABORT;
3803 goto out;
3804 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003805 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003806 cur_arg++;
3807 }
3808 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003809 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003810 char *errmsg = NULL;
3811
Emeric Brun9533a702021-04-02 10:13:43 +02003812 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003813 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3814 err_code |= ERR_ALERT | ERR_FATAL;
3815 goto out;
3816 }
3817 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003818 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003819 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3820 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003821 err_code |= ERR_ALERT | ERR_FATAL;
3822 goto out;
3823 }
3824 out:
3825 return err_code;
3826}
Christopher Faulet11610f32017-09-21 10:23:10 +02003827static int
3828cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3829{
3830 struct spoe_group *grp;
3831 const char *err;
3832 int err_code = 0;
3833
3834 if ((cfg_scope == NULL && curengine != NULL) ||
3835 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003836 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003837 goto out;
3838
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003839 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003840 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003841 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3842 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003843 err_code |= ERR_ALERT | ERR_ABORT;
3844 goto out;
3845 }
3846 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3847 err_code |= ERR_ABORT;
3848 goto out;
3849 }
3850
3851 err = invalid_char(args[1]);
3852 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003853 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3854 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003855 err_code |= ERR_ALERT | ERR_ABORT;
3856 goto out;
3857 }
3858
3859 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003860 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3862 " name as another one declared at %s:%d.\n",
3863 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003864 err_code |= ERR_ALERT | ERR_FATAL;
3865 goto out;
3866 }
3867 }
3868
3869 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003870 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003871 err_code |= ERR_ALERT | ERR_ABORT;
3872 goto out;
3873 }
3874
3875 curgrp->id = strdup(args[1]);
3876 curgrp->conf.file = strdup(file);
3877 curgrp->conf.line = linenum;
3878 LIST_INIT(&curgrp->phs);
3879 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003880 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003881 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003882 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003883 int cur_arg = 1;
3884 while (*args[cur_arg]) {
3885 struct spoe_placeholder *ph = NULL;
3886
3887 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003888 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003889 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3890 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003891 err_code |= ERR_ALERT | ERR_FATAL;
3892 goto out;
3893 }
3894 }
3895
3896 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003897 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003898 err_code |= ERR_ALERT | ERR_ABORT;
3899 goto out;
3900 }
3901 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003902 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003903 cur_arg++;
3904 }
3905 }
3906 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003907 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3908 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003909 err_code |= ERR_ALERT | ERR_FATAL;
3910 goto out;
3911 }
3912 out:
3913 return err_code;
3914}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003915
3916static int
3917cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3918{
3919 struct spoe_message *msg;
3920 struct spoe_arg *arg;
3921 const char *err;
3922 char *errmsg = NULL;
3923 int err_code = 0;
3924
3925 if ((cfg_scope == NULL && curengine != NULL) ||
3926 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003927 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003928 goto out;
3929
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003930 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003931 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003932 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3933 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934 err_code |= ERR_ALERT | ERR_ABORT;
3935 goto out;
3936 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003937 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3938 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003939 goto out;
3940 }
3941
3942 err = invalid_char(args[1]);
3943 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003944 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3945 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 err_code |= ERR_ALERT | ERR_ABORT;
3947 goto out;
3948 }
3949
3950 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003951 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003952 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3953 " name as another one declared at %s:%d.\n",
3954 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003955 err_code |= ERR_ALERT | ERR_FATAL;
3956 goto out;
3957 }
3958 }
3959
3960 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003961 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003962 err_code |= ERR_ALERT | ERR_ABORT;
3963 goto out;
3964 }
3965
3966 curmsg->id = strdup(args[1]);
3967 curmsg->id_len = strlen(curmsg->id);
3968 curmsg->event = SPOE_EV_NONE;
3969 curmsg->conf.file = strdup(file);
3970 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003971 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003972 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003973 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003974 LIST_INIT(&curmsg->by_evt);
3975 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003976 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003977 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003978 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003979 int cur_arg = 1;
3980
3981 curproxy->conf.args.ctx = ARGC_SPOE;
3982 curproxy->conf.args.file = file;
3983 curproxy->conf.args.line = linenum;
3984 while (*args[cur_arg]) {
3985 char *delim = strchr(args[cur_arg], '=');
3986 int idx = 0;
3987
3988 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003989 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003990 err_code |= ERR_ALERT | ERR_ABORT;
3991 goto out;
3992 }
3993
3994 if (!delim) {
3995 arg->name = NULL;
3996 arg->name_len = 0;
3997 delim = args[cur_arg];
3998 }
3999 else {
4000 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
4001 arg->name_len = delim - args[cur_arg];
4002 delim++;
4003 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01004004 arg->expr = sample_parse_expr((char*[]){delim, NULL},
4005 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01004006 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004007 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004008 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004009 err_code |= ERR_ALERT | ERR_FATAL;
4010 free(arg->name);
4011 free(arg);
4012 goto out;
4013 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004014 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004015 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004016 cur_arg++;
4017 }
4018 curproxy->conf.args.file = NULL;
4019 curproxy->conf.args.line = 0;
4020 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004021 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004022 err = invalid_char(args[1]);
4023 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004024 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4025 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004026 err_code |= ERR_ALERT | ERR_FATAL;
4027 goto out;
4028 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004029 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004030 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004031 "logical disjunction within a condition.\n",
4032 file, linenum, args[1]);
4033 err_code |= ERR_ALERT | ERR_FATAL;
4034 goto out;
4035 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004036 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004037 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4038 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004039 err_code |= ERR_ALERT | ERR_FATAL;
4040 goto out;
4041 }
4042 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004043 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004044 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004045 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004046 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004047 goto out;
4048 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004049 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4050 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004051
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004052 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004054 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4056
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004057 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004059 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004060 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004061 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004062 curmsg->event = SPOE_EV_ON_TCP_RSP;
4063
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004064 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004065 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004066 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004067 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004068 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004069 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4070 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004071 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004072 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004073 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004074 goto out;
4075 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004076
4077 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4078 struct acl_cond *cond;
4079
4080 cond = build_acl_cond(file, linenum, &curmsg->acls,
4081 curproxy, (const char **)args+2,
4082 &errmsg);
4083 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004084 ha_alert("parsing [%s:%d] : error detected while "
4085 "parsing an 'event %s' condition : %s.\n",
4086 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004087 err_code |= ERR_ALERT | ERR_FATAL;
4088 goto out;
4089 }
4090 curmsg->cond = cond;
4091 }
4092 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004093 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4094 "or 'unless' followed by a condition but found '%s'.\n",
4095 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004096 err_code |= ERR_ALERT | ERR_FATAL;
4097 goto out;
4098 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004099 }
4100 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004101 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4102 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004103 err_code |= ERR_ALERT | ERR_FATAL;
4104 goto out;
4105 }
4106 out:
4107 free(errmsg);
4108 return err_code;
4109}
4110
4111/* Return -1 on error, else 0 */
4112static int
4113parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4114 struct flt_conf *fconf, char **err, void *private)
4115{
4116 struct list backup_sections;
4117 struct spoe_config *conf;
4118 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004119 struct spoe_group *grp, *grpback;
4120 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004121 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004122 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004123 char *file = NULL, *engine = NULL;
4124 int ret, pos = *cur_arg + 1;
4125
Christopher Faulet84c844e2018-03-23 14:37:14 +01004126 LIST_INIT(&curmsgs);
4127 LIST_INIT(&curgrps);
4128 LIST_INIT(&curmphs);
4129 LIST_INIT(&curgphs);
4130 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004131 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004132 curpxopts = 0;
4133 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004134
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004135 conf = calloc(1, sizeof(*conf));
4136 if (conf == NULL) {
4137 memprintf(err, "%s: out of memory", args[*cur_arg]);
4138 goto error;
4139 }
4140 conf->proxy = px;
4141
4142 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004143 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004144 if (!*args[pos+1]) {
4145 memprintf(err, "'%s' : '%s' option without value",
4146 args[*cur_arg], args[pos]);
4147 goto error;
4148 }
4149 file = args[pos+1];
4150 pos += 2;
4151 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004152 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004153 if (!*args[pos+1]) {
4154 memprintf(err, "'%s' : '%s' option without value",
4155 args[*cur_arg], args[pos]);
4156 goto error;
4157 }
4158 engine = args[pos+1];
4159 pos += 2;
4160 }
4161 else {
4162 memprintf(err, "unknown keyword '%s'", args[pos]);
4163 goto error;
4164 }
4165 }
4166 if (file == NULL) {
4167 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4168 goto error;
4169 }
4170
4171 /* backup sections and register SPOE sections */
4172 LIST_INIT(&backup_sections);
4173 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004174 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4175 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004176 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004177
4178 /* Parse SPOE filter configuration file */
4179 curengine = engine;
4180 curproxy = px;
4181 curagent = NULL;
4182 curmsg = NULL;
4183 ret = readcfgfile(file);
4184 curproxy = NULL;
4185
4186 /* unregister SPOE sections and restore previous sections */
4187 cfg_unregister_sections();
4188 cfg_restore_sections(&backup_sections);
4189
4190 if (ret == -1) {
4191 memprintf(err, "Could not open configuration file %s : %s",
4192 file, strerror(errno));
4193 goto error;
4194 }
4195 if (ret & (ERR_ABORT|ERR_FATAL)) {
4196 memprintf(err, "Error(s) found in configuration file %s", file);
4197 goto error;
4198 }
4199
4200 /* Check SPOE agent */
4201 if (curagent == NULL) {
4202 memprintf(err, "No SPOE agent found in file %s", file);
4203 goto error;
4204 }
4205 if (curagent->b.name == NULL) {
4206 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4207 curagent->id, curagent->conf.file, curagent->conf.line);
4208 goto error;
4209 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004210 if (curagent->timeout.hello == TICK_ETERNITY ||
4211 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004212 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004213 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4214 " | While not properly invalid, you will certainly encounter various problems\n"
4215 " | with such a configuration. To fix this, please ensure that all following\n"
4216 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4217 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004218 }
4219 if (curagent->var_pfx == NULL) {
4220 char *tmp = curagent->id;
4221
4222 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004223 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004224 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4225 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4226 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4227 goto error;
4228 }
4229 tmp++;
4230 }
4231 curagent->var_pfx = strdup(curagent->id);
4232 }
4233
Christopher Fauletb7426d12018-03-21 14:12:17 +01004234 if (curagent->var_on_error) {
4235 struct arg arg;
4236
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004237 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004238 curagent->var_pfx, curagent->var_on_error);
4239
4240 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004241 arg.data.str.area = trash.area;
4242 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004243 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004244 if (!vars_check_arg(&arg, err)) {
4245 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4246 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4247 goto error;
4248 }
4249 }
4250
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004251 if (curagent->var_t_process) {
4252 struct arg arg;
4253
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004254 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004255 curagent->var_pfx, curagent->var_t_process);
4256
4257 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004258 arg.data.str.area = trash.area;
4259 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004260 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004261 if (!vars_check_arg(&arg, err)) {
4262 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4263 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4264 goto error;
4265 }
4266 }
4267
4268 if (curagent->var_t_total) {
4269 struct arg arg;
4270
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004271 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004272 curagent->var_pfx, curagent->var_t_total);
4273
4274 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004275 arg.data.str.area = trash.area;
4276 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004277 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004278 if (!vars_check_arg(&arg, err)) {
4279 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4280 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4281 goto error;
4282 }
4283 }
4284
Christopher Faulet11610f32017-09-21 10:23:10 +02004285 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004286 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4287 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004288 goto finish;
4289 }
4290
Christopher Faulet11610f32017-09-21 10:23:10 +02004291 /* Replace placeholders by the corresponding messages for the SPOE
4292 * agent */
4293 list_for_each_entry(ph, &curmphs, list) {
4294 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004295 struct spoe_arg *arg;
4296 unsigned int where;
4297
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004298 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004299 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4300 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4301 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4302 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4303 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4304 }
4305 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4306 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4307 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004308 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4309 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004310 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004311 }
4312 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004313 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4314 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004315 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004316 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004317
4318 where = 0;
4319 switch (msg->event) {
4320 case SPOE_EV_ON_CLIENT_SESS:
4321 where |= SMP_VAL_FE_CON_ACC;
4322 break;
4323
4324 case SPOE_EV_ON_TCP_REQ_FE:
4325 where |= SMP_VAL_FE_REQ_CNT;
4326 break;
4327
4328 case SPOE_EV_ON_HTTP_REQ_FE:
4329 where |= SMP_VAL_FE_HRQ_HDR;
4330 break;
4331
4332 case SPOE_EV_ON_TCP_REQ_BE:
4333 if (px->cap & PR_CAP_FE)
4334 where |= SMP_VAL_FE_REQ_CNT;
4335 if (px->cap & PR_CAP_BE)
4336 where |= SMP_VAL_BE_REQ_CNT;
4337 break;
4338
4339 case SPOE_EV_ON_HTTP_REQ_BE:
4340 if (px->cap & PR_CAP_FE)
4341 where |= SMP_VAL_FE_HRQ_HDR;
4342 if (px->cap & PR_CAP_BE)
4343 where |= SMP_VAL_BE_HRQ_HDR;
4344 break;
4345
4346 case SPOE_EV_ON_SERVER_SESS:
4347 where |= SMP_VAL_BE_SRV_CON;
4348 break;
4349
4350 case SPOE_EV_ON_TCP_RSP:
4351 if (px->cap & PR_CAP_FE)
4352 where |= SMP_VAL_FE_RES_CNT;
4353 if (px->cap & PR_CAP_BE)
4354 where |= SMP_VAL_BE_RES_CNT;
4355 break;
4356
4357 case SPOE_EV_ON_HTTP_RSP:
4358 if (px->cap & PR_CAP_FE)
4359 where |= SMP_VAL_FE_HRS_HDR;
4360 if (px->cap & PR_CAP_BE)
4361 where |= SMP_VAL_BE_HRS_HDR;
4362 break;
4363
4364 default:
4365 break;
4366 }
4367
4368 list_for_each_entry(arg, &msg->args, list) {
4369 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004370 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004371 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004372 "none of which is available here ('%s')",
4373 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004374 sample_ckp_names(arg->expr->fetch->use),
4375 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004376 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004377 }
4378 }
4379
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004380 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004381 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004382 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004383 }
4384 }
4385 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004386 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004387 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004388 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004389 continue;
4390 }
4391
Christopher Faulet11610f32017-09-21 10:23:10 +02004392 /* Replace placeholders by the corresponding groups for the SPOE
4393 * agent */
4394 list_for_each_entry(ph, &curgphs, list) {
4395 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004396 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004397 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004398 LIST_DELETE(&grp->list);
4399 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004400 goto next_aph;
4401 }
4402 }
4403 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4404 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4405 goto error;
4406 next_aph:
4407 continue;
4408 }
4409
4410 /* Replace placeholders by the corresponding message for each SPOE
4411 * group of the SPOE agent */
4412 list_for_each_entry(grp, &curagent->groups, list) {
4413 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4414 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004415 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004416 if (msg->group != NULL) {
4417 memprintf(err, "SPOE message '%s' already belongs to "
4418 "the SPOE group '%s' declare at %s:%d",
4419 msg->id, msg->group->id,
4420 msg->group->conf.file,
4421 msg->group->conf.line);
4422 goto error;
4423 }
4424
4425 /* Scope for arguments are not checked for now. We will check
4426 * them only if a rule use the corresponding SPOE group. */
4427 msg->agent = curagent;
4428 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004429 LIST_DELETE(&ph->list);
4430 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004431 goto next_mph_grp;
4432 }
4433 }
4434 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4435 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4436 goto error;
4437 next_mph_grp:
4438 continue;
4439 }
4440 }
4441
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004442 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004443 /* move curmsgs to the agent message list */
4444 curmsgs.n->p = &curagent->messages;
4445 curmsgs.p->n = &curagent->messages;
4446 curagent->messages = curmsgs;
4447 LIST_INIT(&curmsgs);
4448
Christopher Faulet7ee86672017-09-19 11:08:28 +02004449 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004450 conf->agent = curagent;
Christopher Faulet434b8522021-08-02 17:51:01 +02004451 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004452
4453 /* Start agent's proxy initialization here. It will be finished during
4454 * the filter init. */
4455 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4456 init_new_proxy(&conf->agent_fe);
4457 conf->agent_fe.id = conf->agent->id;
4458 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004459 conf->agent_fe.options |= curpxopts;
4460 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004461
4462 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004463 LIST_DELETE(&logsrv->list);
4464 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004465 }
4466
Christopher Faulet11610f32017-09-21 10:23:10 +02004467 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004468 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004469 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004470 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004471 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004472 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004473 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004474 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004475 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4476 struct arg arg;
4477
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004478 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004479 curagent->var_pfx, vph->name);
4480
4481 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004482 arg.data.str.area = trash.area;
4483 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004484 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004485 if (!vars_check_arg(&arg, err)) {
4486 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4487 curagent->id, curagent->var_pfx, vph->name, *err);
4488 goto error;
4489 }
4490
Willy Tarreau2b718102021-04-21 07:32:39 +02004491 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004492 free(vph->name);
4493 free(vph);
4494 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004495 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004496 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004497 spoe_release_group(grp);
4498 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004499 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004500 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004501 fconf->ops = &spoe_ops;
4502 fconf->conf = conf;
4503 return 0;
4504
4505 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004506 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004507 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004508 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004509 spoe_release_placeholder(ph);
4510 }
4511 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004512 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004513 spoe_release_placeholder(ph);
4514 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004515 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004516 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004517 free(vph->name);
4518 free(vph);
4519 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004520 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004521 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004522 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004523 }
4524 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004525 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004526 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004527 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004528 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004529 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004530 free(logsrv);
4531 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004532 free(conf);
4533 return -1;
4534}
4535
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004536/* Send message of a SPOE group. This is the action_ptr callback of a rule
4537 * associated to a "send-spoe-group" action.
4538 *
Christopher Faulet13403762019-12-13 09:01:57 +01004539 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4540 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004541static enum act_return
4542spoe_send_group(struct act_rule *rule, struct proxy *px,
4543 struct session *sess, struct stream *s, int flags)
4544{
4545 struct filter *filter;
4546 struct spoe_agent *agent = NULL;
4547 struct spoe_group *group = NULL;
4548 struct spoe_context *ctx = NULL;
4549 int ret, dir;
4550
4551 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4552 if (filter->config == rule->arg.act.p[0]) {
4553 agent = rule->arg.act.p[2];
4554 group = rule->arg.act.p[3];
4555 ctx = filter->ctx;
4556 break;
4557 }
4558 }
4559 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004560 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004561 if (ctx->state == SPOE_CTX_ST_NONE)
4562 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004563
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004564 switch (rule->from) {
4565 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4566 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4567 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4568 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4569 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4570 default:
4571 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4572 " - internal error while execute spoe-send-group\n",
4573 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4574 __FUNCTION__, s);
4575 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4576 agent->id);
4577 return ACT_RET_CONT;
4578 }
4579
4580 ret = spoe_process_group(s, ctx, group, dir);
4581 if (ret == 1)
4582 return ACT_RET_CONT;
4583 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004584 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004585 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4586 " - failed to process group '%s': interrupted by caller\n",
4587 (int)now.tv_sec, (int)now.tv_usec,
4588 agent->id, __FUNCTION__, s, group->id);
4589 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004590 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004591 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004592 return ACT_RET_CONT;
4593 }
4594 return ACT_RET_YIELD;
4595 }
4596 else
Christopher Faulet13403762019-12-13 09:01:57 +01004597 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004598}
4599
4600/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4601 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4602 * action should be:
4603 *
4604 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4605 *
4606 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4607 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4608 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4609 * group.
4610 *
4611 * The function returns 1 in success case, otherwise, it returns 0 and err is
4612 * filled.
4613 */
4614static int
4615check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4616{
4617 struct flt_conf *fconf;
4618 struct spoe_config *conf;
4619 struct spoe_agent *agent = NULL;
4620 struct spoe_group *group;
4621 struct spoe_message *msg;
4622 char *engine_id = rule->arg.act.p[0];
4623 char *group_id = rule->arg.act.p[1];
4624 unsigned int where = 0;
4625
4626 switch (rule->from) {
4627 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4628 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4629 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4630 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4631 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4632 default:
4633 memprintf(err,
4634 "internal error, unexpected rule->from=%d, please report this bug!",
4635 rule->from);
4636 goto error;
4637 }
4638
4639 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4640 * <px> */
4641 list_for_each_entry(fconf, &px->filter_configs, list) {
4642 conf = fconf->conf;
4643
4644 /* This is not an SPOE filter */
4645 if (fconf->id != spoe_filter_id)
4646 continue;
4647
4648 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004649 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004650 agent = conf->agent;
4651 break;
4652 }
4653 }
4654 if (agent == NULL) {
4655 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4656 engine_id, group_id);
4657 goto error;
4658 }
4659
4660 /* Try to find the right group */
4661 list_for_each_entry(group, &agent->groups, list) {
4662 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004663 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004664 break;
4665 }
4666 if (&group->list == &agent->groups) {
4667 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4668 group_id, engine_id);
4669 goto error;
4670 }
4671
4672 /* Ok, we found the group, we need to check messages and their
4673 * arguments */
4674 list_for_each_entry(msg, &group->messages, by_grp) {
4675 struct spoe_arg *arg;
4676
4677 list_for_each_entry(arg, &msg->args, list) {
4678 if (!(arg->expr->fetch->val & where)) {
4679 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4680 "some args extract information from '%s',"
4681 "none of which is available here ('%s')",
4682 msg->id, group->id, msg->conf.file, msg->conf.line,
4683 sample_ckp_names(arg->expr->fetch->use),
4684 sample_ckp_names(where));
4685 goto error;
4686 }
4687 }
4688 }
4689
4690 free(engine_id);
4691 free(group_id);
4692 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4693 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4694 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4695 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4696 return 1;
4697
4698 error:
4699 free(engine_id);
4700 free(group_id);
4701 return 0;
4702}
4703
4704/* Parse 'send-spoe-group' action following the format:
4705 *
4706 * ... send-spoe-group <engine-id> <group-id>
4707 *
4708 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4709 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4710 * ids are saved and used later, when the rule will be checked.
4711 */
4712static enum act_parse_ret
4713parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4714 struct act_rule *rule, char **err)
4715{
4716 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4717 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4718 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4719 return ACT_RET_PRS_ERR;
4720 }
4721 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4722 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4723
4724 (*orig_arg) += 2;
4725
4726 rule->action = ACT_CUSTOM;
4727 rule->action_ptr = spoe_send_group;
4728 rule->check_ptr = check_send_spoe_group;
4729 return ACT_RET_PRS_OK;
4730}
4731
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004732
4733/* Declare the filter parser for "spoe" keyword */
4734static struct flt_kw_list flt_kws = { "SPOE", { }, {
4735 { "spoe", parse_spoe_flt, NULL },
4736 { NULL, NULL, NULL },
4737 }
4738};
4739
Willy Tarreau0108d902018-11-25 19:14:37 +01004740INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4741
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004742/* Delcate the action parser for "spoe-action" keyword */
4743static struct action_kw_list tcp_req_action_kws = { { }, {
4744 { "send-spoe-group", parse_send_spoe_group },
4745 { /* END */ },
4746 }
4747};
Willy Tarreau0108d902018-11-25 19:14:37 +01004748
4749INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4750
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004751static struct action_kw_list tcp_res_action_kws = { { }, {
4752 { "send-spoe-group", parse_send_spoe_group },
4753 { /* END */ },
4754 }
4755};
Willy Tarreau0108d902018-11-25 19:14:37 +01004756
4757INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4758
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004759static struct action_kw_list http_req_action_kws = { { }, {
4760 { "send-spoe-group", parse_send_spoe_group },
4761 { /* END */ },
4762 }
4763};
Willy Tarreau0108d902018-11-25 19:14:37 +01004764
4765INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4766
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004767static struct action_kw_list http_res_action_kws = { { }, {
4768 { "send-spoe-group", parse_send_spoe_group },
4769 { /* END */ },
4770 }
4771};
4772
Willy Tarreau0108d902018-11-25 19:14:37 +01004773INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);