blob: 0d5d44805ebdff6626b45a77ab90dfc5960e787b [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 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100173 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100174 free(agent->var_t_process);
175 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200176 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200177 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 spoe_release_message(msg);
179 }
180 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200181 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200182 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200183 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200185 for (i = 0; i < global.nbthread; ++i) {
186 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200188 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100189 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200190 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200191 free(agent);
192}
193
194static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100195 [SPOE_FRM_ERR_NONE] = "normal",
196 [SPOE_FRM_ERR_IO] = "I/O error",
197 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
198 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
199 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
200 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
201 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
202 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
203 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
204 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
205 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
206 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100207 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100208 [SPOE_FRM_ERR_RES] = "resource allocation error",
209 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200210};
211
212static const char *spoe_event_str[SPOE_EV_EVENTS] = {
213 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
214 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
215 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
216 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
217 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
218
219 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
220 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
221 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
222};
223
224
225#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
226
227static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100228 [SPOE_CTX_ST_NONE] = "NONE",
229 [SPOE_CTX_ST_READY] = "READY",
230 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
231 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
232 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
233 [SPOE_CTX_ST_DONE] = "DONE",
234 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200235};
236
237static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100238 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
239 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
240 [SPOE_APPCTX_ST_IDLE] = "IDLE",
241 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
242 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
243 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
244 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
245 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
246 [SPOE_APPCTX_ST_EXIT] = "EXIT",
247 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200248};
249
250#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100251
Christopher Faulet8ef75252017-02-20 22:56:03 +0100252/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500253 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100254 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100255static char *
256generate_pseudo_uuid()
257{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100258 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800259 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100260}
261
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100262
263static inline void
264spoe_update_stat_time(struct timeval *tv, long *t)
265{
266 if (*t == -1)
267 *t = tv_ms_elapsed(tv, &now);
268 else
269 *t += tv_ms_elapsed(tv, &now);
270 tv_zero(tv);
271}
272
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/********************************************************************
274 * Functions that encode/decode SPOE frames
275 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200276/* Helper to get static string length, excluding the terminating null byte */
277#define SLEN(str) (sizeof(str)-1)
278
279/* Predefined key used in HELLO/DISCONNECT frames */
280#define SUPPORTED_VERSIONS_KEY "supported-versions"
281#define VERSION_KEY "version"
282#define MAX_FRAME_SIZE_KEY "max-frame-size"
283#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100284#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100285#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200286#define STATUS_CODE_KEY "status-code"
287#define MSG_KEY "message"
288
289struct spoe_version {
290 char *str;
291 int min;
292 int max;
293};
294
295/* All supported versions */
296static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200297 /* 1.0 is now unsupported because of a bug about frame's flags*/
298 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299 {NULL, 0, 0}
300};
301
302/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200303#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305/* Convert a string to a SPOE version value. The string must follow the format
306 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
307 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 const char *p, *end;
312 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200313
Christopher Faulet8ef75252017-02-20 22:56:03 +0100314 p = str;
315 end = str+len;
316 maj = min = 0;
317 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200318
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100320 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200322
Christopher Faulet8ef75252017-02-20 22:56:03 +0100323 /* parse Major number, until the '.' */
324 while (*p != '.') {
325 if (p >= end || *p < '0' || *p > '9')
326 goto out;
327 maj *= 10;
328 maj += (*p - '0');
329 p++;
330 }
331
332 /* check Major version */
333 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334 goto out;
335
Christopher Faulet8ef75252017-02-20 22:56:03 +0100336 p++; /* skip the '.' */
337 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
338 goto out;
339
340 /* Parse Minor number */
341 while (p < end) {
342 if (*p < '0' || *p > '9')
343 break;
344 min *= 10;
345 min += (*p - '0');
346 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200347 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100348
349 /* check Minor number */
350 if (min > 999)
351 goto out;
352
353 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100354 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++;
356 if (p != end)
357 goto out;
358
359 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200360 out:
361 return vsn;
362}
363
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800365 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200367static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369{
Willy Tarreau83061a82018-07-13 11:56:34 +0200370 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100371 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372 char *p, *end;
373 unsigned int flags = SPOE_FRM_FL_FIN;
374 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 p = frame;
377 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379 /* Set Frame type */
380 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100382 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200383 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 memcpy(p, (char *)&flags, 4);
385 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200389
390 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
391 * and "capabilities" */
392
393 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 sz = SLEN(SUPPORTED_VERSIONS_KEY);
395 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
396 goto too_big;
397
398 *p++ = SPOE_DATA_T_STR;
399 sz = SLEN(SUPPORTED_VERSIONS_VAL);
400 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
401 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
403 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 sz = SLEN(MAX_FRAME_SIZE_KEY);
405 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
406 goto too_big;
407
408 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200409 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
412 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(CAPABILITIES_KEY);
414 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
415 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200416
Christopher Faulet8ef75252017-02-20 22:56:03 +0100417 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100418 chk = get_trash_chunk();
419 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200420 memcpy(chk->area, "pipelining", 10);
421 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100422 }
423 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 if (chk->data) chk->area[chk->data++] = ',';
425 memcpy(chk->area+chk->data, "async", 5);
426 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100427 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100428 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 if (chk->data) chk->area[chk->data++] = ',';
430 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100431 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100432 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200433 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 goto too_big;
435
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500436 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200437 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 sz = SLEN(ENGINE_ID_KEY);
439 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
440 goto too_big;
441
442 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200443 sz = strlen(agent->rt[tid].engine_id);
444 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100446 }
447
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448 return (p - frame);
449
450 too_big:
451 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
452 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453}
454
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
456 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
457 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200460{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 const char *reason;
462 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100463 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 p = frame;
467 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 /* Set Frame type */
470 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100472 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200473 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474 memcpy(p, (char *)&flags, 4);
475 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476
477 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 *p++ = 0; *p++ = 0;
479
480 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
481 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482
483 /* There are 2 mandatory items: "status-code" and "message" */
484
485 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 sz = SLEN(STATUS_CODE_KEY);
487 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
488 goto too_big;
489
490 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200491 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
494 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 sz = SLEN(MSG_KEY);
496 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
497 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200498
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499 /*Get the message corresponding to the status code */
500 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
501
502 *p++ = SPOE_DATA_T_STR;
503 sz = strlen(reason);
504 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
505 goto too_big;
506
507 return (p - frame);
508
509 too_big:
510 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
511 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512}
513
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
515 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
516 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100519 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 char *p, *end;
522 unsigned int stream_id, frame_id;
523 unsigned int flags = SPOE_FRM_FL_FIN;
524 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Faulet8ef75252017-02-20 22:56:03 +0100526 p = frame;
527 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200528
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100529 stream_id = ctx->stream_id;
530 frame_id = ctx->frame_id;
531
532 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
533 /* The fragmentation is not supported by the applet */
534 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
535 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
536 return -1;
537 }
538 flags = ctx->frag_ctx.flags;
539 }
540
541 /* Set Frame type */
542 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
543
544 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200545 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 memcpy(p, (char *)&flags, 4);
547 p += 4;
548
549 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200550 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200552 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 goto too_big;
554
555 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 if (p + sz >= end)
558 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200559 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100560 p += sz;
561
562 return (p - frame);
563
564 too_big:
565 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
566 return 0;
567}
568
569/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
570 * returns the number of encoded bytes in the frame on success, 0 if an encoding
571 * error occurred and -1 if a fatal error occurred. */
572static int
573spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
574 char *frame, size_t size)
575{
576 char *p, *end;
577 unsigned int stream_id, frame_id;
578 unsigned int flags;
579 size_t sz;
580
581 p = frame;
582 end = frame+size;
583
Christopher Faulet8ef75252017-02-20 22:56:03 +0100584 /* <ctx> is null when the stream has aborted the processing of a
585 * fragmented frame. In this case, we must notify the corresponding
586 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100587 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100589 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
590 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
591 }
592 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 stream_id = ctx->stream_id;
595 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 }
597
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100599 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200602 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 memcpy(p, (char *)&flags, 4);
604 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200605
606 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200607 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200609 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200611
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 if (ctx == NULL)
613 goto end;
614
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200616 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 if (p + sz >= end)
618 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200619 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100621
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100622 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 return (p - frame);
624
625 too_big:
626 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
627 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628}
629
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630/* Decode and process the HELLO frame sent by an agent. It returns the number of
631 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
632 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635{
Christopher Faulet305c6072017-02-23 16:17:53 +0100636 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
637 char *p, *end;
638 int vsn, max_frame_size;
639 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640
641 p = frame;
642 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643
644 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 if (size < 7 /* TYPE + METADATA */) {
651 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
652 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 }
654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200657 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100660 /* Fragmentation is not supported for HELLO frame */
661 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100663 return -1;
664 }
665
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 if (*p != 0 || *(p+1) != 0) {
668 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
669 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672
673 /* There are 3 mandatory items: "version", "max-frame-size" and
674 * "capabilities" */
675
676 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100677 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 while (p < end) {
679 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200680 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682
683 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 ret = spoe_decode_buffer(&p, end, &str, &sz);
685 if (ret == -1 || !sz) {
686 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
687 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200691 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 int i, type = *p++;
693
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
696 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
697 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200698 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
701 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 }
703
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 return -1;
708 }
709 for (i = 0; supported_versions[i].str != NULL; ++i) {
710 if (vsn >= supported_versions[i].min &&
711 vsn <= supported_versions[i].max)
712 break;
713 }
714 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 return -1;
717 }
718 }
719 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200720 else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100721 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722
723 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
725 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
726 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
727 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
729 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200731 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
733 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 if (sz < MIN_FRAME_SIZE ||
736 sz > SPOE_APPCTX(appctx)->max_frame_size) {
737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738 return -1;
739 }
740 max_frame_size = sz;
741 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200743 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100744 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100745
746 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
748 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
749 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
752 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
753 return 0;
754 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 char *delim;
758
759 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100760 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
763 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100764 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765 flags |= SPOE_APPCTX_FL_PIPELINING;
766 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 else if (sz >= 5 && !strncmp(str, "async", 5)) {
768 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100769 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 flags |= SPOE_APPCTX_FL_ASYNC;
771 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
773 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100774 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100775 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
776 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 /* Get the next comma or break */
779 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 delim++;
782 sz -= (delim - str);
783 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 }
785 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 else {
787 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 if (spoe_skip_data(&p, end) == -1) {
789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
790 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792 }
793 }
794
795 /* Final checks */
796 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200798 return -1;
799 }
800 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100801 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802 return -1;
803 }
Christopher Faulet11389012019-02-07 16:13:26 +0100804 if (!agent)
805 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
806 else {
807 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
808 flags &= ~SPOE_APPCTX_FL_PIPELINING;
809 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
810 flags &= ~SPOE_APPCTX_FL_ASYNC;
811 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812
Christopher Faulet42bfa462017-01-04 14:14:19 +0100813 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
814 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
815 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816
817 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818}
819
820/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
821 * bytes on success, 0 if the frame can be ignored and -1 if an error
822 * occurred. */
823static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100827 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828
829 p = frame;
830 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831
832 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
834 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838 if (size < 7 /* TYPE + METADATA */) {
839 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
840 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 }
842
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200845 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 /* Fragmentation is not supported for DISCONNECT frame */
849 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 return -1;
852 }
853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (*p != 0 || *(p+1) != 0) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860
861 /* There are 2 mandatory items: "status-code" and "message" */
862
863 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 while (p < end) {
865 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200866 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868
869 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 ret = spoe_decode_buffer(&p, end, &str, &sz);
871 if (ret == -1 || !sz) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
875
876 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200877 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879
880 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
883 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200888 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
890 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
894
895 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200896 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 int type = *p++;
898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 ret = spoe_decode_buffer(&p, end, &str, &sz);
905 if (ret == -1 || sz > 255) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
910 SPOE_APPCTX(appctx)->reason = str;
911 SPOE_APPCTX(appctx)->rlen = sz;
912#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
914 else {
915 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 if (spoe_skip_data(&p, end) == -1) {
917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921 }
922
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924}
925
926
Christopher Fauleta1cda022016-12-21 08:58:06 +0100927/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 * success, 0 if the frame can be ignored and -1 if an error occurred. */
929static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938
939 p = frame;
940 end = frame + size;
941 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942
943 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (size < 7 /* TYPE + METADATA */) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
951 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 }
953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200956 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 /* Fragmentation is not supported for now */
960 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100962 return -1;
963 }
964
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200965 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200966 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100968 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200970 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200972 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100974
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100976 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200977 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 if ((*ctx)->stream_id == (unsigned int)stream_id &&
979 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100980 goto found;
981 }
982 }
983 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100984 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
985 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100987 goto found;
988 }
989 }
990
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100991 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
992 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
993 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
994
995 /* ABRT bit is set for an unfinished fragmented frame */
996 if (flags & SPOE_FRM_FL_ABRT) {
997 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100998 (*ctx)->state = SPOE_CTX_ST_ERROR;
999 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1000 /* Ignore the payload */
1001 goto end;
1002 }
1003 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1004 /* For now, we ignore the ack */
1005 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1006 return 0;
1007 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008
Christopher Fauleta1cda022016-12-21 08:58:06 +01001009 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1011 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 " - stream-id=%u - frame-id=%u\n",
1013 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1014 __FUNCTION__, appctx,
1015 (unsigned int)stream_id, (unsigned int)frame_id);
1016
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001017 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001018 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1019 /* Report an error if we are waiting the ack for another frame,
1020 * but not if there is no longer frame waiting for a ack
1021 * (timeout)
1022 */
1023 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1024 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1025 return -1;
1026 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1027 SPOE_APPCTX(appctx)->cur_fpa = 0;
1028 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029 return 0;
1030
1031 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1033 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001034 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001035 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001037
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001038 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001040 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1041 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001043
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 /* Transfer the buffer ownership to the SPOE context */
1045 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001046 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001047
Christopher Faulet8ef75252017-02-20 22:56:03 +01001048 (*ctx)->state = SPOE_CTX_ST_DONE;
1049
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001050 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 " - ACK frame received"
1053 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001054 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1056 (*ctx)->frame_id, flags);
1057 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001058}
1059
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060/* This function is used in cfgparse.c and declared in proto/checks.h. It
1061 * prepare the request to send to agents during a healthcheck. It returns 0 on
1062 * success and -1 if an error occurred. */
1063int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001066 struct appctx appctx;
1067 struct spoe_appctx spoe_appctx;
1068 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1069 size_t sz;
1070 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072 memset(&appctx, 0, sizeof(appctx));
1073 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001075
Willy Tarreau23a24072022-05-05 20:18:44 +02001076 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001077 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1080 end = frame + MAX_FRAME_SIZE;
1081
1082 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1083 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 /* Add "healthcheck" K/V item */
1088 sz = SLEN(HEALTHCHECK_KEY);
1089 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1090 return -1;
1091 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 *len = frame - buf;
1094 sz = htonl(*len - 4);
1095 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001099 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100 return 0;
1101}
1102
1103/* This function is used in checks.c and declared in proto/checks.h. It decode
1104 * the response received from an agent during a healthcheck. It returns 0 on
1105 * success and -1 if an error occurred. */
1106int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 struct appctx appctx;
1110 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112 memset(&appctx, 0, sizeof(appctx));
1113 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
Willy Tarreau23a24072022-05-05 20:18:44 +02001115 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001116 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001117
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1119 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120 goto error;
1121 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1123 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124
1125 return 0;
1126
1127 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1129 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1130 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001131 return -1;
1132}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001133
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001135 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136 * success. */
1137static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001140 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 int ret;
1142 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1145 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 netint = htonl(framesz);
1147 memcpy(buf, (char *)&netint, 4);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001148 ret = applet_putblk(appctx, buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 if (ret <= 0) {
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001150 if ((ret == -3 && b_is_null(&sc_ic(sc)->buf)) || ret == -1) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001151 /* WT: is this still needed for the case ret==-3 ? */
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001152 sc_need_room(sc);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001154 }
1155 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 return -1; /* error */
1157 }
1158 return framesz;
1159}
1160
1161/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1162 * when the frame can be ignored, 1 to retry later and the frame length on
1163 * success. */
1164static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001167 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168 int ret;
1169 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001171 ret = co_getblk(sc_oc(sc), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 if (ret > 0) {
1173 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001174 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176 return -1;
1177 }
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001178 ret = co_getblk(sc_oc(sc), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 }
1180 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001183 }
1184 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 return -1; /* error */
1186 }
1187 return framesz;
1188}
1189
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001190/********************************************************************
1191 * Functions that manage the SPOE applet
1192 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001193static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001194spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001195{
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02001196 applet_will_consume(appctx);
Willy Tarreau4164eb92022-05-25 15:42:03 +02001197 applet_have_more_data(appctx);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198 appctx_wakeup(appctx);
1199 return 1;
1200}
1201
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202/* Callback function that catches applet timeouts. If a timeout occurred, we set
1203 * <appctx->st1> flag and the SPOE applet is woken up. */
1204static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001205spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001206{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001207 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208
1209 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1210 if (tick_is_expired(task->expire, now_ms)) {
1211 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001212 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215 return task;
1216}
1217
Christopher Faulet69ebc302022-05-12 15:28:51 +02001218static int
1219spoe_init_appctx(struct appctx *appctx)
1220{
1221 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1222 struct spoe_agent *agent = spoe_appctx->agent;
1223 struct task *task;
1224 struct stream *s;
1225
1226 if ((task = task_new_here()) == NULL)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001227 goto out_error;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001228 task->process = spoe_process_appctx;
1229 task->context = appctx;
1230
1231 if (appctx_finalize_startup(appctx, &agent->spoe_conf->agent_fe, &BUF_NULL) == -1)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001232 goto out_free_task;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001233
1234 spoe_appctx->owner = appctx;
1235 spoe_appctx->task = task;
1236
1237 LIST_INIT(&spoe_appctx->buffer_wait.list);
1238 spoe_appctx->buffer_wait.target = appctx;
1239 spoe_appctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
1240
1241 s = appctx_strm(appctx);
1242 stream_set_backend(s, agent->b.be);
1243
1244 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001245 applet_need_more_data(appctx);
Christopher Faulet69ebc302022-05-12 15:28:51 +02001246
1247 s->do_log = NULL;
Christopher Faulet9a790f62023-03-16 14:40:03 +01001248 s->scb->flags |= SC_FL_RCV_ONCE;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001249
1250 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1251 LIST_APPEND(&agent->rt[tid].applets, &spoe_appctx->list);
1252 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1253 _HA_ATOMIC_INC(&agent->counters.applets);
1254
1255 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1256 task_wakeup(spoe_appctx->task, TASK_WOKEN_INIT);
1257 return 0;
1258 out_free_task:
1259 task_destroy(task);
1260 out_error:
1261 return -1;
1262}
1263
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264/* Callback function that releases a SPOE applet. This happens when the
1265 * connection with the agent is closed. */
1266static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001267spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001268{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001269 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001270 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1271 struct spoe_agent *agent;
1272 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273
1274 if (spoe_appctx == NULL)
1275 return;
1276
Willy Tarreau23a24072022-05-05 20:18:44 +02001277 appctx->svcctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001279
1280 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1281 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1282 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283
Christopher Faulet8ef75252017-02-20 22:56:03 +01001284 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001285 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001286 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001287 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001288 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001290 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001291 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001295 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1296 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001297 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001298 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1302 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001303
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001304 sc_shutw(sc);
1305 sc_shutr(sc);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001306 }
1307
Christopher Faulet8ef75252017-02-20 22:56:03 +01001308 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001309 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310
Christopher Faulet42a06622022-08-25 18:50:18 +02001311 /* Report an error to all streams in the appctx waiting queue */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001313 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001315 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001316 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001317 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001318 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001320 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001321 }
1322
Christopher Faulet42a06622022-08-25 18:50:18 +02001323 /* If the applet was processing a fragmented frame, report an error to
1324 * the corresponding stream. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001325 if (spoe_appctx->frag_ctx.ctx) {
1326 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001327 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001328 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001329 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001330 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1331 }
1332
Christopher Faulet42a06622022-08-25 18:50:18 +02001333 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1334 /* If there are still some running applets, remove reference on
1335 * the current one from streams in the async waiting queue. In
1336 * async mode, the ACK may be received from another appctx.
1337 */
Christopher Fauletcf181c72020-11-10 18:45:34 +01001338 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1339 if (ctx->spoe_appctx == spoe_appctx)
1340 ctx->spoe_appctx = NULL;
1341 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001342 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001343 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001344 else {
Christopher Faulet42a06622022-08-25 18:50:18 +02001345 /* It is the last running applet and the sending and async
1346 * waiting queues are not empty. So try to start a new applet if
1347 * HAproxy is not stopping. On success, we remove reference on
1348 * the current appctx from streams in the async waiting queue.
1349 * In async mode, the ACK may be received from another appctx.
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001350 */
1351 if (!stopping &&
1352 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
Christopher Faulet42a06622022-08-25 18:50:18 +02001353 spoe_create_appctx(agent->spoe_conf)) {
1354 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1355 if (ctx->spoe_appctx == spoe_appctx)
1356 ctx->spoe_appctx = NULL;
1357 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001358 goto end;
Christopher Faulet42a06622022-08-25 18:50:18 +02001359 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001360
Christopher Faulet42a06622022-08-25 18:50:18 +02001361 /* Otherwise, report an error to all streams in the sending and
1362 * async waiting queues.
1363 */
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001364 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1365 LIST_DELETE(&ctx->list);
1366 LIST_INIT(&ctx->list);
1367 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
1368 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1369 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 }
1374 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1375 LIST_DELETE(&ctx->list);
1376 LIST_INIT(&ctx->list);
1377 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
1378 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1379 ctx->spoe_appctx = NULL;
1380 ctx->state = SPOE_CTX_ST_ERROR;
1381 ctx->status_code = (spoe_appctx->status_code + 0x100);
1382 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1383 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001385
1386 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001387 /* Release allocated memory */
1388 spoe_release_buffer(&spoe_appctx->buffer,
1389 &spoe_appctx->buffer_wait);
1390 pool_free(pool_head_spoe_appctx, spoe_appctx);
1391
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001392 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001393 agent->rt[tid].frame_size = agent->max_frame_size;
1394 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1395 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001396}
1397
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001399spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001400{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001401 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001402 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001406 if (sc_state_in(sc->state, SC_SB_CER|SC_SB_DIS|SC_SB_CLO)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001407 /* closed */
1408 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1409 goto exit;
1410 }
1411
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001412 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001413 /* not connected yet */
Willy Tarreau4164eb92022-05-25 15:42:03 +02001414 applet_have_more_data(appctx);
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001415 task_wakeup(__sc_strm(sc)->task, TASK_WOKEN_MSG);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 goto stop;
1417 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001418
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1421 " - Connection timed out\n",
1422 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1423 __FUNCTION__, appctx);
1424 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001426 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001427
Christopher Faulet42bfa462017-01-04 14:14:19 +01001428 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001429 SPOE_APPCTX(appctx)->task->expire =
1430 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431
Christopher Faulet8ef75252017-02-20 22:56:03 +01001432 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1433 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001434 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 ret = spoe_prepare_hahello_frame(appctx, frame,
1436 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439
1440 switch (ret) {
1441 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001442 case 0: /* ignore => an error, cannot be ignored */
1443 goto exit;
1444
1445 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001446 goto stop;
1447
Christopher Faulet8ef75252017-02-20 22:56:03 +01001448 default:
1449 /* HELLO frame successfully sent, now wait for the
1450 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001451 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1452 goto next;
1453 }
1454
1455 next:
1456 return 0;
1457 stop:
1458 return 1;
1459 exit:
1460 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1461 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001462}
1463
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001464static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001465spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001467 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001468 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001469 char *frame;
1470 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001471
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001472
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001473 if (sc->state == SC_ST_CLO || sc_opposite(sc)->state == SC_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001475 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001476 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001477
Christopher Fauleta1cda022016-12-21 08:58:06 +01001478 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1480 " - Connection timed out\n",
1481 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1482 __FUNCTION__, appctx);
1483 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001484 goto exit;
1485 }
1486
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001487 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001488 ret = spoe_recv_frame(appctx, frame,
1489 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001490 if (ret > 1) {
1491 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1492 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1493 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001494 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001495 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001497 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001498
Christopher Fauleta1cda022016-12-21 08:58:06 +01001499 switch (ret) {
1500 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001501 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001502 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1503 goto next;
1504
1505 case 1: /* retry later */
1506 goto stop;
1507
1508 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001509 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001510 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001511 SPOE_APPCTX(appctx)->node.key = 0;
1512 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001513
1514 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001515 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001516 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001517 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001518
Christopher Fauleta1cda022016-12-21 08:58:06 +01001519 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001521 if (trash.data)
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001522 co_skip(sc_oc(sc), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523
1524 SPOE_APPCTX(appctx)->task->expire =
1525 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001526 return 0;
1527 stop:
1528 return 1;
1529 exit:
1530 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1531 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001532}
1533
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001534
Christopher Fauleta1cda022016-12-21 08:58:06 +01001535static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001536spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001537{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001538 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1539 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 char *frame, *buf;
1541 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001542
Christopher Faulet8ef75252017-02-20 22:56:03 +01001543 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1544 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001545 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001546
1547 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1548 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1549 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1550 SPOE_APPCTX(appctx)->max_frame_size);
1551 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001552 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001553 *skip = 1;
1554 ret = 1;
1555 goto end;
1556 }
1557 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001558 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001559 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1560 SPOE_APPCTX(appctx)->max_frame_size);
1561
1562 }
1563
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 if (ret > 1)
1565 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001566
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 switch (ret) {
1568 case -1: /* error */
1569 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1570 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001571
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572 case 0: /* ignore */
1573 if (ctx == NULL)
1574 goto abort_frag_frame;
1575
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001576 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001577 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001578 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001579 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001580 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001581 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001582 ctx->state = SPOE_CTX_ST_ERROR;
1583 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1584 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001585 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001586 break;
1587
1588 case 1: /* retry */
1589 *skip = 1;
1590 break;
1591
1592 default:
1593 if (ctx == NULL)
1594 goto abort_frag_frame;
1595
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001596 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001597 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001599 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001600 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001601 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1603 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1604 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001605 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001606 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001607 }
1608 goto end;
1609
1610 frag_frame_sent:
1611 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001612 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001613 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1614 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1615 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001616 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1617 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1618 goto end;
1619
1620 no_frag_frame_sent:
1621 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1622 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001623 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 }
1625 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1626 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001627 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 }
1629 else {
1630 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001631 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001632 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001633 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001634 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001635 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001636 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1637 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1638 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001639 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001640
Christopher Faulet8ef75252017-02-20 22:56:03 +01001641 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1642 goto end;
1643
1644 abort_frag_frame:
1645 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1646 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1647 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1648 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1649 goto end;
1650
1651 end:
1652 return ret;
1653}
1654
1655static int
1656spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1657{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001658 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001659 struct spoe_context *ctx = NULL;
1660 char *frame;
1661 int ret;
1662
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001663 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001664 ret = spoe_recv_frame(appctx, frame,
1665 SPOE_APPCTX(appctx)->max_frame_size);
1666 if (ret > 1) {
1667 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1668 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001669 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001670 goto end;
1671 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001672 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001673 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1674 }
1675 switch (ret) {
1676 case -1: /* error */
1677 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1678 break;
1679
1680 case 0: /* ignore */
1681 break;
1682
1683 case 1: /* retry */
1684 *skip = 1;
1685 break;
1686
1687 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001688 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001689 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001690 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001691 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1692 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001693 if (ctx->spoe_appctx) {
1694 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001695 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001697 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1698 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1699 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1700 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1701 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1702 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1703 }
1704 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1705 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001706 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1707 break;
1708 }
1709
1710 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001711 if (trash.data)
Willy Tarreauc12b3212022-05-27 11:08:15 +02001712 co_skip(sc_oc(appctx_sc(appctx)), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001713 end:
1714 return ret;
1715}
1716
1717static int
1718spoe_handle_processing_appctx(struct appctx *appctx)
1719{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001720 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001721 struct server *srv = objt_server(__sc_strm(sc)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001722 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001723 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001724
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001725 if (sc->state == SC_ST_CLO || sc_opposite(sc)->state == SC_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1727 goto exit;
1728 }
1729
1730 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1731 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1732 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1733 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1734 goto next;
1735 }
1736
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001737 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001738 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001739 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001740 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1741 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001742 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001743
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001744
1745 /* Close the applet ASAP because some sessions are waiting for a free
1746 * connection slot. It is only an issue in multithreaded mode.
1747 */
1748 close_asap = (global.nbthread > 1 &&
1749 (agent->b.be->queue.length ||
1750 (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1751
1752 /* Don"t try to send new frame we are waiting for at lease a ack, in
1753 * sync mode or if applet must be closed ASAP
1754 */
1755 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001756 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001757
Christopher Faulet8f82b202018-01-24 16:23:03 +01001758 /* receiving_frame loop */
1759 while (!skip_receiving) {
1760 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1761 switch (ret) {
1762 case -1: /* error */
1763 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001764
Christopher Faulet8f82b202018-01-24 16:23:03 +01001765 case 0: /* ignore */
1766 active_r = 1;
1767 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001768
Christopher Faulet8f82b202018-01-24 16:23:03 +01001769 case 1: /* retry */
1770 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001771
Christopher Faulet8f82b202018-01-24 16:23:03 +01001772 default:
1773 active_r = 1;
1774 break;
1775 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001777
Christopher Faulet8f82b202018-01-24 16:23:03 +01001778 /* send_frame loop */
1779 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1780 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1781 switch (ret) {
1782 case -1: /* error */
1783 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784
Christopher Faulet8f82b202018-01-24 16:23:03 +01001785 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001786 if (SPOE_APPCTX(appctx)->node.key)
1787 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001788 active_s++;
1789 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790
Christopher Faulet8f82b202018-01-24 16:23:03 +01001791 case 1: /* retry */
1792 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793
Christopher Faulet8f82b202018-01-24 16:23:03 +01001794 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001795 if (SPOE_APPCTX(appctx)->node.key)
1796 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001797 active_s++;
1798 break;
1799 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001800
1801 /* if applet must be close ASAP, don't send more than a frame */
1802 if (close_asap)
1803 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805
Christopher Faulet8f82b202018-01-24 16:23:03 +01001806 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001807 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1808 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1809 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001810
Christopher Faulet8f82b202018-01-24 16:23:03 +01001811 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001812 /* If applet must be closed, don't switch it in IDLE state and
1813 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001814 */
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001815 if (close_asap) {
1816 if (SPOE_APPCTX(appctx)->cur_fpa)
1817 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001818 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1819 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1820 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1821 goto next;
1822 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001823 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001824 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001825 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001827
1828 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 return 1;
1830
Christopher Faulet8f82b202018-01-24 16:23:03 +01001831 next:
1832 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1833 return 0;
1834
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 exit:
1836 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1837 return 0;
1838}
1839
1840static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001841spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001843 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001844 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 char *frame, *buf;
1846 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001847
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001848 if (sc->state == SC_ST_CLO || sc_opposite(sc)->state == SC_ST_CLO)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 goto exit;
1850
1851 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1852 goto exit;
1853
Christopher Faulet8ef75252017-02-20 22:56:03 +01001854 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1855 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001856 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1858 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001860 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001861
1862 switch (ret) {
1863 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865 goto exit;
1866
1867 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto stop;
1869
1870 default:
1871 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1872 " - disconnected by HAProxy (%d): %s\n",
1873 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001874 __FUNCTION__, appctx,
1875 SPOE_APPCTX(appctx)->status_code,
1876 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001877
1878 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1879 goto next;
1880 }
1881
1882 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 SPOE_APPCTX(appctx)->task->expire =
1884 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 return 0;
1886 stop:
1887 return 1;
1888 exit:
1889 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1890 return 0;
1891}
1892
1893static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001896 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001897 char *frame;
1898 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001899
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001900 if (sc->state == SC_ST_CLO || sc_opposite(sc)->state == SC_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001902 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001903 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904
Christopher Fauletb067b062017-01-04 16:39:11 +01001905 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001907 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001908 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001910 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001911 ret = spoe_recv_frame(appctx, frame,
1912 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001913 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001914 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001916 }
1917
1918 switch (ret) {
1919 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1921 " - error on frame (%s)\n",
1922 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001923 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001924 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001925 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001926 goto exit;
1927
1928 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929 goto next;
1930
1931 case 1: /* retry */
1932 goto stop;
1933
1934 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001935 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001936 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001937 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001938 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001939 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1940 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001941 goto exit;
1942 }
1943
1944 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001945 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001946 if (trash.data)
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001947 co_skip(sc_oc(sc), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001948
Christopher Fauleta1cda022016-12-21 08:58:06 +01001949 return 0;
1950 stop:
1951 return 1;
1952 exit:
1953 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1954 return 0;
1955}
1956
1957/* I/O Handler processing messages exchanged with the agent */
1958static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001959spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001960{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001961 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001962 struct spoe_agent *agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001963
1964 if (SPOE_APPCTX(appctx) == NULL)
1965 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001966
Christopher Faulet8ef75252017-02-20 22:56:03 +01001967 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1968 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001969
Christopher Fauleta1cda022016-12-21 08:58:06 +01001970 switchstate:
1971 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1972 " - appctx-state=%s\n",
1973 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1974 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1975
1976 switch (appctx->st0) {
1977 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001978 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001979 goto out;
1980 goto switchstate;
1981
1982 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001983 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001984 goto out;
1985 goto switchstate;
1986
1987 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001988 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001989 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001990 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001991 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001992 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001993 SPOE_APPCTX(appctx)->task->expire =
1994 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001995 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 goto switchstate;
1997 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001998 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau3064f522022-11-14 07:22:14 +01001999 __fallthrough;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002000
2001 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002002 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
2003 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002004 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002005 goto out;
2006 goto switchstate;
2007
2008 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002009 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002010 goto out;
2011 goto switchstate;
2012
2013 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002014 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002015 goto out;
2016 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017
2018 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002019 appctx->st0 = SPOE_APPCTX_ST_END;
2020 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
2021
Willy Tarreau9002a2e2022-05-27 10:34:25 +02002022 sc_shutw(sc);
2023 sc_shutr(sc);
Willy Tarreau3064f522022-11-14 07:22:14 +01002024 __fallthrough;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002025
2026 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002027 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002029 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02002030 if (stopping)
2031 spoe_wakeup_appctx(appctx);
2032
Christopher Faulet42bfa462017-01-04 14:14:19 +01002033 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
2034 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035}
2036
2037struct applet spoe_applet = {
2038 .obj_type = OBJ_TYPE_APPLET,
2039 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002040 .fct = spoe_handle_appctx,
Christopher Faulet69ebc302022-05-12 15:28:51 +02002041 .init = spoe_init_appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002042 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002043};
2044
2045/* Create a SPOE applet. On success, the created applet is returned, else
2046 * NULL. */
2047static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002048spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049{
Christopher Faulet69ebc302022-05-12 15:28:51 +02002050 struct spoe_appctx *spoe_appctx;
2051 struct appctx *appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002052
Christopher Faulet69ebc302022-05-12 15:28:51 +02002053 spoe_appctx = pool_zalloc(pool_head_spoe_appctx);
2054 if (spoe_appctx == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002055 goto out_error;
2056
Christopher Faulet69ebc302022-05-12 15:28:51 +02002057 spoe_appctx->agent = conf->agent;
2058 spoe_appctx->version = 0;
2059 spoe_appctx->max_frame_size = conf->agent->max_frame_size;
2060 spoe_appctx->flags = 0;
2061 spoe_appctx->status_code = SPOE_FRM_ERR_NONE;
2062 spoe_appctx->buffer = BUF_NULL;
2063 spoe_appctx->cur_fpa = 0;
2064 LIST_INIT(&spoe_appctx->list);
2065 LIST_INIT(&spoe_appctx->waiting_queue);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002066
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002067
Christopher Faulet6095d572022-05-16 17:09:48 +02002068 if ((appctx = appctx_new_here(&spoe_applet, NULL)) == NULL)
Christopher Faulet69ebc302022-05-12 15:28:51 +02002069 goto out_free_spoe_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002070
Christopher Faulet69ebc302022-05-12 15:28:51 +02002071 appctx->svcctx = spoe_appctx;
2072 if (appctx_init(appctx) == -1)
2073 goto out_free_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002075 appctx_wakeup(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002076 return appctx;
2077
2078 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 out_free_appctx:
Christopher Faulet69ebc302022-05-12 15:28:51 +02002080 appctx_free_on_early_error(appctx);
2081 out_free_spoe_appctx:
2082 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002083 out_error:
2084 return NULL;
2085}
2086
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002088spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002089{
2090 struct spoe_config *conf = FLT_CONF(ctx->filter);
2091 struct spoe_agent *agent = conf->agent;
2092 struct appctx *appctx;
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 Fauletb077cdc2018-01-24 16:37:57 +01002096 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002097 (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002098 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099
2100 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002101 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002102 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2103 ctx->strm);
2104
Christopher Fauleta1cda022016-12-21 08:58:06 +01002105 /* Do not try to create a new applet if there is no server up for the
2106 * agent's backend. */
2107 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2108 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2109 " - cannot create SPOE appctx: no server up\n",
2110 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2111 __FUNCTION__, ctx->strm);
2112 goto end;
2113 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002114
Christopher Fauleta1cda022016-12-21 08:58:06 +01002115 /* Do not try to create a new applet if we have reached the maximum of
2116 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002117 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002118 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002119 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2120 " - cannot create SPOE appctx: max CPS reached\n",
2121 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2122 __FUNCTION__, ctx->strm);
2123 goto end;
2124 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002125 }
2126
Christopher Faulet8ef75252017-02-20 22:56:03 +01002127 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002128 if (appctx == NULL) {
2129 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2130 " - failed to create SPOE appctx\n",
2131 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2132 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002133 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002134 "SPOE: [%s] failed to create SPOE applet\n",
2135 agent->id);
2136
Christopher Fauleta1cda022016-12-21 08:58:06 +01002137 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002138 }
2139
Christopher Fauleta1cda022016-12-21 08:58:06 +01002140 /* Increase the per-process number of cumulated connections */
2141 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002142 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002143
Christopher Fauleta1cda022016-12-21 08:58:06 +01002144 end:
2145 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002146 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002147 ctx->status_code = SPOE_CTX_ERR_RES;
2148 return -1;
2149 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002150
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002151 /* Add the SPOE context in the sending queue if the stream has no applet
2152 * already assigned and wakeup all idle applets. Otherwise, don't queue
2153 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002154 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002155 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2156 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002157 if (ctx->spoe_appctx)
2158 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002159 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002160
2161 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002162 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002163 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002164 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002165 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002166 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002167
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002168 /* Finally try to wakeup an IDLE applet. */
2169 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2170 struct eb32_node *node;
2171
2172 node = eb32_first(&agent->rt[tid].idle_applets);
2173 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2174 if (node && spoe_appctx) {
2175 eb32_delete(&spoe_appctx->node);
2176 spoe_appctx->node.key++;
2177 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2178 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002179 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002180 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002181 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002182}
2183
2184/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002185 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002186 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002187/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2188 * fragmented_content. If the next message can be processed, it returns 0. If
2189 * the message is too big, it returns -1.*/
2190static int
2191spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2192 struct spoe_message *msg, int dir,
2193 char **buf, char *end)
2194{
2195 struct sample *smp;
2196 struct spoe_arg *arg;
2197 int ret;
2198
2199 if (msg->cond) {
2200 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2201 ret = acl_pass(ret);
2202 if (msg->cond->pol == ACL_COND_UNLESS)
2203 ret = !ret;
2204
2205 /* the rule does not match */
2206 if (!ret)
2207 goto next;
2208 }
2209
2210 /* Resume encoding of a SPOE argument */
2211 if (ctx->frag_ctx.curarg != NULL) {
2212 arg = ctx->frag_ctx.curarg;
2213 goto encode_argument;
2214 }
2215
2216 if (ctx->frag_ctx.curoff != UINT_MAX)
2217 goto encode_msg_payload;
2218
2219 /* Check if there is enough space for the message name and the
2220 * number of arguments. It implies <msg->id_len> is encoded on 2
2221 * bytes, at most (< 2288). */
2222 if (*buf + 2 + msg->id_len + 1 > end)
2223 goto too_big;
2224
2225 /* Encode the message name */
2226 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2227 goto too_big;
2228
2229 /* Set the number of arguments for this message */
2230 **buf = msg->nargs;
2231 (*buf)++;
2232
2233 ctx->frag_ctx.curoff = 0;
2234 encode_msg_payload:
2235
2236 /* Loop on arguments */
2237 list_for_each_entry(arg, &msg->args, list) {
2238 ctx->frag_ctx.curarg = arg;
2239 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002240 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002241
2242 encode_argument:
2243 if (ctx->frag_ctx.curoff != UINT_MAX)
2244 goto encode_arg_value;
2245
Joseph Herlantf7f60312018-11-15 13:46:49 -08002246 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002247 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2248 goto too_big;
2249
2250 ctx->frag_ctx.curoff = 0;
2251 encode_arg_value:
2252
Joseph Herlantf7f60312018-11-15 13:46:49 -08002253 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002254 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002255 if (smp) {
2256 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2257 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2258 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002259 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002260 if (ret == -1 || ctx->frag_ctx.curoff)
2261 goto too_big;
2262 }
2263
2264 next:
2265 return 0;
2266
2267 too_big:
2268 return -1;
2269}
2270
Christopher Fauletc718b822017-09-21 16:50:56 +02002271/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2272 * handle fragmented content. On success it returns 1. If an error occurred, -1
2273 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2274 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002275static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002276spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002277 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002278{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002279 struct spoe_config *conf = FLT_CONF(ctx->filter);
2280 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002281 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002282 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002283
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002284 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002285 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002286
Christopher Fauletc718b822017-09-21 16:50:56 +02002287 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2288 /* Resume encoding of a SPOE message */
2289 if (ctx->frag_ctx.curmsg != NULL) {
2290 msg = ctx->frag_ctx.curmsg;
2291 goto encode_evt_message;
2292 }
2293
2294 list_for_each_entry(msg, messages, by_evt) {
2295 ctx->frag_ctx.curmsg = msg;
2296 ctx->frag_ctx.curarg = NULL;
2297 ctx->frag_ctx.curoff = UINT_MAX;
2298
2299 encode_evt_message:
2300 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2301 goto too_big;
2302 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002303 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002304 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2305 /* Resume encoding of a SPOE message */
2306 if (ctx->frag_ctx.curmsg != NULL) {
2307 msg = ctx->frag_ctx.curmsg;
2308 goto encode_grp_message;
2309 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002310
Christopher Fauletc718b822017-09-21 16:50:56 +02002311 list_for_each_entry(msg, messages, by_grp) {
2312 ctx->frag_ctx.curmsg = msg;
2313 ctx->frag_ctx.curarg = NULL;
2314 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002315
Christopher Fauletc718b822017-09-21 16:50:56 +02002316 encode_grp_message:
2317 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2318 goto too_big;
2319 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002321 else
2322 goto skip;
2323
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002324
Christopher Faulet57583e42017-09-04 15:41:09 +02002325 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002326 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002327 goto skip;
2328
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002329 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002330 " - encode %s messages - spoe_appctx=%p"
2331 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002332 (int)now.tv_sec, (int)now.tv_usec,
2333 agent->id, __FUNCTION__, s,
2334 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002335 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002336 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002337
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002338 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002339 ctx->frag_ctx.curmsg = NULL;
2340 ctx->frag_ctx.curarg = NULL;
2341 ctx->frag_ctx.curoff = 0;
2342 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002343
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344 return 1;
2345
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002346 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002347 /* Return an error if fragmentation is unsupported or if nothing has
2348 * been encoded because its too big and not splittable. */
2349 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002350 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2351 return -1;
2352 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002353
2354 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002355 " - encode fragmented messages - spoe_appctx=%p"
2356 " - curmsg=%p - curarg=%p - curoff=%u"
2357 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002358 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002359 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002360 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002361 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002362
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002363 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002364 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2365 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2366 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002367
2368 skip:
2369 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2370 " - skip the frame because nothing has been encoded\n",
2371 (int)now.tv_sec, (int)now.tv_usec,
2372 agent->id, __FUNCTION__, s);
2373 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374}
2375
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002376
2377/***************************************************************************
2378 * Functions that handle SPOE actions
2379 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380/* Helper function to set a variable */
2381static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002382spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002383 struct sample *smp)
2384{
2385 struct spoe_config *conf = FLT_CONF(ctx->filter);
2386 struct spoe_agent *agent = conf->agent;
2387 char varname[64];
2388
2389 memset(varname, 0, sizeof(varname));
2390 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2391 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002392 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2393 vars_set_by_name(varname, len, smp);
2394 else
2395 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396}
2397
2398/* Helper function to unset a variable */
2399static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401 struct sample *smp)
2402{
2403 struct spoe_config *conf = FLT_CONF(ctx->filter);
2404 struct spoe_agent *agent = conf->agent;
2405 char varname[64];
2406
2407 memset(varname, 0, sizeof(varname));
2408 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2409 scope, agent->var_pfx, len, name);
2410 vars_unset_by_name_ifexist(varname, len, smp);
2411}
2412
2413
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414static inline int
2415spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2416 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 char *str, *scope, *p = *buf;
2419 struct sample smp;
2420 uint64_t sz;
2421 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 if (p + 2 >= end)
2424 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002425
Christopher Faulet8ef75252017-02-20 22:56:03 +01002426 /* SET-VAR requires 3 arguments */
2427 if (*p++ != 3)
2428 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002429
Christopher Faulet8ef75252017-02-20 22:56:03 +01002430 switch (*p++) {
2431 case SPOE_SCOPE_PROC: scope = "proc"; break;
2432 case SPOE_SCOPE_SESS: scope = "sess"; break;
2433 case SPOE_SCOPE_TXN : scope = "txn"; break;
2434 case SPOE_SCOPE_REQ : scope = "req"; break;
2435 case SPOE_SCOPE_RES : scope = "res"; break;
2436 default: goto skip;
2437 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002438
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2440 goto skip;
2441 memset(&smp, 0, sizeof(smp));
2442 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443
Christopher Faulet8ef75252017-02-20 22:56:03 +01002444 if (spoe_decode_data(&p, end, &smp) == -1)
2445 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002446
Christopher Faulet8ef75252017-02-20 22:56:03 +01002447 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2448 " - set-var '%s.%s.%.*s'\n",
2449 (int)now.tv_sec, (int)now.tv_usec,
2450 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2451 __FUNCTION__, s, scope,
2452 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2453 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002454
Christopher Faulet2469eba2020-10-15 16:08:30 +02002455 if (smp.data.type == SMP_T_ANY)
2456 spoe_unset_var(ctx, scope, str, sz, &smp);
2457 else
2458 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460 ret = (p - *buf);
2461 *buf = p;
2462 return ret;
2463 skip:
2464 return 0;
2465}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002466
Christopher Faulet8ef75252017-02-20 22:56:03 +01002467static inline int
2468spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2469 char **buf, char *end, int dir)
2470{
2471 char *str, *scope, *p = *buf;
2472 struct sample smp;
2473 uint64_t sz;
2474 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002475
Christopher Faulet8ef75252017-02-20 22:56:03 +01002476 if (p + 2 >= end)
2477 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002478
Christopher Faulet8ef75252017-02-20 22:56:03 +01002479 /* UNSET-VAR requires 2 arguments */
2480 if (*p++ != 2)
2481 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002482
Christopher Faulet8ef75252017-02-20 22:56:03 +01002483 switch (*p++) {
2484 case SPOE_SCOPE_PROC: scope = "proc"; break;
2485 case SPOE_SCOPE_SESS: scope = "sess"; break;
2486 case SPOE_SCOPE_TXN : scope = "txn"; break;
2487 case SPOE_SCOPE_REQ : scope = "req"; break;
2488 case SPOE_SCOPE_RES : scope = "res"; break;
2489 default: goto skip;
2490 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002491
Christopher Faulet8ef75252017-02-20 22:56:03 +01002492 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2493 goto skip;
2494 memset(&smp, 0, sizeof(smp));
2495 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002496
Christopher Faulet8ef75252017-02-20 22:56:03 +01002497 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2498 " - unset-var '%s.%s.%.*s'\n",
2499 (int)now.tv_sec, (int)now.tv_usec,
2500 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2501 __FUNCTION__, s, scope,
2502 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2503 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002504
Christopher Faulet8ef75252017-02-20 22:56:03 +01002505 spoe_unset_var(ctx, scope, str, sz, &smp);
2506
2507 ret = (p - *buf);
2508 *buf = p;
2509 return ret;
2510 skip:
2511 return 0;
2512}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002513
Christopher Faulet8ef75252017-02-20 22:56:03 +01002514/* Process SPOE actions for a specific event. It returns 1 on success. If an
2515 * error occurred, 0 is returned. */
2516static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002517spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002518{
2519 char *p, *end;
2520 int ret;
2521
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002522 p = b_head(&ctx->buffer);
2523 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002524
2525 while (p < end) {
2526 enum spoe_action_type type;
2527
2528 type = *p++;
2529 switch (type) {
2530 case SPOE_ACT_T_SET_VAR:
2531 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2532 if (!ret)
2533 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002534 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002535
Christopher Faulet8ef75252017-02-20 22:56:03 +01002536 case SPOE_ACT_T_UNSET_VAR:
2537 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2538 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002539 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002540 break;
2541
2542 default:
2543 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002544 }
2545 }
2546
2547 return 1;
2548 skip:
2549 return 0;
2550}
2551
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002552/***************************************************************************
2553 * Functions that process SPOE events
2554 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002555static void
2556spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2557 struct spoe_context *ctx, int dir)
2558{
2559 if (!tv_iszero(&ctx->stats.tv_start)) {
2560 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2561 ctx->stats.t_total += ctx->stats.t_process;
2562 tv_zero(&ctx->stats.tv_request);
2563 tv_zero(&ctx->stats.tv_queue);
2564 tv_zero(&ctx->stats.tv_wait);
2565 tv_zero(&ctx->stats.tv_response);
2566 }
2567
2568 if (agent->var_t_process) {
2569 struct sample smp;
2570
2571 memset(&smp, 0, sizeof(smp));
2572 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2573 smp.data.u.sint = ctx->stats.t_process;
2574 smp.data.type = SMP_T_SINT;
2575
2576 spoe_set_var(ctx, "txn", agent->var_t_process,
2577 strlen(agent->var_t_process), &smp);
2578 }
2579
2580 if (agent->var_t_total) {
2581 struct sample smp;
2582
2583 memset(&smp, 0, sizeof(smp));
2584 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2585 smp.data.u.sint = ctx->stats.t_total;
2586 smp.data.type = SMP_T_SINT;
2587
2588 spoe_set_var(ctx, "txn", agent->var_t_total,
2589 strlen(agent->var_t_total), &smp);
2590 }
2591}
2592
2593static void
2594spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2595 struct spoe_context *ctx, int dir)
2596{
2597 if (agent->eps_max > 0)
2598 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2599
2600 if (agent->var_on_error) {
2601 struct sample smp;
2602
2603 memset(&smp, 0, sizeof(smp));
2604 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2605 smp.data.u.sint = ctx->status_code;
2606 smp.data.type = SMP_T_BOOL;
2607
2608 spoe_set_var(ctx, "txn", agent->var_on_error,
2609 strlen(agent->var_on_error), &smp);
2610 }
2611
2612 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2613 ? SPOE_CTX_ST_READY
2614 : SPOE_CTX_ST_NONE);
2615}
2616
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002617static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002618spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002619{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002620 /* If a process is already started for this SPOE context, retry
2621 * later. */
2622 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002623 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002624
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002625 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002626 ctx->stats.tv_start = now;
2627 ctx->stats.tv_request = now;
2628 ctx->stats.t_request = -1;
2629 ctx->stats.t_queue = -1;
2630 ctx->stats.t_waiting = -1;
2631 ctx->stats.t_response = -1;
2632 ctx->stats.t_process = -1;
2633
2634 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002635
Christopher Fauleta1cda022016-12-21 08:58:06 +01002636 /* Set the right flag to prevent request and response processing
2637 * in same time. */
2638 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2639 ? SPOE_CTX_FL_REQ_PROCESS
2640 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642}
2643
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002644static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002645spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002647 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002648
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002649 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2650 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002651 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002652 if (sa) {
2653 if (sa->frag_ctx.ctx == ctx) {
2654 sa->frag_ctx.ctx = NULL;
2655 spoe_wakeup_appctx(sa->owner);
2656 }
2657 else
2658 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002659 }
2660
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002662 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002663 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002664
2665 /* Reset processing timer */
2666 ctx->process_exp = TICK_ETERNITY;
2667
Christopher Faulet8ef75252017-02-20 22:56:03 +01002668 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002669
Christopher Fauletfce747b2018-01-24 15:59:32 +01002670 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002671 ctx->frag_ctx.curmsg = NULL;
2672 ctx->frag_ctx.curarg = NULL;
2673 ctx->frag_ctx.curoff = 0;
2674 ctx->frag_ctx.flags = 0;
2675
Christopher Fauleta1cda022016-12-21 08:58:06 +01002676 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002677 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002678 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002679 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002680 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002681
Willy Tarreau2b718102021-04-21 07:32:39 +02002682 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002683 LIST_INIT(&ctx->list);
2684 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002685}
2686
Christopher Faulet58d03682017-09-21 16:57:24 +02002687/* Process a list of SPOE messages. First, this functions will process messages
2688 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2689 * to process corresponding actions. During all the processing, it returns 0
2690 * and it returns 1 when the processing is finished. If an error occurred, -1
2691 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002692static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002693spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2694 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002695{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002696 struct spoe_config *conf = FLT_CONF(ctx->filter);
2697 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002698 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002699
2700 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002701 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002702
2703 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2704 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002705 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002706 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002707 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002708 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002709 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002710 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002711
2712 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002713 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002714 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002715 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002716 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002717 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002718 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002719 goto skip;
2720 }
2721 }
2722
Christopher Fauletf7a30922016-11-10 15:04:51 +01002723 if (!tick_isset(ctx->process_exp)) {
2724 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2725 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2726 ctx->process_exp);
2727 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002728 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002729 if (!ret)
2730 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002731
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002732 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002733 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002734 }
2735
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002736 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002737 if (tv_iszero(&ctx->stats.tv_request))
2738 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002739 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002740 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002741 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002742 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002743 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002744 if (!ret)
2745 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002746 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002747 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002748 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2749 }
2750
2751 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002752 if (ctx->spoe_appctx)
2753 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002754 ret = 0;
2755 goto out;
2756 }
2757
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002758 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2759 ret = 0;
2760 goto out;
2761 }
2762
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002763 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002764 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002765 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002767 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002768 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002769 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002770 }
2771
2772 out:
2773 return ret;
2774
Christopher Fauleta1cda022016-12-21 08:58:06 +01002775 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002776 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002777 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002778 spoe_stop_processing(agent, ctx);
2779 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002780
Christopher Fauleta1cda022016-12-21 08:58:06 +01002781 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002782 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002783 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002784 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002785 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002786 spoe_handle_processing_error(s, agent, ctx, dir);
2787 ret = 1;
2788 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002789 return ret;
2790}
2791
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002792/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2793 * See spoe_process_message for details. */
2794static int
2795spoe_process_group(struct stream *s, struct spoe_context *ctx,
2796 struct spoe_group *group, int dir)
2797{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002798 struct spoe_config *conf = FLT_CONF(ctx->filter);
2799 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002800 int ret;
2801
2802 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2803 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002804 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002805 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2806 group->id);
2807
2808 if (LIST_ISEMPTY(&group->messages))
2809 return 1;
2810
2811 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002812 if (ret && ctx->stats.t_process != -1) {
2813 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002814 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002815 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002816 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002817 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002818 ctx->stats.t_response, ctx->stats.t_process,
2819 agent->counters.idles, agent->counters.applets,
2820 agent->counters.nb_sending, agent->counters.nb_waiting,
2821 agent->counters.nb_errors, agent->counters.nb_processed,
2822 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2823 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2824 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2825 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2826 agent->id, group->id, s->uniq_id, ctx->status_code,
2827 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2828 ctx->stats.t_response, ctx->stats.t_process,
2829 agent->counters.idles, agent->counters.applets,
2830 agent->counters.nb_sending, agent->counters.nb_waiting,
2831 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002832 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002833 return ret;
2834}
2835
Christopher Faulet58d03682017-09-21 16:57:24 +02002836/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2837 * See spoe_process_message for details. */
2838static int
2839spoe_process_event(struct stream *s, struct spoe_context *ctx,
2840 enum spoe_event ev)
2841{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002842 struct spoe_config *conf = FLT_CONF(ctx->filter);
2843 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002844 int dir, ret;
2845
2846 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002847 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002848 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002849 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2850 spoe_event_str[ev]);
2851
2852 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2853
2854 if (LIST_ISEMPTY(&(ctx->events[ev])))
2855 return 1;
2856
2857 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002858 if (ret && ctx->stats.t_process != -1) {
2859 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002860 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002861 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2862 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2863 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002864 ctx->stats.t_response, ctx->stats.t_process,
2865 agent->counters.idles, agent->counters.applets,
2866 agent->counters.nb_sending, agent->counters.nb_waiting,
2867 agent->counters.nb_errors, agent->counters.nb_processed,
2868 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2869 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2870 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2871 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2872 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2873 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2874 ctx->stats.t_response, ctx->stats.t_process,
2875 agent->counters.idles, agent->counters.applets,
2876 agent->counters.nb_sending, agent->counters.nb_waiting,
2877 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002878 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002879 return ret;
2880}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881
2882/***************************************************************************
2883 * Functions that create/destroy SPOE contexts
2884 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002885static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002886spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002887{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002888 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002889 return 1;
2890
Willy Tarreau2b718102021-04-21 07:32:39 +02002891 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002892 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002893
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002894 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002895 return 1;
2896
Willy Tarreaub4e34762021-09-30 19:02:18 +02002897 LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002898 return 0;
2899}
2900
2901static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002902spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002903{
Willy Tarreau2b718102021-04-21 07:32:39 +02002904 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002905 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002906
2907 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002908 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002909 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002910 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002911 }
2912}
2913
Christopher Faulet4596fb72017-01-11 14:05:19 +01002914static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002915spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002916{
2917 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2918 return 1;
2919}
2920
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002921static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002922spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923{
2924 struct spoe_config *conf = FLT_CONF(filter);
2925 struct spoe_context *ctx;
2926
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002927 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002928 if (ctx == NULL) {
2929 return NULL;
2930 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002931 ctx->filter = filter;
2932 ctx->state = SPOE_CTX_ST_NONE;
2933 ctx->status_code = SPOE_CTX_ERR_NONE;
2934 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002935 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002936 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002937 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002938 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002939 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002941 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942
Christopher Fauletf7a30922016-11-10 15:04:51 +01002943 ctx->stream_id = 0;
2944 ctx->frame_id = 1;
2945 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002947 tv_zero(&ctx->stats.tv_start);
2948 tv_zero(&ctx->stats.tv_request);
2949 tv_zero(&ctx->stats.tv_queue);
2950 tv_zero(&ctx->stats.tv_wait);
2951 tv_zero(&ctx->stats.tv_response);
2952 ctx->stats.t_request = -1;
2953 ctx->stats.t_queue = -1;
2954 ctx->stats.t_waiting = -1;
2955 ctx->stats.t_response = -1;
2956 ctx->stats.t_process = -1;
2957 ctx->stats.t_total = 0;
2958
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002959 ctx->strm = s;
2960 ctx->state = SPOE_CTX_ST_READY;
2961 filter->ctx = ctx;
2962
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963 return ctx;
2964}
2965
2966static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002967spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002968{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002969 struct spoe_config *conf = FLT_CONF(filter);
2970 struct spoe_context *ctx = filter->ctx;
2971
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002972 if (!ctx)
2973 return;
2974
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002975 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002976 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002977 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978}
2979
2980static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002981spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982{
2983 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002984 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002985
2986 tv_zero(&ctx->stats.tv_start);
2987 tv_zero(&ctx->stats.tv_request);
2988 tv_zero(&ctx->stats.tv_queue);
2989 tv_zero(&ctx->stats.tv_wait);
2990 tv_zero(&ctx->stats.tv_response);
2991 ctx->stats.t_request = -1;
2992 ctx->stats.t_queue = -1;
2993 ctx->stats.t_waiting = -1;
2994 ctx->stats.t_response = -1;
2995 ctx->stats.t_process = -1;
2996 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997}
2998
2999
3000/***************************************************************************
3001 * Hooks that manage the filter lifecycle (init/check/deinit)
3002 **************************************************************************/
3003/* Signal handler: Do a soft stop, wakeup SPOE applet */
3004static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01003005spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003006{
3007 struct proxy *p;
3008
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003009 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 while (p) {
3011 struct flt_conf *fconf;
3012
3013 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01003014 struct spoe_config *conf;
3015 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01003016 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02003017 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018
Christopher Faulet3b386a32017-02-23 10:17:15 +01003019 if (fconf->id != spoe_filter_id)
3020 continue;
3021
3022 conf = fconf->conf;
3023 agent = conf->agent;
3024
Christopher Faulet24289f22017-09-25 14:48:02 +02003025 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003026 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003027 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3028 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003029 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003030 }
3031 }
3032 p = p->next;
3033 }
3034}
3035
3036
3037/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3038static int
3039spoe_init(struct proxy *px, struct flt_conf *fconf)
3040{
3041 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003043 /* conf->agent_fe was already initialized during the config
3044 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046 conf->agent_fe.cap = PR_CAP_FE;
3047 conf->agent_fe.mode = PR_MODE_TCP;
3048 conf->agent_fe.maxconn = 0;
3049 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3050 conf->agent_fe.conn_retries = CONN_RETRIES;
3051 conf->agent_fe.accept = frontend_accept;
3052 conf->agent_fe.srv = NULL;
3053 conf->agent_fe.timeout.client = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003054 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3055
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003057 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 sighandler_registered = 1;
3059 }
3060
Christopher Faulet00292352019-01-09 15:05:10 +01003061 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003063}
3064
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003065/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003066static void
3067spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3068{
3069 struct spoe_config *conf = fconf->conf;
3070
3071 if (conf) {
3072 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003073
Christopher Faulet8ef75252017-02-20 22:56:03 +01003074 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003075 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 free(conf);
3077 }
3078 fconf->conf = NULL;
3079}
3080
3081/* Check configuration of a SPOE filter for a specified proxy.
3082 * Return 1 on error, else 0. */
3083static int
3084spoe_check(struct proxy *px, struct flt_conf *fconf)
3085{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003086 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003087 struct spoe_config *conf = fconf->conf;
3088 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003089 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003090 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091
Christopher Faulet7ee86672017-09-19 11:08:28 +02003092 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3093 * are uniq */
3094 list_for_each_entry(f, &px->filter_configs, list) {
3095 struct spoe_config *c = f->conf;
3096
3097 /* This is not an SPOE filter */
3098 if (f->id != spoe_filter_id)
3099 continue;
3100 /* This is the current SPOE filter */
3101 if (f == fconf)
3102 continue;
3103
3104 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003105 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003106 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3107 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003108 return 1;
3109 }
3110 }
3111
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003112 target = proxy_be_by_name(conf->agent->b.name);
3113 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003114 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3115 " declared at %s:%d.\n",
3116 px->id, conf->agent->b.name, conf->agent->id,
3117 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003118 return 1;
3119 }
3120 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003121 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3122 " at %s:%d does not support HTTP mode.\n",
3123 px->id, target->id, conf->agent->id,
3124 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003125 return 1;
3126 }
3127
Christopher Fauletfe261552019-03-18 13:57:42 +01003128 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003129 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3130 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3131 return 1;
3132 }
3133 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003134 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003135 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3136 conf->agent->rt[i].processing = 0;
3137 LIST_INIT(&conf->agent->rt[i].applets);
3138 LIST_INIT(&conf->agent->rt[i].sending_queue);
3139 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3140 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003141 }
3142
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003143 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3144 if (logsrv->type == LOG_TARGET_BUFFER) {
3145 struct sink *sink = sink_find(logsrv->ring_name);
3146
3147 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3148 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003149 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003150 px->id, conf->agent->id, conf->agent->conf.file,
3151 conf->agent->conf.line, logsrv->ring_name);
3152 return 1;
3153 }
3154 logsrv->sink = sink;
3155 }
3156 }
3157
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003158 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003159 conf->agent->b.be = target;
3160 return 0;
3161}
3162
Kevin Zhud87b1a52019-09-17 15:05:45 +02003163/* Initializes the SPOE filter for a proxy for a specific thread.
3164 * Returns a negative value if an error occurs. */
3165static int
3166spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3167{
3168 struct spoe_config *conf = fconf->conf;
3169 struct spoe_agent *agent = conf->agent;
3170
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003171 agent->rt[tid].engine_id = generate_pseudo_uuid();
3172 if (agent->rt[tid].engine_id == NULL)
3173 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003174 return 0;
3175}
3176
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003177/**************************************************************************
3178 * Hooks attached to a stream
3179 *************************************************************************/
3180/* Called when a filter instance is created and attach to a stream. It creates
3181 * the context that will be used to process this stream. */
3182static int
3183spoe_start(struct stream *s, struct filter *filter)
3184{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003185 struct spoe_config *conf = FLT_CONF(filter);
3186 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 struct spoe_context *ctx;
3188
3189 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003190 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 __FUNCTION__, s);
3192
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003193 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003194 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3195 " - failed to create SPOE context\n",
3196 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003197 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003198 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003199 "SPOE: [%s] failed to create SPOE context\n",
3200 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003201 return 0;
3202 }
3203
Christopher Faulet11610f32017-09-21 10:23:10 +02003204 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3206
Christopher Faulet11610f32017-09-21 10:23:10 +02003207 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3209
Christopher Faulet11610f32017-09-21 10:23:10 +02003210 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 filter->pre_analyzers |= AN_RES_INSPECT;
3212
Christopher Faulet11610f32017-09-21 10:23:10 +02003213 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3215
Christopher Faulet11610f32017-09-21 10:23:10 +02003216 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003217 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3218
Christopher Faulet11610f32017-09-21 10:23:10 +02003219 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003220 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3221
3222 return 1;
3223}
3224
3225/* Called when a filter instance is detached from a stream. It release the
3226 * attached SPOE context. */
3227static void
3228spoe_stop(struct stream *s, struct filter *filter)
3229{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3231 (int)now.tv_sec, (int)now.tv_usec,
3232 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3233 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003234 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003235}
3236
Christopher Fauletf7a30922016-11-10 15:04:51 +01003237
3238/*
3239 * Called when the stream is woken up because of expired timer.
3240 */
3241static void
3242spoe_check_timeouts(struct stream *s, struct filter *filter)
3243{
3244 struct spoe_context *ctx = filter->ctx;
3245
Christopher Fauletac580602018-03-20 16:09:20 +01003246 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003247 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003248}
3249
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250/* Called when we are ready to filter data on a channel */
3251static int
3252spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3253{
3254 struct spoe_context *ctx = filter->ctx;
3255 int ret = 1;
3256
3257 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3258 " - ctx-flags=0x%08x\n",
3259 (int)now.tv_sec, (int)now.tv_usec,
3260 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3261 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3262
Christopher Fauletb067b062017-01-04 16:39:11 +01003263 if (ctx->state == SPOE_CTX_ST_NONE)
3264 goto out;
3265
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003266 if (!(chn->flags & CF_ISRESP)) {
3267 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3268 chn->analysers |= AN_REQ_INSPECT_FE;
3269 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3270 chn->analysers |= AN_REQ_INSPECT_BE;
3271
3272 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3273 goto out;
3274
3275 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003276 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003277 if (!ret)
3278 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3280 }
3281 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003282 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003283 chn->analysers |= AN_RES_INSPECT;
3284
3285 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3286 goto out;
3287
Christopher Faulet8ef75252017-02-20 22:56:03 +01003288 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003289 if (!ret) {
3290 channel_dont_read(chn);
3291 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003292 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003293 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003294 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295 }
3296
3297 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003298 return ret;
3299}
3300
3301/* Called before a processing happens on a given channel */
3302static int
3303spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3304 struct channel *chn, unsigned an_bit)
3305{
3306 struct spoe_context *ctx = filter->ctx;
3307 int ret = 1;
3308
3309 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3310 " - ctx-flags=0x%08x - ana=0x%08x\n",
3311 (int)now.tv_sec, (int)now.tv_usec,
3312 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3313 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3314 ctx->flags, an_bit);
3315
Christopher Fauletb067b062017-01-04 16:39:11 +01003316 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 goto out;
3318
3319 switch (an_bit) {
3320 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003321 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 break;
3323 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003324 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 break;
3326 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003327 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003328 break;
3329 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003330 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331 break;
3332 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003333 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003334 break;
3335 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003336 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003337 break;
3338 }
3339
3340 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003341 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342 channel_dont_read(chn);
3343 channel_dont_close(chn);
3344 }
3345 return ret;
3346}
3347
3348/* Called when the filtering on the channel ends. */
3349static int
3350spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3351{
3352 struct spoe_context *ctx = filter->ctx;
3353
3354 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3355 " - ctx-flags=0x%08x\n",
3356 (int)now.tv_sec, (int)now.tv_usec,
3357 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3358 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3359
3360 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003361 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003362 }
3363
3364 return 1;
3365}
3366
3367/********************************************************************
3368 * Functions that manage the filter initialization
3369 ********************************************************************/
3370struct flt_ops spoe_ops = {
3371 /* Manage SPOE filter, called for each filter declaration */
3372 .init = spoe_init,
3373 .deinit = spoe_deinit,
3374 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003375 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003376
3377 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003378 .attach = spoe_start,
3379 .detach = spoe_stop,
3380 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381
3382 /* Handle channels activity */
3383 .channel_start_analyze = spoe_start_analyze,
3384 .channel_pre_analyze = spoe_chn_pre_analyze,
3385 .channel_end_analyze = spoe_end_analyze,
3386};
3387
3388
3389static int
3390cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3391{
3392 const char *err;
3393 int i, err_code = 0;
3394
3395 if ((cfg_scope == NULL && curengine != NULL) ||
3396 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003397 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 goto out;
3399
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003400 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003401 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003402 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3403 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003404 err_code |= ERR_ALERT | ERR_ABORT;
3405 goto out;
3406 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003407 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3408 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003409 goto out;
3410 }
3411
3412 err = invalid_char(args[1]);
3413 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003414 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3415 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003416 err_code |= ERR_ALERT | ERR_ABORT;
3417 goto out;
3418 }
3419
3420 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003421 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3422 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003423 err_code |= ERR_ALERT | ERR_ABORT;
3424 goto out;
3425 }
3426 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003427 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003428 err_code |= ERR_ALERT | ERR_ABORT;
3429 goto out;
3430 }
3431
3432 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003433
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 curagent->conf.file = strdup(file);
3435 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003436
3437 curagent->timeout.hello = TICK_ETERNITY;
3438 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003439 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003440
Christopher Fauleta1cda022016-12-21 08:58:06 +01003441 curagent->var_pfx = NULL;
3442 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003443 curagent->var_t_process = NULL;
3444 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003445 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003446 curagent->cps_max = 0;
3447 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003448 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003449 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450
3451 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003452 LIST_INIT(&curagent->events[i]);
3453 LIST_INIT(&curagent->groups);
3454 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003455 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003456 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003457 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003458 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3459 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003460 err_code |= ERR_ALERT | ERR_FATAL;
3461 goto out;
3462 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003463 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003464 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003465 free(curagent->b.name);
3466 curagent->b.name = strdup(args[1]);
3467 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003468 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003469 int cur_arg = 1;
3470 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003471 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003472
Christopher Faulet11610f32017-09-21 10:23:10 +02003473 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003474 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003475 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3476 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003477 err_code |= ERR_ALERT | ERR_FATAL;
3478 goto out;
3479 }
3480 }
3481
Christopher Faulet11610f32017-09-21 10:23:10 +02003482 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003483 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003484 err_code |= ERR_ALERT | ERR_ABORT;
3485 goto out;
3486 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003487 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003488 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003489 cur_arg++;
3490 }
3491 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003492 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003493 int cur_arg = 1;
3494 while (*args[cur_arg]) {
3495 struct spoe_placeholder *ph = NULL;
3496
3497 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003498 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003499 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3500 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003501 err_code |= ERR_ALERT | ERR_FATAL;
3502 goto out;
3503 }
3504 }
3505
3506 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003507 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003508 err_code |= ERR_ALERT | ERR_ABORT;
3509 goto out;
3510 }
3511 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003512 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003513 cur_arg++;
3514 }
3515 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003516 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003517 unsigned int *tv = NULL;
3518 const char *res;
3519 unsigned timeout;
3520
3521 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003522 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3523 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003524 err_code |= ERR_ALERT | ERR_FATAL;
3525 goto out;
3526 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003527 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3528 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003529 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003530 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003531 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003532 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003533 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003534 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003535 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003536 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3537 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003538 err_code |= ERR_ALERT | ERR_FATAL;
3539 goto out;
3540 }
3541 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003542 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3543 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003544 err_code |= ERR_ALERT | ERR_FATAL;
3545 goto out;
3546 }
3547 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003548 if (res == PARSE_TIME_OVER) {
3549 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3550 file, linenum, args[2], args[0], args[1]);
3551 err_code |= ERR_ALERT | ERR_FATAL;
3552 goto out;
3553 }
3554 else if (res == PARSE_TIME_UNDER) {
3555 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3556 file, linenum, args[2], args[0], args[1]);
3557 err_code |= ERR_ALERT | ERR_FATAL;
3558 goto out;
3559 }
3560 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003561 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3562 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003563 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003564 goto out;
3565 }
3566 *tv = MS_TO_TICKS(timeout);
3567 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003568 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003569 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003570 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3571 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003572 err_code |= ERR_ALERT | ERR_FATAL;
3573 goto out;
3574 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003575
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003576 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003577 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003578 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003579 if (kwm == 1)
3580 curagent->flags &= ~SPOE_FL_PIPELINING;
3581 else
3582 curagent->flags |= SPOE_FL_PIPELINING;
3583 goto out;
3584 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003585 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003586 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003587 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003588 if (kwm == 1)
3589 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003590 else
3591 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003592 goto out;
3593 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003594 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003595 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3596 goto out;
3597 if (kwm == 1)
3598 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3599 else
3600 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3601 goto out;
3602 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003603 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003604 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3605 goto out;
3606 if (kwm == 1)
3607 curpxopts2 &= ~PR_O2_NOLOGNORM;
3608 else
3609 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003610 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003611 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003612
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003613 /* Following options does not support negation */
3614 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003615 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3616 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003617 err_code |= ERR_ALERT | ERR_FATAL;
3618 goto out;
3619 }
3620
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003621 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003622 char *tmp;
3623
3624 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003625 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3626 file, linenum, args[0],
3627 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003628 err_code |= ERR_ALERT | ERR_FATAL;
3629 goto out;
3630 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003631 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3632 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003633 tmp = args[2];
3634 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003635 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003636 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003637 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003638 err_code |= ERR_ALERT | ERR_FATAL;
3639 goto out;
3640 }
3641 tmp++;
3642 }
3643 curagent->var_pfx = strdup(args[2]);
3644 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003645 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003646 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3647 goto out;
3648 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3649 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003650 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003651 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003652 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003653 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3654 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003655 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003656 char *tmp;
3657
3658 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003659 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3660 file, linenum, args[0],
3661 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003662 err_code |= ERR_ALERT | ERR_FATAL;
3663 goto out;
3664 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003665 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3666 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003667 tmp = args[2];
3668 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003669 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003670 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003671 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003672 err_code |= ERR_ALERT | ERR_FATAL;
3673 goto out;
3674 }
3675 tmp++;
3676 }
3677 curagent->var_on_error = strdup(args[2]);
3678 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003679 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003680 char *tmp;
3681
3682 if (!*args[2]) {
3683 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3684 file, linenum, args[0],
3685 args[1]);
3686 err_code |= ERR_ALERT | ERR_FATAL;
3687 goto out;
3688 }
3689 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3690 goto out;
3691 tmp = args[2];
3692 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003693 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003694 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003695 file, linenum, args[0], args[1]);
3696 err_code |= ERR_ALERT | ERR_FATAL;
3697 goto out;
3698 }
3699 tmp++;
3700 }
3701 curagent->var_t_process = strdup(args[2]);
3702 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003703 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003704 char *tmp;
3705
3706 if (!*args[2]) {
3707 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3708 file, linenum, args[0],
3709 args[1]);
3710 err_code |= ERR_ALERT | ERR_FATAL;
3711 goto out;
3712 }
3713 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3714 goto out;
3715 tmp = args[2];
3716 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003717 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003718 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003719 file, linenum, args[0], args[1]);
3720 err_code |= ERR_ALERT | ERR_FATAL;
3721 goto out;
3722 }
3723 tmp++;
3724 }
3725 curagent->var_t_total = strdup(args[2]);
3726 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003727 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003728 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3729 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003730 err_code |= ERR_ALERT | ERR_FATAL;
3731 goto out;
3732 }
Christopher Faulet48026722016-11-16 15:01:12 +01003733 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003734 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003735 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003736 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3737 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003738 err_code |= ERR_ALERT | ERR_FATAL;
3739 goto out;
3740 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003741 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003742 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003743 curagent->cps_max = atol(args[1]);
3744 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003745 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003746 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003747 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3748 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003749 err_code |= ERR_ALERT | ERR_FATAL;
3750 goto out;
3751 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003752 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003753 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003754 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003755 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003756 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003757 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003758 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3759 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003760 err_code |= ERR_ALERT | ERR_FATAL;
3761 goto out;
3762 }
3763 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3764 goto out;
3765 curagent->max_frame_size = atol(args[1]);
3766 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3767 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003768 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3769 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003770 err_code |= ERR_ALERT | ERR_FATAL;
3771 goto out;
3772 }
3773 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003774 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003775 if (!*args[1]) {
3776 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3777 file, linenum, args[0]);
3778 err_code |= ERR_ALERT | ERR_FATAL;
3779 goto out;
3780 }
3781 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3782 goto out;
3783 curagent->max_fpa = atol(args[1]);
3784 if (curagent->max_fpa < 1) {
3785 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3786 file, linenum, args[0]);
3787 err_code |= ERR_ALERT | ERR_FATAL;
3788 goto out;
3789 }
3790 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003791 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003792 int cur_arg;
3793
3794 if (!*args[1]) {
3795 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3796 file, linenum, args[0]);
3797 err_code |= ERR_ALERT | ERR_FATAL;
3798 goto out;
3799 }
3800 cur_arg = 1;
3801 while (*args[cur_arg]) {
3802 struct spoe_var_placeholder *vph;
3803
3804 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3805 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3806 err_code |= ERR_ALERT | ERR_ABORT;
3807 goto out;
3808 }
3809 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003810 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003811 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3812 err_code |= ERR_ALERT | ERR_ABORT;
3813 goto out;
3814 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003815 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003816 cur_arg++;
3817 }
3818 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003819 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003820 char *errmsg = NULL;
3821
Emeric Brun9533a702021-04-02 10:13:43 +02003822 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003823 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3824 err_code |= ERR_ALERT | ERR_FATAL;
3825 goto out;
3826 }
3827 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003828 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003829 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3830 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003831 err_code |= ERR_ALERT | ERR_FATAL;
3832 goto out;
3833 }
3834 out:
3835 return err_code;
3836}
Christopher Faulet11610f32017-09-21 10:23:10 +02003837static int
3838cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3839{
3840 struct spoe_group *grp;
3841 const char *err;
3842 int err_code = 0;
3843
3844 if ((cfg_scope == NULL && curengine != NULL) ||
3845 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003846 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003847 goto out;
3848
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003849 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003850 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003851 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3852 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003853 err_code |= ERR_ALERT | ERR_ABORT;
3854 goto out;
3855 }
3856 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3857 err_code |= ERR_ABORT;
3858 goto out;
3859 }
3860
3861 err = invalid_char(args[1]);
3862 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003863 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3864 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003865 err_code |= ERR_ALERT | ERR_ABORT;
3866 goto out;
3867 }
3868
3869 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003870 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003871 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3872 " name as another one declared at %s:%d.\n",
3873 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003874 err_code |= ERR_ALERT | ERR_FATAL;
3875 goto out;
3876 }
3877 }
3878
3879 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003880 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003881 err_code |= ERR_ALERT | ERR_ABORT;
3882 goto out;
3883 }
3884
3885 curgrp->id = strdup(args[1]);
3886 curgrp->conf.file = strdup(file);
3887 curgrp->conf.line = linenum;
3888 LIST_INIT(&curgrp->phs);
3889 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003890 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003891 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003892 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003893 int cur_arg = 1;
3894 while (*args[cur_arg]) {
3895 struct spoe_placeholder *ph = NULL;
3896
3897 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003898 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003899 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3900 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003901 err_code |= ERR_ALERT | ERR_FATAL;
3902 goto out;
3903 }
3904 }
3905
3906 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003907 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003908 err_code |= ERR_ALERT | ERR_ABORT;
3909 goto out;
3910 }
3911 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003912 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003913 cur_arg++;
3914 }
3915 }
3916 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003917 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3918 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003919 err_code |= ERR_ALERT | ERR_FATAL;
3920 goto out;
3921 }
3922 out:
3923 return err_code;
3924}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003925
3926static int
3927cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3928{
3929 struct spoe_message *msg;
3930 struct spoe_arg *arg;
3931 const char *err;
3932 char *errmsg = NULL;
3933 int err_code = 0;
3934
3935 if ((cfg_scope == NULL && curengine != NULL) ||
3936 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003937 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003938 goto out;
3939
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003940 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003941 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003942 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3943 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003944 err_code |= ERR_ALERT | ERR_ABORT;
3945 goto out;
3946 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003947 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3948 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003949 goto out;
3950 }
3951
3952 err = invalid_char(args[1]);
3953 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003954 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3955 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003956 err_code |= ERR_ALERT | ERR_ABORT;
3957 goto out;
3958 }
3959
3960 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003961 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003962 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3963 " name as another one declared at %s:%d.\n",
3964 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 err_code |= ERR_ALERT | ERR_FATAL;
3966 goto out;
3967 }
3968 }
3969
3970 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003971 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003972 err_code |= ERR_ALERT | ERR_ABORT;
3973 goto out;
3974 }
3975
3976 curmsg->id = strdup(args[1]);
3977 curmsg->id_len = strlen(curmsg->id);
3978 curmsg->event = SPOE_EV_NONE;
3979 curmsg->conf.file = strdup(file);
3980 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003981 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003983 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003984 LIST_INIT(&curmsg->by_evt);
3985 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003986 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003987 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003988 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003989 int cur_arg = 1;
3990
3991 curproxy->conf.args.ctx = ARGC_SPOE;
3992 curproxy->conf.args.file = file;
3993 curproxy->conf.args.line = linenum;
3994 while (*args[cur_arg]) {
3995 char *delim = strchr(args[cur_arg], '=');
3996 int idx = 0;
3997
3998 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003999 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004000 err_code |= ERR_ALERT | ERR_ABORT;
4001 goto out;
4002 }
4003
4004 if (!delim) {
4005 arg->name = NULL;
4006 arg->name_len = 0;
4007 delim = args[cur_arg];
4008 }
4009 else {
4010 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
4011 arg->name_len = delim - args[cur_arg];
4012 delim++;
4013 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01004014 arg->expr = sample_parse_expr((char*[]){delim, NULL},
4015 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01004016 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004017 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004018 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004019 err_code |= ERR_ALERT | ERR_FATAL;
4020 free(arg->name);
4021 free(arg);
4022 goto out;
4023 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004024 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004025 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004026 cur_arg++;
4027 }
4028 curproxy->conf.args.file = NULL;
4029 curproxy->conf.args.line = 0;
4030 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004031 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004032 err = invalid_char(args[1]);
4033 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004034 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4035 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004036 err_code |= ERR_ALERT | ERR_FATAL;
4037 goto out;
4038 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004039 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004040 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004041 "logical disjunction within a condition.\n",
4042 file, linenum, args[1]);
4043 err_code |= ERR_ALERT | ERR_FATAL;
4044 goto out;
4045 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004046 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004047 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4048 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004049 err_code |= ERR_ALERT | ERR_FATAL;
4050 goto out;
4051 }
4052 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004053 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004054 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004055 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004056 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 goto out;
4058 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004059 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4060 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004061
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004062 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004063 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004064 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004065 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4066
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004067 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004068 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004069 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004070 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004071 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004072 curmsg->event = SPOE_EV_ON_TCP_RSP;
4073
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004074 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004075 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004076 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004077 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004078 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004079 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4080 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004081 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004082 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004083 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004084 goto out;
4085 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004086
4087 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4088 struct acl_cond *cond;
4089
4090 cond = build_acl_cond(file, linenum, &curmsg->acls,
4091 curproxy, (const char **)args+2,
4092 &errmsg);
4093 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004094 ha_alert("parsing [%s:%d] : error detected while "
4095 "parsing an 'event %s' condition : %s.\n",
4096 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004097 err_code |= ERR_ALERT | ERR_FATAL;
4098 goto out;
4099 }
4100 curmsg->cond = cond;
4101 }
4102 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004103 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4104 "or 'unless' followed by a condition but found '%s'.\n",
4105 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004106 err_code |= ERR_ALERT | ERR_FATAL;
4107 goto out;
4108 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004109 }
4110 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004111 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4112 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004113 err_code |= ERR_ALERT | ERR_FATAL;
4114 goto out;
4115 }
4116 out:
4117 free(errmsg);
4118 return err_code;
4119}
4120
4121/* Return -1 on error, else 0 */
4122static int
4123parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4124 struct flt_conf *fconf, char **err, void *private)
4125{
4126 struct list backup_sections;
4127 struct spoe_config *conf;
4128 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004129 struct spoe_group *grp, *grpback;
4130 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004131 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004132 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004133 char *file = NULL, *engine = NULL;
4134 int ret, pos = *cur_arg + 1;
4135
Christopher Faulet84c844e2018-03-23 14:37:14 +01004136 LIST_INIT(&curmsgs);
4137 LIST_INIT(&curgrps);
4138 LIST_INIT(&curmphs);
4139 LIST_INIT(&curgphs);
4140 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004141 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004142 curpxopts = 0;
4143 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004144
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004145 conf = calloc(1, sizeof(*conf));
4146 if (conf == NULL) {
4147 memprintf(err, "%s: out of memory", args[*cur_arg]);
4148 goto error;
4149 }
4150 conf->proxy = px;
4151
4152 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004153 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004154 if (!*args[pos+1]) {
4155 memprintf(err, "'%s' : '%s' option without value",
4156 args[*cur_arg], args[pos]);
4157 goto error;
4158 }
4159 file = args[pos+1];
4160 pos += 2;
4161 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004162 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004163 if (!*args[pos+1]) {
4164 memprintf(err, "'%s' : '%s' option without value",
4165 args[*cur_arg], args[pos]);
4166 goto error;
4167 }
4168 engine = args[pos+1];
4169 pos += 2;
4170 }
4171 else {
4172 memprintf(err, "unknown keyword '%s'", args[pos]);
4173 goto error;
4174 }
4175 }
4176 if (file == NULL) {
4177 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4178 goto error;
4179 }
4180
4181 /* backup sections and register SPOE sections */
4182 LIST_INIT(&backup_sections);
4183 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004184 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4185 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004186 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004187
4188 /* Parse SPOE filter configuration file */
4189 curengine = engine;
4190 curproxy = px;
4191 curagent = NULL;
4192 curmsg = NULL;
4193 ret = readcfgfile(file);
4194 curproxy = NULL;
4195
4196 /* unregister SPOE sections and restore previous sections */
4197 cfg_unregister_sections();
4198 cfg_restore_sections(&backup_sections);
4199
4200 if (ret == -1) {
4201 memprintf(err, "Could not open configuration file %s : %s",
4202 file, strerror(errno));
4203 goto error;
4204 }
4205 if (ret & (ERR_ABORT|ERR_FATAL)) {
4206 memprintf(err, "Error(s) found in configuration file %s", file);
4207 goto error;
4208 }
4209
4210 /* Check SPOE agent */
4211 if (curagent == NULL) {
4212 memprintf(err, "No SPOE agent found in file %s", file);
4213 goto error;
4214 }
4215 if (curagent->b.name == NULL) {
4216 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4217 curagent->id, curagent->conf.file, curagent->conf.line);
4218 goto error;
4219 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004220 if (curagent->timeout.hello == TICK_ETERNITY ||
4221 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004222 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004223 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4224 " | While not properly invalid, you will certainly encounter various problems\n"
4225 " | with such a configuration. To fix this, please ensure that all following\n"
4226 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4227 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004228 }
4229 if (curagent->var_pfx == NULL) {
4230 char *tmp = curagent->id;
4231
4232 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004233 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004234 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4235 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4236 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4237 goto error;
4238 }
4239 tmp++;
4240 }
4241 curagent->var_pfx = strdup(curagent->id);
4242 }
4243
Christopher Fauletb7426d12018-03-21 14:12:17 +01004244 if (curagent->var_on_error) {
4245 struct arg arg;
4246
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004247 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004248 curagent->var_pfx, curagent->var_on_error);
4249
4250 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004251 arg.data.str.area = trash.area;
4252 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004253 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004254 if (!vars_check_arg(&arg, err)) {
4255 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4256 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4257 goto error;
4258 }
4259 }
4260
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004261 if (curagent->var_t_process) {
4262 struct arg arg;
4263
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004264 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004265 curagent->var_pfx, curagent->var_t_process);
4266
4267 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004268 arg.data.str.area = trash.area;
4269 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004270 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004271 if (!vars_check_arg(&arg, err)) {
4272 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4273 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4274 goto error;
4275 }
4276 }
4277
4278 if (curagent->var_t_total) {
4279 struct arg arg;
4280
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004281 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004282 curagent->var_pfx, curagent->var_t_total);
4283
4284 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004285 arg.data.str.area = trash.area;
4286 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004287 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004288 if (!vars_check_arg(&arg, err)) {
4289 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4290 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4291 goto error;
4292 }
4293 }
4294
Christopher Faulet11610f32017-09-21 10:23:10 +02004295 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004296 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4297 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004298 goto finish;
4299 }
4300
Christopher Faulet11610f32017-09-21 10:23:10 +02004301 /* Replace placeholders by the corresponding messages for the SPOE
4302 * agent */
4303 list_for_each_entry(ph, &curmphs, list) {
4304 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004305 struct spoe_arg *arg;
4306 unsigned int where;
4307
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004308 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004309 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4310 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4311 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4312 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4313 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4314 }
4315 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4316 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4317 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004318 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4319 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004320 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004321 }
4322 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004323 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4324 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004325 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004326 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004327
4328 where = 0;
4329 switch (msg->event) {
4330 case SPOE_EV_ON_CLIENT_SESS:
4331 where |= SMP_VAL_FE_CON_ACC;
4332 break;
4333
4334 case SPOE_EV_ON_TCP_REQ_FE:
4335 where |= SMP_VAL_FE_REQ_CNT;
4336 break;
4337
4338 case SPOE_EV_ON_HTTP_REQ_FE:
4339 where |= SMP_VAL_FE_HRQ_HDR;
4340 break;
4341
4342 case SPOE_EV_ON_TCP_REQ_BE:
4343 if (px->cap & PR_CAP_FE)
4344 where |= SMP_VAL_FE_REQ_CNT;
4345 if (px->cap & PR_CAP_BE)
4346 where |= SMP_VAL_BE_REQ_CNT;
4347 break;
4348
4349 case SPOE_EV_ON_HTTP_REQ_BE:
4350 if (px->cap & PR_CAP_FE)
4351 where |= SMP_VAL_FE_HRQ_HDR;
4352 if (px->cap & PR_CAP_BE)
4353 where |= SMP_VAL_BE_HRQ_HDR;
4354 break;
4355
4356 case SPOE_EV_ON_SERVER_SESS:
4357 where |= SMP_VAL_BE_SRV_CON;
4358 break;
4359
4360 case SPOE_EV_ON_TCP_RSP:
4361 if (px->cap & PR_CAP_FE)
4362 where |= SMP_VAL_FE_RES_CNT;
4363 if (px->cap & PR_CAP_BE)
4364 where |= SMP_VAL_BE_RES_CNT;
4365 break;
4366
4367 case SPOE_EV_ON_HTTP_RSP:
4368 if (px->cap & PR_CAP_FE)
4369 where |= SMP_VAL_FE_HRS_HDR;
4370 if (px->cap & PR_CAP_BE)
4371 where |= SMP_VAL_BE_HRS_HDR;
4372 break;
4373
4374 default:
4375 break;
4376 }
4377
4378 list_for_each_entry(arg, &msg->args, list) {
4379 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004380 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004381 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004382 "none of which is available here ('%s')",
4383 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004384 sample_ckp_names(arg->expr->fetch->use),
4385 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004386 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004387 }
4388 }
4389
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004390 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004391 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004392 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004393 }
4394 }
4395 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004396 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004397 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004398 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004399 continue;
4400 }
4401
Christopher Faulet11610f32017-09-21 10:23:10 +02004402 /* Replace placeholders by the corresponding groups for the SPOE
4403 * agent */
4404 list_for_each_entry(ph, &curgphs, list) {
4405 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004406 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004407 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004408 LIST_DELETE(&grp->list);
4409 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004410 goto next_aph;
4411 }
4412 }
4413 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4414 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4415 goto error;
4416 next_aph:
4417 continue;
4418 }
4419
4420 /* Replace placeholders by the corresponding message for each SPOE
4421 * group of the SPOE agent */
4422 list_for_each_entry(grp, &curagent->groups, list) {
4423 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4424 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004425 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004426 if (msg->group != NULL) {
4427 memprintf(err, "SPOE message '%s' already belongs to "
4428 "the SPOE group '%s' declare at %s:%d",
4429 msg->id, msg->group->id,
4430 msg->group->conf.file,
4431 msg->group->conf.line);
4432 goto error;
4433 }
4434
4435 /* Scope for arguments are not checked for now. We will check
4436 * them only if a rule use the corresponding SPOE group. */
4437 msg->agent = curagent;
4438 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004439 LIST_DELETE(&ph->list);
4440 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004441 goto next_mph_grp;
4442 }
4443 }
4444 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4445 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4446 goto error;
4447 next_mph_grp:
4448 continue;
4449 }
4450 }
4451
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004452 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004453 /* move curmsgs to the agent message list */
4454 curmsgs.n->p = &curagent->messages;
4455 curmsgs.p->n = &curagent->messages;
4456 curagent->messages = curmsgs;
4457 LIST_INIT(&curmsgs);
4458
Christopher Faulet7ee86672017-09-19 11:08:28 +02004459 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004460 conf->agent = curagent;
Christopher Faulet434b8522021-08-02 17:51:01 +02004461 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004462
4463 /* Start agent's proxy initialization here. It will be finished during
4464 * the filter init. */
4465 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4466 init_new_proxy(&conf->agent_fe);
4467 conf->agent_fe.id = conf->agent->id;
4468 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004469 conf->agent_fe.options |= curpxopts;
4470 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004471
4472 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004473 LIST_DELETE(&logsrv->list);
4474 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004475 }
4476
Christopher Faulet11610f32017-09-21 10:23:10 +02004477 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004478 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004479 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004480 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004481 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004482 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004483 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004484 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004485 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4486 struct arg arg;
4487
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004488 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004489 curagent->var_pfx, vph->name);
4490
4491 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004492 arg.data.str.area = trash.area;
4493 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004494 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004495 if (!vars_check_arg(&arg, err)) {
4496 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4497 curagent->id, curagent->var_pfx, vph->name, *err);
4498 goto error;
4499 }
4500
Willy Tarreau2b718102021-04-21 07:32:39 +02004501 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004502 free(vph->name);
4503 free(vph);
4504 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004505 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004506 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004507 spoe_release_group(grp);
4508 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004509 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004510 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004511 fconf->ops = &spoe_ops;
4512 fconf->conf = conf;
4513 return 0;
4514
4515 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004516 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004517 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004518 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004519 spoe_release_placeholder(ph);
4520 }
4521 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004522 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004523 spoe_release_placeholder(ph);
4524 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004525 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004526 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004527 free(vph->name);
4528 free(vph);
4529 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004530 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004531 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004532 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004533 }
4534 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004535 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004536 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004537 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004538 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004539 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004540 free(logsrv);
4541 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004542 free(conf);
4543 return -1;
4544}
4545
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004546/* Send message of a SPOE group. This is the action_ptr callback of a rule
4547 * associated to a "send-spoe-group" action.
4548 *
Christopher Faulet13403762019-12-13 09:01:57 +01004549 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4550 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004551static enum act_return
4552spoe_send_group(struct act_rule *rule, struct proxy *px,
4553 struct session *sess, struct stream *s, int flags)
4554{
4555 struct filter *filter;
4556 struct spoe_agent *agent = NULL;
4557 struct spoe_group *group = NULL;
4558 struct spoe_context *ctx = NULL;
4559 int ret, dir;
4560
4561 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4562 if (filter->config == rule->arg.act.p[0]) {
4563 agent = rule->arg.act.p[2];
4564 group = rule->arg.act.p[3];
4565 ctx = filter->ctx;
4566 break;
4567 }
4568 }
4569 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004570 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004571 if (ctx->state == SPOE_CTX_ST_NONE)
4572 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004573
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004574 switch (rule->from) {
4575 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4576 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4577 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4578 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4579 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4580 default:
4581 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4582 " - internal error while execute spoe-send-group\n",
4583 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4584 __FUNCTION__, s);
4585 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4586 agent->id);
4587 return ACT_RET_CONT;
4588 }
4589
4590 ret = spoe_process_group(s, ctx, group, dir);
4591 if (ret == 1)
4592 return ACT_RET_CONT;
4593 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004594 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004595 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4596 " - failed to process group '%s': interrupted by caller\n",
4597 (int)now.tv_sec, (int)now.tv_usec,
4598 agent->id, __FUNCTION__, s, group->id);
4599 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004600 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004601 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004602 return ACT_RET_CONT;
4603 }
4604 return ACT_RET_YIELD;
4605 }
4606 else
Christopher Faulet13403762019-12-13 09:01:57 +01004607 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004608}
4609
4610/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4611 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4612 * action should be:
4613 *
4614 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4615 *
4616 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4617 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4618 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4619 * group.
4620 *
4621 * The function returns 1 in success case, otherwise, it returns 0 and err is
4622 * filled.
4623 */
4624static int
4625check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4626{
4627 struct flt_conf *fconf;
4628 struct spoe_config *conf;
4629 struct spoe_agent *agent = NULL;
4630 struct spoe_group *group;
4631 struct spoe_message *msg;
4632 char *engine_id = rule->arg.act.p[0];
4633 char *group_id = rule->arg.act.p[1];
4634 unsigned int where = 0;
4635
4636 switch (rule->from) {
4637 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4638 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4639 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4640 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4641 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4642 default:
4643 memprintf(err,
4644 "internal error, unexpected rule->from=%d, please report this bug!",
4645 rule->from);
4646 goto error;
4647 }
4648
4649 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4650 * <px> */
4651 list_for_each_entry(fconf, &px->filter_configs, list) {
4652 conf = fconf->conf;
4653
4654 /* This is not an SPOE filter */
4655 if (fconf->id != spoe_filter_id)
4656 continue;
4657
4658 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004659 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004660 agent = conf->agent;
4661 break;
4662 }
4663 }
4664 if (agent == NULL) {
4665 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4666 engine_id, group_id);
4667 goto error;
4668 }
4669
4670 /* Try to find the right group */
4671 list_for_each_entry(group, &agent->groups, list) {
4672 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004673 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004674 break;
4675 }
4676 if (&group->list == &agent->groups) {
4677 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4678 group_id, engine_id);
4679 goto error;
4680 }
4681
4682 /* Ok, we found the group, we need to check messages and their
4683 * arguments */
4684 list_for_each_entry(msg, &group->messages, by_grp) {
4685 struct spoe_arg *arg;
4686
4687 list_for_each_entry(arg, &msg->args, list) {
4688 if (!(arg->expr->fetch->val & where)) {
4689 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4690 "some args extract information from '%s',"
4691 "none of which is available here ('%s')",
4692 msg->id, group->id, msg->conf.file, msg->conf.line,
4693 sample_ckp_names(arg->expr->fetch->use),
4694 sample_ckp_names(where));
4695 goto error;
4696 }
4697 }
4698 }
4699
4700 free(engine_id);
4701 free(group_id);
4702 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4703 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4704 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4705 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4706 return 1;
4707
4708 error:
4709 free(engine_id);
4710 free(group_id);
4711 return 0;
4712}
4713
4714/* Parse 'send-spoe-group' action following the format:
4715 *
4716 * ... send-spoe-group <engine-id> <group-id>
4717 *
4718 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4719 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4720 * ids are saved and used later, when the rule will be checked.
4721 */
4722static enum act_parse_ret
4723parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4724 struct act_rule *rule, char **err)
4725{
4726 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4727 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4728 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4729 return ACT_RET_PRS_ERR;
4730 }
4731 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4732 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4733
4734 (*orig_arg) += 2;
4735
4736 rule->action = ACT_CUSTOM;
4737 rule->action_ptr = spoe_send_group;
4738 rule->check_ptr = check_send_spoe_group;
4739 return ACT_RET_PRS_OK;
4740}
4741
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004742
4743/* Declare the filter parser for "spoe" keyword */
4744static struct flt_kw_list flt_kws = { "SPOE", { }, {
4745 { "spoe", parse_spoe_flt, NULL },
4746 { NULL, NULL, NULL },
4747 }
4748};
4749
Willy Tarreau0108d902018-11-25 19:14:37 +01004750INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4751
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004752/* Delcate the action parser for "spoe-action" keyword */
4753static struct action_kw_list tcp_req_action_kws = { { }, {
4754 { "send-spoe-group", parse_send_spoe_group },
4755 { /* END */ },
4756 }
4757};
Willy Tarreau0108d902018-11-25 19:14:37 +01004758
4759INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4760
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004761static struct action_kw_list tcp_res_action_kws = { { }, {
4762 { "send-spoe-group", parse_send_spoe_group },
4763 { /* END */ },
4764 }
4765};
Willy Tarreau0108d902018-11-25 19:14:37 +01004766
4767INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4768
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004769static struct action_kw_list http_req_action_kws = { { }, {
4770 { "send-spoe-group", parse_send_spoe_group },
4771 { /* END */ },
4772 }
4773};
Willy Tarreau0108d902018-11-25 19:14:37 +01004774
4775INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4776
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004777static struct action_kw_list http_res_action_kws = { { }, {
4778 { "send-spoe-group", parse_send_spoe_group },
4779 { /* END */ },
4780 }
4781};
4782
Willy Tarreau0108d902018-11-25 19:14:37 +01004783INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);