blob: beee7036b010e47a643dbcc4b3bcea542b95eccd [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>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020023#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020024#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020026#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020027#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020028#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020029#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020031#include <haproxy/sc_strm.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020032#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010034#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020035#include <haproxy/spoe.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020036#include <haproxy/stconn.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 }
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200144 free_acl_cond(msg->cond);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200145 free(msg);
146}
147
148static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200149spoe_release_group(struct spoe_group *grp)
150{
151 if (!grp)
152 return;
153 free(grp->id);
154 free(grp->conf.file);
155 free(grp);
156}
157
158static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100159spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200160{
Christopher Faulet11610f32017-09-21 10:23:10 +0200161 struct spoe_message *msg, *msgback;
162 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200163 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200164
165 if (!agent)
166 return;
167 free(agent->id);
168 free(agent->conf.file);
169 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100170 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100171 free(agent->var_t_process);
172 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200173 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200174 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200175 spoe_release_message(msg);
176 }
177 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200178 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200179 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200180 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100181 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200182 for (i = 0; i < global.nbthread; ++i) {
183 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200185 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100186 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500250 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100255 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800256 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100257}
258
Willy Tarreauaaebcae2023-04-27 11:54:11 +0200259/* set/add to <t> the elapsed time since <since> and now */
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100260static inline void
Willy Tarreauaaebcae2023-04-27 11:54:11 +0200261spoe_update_stat_time(ullong *since, long *t)
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100262{
263 if (*t == -1)
Willy Tarreau69530f52023-04-28 09:16:15 +0200264 *t = ns_to_ms(now_ns - *since);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100265 else
Willy Tarreau69530f52023-04-28 09:16:15 +0200266 *t += ns_to_ms(now_ns - *since);
Willy Tarreauaaebcae2023-04-27 11:54:11 +0200267 *since = 0;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100268}
269
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270/********************************************************************
271 * Functions that encode/decode SPOE frames
272 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/* Helper to get static string length, excluding the terminating null byte */
274#define SLEN(str) (sizeof(str)-1)
275
276/* Predefined key used in HELLO/DISCONNECT frames */
277#define SUPPORTED_VERSIONS_KEY "supported-versions"
278#define VERSION_KEY "version"
279#define MAX_FRAME_SIZE_KEY "max-frame-size"
280#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100281#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100282#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200283#define STATUS_CODE_KEY "status-code"
284#define MSG_KEY "message"
285
286struct spoe_version {
287 char *str;
288 int min;
289 int max;
290};
291
292/* All supported versions */
293static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200294 /* 1.0 is now unsupported because of a bug about frame's flags*/
295 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296 {NULL, 0, 0}
297};
298
299/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200300#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302/* Convert a string to a SPOE version value. The string must follow the format
303 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
304 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 const char *p, *end;
309 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 p = str;
312 end = str+len;
313 maj = min = 0;
314 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200315
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100317 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320 /* parse Major number, until the '.' */
321 while (*p != '.') {
322 if (p >= end || *p < '0' || *p > '9')
323 goto out;
324 maj *= 10;
325 maj += (*p - '0');
326 p++;
327 }
328
329 /* check Major version */
330 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331 goto out;
332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p++; /* skip the '.' */
334 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
335 goto out;
336
337 /* Parse Minor number */
338 while (p < end) {
339 if (*p < '0' || *p > '9')
340 break;
341 min *= 10;
342 min += (*p - '0');
343 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100345
346 /* check Minor number */
347 if (min > 999)
348 goto out;
349
350 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100351 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100352 p++;
353 if (p != end)
354 goto out;
355
356 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 out:
358 return vsn;
359}
360
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800362 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100365spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366{
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100368 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100369 char *p, *end;
370 unsigned int flags = SPOE_FRM_FL_FIN;
371 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 p = frame;
374 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 /* Set Frame type */
377 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100379 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200380 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100381 memcpy(p, (char *)&flags, 4);
382 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200383
384 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100385 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
388 * and "capabilities" */
389
390 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 sz = SLEN(SUPPORTED_VERSIONS_KEY);
392 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
393 goto too_big;
394
395 *p++ = SPOE_DATA_T_STR;
396 sz = SLEN(SUPPORTED_VERSIONS_VAL);
397 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
398 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200399
400 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100401 sz = SLEN(MAX_FRAME_SIZE_KEY);
402 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
403 goto too_big;
404
405 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200406 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 sz = SLEN(CAPABILITIES_KEY);
411 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
412 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
Christopher Faulet8ef75252017-02-20 22:56:03 +0100414 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100415 chk = get_trash_chunk();
416 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 memcpy(chk->area, "pipelining", 10);
418 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100419 }
420 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 if (chk->data) chk->area[chk->data++] = ',';
422 memcpy(chk->area+chk->data, "async", 5);
423 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100424 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100425 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 if (chk->data) chk->area[chk->data++] = ',';
427 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100428 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100429 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200430 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 goto too_big;
432
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500433 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200434 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100435 sz = SLEN(ENGINE_ID_KEY);
436 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
437 goto too_big;
438
439 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200440 sz = strlen(agent->rt[tid].engine_id);
441 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100442 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100443 }
444
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 return (p - frame);
446
447 too_big:
448 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
449 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200450}
451
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
453 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
454 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200455static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200457{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 const char *reason;
459 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100460 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 p = frame;
464 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 /* Set Frame type */
467 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100469 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200470 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100471 memcpy(p, (char *)&flags, 4);
472 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473
474 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 *p++ = 0; *p++ = 0;
476
477 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
478 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
480 /* There are 2 mandatory items: "status-code" and "message" */
481
482 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 sz = SLEN(STATUS_CODE_KEY);
484 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
485 goto too_big;
486
487 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200488 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 sz = SLEN(MSG_KEY);
493 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
494 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 /*Get the message corresponding to the status code */
497 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
498
499 *p++ = SPOE_DATA_T_STR;
500 sz = strlen(reason);
501 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
502 goto too_big;
503
504 return (p - frame);
505
506 too_big:
507 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
508 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509}
510
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
512 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
513 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100515spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100516 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 char *p, *end;
519 unsigned int stream_id, frame_id;
520 unsigned int flags = SPOE_FRM_FL_FIN;
521 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 p = frame;
524 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100526 stream_id = ctx->stream_id;
527 frame_id = ctx->frame_id;
528
529 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
530 /* The fragmentation is not supported by the applet */
531 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
532 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
533 return -1;
534 }
535 flags = ctx->frag_ctx.flags;
536 }
537
538 /* Set Frame type */
539 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
540
541 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200542 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100543 memcpy(p, (char *)&flags, 4);
544 p += 4;
545
546 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200547 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200549 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100550 goto too_big;
551
552 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200553 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100554 if (p + sz >= end)
555 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 p += sz;
558
559 return (p - frame);
560
561 too_big:
562 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
563 return 0;
564}
565
566/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
567 * returns the number of encoded bytes in the frame on success, 0 if an encoding
568 * error occurred and -1 if a fatal error occurred. */
569static int
570spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
571 char *frame, size_t size)
572{
573 char *p, *end;
574 unsigned int stream_id, frame_id;
575 unsigned int flags;
576 size_t sz;
577
578 p = frame;
579 end = frame+size;
580
Christopher Faulet8ef75252017-02-20 22:56:03 +0100581 /* <ctx> is null when the stream has aborted the processing of a
582 * fragmented frame. In this case, we must notify the corresponding
583 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100584 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100585 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100586 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
587 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
588 }
589 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100590 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100591 stream_id = ctx->stream_id;
592 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100593 }
594
Christopher Faulet8ef75252017-02-20 22:56:03 +0100595 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100596 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100598 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200599 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 memcpy(p, (char *)&flags, 4);
601 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200602
603 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200604 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200606 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200608
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 if (ctx == NULL)
610 goto end;
611
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200613 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 if (p + sz >= end)
615 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200616 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100618
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100619 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 return (p - frame);
621
622 too_big:
623 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
624 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200625}
626
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627/* Decode and process the HELLO frame sent by an agent. It returns the number of
628 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
629 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632{
Christopher Faulet305c6072017-02-23 16:17:53 +0100633 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
634 char *p, *end;
635 int vsn, max_frame_size;
636 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100637
638 p = frame;
639 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200640
641 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
643 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 if (size < 7 /* TYPE + METADATA */) {
648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
649 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650 }
651
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100652 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200654 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200656
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100657 /* Fragmentation is not supported for HELLO frame */
658 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100660 return -1;
661 }
662
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (*p != 0 || *(p+1) != 0) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
666 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669
670 /* There are 3 mandatory items: "version", "max-frame-size" and
671 * "capabilities" */
672
673 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100674 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 while (p < end) {
676 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200677 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200679
680 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 ret = spoe_decode_buffer(&p, end, &str, &sz);
682 if (ret == -1 || !sz) {
683 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
684 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200688 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 int i, type = *p++;
690
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
693 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
694 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
697 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
698 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 }
700
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 return -1;
705 }
706 for (i = 0; supported_versions[i].str != NULL; ++i) {
707 if (vsn >= supported_versions[i].min &&
708 vsn <= supported_versions[i].max)
709 break;
710 }
711 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 return -1;
714 }
715 }
716 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200717 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 +0100718 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719
720 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
722 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
723 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
724 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
726 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200728 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
730 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200731 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 if (sz < MIN_FRAME_SIZE ||
733 sz > SPOE_APPCTX(appctx)->max_frame_size) {
734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 max_frame_size = sz;
738 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200740 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100741 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742
743 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100744 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
745 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
746 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
749 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
750 return 0;
751 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752
Christopher Faulet8ef75252017-02-20 22:56:03 +0100753 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100754 char *delim;
755
756 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100757 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100758
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
760 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100761 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 flags |= SPOE_APPCTX_FL_PIPELINING;
763 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 else if (sz >= 5 && !strncmp(str, "async", 5)) {
765 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100766 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767 flags |= SPOE_APPCTX_FL_ASYNC;
768 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100769 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
770 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100771 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100772 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 /* Get the next comma or break */
776 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 delim++;
779 sz -= (delim - str);
780 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781 }
782 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200783 else {
784 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100785 if (spoe_skip_data(&p, end) == -1) {
786 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
787 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200788 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200789 }
790 }
791
792 /* Final checks */
793 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200795 return -1;
796 }
797 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200799 return -1;
800 }
Christopher Faulet11389012019-02-07 16:13:26 +0100801 if (!agent)
802 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
803 else {
804 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
805 flags &= ~SPOE_APPCTX_FL_PIPELINING;
806 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
807 flags &= ~SPOE_APPCTX_FL_ASYNC;
808 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809
Christopher Faulet42bfa462017-01-04 14:14:19 +0100810 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
811 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
812 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100813
814 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815}
816
817/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
818 * bytes on success, 0 if the frame can be ignored and -1 if an error
819 * occurred. */
820static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100824 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825
826 p = frame;
827 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828
829 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
831 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834
Christopher Faulet8ef75252017-02-20 22:56:03 +0100835 if (size < 7 /* TYPE + METADATA */) {
836 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
837 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838 }
839
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100840 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200842 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200844
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100845 /* Fragmentation is not supported for DISCONNECT frame */
846 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100847 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 return -1;
849 }
850
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 if (*p != 0 || *(p+1) != 0) {
853 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
854 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857
858 /* There are 2 mandatory items: "status-code" and "message" */
859
860 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 while (p < end) {
862 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200863 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
866 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 ret = spoe_decode_buffer(&p, end, &str, &sz);
868 if (ret == -1 || !sz) {
869 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
870 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871 }
872
873 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200874 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876
877 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
879 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
880 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
881 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
883 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200885 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
887 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
891
892 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200893 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 int type = *p++;
895
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
898 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
899 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 ret = spoe_decode_buffer(&p, end, &str, &sz);
902 if (ret == -1 || sz > 255) {
903 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
904 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
907 SPOE_APPCTX(appctx)->reason = str;
908 SPOE_APPCTX(appctx)->rlen = sz;
909#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910 }
911 else {
912 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 if (spoe_skip_data(&p, end) == -1) {
914 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
915 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
918 }
919
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921}
922
923
Christopher Fauleta1cda022016-12-21 08:58:06 +0100924/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 * success, 0 if the frame can be ignored and -1 if an error occurred. */
926static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100928 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100930 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100934 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100935
936 p = frame;
937 end = frame + size;
938 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939
940 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
942 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200945
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946 if (size < 7 /* TYPE + METADATA */) {
947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
948 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200949 }
950
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200953 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 /* Fragmentation is not supported for now */
957 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 return -1;
960 }
961
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200963 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100965 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200967 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100971
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100973 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200974 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100975 if ((*ctx)->stream_id == (unsigned int)stream_id &&
976 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100977 goto found;
978 }
979 }
980 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100981 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
982 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100984 goto found;
985 }
986 }
987
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100988 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
989 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
990 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
991
992 /* ABRT bit is set for an unfinished fragmented frame */
993 if (flags & SPOE_FRM_FL_ABRT) {
994 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100995 (*ctx)->state = SPOE_CTX_ST_ERROR;
996 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
997 /* Ignore the payload */
998 goto end;
999 }
1000 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1001 /* For now, we ignore the ack */
1002 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1003 return 0;
1004 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001005
Christopher Fauleta1cda022016-12-21 08:58:06 +01001006 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001007 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1008 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001009 " - stream-id=%u - frame-id=%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001010 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 __FUNCTION__, appctx,
1012 (unsigned int)stream_id, (unsigned int)frame_id);
1013
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001014 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001015 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1016 /* Report an error if we are waiting the ack for another frame,
1017 * but not if there is no longer frame waiting for a ack
1018 * (timeout)
1019 */
1020 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1021 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1022 return -1;
1023 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1024 SPOE_APPCTX(appctx)->cur_fpa = 0;
1025 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001026 return 0;
1027
1028 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1030 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001032 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001034
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001035 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001037 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1038 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001040
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 /* Transfer the buffer ownership to the SPOE context */
1042 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001043 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 (*ctx)->state = SPOE_CTX_ST_DONE;
1046
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001047 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 " - ACK frame received"
1050 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001051 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1053 (*ctx)->frame_id, flags);
1054 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001055}
1056
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057/* This function is used in cfgparse.c and declared in proto/checks.h. It
1058 * prepare the request to send to agents during a healthcheck. It returns 0 on
1059 * success and -1 if an error occurred. */
1060int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 struct appctx appctx;
1064 struct spoe_appctx spoe_appctx;
1065 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1066 size_t sz;
1067 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069 memset(&appctx, 0, sizeof(appctx));
1070 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072
Willy Tarreau23a24072022-05-05 20:18:44 +02001073 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001074 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1077 end = frame + MAX_FRAME_SIZE;
1078
1079 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1080 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 /* Add "healthcheck" K/V item */
1085 sz = SLEN(HEALTHCHECK_KEY);
1086 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1087 return -1;
1088 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 *len = frame - buf;
1091 sz = htonl(*len - 4);
1092 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097 return 0;
1098}
1099
1100/* This function is used in checks.c and declared in proto/checks.h. It decode
1101 * the response received from an agent during a healthcheck. It returns 0 on
1102 * success and -1 if an error occurred. */
1103int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 struct appctx appctx;
1107 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 memset(&appctx, 0, sizeof(appctx));
1110 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Willy Tarreau23a24072022-05-05 20:18:44 +02001112 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001113 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1116 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117 goto error;
1118 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001119 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1120 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121
1122 return 0;
1123
1124 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1126 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1127 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return -1;
1129}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001130
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001132 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133 * success. */
1134static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001137 int ret;
1138 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139
Christopher Faulet8ef75252017-02-20 22:56:03 +01001140 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1141 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142 netint = htonl(framesz);
1143 memcpy(buf, (char *)&netint, 4);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001144 ret = applet_putblk(appctx, buf, framesz+4);
Christopher Faulet5bacdae2024-04-18 08:58:55 +02001145 if (ret <= 0)
1146 return 1; /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147 return framesz;
1148}
1149
1150/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1151 * when the frame can be ignored, 1 to retry later and the frame length on
1152 * success. */
1153static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001154spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001156 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 int ret;
1158 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001160 ret = co_getblk(sc_oc(sc), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 if (ret > 0) {
1162 framesz = ntohl(netint);
Christopher Faulet6fe10602024-03-18 08:02:32 +01001163 if (framesz < 7) {
1164 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1165 return -1;
1166 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001167 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169 return -1;
1170 }
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001171 ret = co_getblk(sc_oc(sc), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 }
1173 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001174 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 }
1177 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 return -1; /* error */
1179 }
1180 return framesz;
1181}
1182
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001183/********************************************************************
1184 * Functions that manage the SPOE applet
1185 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001186static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001187spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001188{
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02001189 applet_will_consume(appctx);
Willy Tarreau4164eb92022-05-25 15:42:03 +02001190 applet_have_more_data(appctx);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001191 appctx_wakeup(appctx);
1192 return 1;
1193}
1194
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001195/* Callback function that catches applet timeouts. If a timeout occurred, we set
1196 * <appctx->st1> flag and the SPOE applet is woken up. */
1197static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001198spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001200 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001201
1202 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1203 if (tick_is_expired(task->expire, now_ms)) {
1204 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001205 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001206 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001207 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208 return task;
1209}
1210
Christopher Faulet69ebc302022-05-12 15:28:51 +02001211static int
1212spoe_init_appctx(struct appctx *appctx)
1213{
1214 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1215 struct spoe_agent *agent = spoe_appctx->agent;
1216 struct task *task;
1217 struct stream *s;
1218
1219 if ((task = task_new_here()) == NULL)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001220 goto out_error;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001221 task->process = spoe_process_appctx;
1222 task->context = appctx;
1223
1224 if (appctx_finalize_startup(appctx, &agent->spoe_conf->agent_fe, &BUF_NULL) == -1)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001225 goto out_free_task;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001226
1227 spoe_appctx->owner = appctx;
1228 spoe_appctx->task = task;
1229
1230 LIST_INIT(&spoe_appctx->buffer_wait.list);
1231 spoe_appctx->buffer_wait.target = appctx;
1232 spoe_appctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
1233
1234 s = appctx_strm(appctx);
1235 stream_set_backend(s, agent->b.be);
1236
1237 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001238 applet_need_more_data(appctx);
Christopher Faulet69ebc302022-05-12 15:28:51 +02001239
1240 s->do_log = NULL;
Christopher Faulet9a790f62023-03-16 14:40:03 +01001241 s->scb->flags |= SC_FL_RCV_ONCE;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001242
1243 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1244 LIST_APPEND(&agent->rt[tid].applets, &spoe_appctx->list);
1245 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1246 _HA_ATOMIC_INC(&agent->counters.applets);
1247
1248 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1249 task_wakeup(spoe_appctx->task, TASK_WOKEN_INIT);
1250 return 0;
1251 out_free_task:
1252 task_destroy(task);
1253 out_error:
1254 return -1;
1255}
1256
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001257/* Callback function that releases a SPOE applet. This happens when the
1258 * connection with the agent is closed. */
1259static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001261{
Christopher Faulet908628c2022-03-25 16:43:49 +01001262 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1263 struct spoe_agent *agent;
1264 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265
1266 if (spoe_appctx == NULL)
1267 return;
1268
Willy Tarreau23a24072022-05-05 20:18:44 +02001269 appctx->svcctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001270 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001271
1272 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001273 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275
Christopher Faulet8ef75252017-02-20 22:56:03 +01001276 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001277 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001278 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001279 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001280 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001282 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001283 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001284
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001286 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001287 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1288 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001289 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001290 agent->rt[tid].idles--;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001291 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1295 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001296 }
1297
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001299 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300
Christopher Faulet42a06622022-08-25 18:50:18 +02001301 /* Report an error to all streams in the appctx waiting queue */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001302 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001303 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001304 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001305 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001306 spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001307 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001308 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001309 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001310 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311 }
1312
Christopher Faulet42a06622022-08-25 18:50:18 +02001313 /* If the applet was processing a fragmented frame, report an error to
1314 * the corresponding stream. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001315 if (spoe_appctx->frag_ctx.ctx) {
1316 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001317 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001318 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001320 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1321 }
1322
Christopher Faulet42a06622022-08-25 18:50:18 +02001323 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1324 /* If there are still some running applets, remove reference on
1325 * the current one from streams in the async waiting queue. In
1326 * async mode, the ACK may be received from another appctx.
1327 */
Christopher Fauletcf181c72020-11-10 18:45:34 +01001328 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1329 if (ctx->spoe_appctx == spoe_appctx)
1330 ctx->spoe_appctx = NULL;
1331 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001332 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001333 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001334 else {
Christopher Faulet42a06622022-08-25 18:50:18 +02001335 /* It is the last running applet and the sending and async
1336 * waiting queues are not empty. So try to start a new applet if
1337 * HAproxy is not stopping. On success, we remove reference on
1338 * the current appctx from streams in the async waiting queue.
1339 * In async mode, the ACK may be received from another appctx.
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001340 */
1341 if (!stopping &&
1342 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
Christopher Faulet42a06622022-08-25 18:50:18 +02001343 spoe_create_appctx(agent->spoe_conf)) {
1344 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1345 if (ctx->spoe_appctx == spoe_appctx)
1346 ctx->spoe_appctx = NULL;
1347 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001348 goto end;
Christopher Faulet42a06622022-08-25 18:50:18 +02001349 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001350
Christopher Faulet42a06622022-08-25 18:50:18 +02001351 /* Otherwise, report an error to all streams in the sending and
1352 * async waiting queues.
1353 */
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001354 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1355 LIST_DELETE(&ctx->list);
1356 LIST_INIT(&ctx->list);
1357 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001358 spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue);
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001359 ctx->spoe_appctx = NULL;
1360 ctx->state = SPOE_CTX_ST_ERROR;
1361 ctx->status_code = (spoe_appctx->status_code + 0x100);
1362 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1363 }
1364 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1365 LIST_DELETE(&ctx->list);
1366 LIST_INIT(&ctx->list);
1367 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001368 spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting);
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001369 ctx->spoe_appctx = NULL;
1370 ctx->state = SPOE_CTX_ST_ERROR;
1371 ctx->status_code = (spoe_appctx->status_code + 0x100);
1372 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1373 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001375
1376 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001377 /* Release allocated memory */
1378 spoe_release_buffer(&spoe_appctx->buffer,
1379 &spoe_appctx->buffer_wait);
1380 pool_free(pool_head_spoe_appctx, spoe_appctx);
1381
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001382 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001383 agent->rt[tid].frame_size = agent->max_frame_size;
1384 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1385 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001386}
1387
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001390{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001391 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001392 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001393 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001395
Christopher Fauletd550d262023-03-31 11:04:34 +02001396 /* if the connection is not established, inform the stream that we want
1397 * to be notified whenever the connection completes.
1398 */
1399 if (sc_opposite(sc)->state < SC_ST_EST) {
1400 applet_need_more_data(appctx);
1401 se_need_remote_conn(appctx->sedesc);
1402 applet_have_more_data(appctx);
1403 goto stop;
1404 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001405
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001407 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1408 " - Connection timed out\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001409 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001410 __FUNCTION__, appctx);
1411 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Faulet42bfa462017-01-04 14:14:19 +01001415 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001416 SPOE_APPCTX(appctx)->task->expire =
1417 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418
Christopher Faulet8ef75252017-02-20 22:56:03 +01001419 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1420 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001421 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 ret = spoe_prepare_hahello_frame(appctx, frame,
1423 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001424 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001426
1427 switch (ret) {
1428 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 case 0: /* ignore => an error, cannot be ignored */
1430 goto exit;
1431
1432 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001433 goto stop;
1434
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 default:
1436 /* HELLO frame successfully sent, now wait for the
1437 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001438 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1439 goto next;
1440 }
1441
1442 next:
1443 return 0;
1444 stop:
1445 return 1;
1446 exit:
1447 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1448 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449}
1450
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001451static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001453{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001454 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001455 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001456 char *frame;
1457 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001458
Christopher Fauleta1cda022016-12-21 08:58:06 +01001459 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1461 " - Connection timed out\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001462 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001463 __FUNCTION__, appctx);
1464 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001465 goto exit;
1466 }
1467
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001468 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001469 ret = spoe_recv_frame(appctx, frame,
1470 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001471 if (ret > 1) {
1472 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1473 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1474 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001475 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001476 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001477 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001478 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001479
Christopher Fauleta1cda022016-12-21 08:58:06 +01001480 switch (ret) {
1481 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001483 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1484 goto next;
1485
1486 case 1: /* retry later */
1487 goto stop;
1488
1489 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001490 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001491 agent->rt[tid].idles++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001492 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001493 SPOE_APPCTX(appctx)->node.key = 0;
1494 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001495
1496 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001497 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001498 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001499 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001500
Christopher Fauleta1cda022016-12-21 08:58:06 +01001501 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001502 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001503 if (trash.data)
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001504 co_skip(sc_oc(sc), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505
1506 SPOE_APPCTX(appctx)->task->expire =
1507 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001508 return 0;
1509 stop:
1510 return 1;
1511 exit:
1512 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1513 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001514}
1515
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001516
Christopher Fauleta1cda022016-12-21 08:58:06 +01001517static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001518spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001519{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001520 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1521 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001522 char *frame, *buf;
1523 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001524
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1526 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001527 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001528
1529 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1530 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1531 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1532 SPOE_APPCTX(appctx)->max_frame_size);
1533 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001534 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001535 *skip = 1;
1536 ret = 1;
1537 goto end;
1538 }
1539 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001540 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001541 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1542 SPOE_APPCTX(appctx)->max_frame_size);
1543
1544 }
1545
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 if (ret > 1)
1547 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001548
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 switch (ret) {
1550 case -1: /* error */
1551 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1552 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001553
Christopher Faulet8ef75252017-02-20 22:56:03 +01001554 case 0: /* ignore */
1555 if (ctx == NULL)
1556 goto abort_frag_frame;
1557
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001558 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001559 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001560 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001561 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001562 spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001563 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 ctx->state = SPOE_CTX_ST_ERROR;
1565 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1566 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001567 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568 break;
1569
1570 case 1: /* retry */
1571 *skip = 1;
1572 break;
1573
1574 default:
1575 if (ctx == NULL)
1576 goto abort_frag_frame;
1577
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001578 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001579 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001580 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001581 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001582 spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001583 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001584 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1585 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1586 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001587 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001589 }
1590 goto end;
1591
1592 frag_frame_sent:
1593 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001594 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001595 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1596 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1597 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1599 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1600 goto end;
1601
1602 no_frag_frame_sent:
1603 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1604 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001605 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001606 }
1607 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1608 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001609 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 }
1611 else {
1612 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001613 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001614 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001615 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001616 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Willy Tarreau69530f52023-04-28 09:16:15 +02001617 ctx->stats.wait_ts = now_ns;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001618 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1619 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1620 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001621 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001622
Christopher Faulet8ef75252017-02-20 22:56:03 +01001623 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1624 goto end;
1625
1626 abort_frag_frame:
1627 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1628 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1629 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1630 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1631 goto end;
1632
1633 end:
1634 return ret;
1635}
1636
1637static int
1638spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1639{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001640 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001641 struct spoe_context *ctx = NULL;
1642 char *frame;
1643 int ret;
1644
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001645 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001646 ret = spoe_recv_frame(appctx, frame,
1647 SPOE_APPCTX(appctx)->max_frame_size);
1648 if (ret > 1) {
1649 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1650 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001651 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652 goto end;
1653 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001654 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001655 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1656 }
1657 switch (ret) {
1658 case -1: /* error */
1659 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1660 break;
1661
1662 case 0: /* ignore */
1663 break;
1664
1665 case 1: /* retry */
1666 *skip = 1;
1667 break;
1668
1669 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001670 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001671 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001672 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001673 spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting);
Willy Tarreau69530f52023-04-28 09:16:15 +02001674 ctx->stats.response_ts = now_ns;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 if (ctx->spoe_appctx) {
1676 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001677 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001679 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1680 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1681 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1682 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1683 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1684 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1685 }
1686 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1687 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001688 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1689 break;
1690 }
1691
1692 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001693 if (trash.data)
Willy Tarreauc12b3212022-05-27 11:08:15 +02001694 co_skip(sc_oc(appctx_sc(appctx)), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001695 end:
1696 return ret;
1697}
1698
1699static int
1700spoe_handle_processing_appctx(struct appctx *appctx)
1701{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001702 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001703 struct server *srv = objt_server(__sc_strm(sc)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001704 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001705 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001706
Christopher Faulet8ef75252017-02-20 22:56:03 +01001707 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1708 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1709 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1710 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1711 goto next;
1712 }
1713
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001714 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001715 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001716 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1718 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001719 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001720
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001721
1722 /* Close the applet ASAP because some sessions are waiting for a free
1723 * connection slot. It is only an issue in multithreaded mode.
1724 */
1725 close_asap = (global.nbthread > 1 &&
1726 (agent->b.be->queue.length ||
1727 (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1728
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 /* receiving_frame loop */
1730 while (!skip_receiving) {
1731 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1732 switch (ret) {
1733 case -1: /* error */
1734 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001735
Christopher Faulet8f82b202018-01-24 16:23:03 +01001736 case 0: /* ignore */
1737 active_r = 1;
1738 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001739
Christopher Faulet8f82b202018-01-24 16:23:03 +01001740 case 1: /* retry */
1741 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001742
Christopher Faulet8f82b202018-01-24 16:23:03 +01001743 default:
1744 active_r = 1;
1745 break;
1746 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748
Christopher Fauletd4349b82023-06-05 08:15:59 +02001749 /* Don"t try to send new frame we are waiting for at lease a ack, in
1750 * sync mode or if applet must be closed ASAP
1751 */
1752 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
1753 skip_sending = 1;
1754
Christopher Faulet8f82b202018-01-24 16:23:03 +01001755 /* send_frame loop */
1756 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1757 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1758 switch (ret) {
1759 case -1: /* error */
1760 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761
Christopher Faulet8f82b202018-01-24 16:23:03 +01001762 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001763 if (SPOE_APPCTX(appctx)->node.key)
1764 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001765 active_s++;
1766 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767
Christopher Faulet8f82b202018-01-24 16:23:03 +01001768 case 1: /* retry */
1769 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770
Christopher Faulet8f82b202018-01-24 16:23:03 +01001771 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001772 if (SPOE_APPCTX(appctx)->node.key)
1773 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001774 active_s++;
1775 break;
1776 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001777
1778 /* if applet must be close ASAP, don't send more than a frame */
1779 if (close_asap)
1780 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782
Christopher Faulet8f82b202018-01-24 16:23:03 +01001783 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001784 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1785 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1786 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001787
Christopher Faulet8f82b202018-01-24 16:23:03 +01001788 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001789 /* If applet must be closed, don't switch it in IDLE state and
1790 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001791 */
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001792 if (close_asap) {
1793 if (SPOE_APPCTX(appctx)->cur_fpa)
1794 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1796 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1797 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1798 goto next;
1799 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001800 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001801 agent->rt[tid].idles++;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001802 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001803 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001805
1806 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 return 1;
1808
Christopher Faulet8f82b202018-01-24 16:23:03 +01001809 next:
1810 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1811 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812}
1813
1814static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816{
Christopher Faulet908628c2022-03-25 16:43:49 +01001817 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001818 char *frame, *buf;
1819 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001820
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1822 goto exit;
1823
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1825 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001826 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1828 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001830 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831
1832 switch (ret) {
1833 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 goto exit;
1836
1837 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 goto stop;
1839
1840 default:
1841 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1842 " - disconnected by HAProxy (%d): %s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001843 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844 __FUNCTION__, appctx,
1845 SPOE_APPCTX(appctx)->status_code,
1846 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847
1848 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1849 goto next;
1850 }
1851
1852 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853 SPOE_APPCTX(appctx)->task->expire =
1854 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 return 0;
1856 stop:
1857 return 1;
1858 exit:
1859 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1860 return 0;
1861}
1862
1863static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001866 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 char *frame;
1868 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869
Christopher Fauletb067b062017-01-04 16:39:11 +01001870 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001872 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001873 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001875 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001876 ret = spoe_recv_frame(appctx, frame,
1877 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001879 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 }
1882
1883 switch (ret) {
1884 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1886 " - error on frame (%s)\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001887 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001888 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 goto exit;
1892
1893 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 goto next;
1895
1896 case 1: /* retry */
1897 goto stop;
1898
1899 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001901 " - disconnected by peer (%d): %.*s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001902 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001903 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1905 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001906 goto exit;
1907 }
1908
1909 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001910 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001911 if (trash.data)
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001912 co_skip(sc_oc(sc), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001913
Christopher Fauleta1cda022016-12-21 08:58:06 +01001914 return 0;
1915 stop:
1916 return 1;
1917 exit:
1918 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1919 return 0;
1920}
1921
1922/* I/O Handler processing messages exchanged with the agent */
1923static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001924spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001925{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001926 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001927 struct spoe_agent *agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001928
1929 if (SPOE_APPCTX(appctx) == NULL)
1930 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001931
Christopher Fauletd550d262023-03-31 11:04:34 +02001932 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
1933 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
1934 goto out;
1935 }
1936
Christopher Faulet8ef75252017-02-20 22:56:03 +01001937 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1938 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001939
Christopher Fauleta1cda022016-12-21 08:58:06 +01001940 switchstate:
1941 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1942 " - appctx-state=%s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001943 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001944 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1945
1946 switch (appctx->st0) {
1947 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001948 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001949 goto out;
1950 goto switchstate;
1951
1952 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001953 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001954 goto out;
1955 goto switchstate;
1956
1957 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001958 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001959 agent->rt[tid].idles--;
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001960 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001961 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001962 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001963 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001964 SPOE_APPCTX(appctx)->task->expire =
1965 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001966 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 goto switchstate;
1968 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001969 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau3064f522022-11-14 07:22:14 +01001970 __fallthrough;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001971
1972 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001973 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1974 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001975 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001976 goto out;
1977 goto switchstate;
1978
1979 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001980 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001981 goto out;
1982 goto switchstate;
1983
1984 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001985 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001986 goto out;
1987 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001988
1989 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001990 appctx->st0 = SPOE_APPCTX_ST_END;
1991 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
Christopher Fauletd550d262023-03-31 11:04:34 +02001992 se_fl_set(appctx->sedesc, SE_FL_EOS);
1993 if (SPOE_APPCTX(appctx)->status_code != SPOE_FRM_ERR_NONE)
1994 se_fl_set(appctx->sedesc, SE_FL_ERROR);
1995 else
1996 se_fl_set(appctx->sedesc, SE_FL_EOI);
Willy Tarreau3064f522022-11-14 07:22:14 +01001997 __fallthrough;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998
1999 case SPOE_APPCTX_ST_END:
Christopher Faulet6fe10602024-03-18 08:02:32 +01002000 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002001 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002003 out:
Christopher Faulet5f367ed2024-03-14 10:49:01 +01002004 if (stopping && appctx->st0 == SPOE_APPCTX_ST_IDLE)
2005 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_MSG);
2006 else if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002007 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002008}
2009
2010struct applet spoe_applet = {
2011 .obj_type = OBJ_TYPE_APPLET,
2012 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002013 .fct = spoe_handle_appctx,
Christopher Faulet69ebc302022-05-12 15:28:51 +02002014 .init = spoe_init_appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002015 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002016};
2017
2018/* Create a SPOE applet. On success, the created applet is returned, else
2019 * NULL. */
2020static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002021spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022{
Christopher Faulet60ce4682024-01-05 17:06:48 +01002023 struct spoe_agent *agent = conf->agent;
Christopher Faulet69ebc302022-05-12 15:28:51 +02002024 struct spoe_appctx *spoe_appctx;
2025 struct appctx *appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026
Christopher Faulet60ce4682024-01-05 17:06:48 +01002027 /* Do not try to create a new applet if there is no server up for the
2028 * agent's backend. */
2029 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2030 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: don't create SPOE appctx: no server up\n",
2031 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__);
2032 goto out;
2033 }
2034
2035 /* Do not try to create a new applet if we have reached the maximum of
2036 * connection per seconds */
2037 if (agent->cps_max > 0) {
2038 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
2039 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: don't create SPOE appctx: max CPS reached\n",
2040 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__);
2041 goto out;
2042 }
2043 }
2044
Christopher Faulet69ebc302022-05-12 15:28:51 +02002045 spoe_appctx = pool_zalloc(pool_head_spoe_appctx);
2046 if (spoe_appctx == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002047 goto out_error;
2048
Christopher Faulet60ce4682024-01-05 17:06:48 +01002049 spoe_appctx->agent = agent;
Christopher Faulet69ebc302022-05-12 15:28:51 +02002050 spoe_appctx->version = 0;
Christopher Faulet60ce4682024-01-05 17:06:48 +01002051 spoe_appctx->max_frame_size = agent->max_frame_size;
Christopher Faulet69ebc302022-05-12 15:28:51 +02002052 spoe_appctx->flags = 0;
2053 spoe_appctx->status_code = SPOE_FRM_ERR_NONE;
2054 spoe_appctx->buffer = BUF_NULL;
2055 spoe_appctx->cur_fpa = 0;
2056 LIST_INIT(&spoe_appctx->list);
2057 LIST_INIT(&spoe_appctx->waiting_queue);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002058
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002059
Christopher Faulet6095d572022-05-16 17:09:48 +02002060 if ((appctx = appctx_new_here(&spoe_applet, NULL)) == NULL)
Christopher Faulet69ebc302022-05-12 15:28:51 +02002061 goto out_free_spoe_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002062
Christopher Faulet69ebc302022-05-12 15:28:51 +02002063 appctx->svcctx = spoe_appctx;
2064 if (appctx_init(appctx) == -1)
2065 goto out_free_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066
Christopher Faulet60ce4682024-01-05 17:06:48 +01002067 /* Increase the per-process number of cumulated connections */
2068 if (agent->cps_max > 0)
2069 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
2070
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071 appctx_wakeup(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072 return appctx;
2073
2074 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002075 out_free_appctx:
Christopher Faulet69ebc302022-05-12 15:28:51 +02002076 appctx_free_on_early_error(appctx);
2077 out_free_spoe_appctx:
2078 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 out_error:
Christopher Faulet60ce4682024-01-05 17:06:48 +01002080 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: failed to create SPOE appctx\n",
2081 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__);
2082 send_log(&conf->agent_fe, LOG_EMERG, "SPOE: [%s] failed to create SPOE applet\n", agent->id);
2083 out:
2084
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085 return NULL;
2086}
2087
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002088static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002089spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002090{
2091 struct spoe_config *conf = FLT_CONF(ctx->filter);
2092 struct spoe_agent *agent = conf->agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002093 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002094
Christopher Fauleta1cda022016-12-21 08:58:06 +01002095 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet22878422024-07-09 08:24:05 +02002096 if (!LIST_ISEMPTY(&agent->rt[tid].applets) &&
2097 (agent->rt[tid].processing < agent->rt[tid].idles ||
2098 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002099 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002100
2101 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002102 " - try to create new SPOE appctx\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002103 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002104 ctx->strm);
2105
Christopher Faulet60ce4682024-01-05 17:06:48 +01002106 spoe_create_appctx(conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107
Christopher Fauleta1cda022016-12-21 08:58:06 +01002108 end:
2109 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002110 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002111 ctx->status_code = SPOE_CTX_ERR_RES;
2112 return -1;
2113 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002114
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002115 /* Add the SPOE context in the sending queue if the stream has no applet
2116 * already assigned and wakeup all idle applets. Otherwise, don't queue
2117 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002118 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002119 spoe_update_stat_time(&ctx->stats.request_ts, &ctx->stats.t_request);
Willy Tarreau69530f52023-04-28 09:16:15 +02002120 ctx->stats.queue_ts = now_ns;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002121 if (ctx->spoe_appctx)
2122 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002123 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002124
2125 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002126 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002127 " - applets=%u - idles=%u - processing=%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002128 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002129 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002130 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002131
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002132 /* Finally try to wakeup an IDLE applet. */
2133 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2134 struct eb32_node *node;
2135
2136 node = eb32_first(&agent->rt[tid].idle_applets);
2137 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2138 if (node && spoe_appctx) {
2139 eb32_delete(&spoe_appctx->node);
2140 spoe_appctx->node.key++;
2141 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2142 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002143 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002144 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002145 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002146}
2147
2148/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002149 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002150 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002151/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2152 * fragmented_content. If the next message can be processed, it returns 0. If
2153 * the message is too big, it returns -1.*/
2154static int
2155spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2156 struct spoe_message *msg, int dir,
2157 char **buf, char *end)
2158{
2159 struct sample *smp;
2160 struct spoe_arg *arg;
2161 int ret;
2162
2163 if (msg->cond) {
2164 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2165 ret = acl_pass(ret);
2166 if (msg->cond->pol == ACL_COND_UNLESS)
2167 ret = !ret;
2168
2169 /* the rule does not match */
2170 if (!ret)
2171 goto next;
2172 }
2173
2174 /* Resume encoding of a SPOE argument */
2175 if (ctx->frag_ctx.curarg != NULL) {
2176 arg = ctx->frag_ctx.curarg;
2177 goto encode_argument;
2178 }
2179
2180 if (ctx->frag_ctx.curoff != UINT_MAX)
2181 goto encode_msg_payload;
2182
2183 /* Check if there is enough space for the message name and the
2184 * number of arguments. It implies <msg->id_len> is encoded on 2
2185 * bytes, at most (< 2288). */
2186 if (*buf + 2 + msg->id_len + 1 > end)
2187 goto too_big;
2188
2189 /* Encode the message name */
2190 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2191 goto too_big;
2192
2193 /* Set the number of arguments for this message */
2194 **buf = msg->nargs;
2195 (*buf)++;
2196
2197 ctx->frag_ctx.curoff = 0;
2198 encode_msg_payload:
2199
2200 /* Loop on arguments */
2201 list_for_each_entry(arg, &msg->args, list) {
2202 ctx->frag_ctx.curarg = arg;
2203 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002204 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002205
2206 encode_argument:
2207 if (ctx->frag_ctx.curoff != UINT_MAX)
2208 goto encode_arg_value;
2209
Joseph Herlantf7f60312018-11-15 13:46:49 -08002210 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002211 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2212 goto too_big;
2213
2214 ctx->frag_ctx.curoff = 0;
2215 encode_arg_value:
2216
Joseph Herlantf7f60312018-11-15 13:46:49 -08002217 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002218 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002219 if (smp) {
2220 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2221 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2222 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002223 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002224 if (ret == -1 || ctx->frag_ctx.curoff)
2225 goto too_big;
2226 }
2227
2228 next:
2229 return 0;
2230
2231 too_big:
2232 return -1;
2233}
2234
Christopher Fauletc718b822017-09-21 16:50:56 +02002235/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2236 * handle fragmented content. On success it returns 1. If an error occurred, -1
2237 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2238 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002239static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002240spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002241 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002242{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002243 struct spoe_config *conf = FLT_CONF(ctx->filter);
2244 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002245 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002246 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002247
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002248 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002249 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002250
Christopher Fauletc718b822017-09-21 16:50:56 +02002251 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2252 /* Resume encoding of a SPOE message */
2253 if (ctx->frag_ctx.curmsg != NULL) {
2254 msg = ctx->frag_ctx.curmsg;
2255 goto encode_evt_message;
2256 }
2257
2258 list_for_each_entry(msg, messages, by_evt) {
2259 ctx->frag_ctx.curmsg = msg;
2260 ctx->frag_ctx.curarg = NULL;
2261 ctx->frag_ctx.curoff = UINT_MAX;
2262
2263 encode_evt_message:
2264 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2265 goto too_big;
2266 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002268 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2269 /* Resume encoding of a SPOE message */
2270 if (ctx->frag_ctx.curmsg != NULL) {
2271 msg = ctx->frag_ctx.curmsg;
2272 goto encode_grp_message;
2273 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274
Christopher Fauletc718b822017-09-21 16:50:56 +02002275 list_for_each_entry(msg, messages, by_grp) {
2276 ctx->frag_ctx.curmsg = msg;
2277 ctx->frag_ctx.curarg = NULL;
2278 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279
Christopher Fauletc718b822017-09-21 16:50:56 +02002280 encode_grp_message:
2281 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2282 goto too_big;
2283 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002284 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002285 else
2286 goto skip;
2287
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288
Christopher Faulet57583e42017-09-04 15:41:09 +02002289 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002290 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002291 goto skip;
2292
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002293 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002294 " - encode %s messages - spoe_appctx=%p"
2295 "- max_size=%u - encoded=%ld\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002296 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002297 agent->id, __FUNCTION__, s,
2298 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002299 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002300 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002301
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002302 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002303 ctx->frag_ctx.curmsg = NULL;
2304 ctx->frag_ctx.curarg = NULL;
2305 ctx->frag_ctx.curoff = 0;
2306 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002307
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002308 return 1;
2309
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002310 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002311 /* Return an error if fragmentation is unsupported or if nothing has
2312 * been encoded because its too big and not splittable. */
2313 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002314 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2315 return -1;
2316 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002317
2318 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319 " - encode fragmented messages - spoe_appctx=%p"
2320 " - curmsg=%p - curarg=%p - curoff=%u"
2321 " - max_size=%u - encoded=%ld\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002322 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002323 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002324 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002325 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), 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->flags |= SPOE_CTX_FL_FRAGMENTED;
2329 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2330 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002331
2332 skip:
2333 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2334 " - skip the frame because nothing has been encoded\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002335 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet57583e42017-09-04 15:41:09 +02002336 agent->id, __FUNCTION__, s);
2337 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002338}
2339
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002340
2341/***************************************************************************
2342 * Functions that handle SPOE actions
2343 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344/* Helper function to set a variable */
2345static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002346spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347 struct sample *smp)
2348{
2349 struct spoe_config *conf = FLT_CONF(ctx->filter);
2350 struct spoe_agent *agent = conf->agent;
2351 char varname[64];
2352
2353 memset(varname, 0, sizeof(varname));
2354 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2355 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002356 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2357 vars_set_by_name(varname, len, smp);
2358 else
2359 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002360}
2361
2362/* Helper function to unset a variable */
2363static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002365 struct sample *smp)
2366{
2367 struct spoe_config *conf = FLT_CONF(ctx->filter);
2368 struct spoe_agent *agent = conf->agent;
2369 char varname[64];
2370
2371 memset(varname, 0, sizeof(varname));
2372 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2373 scope, agent->var_pfx, len, name);
2374 vars_unset_by_name_ifexist(varname, len, smp);
2375}
2376
2377
Christopher Faulet8ef75252017-02-20 22:56:03 +01002378static inline int
2379spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2380 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002381{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002382 char *str, *scope, *p = *buf;
2383 struct sample smp;
2384 uint64_t sz;
2385 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002386
Christopher Faulet8ef75252017-02-20 22:56:03 +01002387 if (p + 2 >= end)
2388 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 /* SET-VAR requires 3 arguments */
2391 if (*p++ != 3)
2392 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002393
Christopher Faulet8ef75252017-02-20 22:56:03 +01002394 switch (*p++) {
2395 case SPOE_SCOPE_PROC: scope = "proc"; break;
2396 case SPOE_SCOPE_SESS: scope = "sess"; break;
2397 case SPOE_SCOPE_TXN : scope = "txn"; break;
2398 case SPOE_SCOPE_REQ : scope = "req"; break;
2399 case SPOE_SCOPE_RES : scope = "res"; break;
2400 default: goto skip;
2401 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2404 goto skip;
2405 memset(&smp, 0, sizeof(smp));
2406 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002407
Christopher Faulet8ef75252017-02-20 22:56:03 +01002408 if (spoe_decode_data(&p, end, &smp) == -1)
2409 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002410
Christopher Faulet8ef75252017-02-20 22:56:03 +01002411 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2412 " - set-var '%s.%s.%.*s'\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002413 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2415 __FUNCTION__, s, scope,
2416 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2417 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet2469eba2020-10-15 16:08:30 +02002419 if (smp.data.type == SMP_T_ANY)
2420 spoe_unset_var(ctx, scope, str, sz, &smp);
2421 else
2422 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002423
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424 ret = (p - *buf);
2425 *buf = p;
2426 return ret;
2427 skip:
2428 return 0;
2429}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002430
Christopher Faulet8ef75252017-02-20 22:56:03 +01002431static inline int
2432spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2433 char **buf, char *end, int dir)
2434{
2435 char *str, *scope, *p = *buf;
2436 struct sample smp;
2437 uint64_t sz;
2438 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002439
Christopher Faulet8ef75252017-02-20 22:56:03 +01002440 if (p + 2 >= end)
2441 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443 /* UNSET-VAR requires 2 arguments */
2444 if (*p++ != 2)
2445 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002446
Christopher Faulet8ef75252017-02-20 22:56:03 +01002447 switch (*p++) {
2448 case SPOE_SCOPE_PROC: scope = "proc"; break;
2449 case SPOE_SCOPE_SESS: scope = "sess"; break;
2450 case SPOE_SCOPE_TXN : scope = "txn"; break;
2451 case SPOE_SCOPE_REQ : scope = "req"; break;
2452 case SPOE_SCOPE_RES : scope = "res"; break;
2453 default: goto skip;
2454 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002455
Christopher Faulet8ef75252017-02-20 22:56:03 +01002456 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2457 goto skip;
2458 memset(&smp, 0, sizeof(smp));
2459 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460
Christopher Faulet8ef75252017-02-20 22:56:03 +01002461 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2462 " - unset-var '%s.%s.%.*s'\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002463 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2465 __FUNCTION__, s, scope,
2466 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2467 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002468
Christopher Faulet8ef75252017-02-20 22:56:03 +01002469 spoe_unset_var(ctx, scope, str, sz, &smp);
2470
2471 ret = (p - *buf);
2472 *buf = p;
2473 return ret;
2474 skip:
2475 return 0;
2476}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002477
Christopher Faulet8ef75252017-02-20 22:56:03 +01002478/* Process SPOE actions for a specific event. It returns 1 on success. If an
2479 * error occurred, 0 is returned. */
2480static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002481spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002482{
2483 char *p, *end;
2484 int ret;
2485
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002486 p = b_head(&ctx->buffer);
2487 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002488
2489 while (p < end) {
2490 enum spoe_action_type type;
2491
2492 type = *p++;
2493 switch (type) {
2494 case SPOE_ACT_T_SET_VAR:
2495 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2496 if (!ret)
2497 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002498 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002499
Christopher Faulet8ef75252017-02-20 22:56:03 +01002500 case SPOE_ACT_T_UNSET_VAR:
2501 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2502 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002503 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002504 break;
2505
2506 default:
2507 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002508 }
2509 }
2510
2511 return 1;
2512 skip:
2513 return 0;
2514}
2515
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002516/***************************************************************************
2517 * Functions that process SPOE events
2518 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002519static void
2520spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2521 struct spoe_context *ctx, int dir)
2522{
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002523 if (ctx->stats.start_ts != 0) {
2524 spoe_update_stat_time(&ctx->stats.start_ts, &ctx->stats.t_process);
2525 ctx->stats.t_total += ctx->stats.t_process;
2526 ctx->stats.request_ts = 0;
2527 ctx->stats.queue_ts = 0;
2528 ctx->stats.wait_ts = 0;
2529 ctx->stats.response_ts = 0;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002530 }
2531
2532 if (agent->var_t_process) {
2533 struct sample smp;
2534
2535 memset(&smp, 0, sizeof(smp));
2536 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2537 smp.data.u.sint = ctx->stats.t_process;
2538 smp.data.type = SMP_T_SINT;
2539
2540 spoe_set_var(ctx, "txn", agent->var_t_process,
2541 strlen(agent->var_t_process), &smp);
2542 }
2543
2544 if (agent->var_t_total) {
2545 struct sample smp;
2546
2547 memset(&smp, 0, sizeof(smp));
2548 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2549 smp.data.u.sint = ctx->stats.t_total;
2550 smp.data.type = SMP_T_SINT;
2551
2552 spoe_set_var(ctx, "txn", agent->var_t_total,
2553 strlen(agent->var_t_total), &smp);
2554 }
2555}
2556
2557static void
2558spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2559 struct spoe_context *ctx, int dir)
2560{
2561 if (agent->eps_max > 0)
2562 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2563
2564 if (agent->var_on_error) {
2565 struct sample smp;
2566
2567 memset(&smp, 0, sizeof(smp));
2568 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2569 smp.data.u.sint = ctx->status_code;
2570 smp.data.type = SMP_T_BOOL;
2571
2572 spoe_set_var(ctx, "txn", agent->var_on_error,
2573 strlen(agent->var_on_error), &smp);
2574 }
2575
2576 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2577 ? SPOE_CTX_ST_READY
2578 : SPOE_CTX_ST_NONE);
2579}
2580
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002581static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002582spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002583{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002584 /* If a process is already started for this SPOE context, retry
2585 * later. */
2586 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002587 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002589 agent->rt[tid].processing++;
Willy Tarreau69530f52023-04-28 09:16:15 +02002590 ctx->stats.start_ts = now_ns;
2591 ctx->stats.request_ts = now_ns;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002592 ctx->stats.t_request = -1;
2593 ctx->stats.t_queue = -1;
2594 ctx->stats.t_waiting = -1;
2595 ctx->stats.t_response = -1;
2596 ctx->stats.t_process = -1;
2597
2598 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002599
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600 /* Set the right flag to prevent request and response processing
2601 * in same time. */
2602 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2603 ? SPOE_CTX_FL_REQ_PROCESS
2604 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002606}
2607
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002608static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002609spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002611 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002612
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002613 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2614 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002615 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002616 if (sa) {
2617 if (sa->frag_ctx.ctx == ctx) {
2618 sa->frag_ctx.ctx = NULL;
2619 spoe_wakeup_appctx(sa->owner);
2620 }
2621 else
2622 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002623 }
2624
Christopher Fauleta1cda022016-12-21 08:58:06 +01002625 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002626 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002627 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002628
2629 /* Reset processing timer */
2630 ctx->process_exp = TICK_ETERNITY;
Christopher Faulet662aff52024-03-14 10:21:56 +01002631 ctx->strm->req.analyse_exp = TICK_ETERNITY;
2632 ctx->strm->res.analyse_exp = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002633
Christopher Faulet8ef75252017-02-20 22:56:03 +01002634 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635
Christopher Fauletfce747b2018-01-24 15:59:32 +01002636 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002637 ctx->frag_ctx.curmsg = NULL;
2638 ctx->frag_ctx.curarg = NULL;
2639 ctx->frag_ctx.curoff = 0;
2640 ctx->frag_ctx.flags = 0;
2641
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002643 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002644 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002645 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002646 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002647
Willy Tarreau2b718102021-04-21 07:32:39 +02002648 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002649 LIST_INIT(&ctx->list);
2650 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002651}
2652
Christopher Faulet58d03682017-09-21 16:57:24 +02002653/* Process a list of SPOE messages. First, this functions will process messages
2654 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2655 * to process corresponding actions. During all the processing, it returns 0
2656 * and it returns 1 when the processing is finished. If an error occurred, -1
2657 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002658static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002659spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2660 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002661{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002662 struct spoe_config *conf = FLT_CONF(ctx->filter);
2663 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002665
2666 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002667 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002668
2669 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2670 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002671 " - failed to process messages: timeout\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002672 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002673 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002674 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002675 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002676 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677
2678 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002679 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002680 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002681 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002682 " - skip processing of messages: max EPS reached\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002683 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002684 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002685 goto skip;
2686 }
2687 }
2688
Christopher Fauletf7a30922016-11-10 15:04:51 +01002689 if (!tick_isset(ctx->process_exp)) {
2690 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
Christopher Faulet662aff52024-03-14 10:21:56 +01002691 if (dir == SMP_OPT_DIR_REQ)
2692 s->req.analyse_exp = ctx->process_exp;
2693 else
2694 s->res.analyse_exp = ctx->process_exp;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002695 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002696 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002697 if (!ret)
2698 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002700 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002701 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002702 }
2703
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002704 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002705 if (ctx->stats.request_ts == 0)
Willy Tarreau69530f52023-04-28 09:16:15 +02002706 ctx->stats.request_ts = now_ns;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002707 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002708 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002709 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002710 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002711 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002712 if (!ret)
2713 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002714 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002715 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002716 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2717 }
2718
2719 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002720 if (ctx->spoe_appctx)
2721 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002722 ret = 0;
2723 goto out;
2724 }
2725
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002726 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2727 ret = 0;
2728 goto out;
2729 }
2730
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002731 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002732 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002733 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002734 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002735 ctx->state = SPOE_CTX_ST_READY;
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002736 spoe_update_stat_time(&ctx->stats.response_ts, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002737 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002738 }
2739
2740 out:
2741 return ret;
2742
Christopher Fauleta1cda022016-12-21 08:58:06 +01002743 skip:
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002744 ctx->stats.start_ts = 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002745 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002746 spoe_stop_processing(agent, ctx);
2747 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002748
Christopher Fauleta1cda022016-12-21 08:58:06 +01002749 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002750 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002751 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002752 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002753 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002754 spoe_handle_processing_error(s, agent, ctx, dir);
2755 ret = 1;
2756 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002757 return ret;
2758}
2759
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002760/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2761 * See spoe_process_message for details. */
2762static int
2763spoe_process_group(struct stream *s, struct spoe_context *ctx,
2764 struct spoe_group *group, int dir)
2765{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002766 struct spoe_config *conf = FLT_CONF(ctx->filter);
2767 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002768 int ret;
2769
2770 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2771 " - ctx-state=%s - Process messages for group=%s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002772 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002773 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2774 group->id);
2775
2776 if (LIST_ISEMPTY(&group->messages))
2777 return 1;
2778
2779 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002780 if (ret && ctx->stats.t_process != -1) {
2781 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002782 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002783 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002784 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002785 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002786 ctx->stats.t_response, ctx->stats.t_process,
2787 agent->counters.idles, agent->counters.applets,
2788 agent->counters.nb_sending, agent->counters.nb_waiting,
2789 agent->counters.nb_errors, agent->counters.nb_processed,
2790 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2791 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2792 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2793 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2794 agent->id, group->id, s->uniq_id, ctx->status_code,
2795 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2796 ctx->stats.t_response, ctx->stats.t_process,
2797 agent->counters.idles, agent->counters.applets,
2798 agent->counters.nb_sending, agent->counters.nb_waiting,
2799 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002800 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002801 return ret;
2802}
2803
Christopher Faulet58d03682017-09-21 16:57:24 +02002804/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2805 * See spoe_process_message for details. */
2806static int
2807spoe_process_event(struct stream *s, struct spoe_context *ctx,
2808 enum spoe_event ev)
2809{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002810 struct spoe_config *conf = FLT_CONF(ctx->filter);
2811 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002812 int dir, ret;
2813
2814 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002815 " - ctx-state=%s - Process messages for event=%s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002816 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002817 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2818 spoe_event_str[ev]);
2819
2820 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2821
2822 if (LIST_ISEMPTY(&(ctx->events[ev])))
2823 return 1;
2824
2825 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002826 if (ret && ctx->stats.t_process != -1) {
2827 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002828 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002829 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002830 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2831 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002832 ctx->stats.t_response, ctx->stats.t_process,
2833 agent->counters.idles, agent->counters.applets,
2834 agent->counters.nb_sending, agent->counters.nb_waiting,
2835 agent->counters.nb_errors, agent->counters.nb_processed,
2836 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2837 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2838 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2839 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2840 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2841 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2842 ctx->stats.t_response, ctx->stats.t_process,
2843 agent->counters.idles, agent->counters.applets,
2844 agent->counters.nb_sending, agent->counters.nb_waiting,
2845 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002846 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002847 return ret;
2848}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002849
2850/***************************************************************************
2851 * Functions that create/destroy SPOE contexts
2852 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002853static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002854spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002855{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002856 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002857 return 1;
2858
Willy Tarreau2b718102021-04-21 07:32:39 +02002859 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002860 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002861
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002862 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002863 return 1;
2864
Willy Tarreaub4e34762021-09-30 19:02:18 +02002865 LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002866 return 0;
2867}
2868
2869static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002870spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002871{
Willy Tarreau2b718102021-04-21 07:32:39 +02002872 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002873 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002874
2875 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002876 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002877 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002878 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002879 }
2880}
2881
Christopher Faulet4596fb72017-01-11 14:05:19 +01002882static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002883spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002884{
2885 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2886 return 1;
2887}
2888
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002889static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002890spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002891{
2892 struct spoe_config *conf = FLT_CONF(filter);
2893 struct spoe_context *ctx;
2894
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002895 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896 if (ctx == NULL) {
2897 return NULL;
2898 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002899 ctx->filter = filter;
2900 ctx->state = SPOE_CTX_ST_NONE;
2901 ctx->status_code = SPOE_CTX_ERR_NONE;
2902 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002903 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002904 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002906 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002907 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002908 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002909 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002910
Christopher Fauletf7a30922016-11-10 15:04:51 +01002911 ctx->stream_id = 0;
2912 ctx->frame_id = 1;
2913 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002915 ctx->stats.start_ts = 0;
2916 ctx->stats.request_ts = 0;
2917 ctx->stats.queue_ts = 0;
2918 ctx->stats.wait_ts = 0;
2919 ctx->stats.response_ts= 0;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002920 ctx->stats.t_request = -1;
2921 ctx->stats.t_queue = -1;
2922 ctx->stats.t_waiting = -1;
2923 ctx->stats.t_response = -1;
2924 ctx->stats.t_process = -1;
2925 ctx->stats.t_total = 0;
2926
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002927 ctx->strm = s;
2928 ctx->state = SPOE_CTX_ST_READY;
2929 filter->ctx = ctx;
2930
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931 return ctx;
2932}
2933
2934static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002935spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002937 struct spoe_config *conf = FLT_CONF(filter);
2938 struct spoe_context *ctx = filter->ctx;
2939
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940 if (!ctx)
2941 return;
2942
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002943 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002944 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002945 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946}
2947
2948static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002949spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002950{
2951 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002952 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002953
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002954 ctx->stats.start_ts = 0;
2955 ctx->stats.request_ts = 0;
2956 ctx->stats.queue_ts = 0;
2957 ctx->stats.wait_ts = 0;
2958 ctx->stats.response_ts= 0;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002959 ctx->stats.t_request = -1;
2960 ctx->stats.t_queue = -1;
2961 ctx->stats.t_waiting = -1;
2962 ctx->stats.t_response = -1;
2963 ctx->stats.t_process = -1;
2964 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965}
2966
2967
2968/***************************************************************************
2969 * Hooks that manage the filter lifecycle (init/check/deinit)
2970 **************************************************************************/
2971/* Signal handler: Do a soft stop, wakeup SPOE applet */
2972static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002973spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002974{
2975 struct proxy *p;
2976
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002977 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978 while (p) {
2979 struct flt_conf *fconf;
2980
Christopher Faulet7f4ffad2023-05-11 09:08:28 +02002981 /* SPOE filter are not initialized for disabled proxoes. Move to
2982 * the next one
2983 */
2984 if (p->flags & PR_FL_DISABLED) {
2985 p = p->next;
2986 continue;
2987 }
2988
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002990 struct spoe_config *conf;
2991 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002992 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002993 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002994
Christopher Faulet3b386a32017-02-23 10:17:15 +01002995 if (fconf->id != spoe_filter_id)
2996 continue;
2997
2998 conf = fconf->conf;
2999 agent = conf->agent;
3000
Christopher Faulet24289f22017-09-25 14:48:02 +02003001 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003002 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003003 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3004 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003005 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003006 }
3007 }
3008 p = p->next;
3009 }
3010}
3011
3012
3013/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3014static int
3015spoe_init(struct proxy *px, struct flt_conf *fconf)
3016{
3017 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003019 /* conf->agent_fe was already initialized during the config
3020 * parsing. Finish initialization. */
Willy Tarreau69530f52023-04-28 09:16:15 +02003021 conf->agent_fe.last_change = ns_to_sec(now_ns);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003022 conf->agent_fe.cap = PR_CAP_FE;
3023 conf->agent_fe.mode = PR_MODE_TCP;
3024 conf->agent_fe.maxconn = 0;
3025 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3026 conf->agent_fe.conn_retries = CONN_RETRIES;
3027 conf->agent_fe.accept = frontend_accept;
3028 conf->agent_fe.srv = NULL;
3029 conf->agent_fe.timeout.client = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003030 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3031
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003032 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003033 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 sighandler_registered = 1;
3035 }
3036
Christopher Faulet00292352019-01-09 15:05:10 +01003037 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003039}
3040
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003041/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042static void
3043spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3044{
3045 struct spoe_config *conf = fconf->conf;
3046
3047 if (conf) {
3048 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049
Christopher Faulet8ef75252017-02-20 22:56:03 +01003050 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003051 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 free(conf);
3053 }
3054 fconf->conf = NULL;
3055}
3056
3057/* Check configuration of a SPOE filter for a specified proxy.
3058 * Return 1 on error, else 0. */
3059static int
3060spoe_check(struct proxy *px, struct flt_conf *fconf)
3061{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003062 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003063 struct spoe_config *conf = fconf->conf;
3064 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003065 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003066 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003067
Christopher Faulet7ee86672017-09-19 11:08:28 +02003068 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3069 * are uniq */
3070 list_for_each_entry(f, &px->filter_configs, list) {
3071 struct spoe_config *c = f->conf;
3072
3073 /* This is not an SPOE filter */
3074 if (f->id != spoe_filter_id)
3075 continue;
3076 /* This is the current SPOE filter */
3077 if (f == fconf)
3078 continue;
3079
3080 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003081 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003082 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3083 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003084 return 1;
3085 }
3086 }
3087
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003088 target = proxy_be_by_name(conf->agent->b.name);
3089 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003090 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3091 " declared at %s:%d.\n",
3092 px->id, conf->agent->b.name, conf->agent->id,
3093 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 return 1;
3095 }
3096 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003097 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3098 " at %s:%d does not support HTTP mode.\n",
3099 px->id, target->id, conf->agent->id,
3100 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003101 return 1;
3102 }
3103
Christopher Fauletfe261552019-03-18 13:57:42 +01003104 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003105 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3106 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3107 return 1;
3108 }
3109 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003110 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003111 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3112 conf->agent->rt[i].processing = 0;
Christopher Faulete99c4392023-04-26 15:56:59 +02003113 conf->agent->rt[i].idles = 0;
Christopher Fauletfe261552019-03-18 13:57:42 +01003114 LIST_INIT(&conf->agent->rt[i].applets);
3115 LIST_INIT(&conf->agent->rt[i].sending_queue);
3116 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3117 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003118 }
3119
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003120 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3121 if (logsrv->type == LOG_TARGET_BUFFER) {
3122 struct sink *sink = sink_find(logsrv->ring_name);
3123
3124 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3125 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003126 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003127 px->id, conf->agent->id, conf->agent->conf.file,
3128 conf->agent->conf.line, logsrv->ring_name);
3129 return 1;
3130 }
3131 logsrv->sink = sink;
3132 }
3133 }
3134
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003135 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003136 conf->agent->b.be = target;
3137 return 0;
3138}
3139
Kevin Zhud87b1a52019-09-17 15:05:45 +02003140/* Initializes the SPOE filter for a proxy for a specific thread.
3141 * Returns a negative value if an error occurs. */
3142static int
3143spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3144{
3145 struct spoe_config *conf = fconf->conf;
3146 struct spoe_agent *agent = conf->agent;
3147
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003148 agent->rt[tid].engine_id = generate_pseudo_uuid();
3149 if (agent->rt[tid].engine_id == NULL)
3150 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003151 return 0;
3152}
3153
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003154/**************************************************************************
3155 * Hooks attached to a stream
3156 *************************************************************************/
3157/* Called when a filter instance is created and attach to a stream. It creates
3158 * the context that will be used to process this stream. */
3159static int
3160spoe_start(struct stream *s, struct filter *filter)
3161{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003162 struct spoe_config *conf = FLT_CONF(filter);
3163 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003164 struct spoe_context *ctx;
3165
3166 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003167 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168 __FUNCTION__, s);
3169
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003170 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003171 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3172 " - failed to create SPOE context\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003173 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003174 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003175 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003176 "SPOE: [%s] failed to create SPOE context\n",
3177 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003178 return 0;
3179 }
3180
Christopher Faulet11610f32017-09-21 10:23:10 +02003181 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003182 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3183
Christopher Faulet11610f32017-09-21 10:23:10 +02003184 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3186
Christopher Faulet11610f32017-09-21 10:23:10 +02003187 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003188 filter->pre_analyzers |= AN_RES_INSPECT;
3189
Christopher Faulet11610f32017-09-21 10:23:10 +02003190 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3192
Christopher Faulet11610f32017-09-21 10:23:10 +02003193 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003194 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3195
Christopher Faulet11610f32017-09-21 10:23:10 +02003196 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003197 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3198
3199 return 1;
3200}
3201
3202/* Called when a filter instance is detached from a stream. It release the
3203 * attached SPOE context. */
3204static void
3205spoe_stop(struct stream *s, struct filter *filter)
3206{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003207 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003208 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3210 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003211 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003212}
3213
Christopher Fauletf7a30922016-11-10 15:04:51 +01003214
3215/*
3216 * Called when the stream is woken up because of expired timer.
3217 */
3218static void
3219spoe_check_timeouts(struct stream *s, struct filter *filter)
3220{
3221 struct spoe_context *ctx = filter->ctx;
3222
Christopher Fauletac580602018-03-20 16:09:20 +01003223 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003224 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003225}
3226
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227/* Called when we are ready to filter data on a channel */
3228static int
3229spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3230{
3231 struct spoe_context *ctx = filter->ctx;
3232 int ret = 1;
3233
3234 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3235 " - ctx-flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003236 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003237 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3238 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3239
Christopher Fauletb067b062017-01-04 16:39:11 +01003240 if (ctx->state == SPOE_CTX_ST_NONE)
3241 goto out;
3242
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003243 if (!(chn->flags & CF_ISRESP)) {
3244 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3245 chn->analysers |= AN_REQ_INSPECT_FE;
3246 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3247 chn->analysers |= AN_REQ_INSPECT_BE;
3248
3249 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3250 goto out;
3251
3252 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003253 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003254 if (!ret)
3255 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3257 }
3258 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003259 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 chn->analysers |= AN_RES_INSPECT;
3261
3262 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3263 goto out;
3264
Christopher Faulet8ef75252017-02-20 22:56:03 +01003265 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003266 if (!ret) {
3267 channel_dont_read(chn);
3268 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003269 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003270 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003271 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003272 }
3273
3274 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003275 return ret;
3276}
3277
3278/* Called before a processing happens on a given channel */
3279static int
3280spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3281 struct channel *chn, unsigned an_bit)
3282{
3283 struct spoe_context *ctx = filter->ctx;
3284 int ret = 1;
3285
3286 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3287 " - ctx-flags=0x%08x - ana=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003288 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3290 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3291 ctx->flags, an_bit);
3292
Christopher Fauletb067b062017-01-04 16:39:11 +01003293 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003294 goto out;
3295
3296 switch (an_bit) {
3297 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003298 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 break;
3300 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003301 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003302 break;
3303 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003304 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003305 break;
3306 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003307 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003308 break;
3309 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003310 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 break;
3312 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003313 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314 break;
3315 }
3316
3317 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003318 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003319 channel_dont_read(chn);
3320 channel_dont_close(chn);
3321 }
3322 return ret;
3323}
3324
3325/* Called when the filtering on the channel ends. */
3326static int
3327spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3328{
3329 struct spoe_context *ctx = filter->ctx;
3330
3331 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3332 " - ctx-flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003333 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003334 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3335 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3336
3337 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003338 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003339 }
3340
3341 return 1;
3342}
3343
3344/********************************************************************
3345 * Functions that manage the filter initialization
3346 ********************************************************************/
3347struct flt_ops spoe_ops = {
3348 /* Manage SPOE filter, called for each filter declaration */
3349 .init = spoe_init,
3350 .deinit = spoe_deinit,
3351 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003352 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353
3354 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003355 .attach = spoe_start,
3356 .detach = spoe_stop,
3357 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003358
3359 /* Handle channels activity */
3360 .channel_start_analyze = spoe_start_analyze,
3361 .channel_pre_analyze = spoe_chn_pre_analyze,
3362 .channel_end_analyze = spoe_end_analyze,
3363};
3364
3365
3366static int
3367cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3368{
3369 const char *err;
3370 int i, err_code = 0;
3371
3372 if ((cfg_scope == NULL && curengine != NULL) ||
3373 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003374 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003375 goto out;
3376
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003377 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003379 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3380 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 err_code |= ERR_ALERT | ERR_ABORT;
3382 goto out;
3383 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003384 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3385 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003386 goto out;
3387 }
3388
3389 err = invalid_char(args[1]);
3390 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003391 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3392 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003393 err_code |= ERR_ALERT | ERR_ABORT;
3394 goto out;
3395 }
3396
3397 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003398 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3399 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003400 err_code |= ERR_ALERT | ERR_ABORT;
3401 goto out;
3402 }
3403 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003404 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405 err_code |= ERR_ALERT | ERR_ABORT;
3406 goto out;
3407 }
3408
3409 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003410
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003411 curagent->conf.file = strdup(file);
3412 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003413
3414 curagent->timeout.hello = TICK_ETERNITY;
3415 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003416 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003417
Christopher Fauleta1cda022016-12-21 08:58:06 +01003418 curagent->var_pfx = NULL;
3419 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003420 curagent->var_t_process = NULL;
3421 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003422 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003423 curagent->cps_max = 0;
3424 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003425 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003426 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003427
3428 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003429 LIST_INIT(&curagent->events[i]);
3430 LIST_INIT(&curagent->groups);
3431 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003433 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003435 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3436 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 err_code |= ERR_ALERT | ERR_FATAL;
3438 goto out;
3439 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003440 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003441 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003442 free(curagent->b.name);
3443 curagent->b.name = strdup(args[1]);
3444 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003445 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003446 int cur_arg = 1;
3447 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003448 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449
Christopher Faulet11610f32017-09-21 10:23:10 +02003450 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003451 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3453 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 err_code |= ERR_ALERT | ERR_FATAL;
3455 goto out;
3456 }
3457 }
3458
Christopher Faulet11610f32017-09-21 10:23:10 +02003459 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003460 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003461 err_code |= ERR_ALERT | ERR_ABORT;
3462 goto out;
3463 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003464 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003465 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003466 cur_arg++;
3467 }
3468 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003469 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003470 int cur_arg = 1;
3471 while (*args[cur_arg]) {
3472 struct spoe_placeholder *ph = NULL;
3473
3474 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003475 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003476 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3477 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003478 err_code |= ERR_ALERT | ERR_FATAL;
3479 goto out;
3480 }
3481 }
3482
3483 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003484 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003485 err_code |= ERR_ALERT | ERR_ABORT;
3486 goto out;
3487 }
3488 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003489 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003490 cur_arg++;
3491 }
3492 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003493 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003494 unsigned int *tv = NULL;
3495 const char *res;
3496 unsigned timeout;
3497
3498 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003499 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3500 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003501 err_code |= ERR_ALERT | ERR_FATAL;
3502 goto out;
3503 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003504 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3505 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003506 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003507 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003508 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003509 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003510 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003511 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003512 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003513 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3514 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003515 err_code |= ERR_ALERT | ERR_FATAL;
3516 goto out;
3517 }
3518 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003519 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3520 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003521 err_code |= ERR_ALERT | ERR_FATAL;
3522 goto out;
3523 }
3524 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003525 if (res == PARSE_TIME_OVER) {
3526 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3527 file, linenum, args[2], args[0], args[1]);
3528 err_code |= ERR_ALERT | ERR_FATAL;
3529 goto out;
3530 }
3531 else if (res == PARSE_TIME_UNDER) {
3532 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3533 file, linenum, args[2], args[0], args[1]);
3534 err_code |= ERR_ALERT | ERR_FATAL;
3535 goto out;
3536 }
3537 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003538 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3539 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003540 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003541 goto out;
3542 }
3543 *tv = MS_TO_TICKS(timeout);
3544 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003545 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003546 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003547 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3548 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003549 err_code |= ERR_ALERT | ERR_FATAL;
3550 goto out;
3551 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003552
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003553 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003554 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003555 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003556 if (kwm == 1)
3557 curagent->flags &= ~SPOE_FL_PIPELINING;
3558 else
3559 curagent->flags |= SPOE_FL_PIPELINING;
3560 goto out;
3561 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003562 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003563 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003564 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003565 if (kwm == 1)
3566 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003567 else
3568 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003569 goto out;
3570 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003571 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003572 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3573 goto out;
3574 if (kwm == 1)
3575 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3576 else
3577 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3578 goto out;
3579 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003580 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003581 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3582 goto out;
3583 if (kwm == 1)
3584 curpxopts2 &= ~PR_O2_NOLOGNORM;
3585 else
3586 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003587 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003588 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003589
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003590 /* Following options does not support negation */
3591 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003592 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3593 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003594 err_code |= ERR_ALERT | ERR_FATAL;
3595 goto out;
3596 }
3597
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003598 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003599 char *tmp;
3600
3601 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003602 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3603 file, linenum, args[0],
3604 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003605 err_code |= ERR_ALERT | ERR_FATAL;
3606 goto out;
3607 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003608 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3609 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003610 tmp = args[2];
3611 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003612 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003613 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003614 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003615 err_code |= ERR_ALERT | ERR_FATAL;
3616 goto out;
3617 }
3618 tmp++;
3619 }
3620 curagent->var_pfx = strdup(args[2]);
3621 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003622 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003623 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3624 goto out;
3625 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3626 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003627 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003628 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003629 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003630 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3631 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003632 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003633 char *tmp;
3634
3635 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003636 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3637 file, linenum, args[0],
3638 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003642 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3643 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003644 tmp = args[2];
3645 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003646 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003647 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003648 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003649 err_code |= ERR_ALERT | ERR_FATAL;
3650 goto out;
3651 }
3652 tmp++;
3653 }
3654 curagent->var_on_error = strdup(args[2]);
3655 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003656 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003657 char *tmp;
3658
3659 if (!*args[2]) {
3660 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3661 file, linenum, args[0],
3662 args[1]);
3663 err_code |= ERR_ALERT | ERR_FATAL;
3664 goto out;
3665 }
3666 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3667 goto out;
3668 tmp = args[2];
3669 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003670 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003671 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003672 file, linenum, args[0], args[1]);
3673 err_code |= ERR_ALERT | ERR_FATAL;
3674 goto out;
3675 }
3676 tmp++;
3677 }
3678 curagent->var_t_process = strdup(args[2]);
3679 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003680 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003681 char *tmp;
3682
3683 if (!*args[2]) {
3684 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3685 file, linenum, args[0],
3686 args[1]);
3687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
3690 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3691 goto out;
3692 tmp = args[2];
3693 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003694 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003695 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003696 file, linenum, args[0], args[1]);
3697 err_code |= ERR_ALERT | ERR_FATAL;
3698 goto out;
3699 }
3700 tmp++;
3701 }
3702 curagent->var_t_total = strdup(args[2]);
3703 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003704 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003705 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3706 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003707 err_code |= ERR_ALERT | ERR_FATAL;
3708 goto out;
3709 }
Christopher Faulet48026722016-11-16 15:01:12 +01003710 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003711 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003712 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003713 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3714 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003715 err_code |= ERR_ALERT | ERR_FATAL;
3716 goto out;
3717 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003718 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003719 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003720 curagent->cps_max = atol(args[1]);
3721 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003722 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003723 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003724 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3725 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003726 err_code |= ERR_ALERT | ERR_FATAL;
3727 goto out;
3728 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003729 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003730 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003731 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003732 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003733 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003734 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003735 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3736 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003737 err_code |= ERR_ALERT | ERR_FATAL;
3738 goto out;
3739 }
3740 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3741 goto out;
3742 curagent->max_frame_size = atol(args[1]);
3743 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3744 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003745 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3746 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003747 err_code |= ERR_ALERT | ERR_FATAL;
3748 goto out;
3749 }
3750 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003751 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003752 if (!*args[1]) {
3753 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3754 file, linenum, args[0]);
3755 err_code |= ERR_ALERT | ERR_FATAL;
3756 goto out;
3757 }
3758 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3759 goto out;
3760 curagent->max_fpa = atol(args[1]);
3761 if (curagent->max_fpa < 1) {
3762 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3763 file, linenum, args[0]);
3764 err_code |= ERR_ALERT | ERR_FATAL;
3765 goto out;
3766 }
3767 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003768 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003769 int cur_arg;
3770
3771 if (!*args[1]) {
3772 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3773 file, linenum, args[0]);
3774 err_code |= ERR_ALERT | ERR_FATAL;
3775 goto out;
3776 }
3777 cur_arg = 1;
3778 while (*args[cur_arg]) {
3779 struct spoe_var_placeholder *vph;
3780
3781 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3782 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3783 err_code |= ERR_ALERT | ERR_ABORT;
3784 goto out;
3785 }
3786 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003787 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003788 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3789 err_code |= ERR_ALERT | ERR_ABORT;
3790 goto out;
3791 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003792 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003793 cur_arg++;
3794 }
3795 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003796 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003797 char *errmsg = NULL;
3798
Emeric Brun9533a702021-04-02 10:13:43 +02003799 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003800 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3801 err_code |= ERR_ALERT | ERR_FATAL;
3802 goto out;
3803 }
3804 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003805 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003806 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3807 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003808 err_code |= ERR_ALERT | ERR_FATAL;
3809 goto out;
3810 }
3811 out:
3812 return err_code;
3813}
Christopher Faulet11610f32017-09-21 10:23:10 +02003814static int
3815cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3816{
3817 struct spoe_group *grp;
3818 const char *err;
3819 int err_code = 0;
3820
3821 if ((cfg_scope == NULL && curengine != NULL) ||
3822 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003823 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003824 goto out;
3825
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003826 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003828 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3829 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003830 err_code |= ERR_ALERT | ERR_ABORT;
3831 goto out;
3832 }
3833 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3834 err_code |= ERR_ABORT;
3835 goto out;
3836 }
3837
3838 err = invalid_char(args[1]);
3839 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003840 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3841 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003842 err_code |= ERR_ALERT | ERR_ABORT;
3843 goto out;
3844 }
3845
3846 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003847 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003848 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3849 " name as another one declared at %s:%d.\n",
3850 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003851 err_code |= ERR_ALERT | ERR_FATAL;
3852 goto out;
3853 }
3854 }
3855
3856 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003857 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003858 err_code |= ERR_ALERT | ERR_ABORT;
3859 goto out;
3860 }
3861
3862 curgrp->id = strdup(args[1]);
3863 curgrp->conf.file = strdup(file);
3864 curgrp->conf.line = linenum;
3865 LIST_INIT(&curgrp->phs);
3866 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003867 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003868 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003869 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003870 int cur_arg = 1;
3871 while (*args[cur_arg]) {
3872 struct spoe_placeholder *ph = NULL;
3873
3874 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003875 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003876 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3877 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003878 err_code |= ERR_ALERT | ERR_FATAL;
3879 goto out;
3880 }
3881 }
3882
3883 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003884 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003885 err_code |= ERR_ALERT | ERR_ABORT;
3886 goto out;
3887 }
3888 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003889 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003890 cur_arg++;
3891 }
3892 }
3893 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003894 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3895 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003896 err_code |= ERR_ALERT | ERR_FATAL;
3897 goto out;
3898 }
3899 out:
3900 return err_code;
3901}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003902
3903static int
3904cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3905{
3906 struct spoe_message *msg;
3907 struct spoe_arg *arg;
3908 const char *err;
3909 char *errmsg = NULL;
3910 int err_code = 0;
3911
3912 if ((cfg_scope == NULL && curengine != NULL) ||
3913 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003914 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003915 goto out;
3916
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003917 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003919 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3920 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003921 err_code |= ERR_ALERT | ERR_ABORT;
3922 goto out;
3923 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003924 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3925 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003926 goto out;
3927 }
3928
3929 err = invalid_char(args[1]);
3930 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003931 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3932 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003933 err_code |= ERR_ALERT | ERR_ABORT;
3934 goto out;
3935 }
3936
3937 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003938 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003939 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3940 " name as another one declared at %s:%d.\n",
3941 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003942 err_code |= ERR_ALERT | ERR_FATAL;
3943 goto out;
3944 }
3945 }
3946
3947 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003948 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003949 err_code |= ERR_ALERT | ERR_ABORT;
3950 goto out;
3951 }
3952
3953 curmsg->id = strdup(args[1]);
3954 curmsg->id_len = strlen(curmsg->id);
3955 curmsg->event = SPOE_EV_NONE;
3956 curmsg->conf.file = strdup(file);
3957 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003958 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003959 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003960 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003961 LIST_INIT(&curmsg->by_evt);
3962 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003963 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003964 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003965 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003966 int cur_arg = 1;
3967
3968 curproxy->conf.args.ctx = ARGC_SPOE;
3969 curproxy->conf.args.file = file;
3970 curproxy->conf.args.line = linenum;
3971 while (*args[cur_arg]) {
3972 char *delim = strchr(args[cur_arg], '=');
3973 int idx = 0;
3974
3975 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003976 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003977 err_code |= ERR_ALERT | ERR_ABORT;
3978 goto out;
3979 }
3980
3981 if (!delim) {
3982 arg->name = NULL;
3983 arg->name_len = 0;
3984 delim = args[cur_arg];
3985 }
3986 else {
3987 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3988 arg->name_len = delim - args[cur_arg];
3989 delim++;
3990 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003991 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3992 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003993 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003994 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003995 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003996 err_code |= ERR_ALERT | ERR_FATAL;
3997 free(arg->name);
3998 free(arg);
3999 goto out;
4000 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004001 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004002 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004003 cur_arg++;
4004 }
4005 curproxy->conf.args.file = NULL;
4006 curproxy->conf.args.line = 0;
4007 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004008 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004009 err = invalid_char(args[1]);
4010 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004011 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4012 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004013 err_code |= ERR_ALERT | ERR_FATAL;
4014 goto out;
4015 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004016 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004017 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004018 "logical disjunction within a condition.\n",
4019 file, linenum, args[1]);
4020 err_code |= ERR_ALERT | ERR_FATAL;
4021 goto out;
4022 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004023 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004024 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4025 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004026 err_code |= ERR_ALERT | ERR_FATAL;
4027 goto out;
4028 }
4029 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004030 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004031 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004032 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004033 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004034 goto out;
4035 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004036 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4037 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004038
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004039 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004040 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004041 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004042 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4043
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004044 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004045 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004046 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004047 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004048 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004049 curmsg->event = SPOE_EV_ON_TCP_RSP;
4050
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004051 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004052 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004053 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004054 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004055 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004056 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4057 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004058 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004059 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004060 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004061 goto out;
4062 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004063
4064 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4065 struct acl_cond *cond;
4066
4067 cond = build_acl_cond(file, linenum, &curmsg->acls,
4068 curproxy, (const char **)args+2,
4069 &errmsg);
4070 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004071 ha_alert("parsing [%s:%d] : error detected while "
4072 "parsing an 'event %s' condition : %s.\n",
4073 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004074 err_code |= ERR_ALERT | ERR_FATAL;
4075 goto out;
4076 }
4077 curmsg->cond = cond;
4078 }
4079 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004080 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4081 "or 'unless' followed by a condition but found '%s'.\n",
4082 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004083 err_code |= ERR_ALERT | ERR_FATAL;
4084 goto out;
4085 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004086 }
4087 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004088 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4089 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004090 err_code |= ERR_ALERT | ERR_FATAL;
4091 goto out;
4092 }
4093 out:
4094 free(errmsg);
4095 return err_code;
4096}
4097
4098/* Return -1 on error, else 0 */
4099static int
4100parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4101 struct flt_conf *fconf, char **err, void *private)
4102{
4103 struct list backup_sections;
4104 struct spoe_config *conf;
4105 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004106 struct spoe_group *grp, *grpback;
4107 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004108 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004109 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004110 char *file = NULL, *engine = NULL;
4111 int ret, pos = *cur_arg + 1;
4112
Christopher Faulet84c844e2018-03-23 14:37:14 +01004113 LIST_INIT(&curmsgs);
4114 LIST_INIT(&curgrps);
4115 LIST_INIT(&curmphs);
4116 LIST_INIT(&curgphs);
4117 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004118 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004119 curpxopts = 0;
4120 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004121
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004122 conf = calloc(1, sizeof(*conf));
4123 if (conf == NULL) {
4124 memprintf(err, "%s: out of memory", args[*cur_arg]);
4125 goto error;
4126 }
4127 conf->proxy = px;
4128
4129 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004130 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004131 if (!*args[pos+1]) {
4132 memprintf(err, "'%s' : '%s' option without value",
4133 args[*cur_arg], args[pos]);
4134 goto error;
4135 }
4136 file = args[pos+1];
4137 pos += 2;
4138 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004139 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004140 if (!*args[pos+1]) {
4141 memprintf(err, "'%s' : '%s' option without value",
4142 args[*cur_arg], args[pos]);
4143 goto error;
4144 }
4145 engine = args[pos+1];
4146 pos += 2;
4147 }
4148 else {
4149 memprintf(err, "unknown keyword '%s'", args[pos]);
4150 goto error;
4151 }
4152 }
4153 if (file == NULL) {
4154 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4155 goto error;
4156 }
4157
4158 /* backup sections and register SPOE sections */
4159 LIST_INIT(&backup_sections);
4160 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004161 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4162 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004163 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004164
4165 /* Parse SPOE filter configuration file */
4166 curengine = engine;
4167 curproxy = px;
4168 curagent = NULL;
4169 curmsg = NULL;
4170 ret = readcfgfile(file);
4171 curproxy = NULL;
4172
4173 /* unregister SPOE sections and restore previous sections */
4174 cfg_unregister_sections();
4175 cfg_restore_sections(&backup_sections);
4176
4177 if (ret == -1) {
4178 memprintf(err, "Could not open configuration file %s : %s",
4179 file, strerror(errno));
4180 goto error;
4181 }
4182 if (ret & (ERR_ABORT|ERR_FATAL)) {
4183 memprintf(err, "Error(s) found in configuration file %s", file);
4184 goto error;
4185 }
4186
4187 /* Check SPOE agent */
4188 if (curagent == NULL) {
4189 memprintf(err, "No SPOE agent found in file %s", file);
4190 goto error;
4191 }
4192 if (curagent->b.name == NULL) {
4193 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4194 curagent->id, curagent->conf.file, curagent->conf.line);
4195 goto error;
4196 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004197 if (curagent->timeout.hello == TICK_ETERNITY ||
4198 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004199 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004200 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4201 " | While not properly invalid, you will certainly encounter various problems\n"
4202 " | with such a configuration. To fix this, please ensure that all following\n"
4203 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4204 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004205 }
4206 if (curagent->var_pfx == NULL) {
4207 char *tmp = curagent->id;
4208
4209 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004210 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004211 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4212 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4213 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4214 goto error;
4215 }
4216 tmp++;
4217 }
4218 curagent->var_pfx = strdup(curagent->id);
4219 }
4220
Christopher Fauletb7426d12018-03-21 14:12:17 +01004221 if (curagent->var_on_error) {
4222 struct arg arg;
4223
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004224 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004225 curagent->var_pfx, curagent->var_on_error);
4226
4227 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004228 arg.data.str.area = trash.area;
4229 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004230 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004231 if (!vars_check_arg(&arg, err)) {
4232 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4233 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4234 goto error;
4235 }
4236 }
4237
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004238 if (curagent->var_t_process) {
4239 struct arg arg;
4240
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004241 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004242 curagent->var_pfx, curagent->var_t_process);
4243
4244 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004245 arg.data.str.area = trash.area;
4246 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004247 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004248 if (!vars_check_arg(&arg, err)) {
4249 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4250 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4251 goto error;
4252 }
4253 }
4254
4255 if (curagent->var_t_total) {
4256 struct arg arg;
4257
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004258 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004259 curagent->var_pfx, curagent->var_t_total);
4260
4261 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004262 arg.data.str.area = trash.area;
4263 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004264 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004265 if (!vars_check_arg(&arg, err)) {
4266 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4267 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4268 goto error;
4269 }
4270 }
4271
Christopher Faulet11610f32017-09-21 10:23:10 +02004272 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004273 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4274 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004275 goto finish;
4276 }
4277
Christopher Faulet11610f32017-09-21 10:23:10 +02004278 /* Replace placeholders by the corresponding messages for the SPOE
4279 * agent */
4280 list_for_each_entry(ph, &curmphs, list) {
4281 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004282 struct spoe_arg *arg;
4283 unsigned int where;
4284
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004285 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004286 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4287 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4288 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4289 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4290 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4291 }
4292 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4293 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4294 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004295 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4296 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004297 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004298 }
4299 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004300 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4301 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004302 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004303 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004304
4305 where = 0;
4306 switch (msg->event) {
4307 case SPOE_EV_ON_CLIENT_SESS:
4308 where |= SMP_VAL_FE_CON_ACC;
4309 break;
4310
4311 case SPOE_EV_ON_TCP_REQ_FE:
4312 where |= SMP_VAL_FE_REQ_CNT;
4313 break;
4314
4315 case SPOE_EV_ON_HTTP_REQ_FE:
4316 where |= SMP_VAL_FE_HRQ_HDR;
4317 break;
4318
4319 case SPOE_EV_ON_TCP_REQ_BE:
4320 if (px->cap & PR_CAP_FE)
4321 where |= SMP_VAL_FE_REQ_CNT;
4322 if (px->cap & PR_CAP_BE)
4323 where |= SMP_VAL_BE_REQ_CNT;
4324 break;
4325
4326 case SPOE_EV_ON_HTTP_REQ_BE:
4327 if (px->cap & PR_CAP_FE)
4328 where |= SMP_VAL_FE_HRQ_HDR;
4329 if (px->cap & PR_CAP_BE)
4330 where |= SMP_VAL_BE_HRQ_HDR;
4331 break;
4332
4333 case SPOE_EV_ON_SERVER_SESS:
4334 where |= SMP_VAL_BE_SRV_CON;
4335 break;
4336
4337 case SPOE_EV_ON_TCP_RSP:
4338 if (px->cap & PR_CAP_FE)
4339 where |= SMP_VAL_FE_RES_CNT;
4340 if (px->cap & PR_CAP_BE)
4341 where |= SMP_VAL_BE_RES_CNT;
4342 break;
4343
4344 case SPOE_EV_ON_HTTP_RSP:
4345 if (px->cap & PR_CAP_FE)
4346 where |= SMP_VAL_FE_HRS_HDR;
4347 if (px->cap & PR_CAP_BE)
4348 where |= SMP_VAL_BE_HRS_HDR;
4349 break;
4350
4351 default:
4352 break;
4353 }
4354
4355 list_for_each_entry(arg, &msg->args, list) {
4356 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004357 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004358 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004359 "none of which is available here ('%s')",
4360 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004361 sample_ckp_names(arg->expr->fetch->use),
4362 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004363 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004364 }
4365 }
4366
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004367 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004368 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004369 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004370 }
4371 }
4372 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004373 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004374 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004375 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004376 continue;
4377 }
4378
Christopher Faulet11610f32017-09-21 10:23:10 +02004379 /* Replace placeholders by the corresponding groups for the SPOE
4380 * agent */
4381 list_for_each_entry(ph, &curgphs, list) {
4382 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004383 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004384 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004385 LIST_DELETE(&grp->list);
4386 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004387 goto next_aph;
4388 }
4389 }
4390 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4391 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4392 goto error;
4393 next_aph:
4394 continue;
4395 }
4396
4397 /* Replace placeholders by the corresponding message for each SPOE
4398 * group of the SPOE agent */
4399 list_for_each_entry(grp, &curagent->groups, list) {
4400 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4401 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004402 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004403 if (msg->group != NULL) {
4404 memprintf(err, "SPOE message '%s' already belongs to "
4405 "the SPOE group '%s' declare at %s:%d",
4406 msg->id, msg->group->id,
4407 msg->group->conf.file,
4408 msg->group->conf.line);
4409 goto error;
4410 }
4411
4412 /* Scope for arguments are not checked for now. We will check
4413 * them only if a rule use the corresponding SPOE group. */
4414 msg->agent = curagent;
4415 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004416 LIST_DELETE(&ph->list);
4417 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004418 goto next_mph_grp;
4419 }
4420 }
4421 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4422 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4423 goto error;
4424 next_mph_grp:
4425 continue;
4426 }
4427 }
4428
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004429 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004430 /* move curmsgs to the agent message list */
4431 curmsgs.n->p = &curagent->messages;
4432 curmsgs.p->n = &curagent->messages;
4433 curagent->messages = curmsgs;
4434 LIST_INIT(&curmsgs);
4435
Christopher Faulet7ee86672017-09-19 11:08:28 +02004436 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004437 conf->agent = curagent;
Christopher Faulet434b8522021-08-02 17:51:01 +02004438 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004439
4440 /* Start agent's proxy initialization here. It will be finished during
4441 * the filter init. */
4442 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4443 init_new_proxy(&conf->agent_fe);
4444 conf->agent_fe.id = conf->agent->id;
4445 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004446 conf->agent_fe.options |= curpxopts;
4447 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004448
4449 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004450 LIST_DELETE(&logsrv->list);
4451 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004452 }
4453
Christopher Faulet11610f32017-09-21 10:23:10 +02004454 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004455 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004456 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004457 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004458 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004459 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004460 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004461 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004462 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4463 struct arg arg;
4464
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004465 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004466 curagent->var_pfx, vph->name);
4467
4468 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004469 arg.data.str.area = trash.area;
4470 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004471 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004472 if (!vars_check_arg(&arg, err)) {
4473 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4474 curagent->id, curagent->var_pfx, vph->name, *err);
4475 goto error;
4476 }
4477
Willy Tarreau2b718102021-04-21 07:32:39 +02004478 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004479 free(vph->name);
4480 free(vph);
4481 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004482 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004483 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004484 spoe_release_group(grp);
4485 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004486 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004487 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004488 fconf->ops = &spoe_ops;
4489 fconf->conf = conf;
4490 return 0;
4491
4492 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004493 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004494 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004495 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004496 spoe_release_placeholder(ph);
4497 }
4498 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004499 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004500 spoe_release_placeholder(ph);
4501 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004502 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004503 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004504 free(vph->name);
4505 free(vph);
4506 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004507 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004508 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004509 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004510 }
4511 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004512 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004513 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004514 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004515 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004516 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004517 free(logsrv);
4518 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004519 free(conf);
4520 return -1;
4521}
4522
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004523/* Send message of a SPOE group. This is the action_ptr callback of a rule
4524 * associated to a "send-spoe-group" action.
4525 *
Christopher Faulet13403762019-12-13 09:01:57 +01004526 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4527 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004528static enum act_return
4529spoe_send_group(struct act_rule *rule, struct proxy *px,
4530 struct session *sess, struct stream *s, int flags)
4531{
4532 struct filter *filter;
4533 struct spoe_agent *agent = NULL;
4534 struct spoe_group *group = NULL;
4535 struct spoe_context *ctx = NULL;
4536 int ret, dir;
4537
4538 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4539 if (filter->config == rule->arg.act.p[0]) {
4540 agent = rule->arg.act.p[2];
4541 group = rule->arg.act.p[3];
4542 ctx = filter->ctx;
4543 break;
4544 }
4545 }
4546 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004547 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004548 if (ctx->state == SPOE_CTX_ST_NONE)
4549 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004550
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004551 switch (rule->from) {
4552 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4553 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4554 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4555 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4556 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4557 default:
4558 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4559 " - internal error while execute spoe-send-group\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02004560 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004561 __FUNCTION__, s);
4562 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4563 agent->id);
4564 return ACT_RET_CONT;
4565 }
4566
4567 ret = spoe_process_group(s, ctx, group, dir);
4568 if (ret == 1)
4569 return ACT_RET_CONT;
4570 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004571 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004572 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4573 " - failed to process group '%s': interrupted by caller\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02004574 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004575 agent->id, __FUNCTION__, s, group->id);
4576 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004577 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004578 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004579 return ACT_RET_CONT;
4580 }
4581 return ACT_RET_YIELD;
4582 }
4583 else
Christopher Faulet13403762019-12-13 09:01:57 +01004584 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004585}
4586
4587/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4588 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4589 * action should be:
4590 *
4591 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4592 *
4593 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4594 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4595 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4596 * group.
4597 *
4598 * The function returns 1 in success case, otherwise, it returns 0 and err is
4599 * filled.
4600 */
4601static int
4602check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4603{
4604 struct flt_conf *fconf;
4605 struct spoe_config *conf;
4606 struct spoe_agent *agent = NULL;
4607 struct spoe_group *group;
4608 struct spoe_message *msg;
4609 char *engine_id = rule->arg.act.p[0];
4610 char *group_id = rule->arg.act.p[1];
4611 unsigned int where = 0;
4612
4613 switch (rule->from) {
4614 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4615 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4616 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4617 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4618 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4619 default:
4620 memprintf(err,
4621 "internal error, unexpected rule->from=%d, please report this bug!",
4622 rule->from);
4623 goto error;
4624 }
4625
4626 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4627 * <px> */
4628 list_for_each_entry(fconf, &px->filter_configs, list) {
4629 conf = fconf->conf;
4630
4631 /* This is not an SPOE filter */
4632 if (fconf->id != spoe_filter_id)
4633 continue;
4634
4635 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004636 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004637 agent = conf->agent;
4638 break;
4639 }
4640 }
4641 if (agent == NULL) {
4642 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4643 engine_id, group_id);
4644 goto error;
4645 }
4646
4647 /* Try to find the right group */
4648 list_for_each_entry(group, &agent->groups, list) {
4649 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004650 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004651 break;
4652 }
4653 if (&group->list == &agent->groups) {
4654 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4655 group_id, engine_id);
4656 goto error;
4657 }
4658
4659 /* Ok, we found the group, we need to check messages and their
4660 * arguments */
4661 list_for_each_entry(msg, &group->messages, by_grp) {
4662 struct spoe_arg *arg;
4663
4664 list_for_each_entry(arg, &msg->args, list) {
4665 if (!(arg->expr->fetch->val & where)) {
4666 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4667 "some args extract information from '%s',"
4668 "none of which is available here ('%s')",
4669 msg->id, group->id, msg->conf.file, msg->conf.line,
4670 sample_ckp_names(arg->expr->fetch->use),
4671 sample_ckp_names(where));
4672 goto error;
4673 }
4674 }
4675 }
4676
4677 free(engine_id);
4678 free(group_id);
4679 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4680 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4681 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4682 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4683 return 1;
4684
4685 error:
4686 free(engine_id);
4687 free(group_id);
4688 return 0;
4689}
4690
4691/* Parse 'send-spoe-group' action following the format:
4692 *
4693 * ... send-spoe-group <engine-id> <group-id>
4694 *
4695 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4696 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4697 * ids are saved and used later, when the rule will be checked.
4698 */
4699static enum act_parse_ret
4700parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4701 struct act_rule *rule, char **err)
4702{
4703 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4704 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4705 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4706 return ACT_RET_PRS_ERR;
4707 }
4708 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4709 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4710
4711 (*orig_arg) += 2;
4712
4713 rule->action = ACT_CUSTOM;
4714 rule->action_ptr = spoe_send_group;
4715 rule->check_ptr = check_send_spoe_group;
4716 return ACT_RET_PRS_OK;
4717}
4718
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004719
4720/* Declare the filter parser for "spoe" keyword */
4721static struct flt_kw_list flt_kws = { "SPOE", { }, {
4722 { "spoe", parse_spoe_flt, NULL },
4723 { NULL, NULL, NULL },
4724 }
4725};
4726
Willy Tarreau0108d902018-11-25 19:14:37 +01004727INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4728
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004729/* Delcate the action parser for "spoe-action" keyword */
4730static struct action_kw_list tcp_req_action_kws = { { }, {
4731 { "send-spoe-group", parse_send_spoe_group },
4732 { /* END */ },
4733 }
4734};
Willy Tarreau0108d902018-11-25 19:14:37 +01004735
4736INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4737
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004738static struct action_kw_list tcp_res_action_kws = { { }, {
4739 { "send-spoe-group", parse_send_spoe_group },
4740 { /* END */ },
4741 }
4742};
Willy Tarreau0108d902018-11-25 19:14:37 +01004743
4744INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4745
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004746static struct action_kw_list http_req_action_kws = { { }, {
4747 { "send-spoe-group", parse_send_spoe_group },
4748 { /* END */ },
4749 }
4750};
Willy Tarreau0108d902018-11-25 19:14:37 +01004751
4752INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4753
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004754static struct action_kw_list http_res_action_kws = { { }, {
4755 { "send-spoe-group", parse_send_spoe_group },
4756 { /* END */ },
4757 }
4758};
4759
Willy Tarreau0108d902018-11-25 19:14:37 +01004760INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);