blob: 4826aa02cddf2c88ac7bd7fabf2755df261ff657 [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>
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020020#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020021#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020023#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020024#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020025#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020027#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020029#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010031#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020032#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020035#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020036#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaue16ada12021-05-08 12:57:17 +020039#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020040#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020042
43#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
44#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010045#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020046#else
47#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010048#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020049#endif
50
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010051/* Reserved 4 bytes to the frame size. So a frame and its size can be written
52 * together in a buffer */
53#define MAX_FRAME_SIZE global.tune.bufsize - 4
54
55/* The minimum size for a frame */
56#define MIN_FRAME_SIZE 256
57
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010058/* Reserved for the metadata and the frame type.
59 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010060#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020061
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010063#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
64
Christopher Faulet3b386a32017-02-23 10:17:15 +010065/* SPOE filter id. Used to identify SPOE filters */
66const char *spoe_filter_id = "SPOE filter";
67
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020068/* Set if the handle on SIGUSR1 is registered */
69static int sighandler_registered = 0;
70
71/* proxy used during the parsing */
72struct proxy *curproxy = NULL;
73
74/* The name of the SPOE engine, used during the parsing */
75char *curengine = NULL;
76
77/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020078/* SPOE agent/group/message used during the parsing */
79struct spoe_agent *curagent = NULL;
80struct spoe_group *curgrp = NULL;
81struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020082
83/* list of SPOE messages and placeholders used during the parsing */
84struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020085struct list curgrps;
86struct list curmphs;
87struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010088struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020089
Christopher Faulet7250b8f2018-03-26 17:19:01 +020090/* list of log servers used during the parsing */
91struct list curlogsrvs;
92
Christopher Faulet0e0f0852018-03-26 17:20:36 +020093/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
94int curpxopts;
95int curpxopts2;
96
Christopher Faulet42bfa462017-01-04 14:14:19 +010097/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +010098DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
99DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101struct flt_ops spoe_ops;
102
Christopher Faulet8ef75252017-02-20 22:56:03 +0100103static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
105static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Faulet4d350af2021-08-02 17:53:56 +0200106static struct appctx *spoe_create_appctx(struct spoe_config *conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107
108/********************************************************************
109 * helper functions/globals
110 ********************************************************************/
111static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200112spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113{
Christopher Faulet11610f32017-09-21 10:23:10 +0200114 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200115 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200116 free(ph->id);
117 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118}
119
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100121spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200122{
Christopher Faulet57583e42017-09-04 15:41:09 +0200123 struct spoe_arg *arg, *argback;
124 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125
126 if (!msg)
127 return;
128 free(msg->id);
129 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200130 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200131 release_sample_expr(arg->expr);
132 free(arg->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200133 LIST_DELETE(&arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 free(arg);
135 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200136 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200137 LIST_DELETE(&acl->list);
Christopher Faulet57583e42017-09-04 15:41:09 +0200138 prune_acl(acl);
139 free(acl);
140 }
141 if (msg->cond) {
142 prune_acl_cond(msg->cond);
143 free(msg->cond);
144 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200145 free(msg);
146}
147
148static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200149spoe_release_group(struct spoe_group *grp)
150{
151 if (!grp)
152 return;
153 free(grp->id);
154 free(grp->conf.file);
155 free(grp);
156}
157
158static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100159spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200160{
Christopher Faulet11610f32017-09-21 10:23:10 +0200161 struct spoe_message *msg, *msgback;
162 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200163 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200164
165 if (!agent)
166 return;
167 free(agent->id);
168 free(agent->conf.file);
169 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100170 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100171 free(agent->var_t_process);
172 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200173 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200174 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200175 spoe_release_message(msg);
176 }
177 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200178 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200179 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200180 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100181 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200182 for (i = 0; i < global.nbthread; ++i) {
183 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200185 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100186 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500250 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100255 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800256 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100257}
258
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100259
260static inline void
261spoe_update_stat_time(struct timeval *tv, long *t)
262{
263 if (*t == -1)
264 *t = tv_ms_elapsed(tv, &now);
265 else
266 *t += tv_ms_elapsed(tv, &now);
267 tv_zero(tv);
268}
269
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270/********************************************************************
271 * Functions that encode/decode SPOE frames
272 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/* Helper to get static string length, excluding the terminating null byte */
274#define SLEN(str) (sizeof(str)-1)
275
276/* Predefined key used in HELLO/DISCONNECT frames */
277#define SUPPORTED_VERSIONS_KEY "supported-versions"
278#define VERSION_KEY "version"
279#define MAX_FRAME_SIZE_KEY "max-frame-size"
280#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100281#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100282#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200283#define STATUS_CODE_KEY "status-code"
284#define MSG_KEY "message"
285
286struct spoe_version {
287 char *str;
288 int min;
289 int max;
290};
291
292/* All supported versions */
293static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200294 /* 1.0 is now unsupported because of a bug about frame's flags*/
295 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296 {NULL, 0, 0}
297};
298
299/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200300#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302/* Convert a string to a SPOE version value. The string must follow the format
303 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
304 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 const char *p, *end;
309 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 p = str;
312 end = str+len;
313 maj = min = 0;
314 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200315
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100317 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320 /* parse Major number, until the '.' */
321 while (*p != '.') {
322 if (p >= end || *p < '0' || *p > '9')
323 goto out;
324 maj *= 10;
325 maj += (*p - '0');
326 p++;
327 }
328
329 /* check Major version */
330 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331 goto out;
332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p++; /* skip the '.' */
334 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
335 goto out;
336
337 /* Parse Minor number */
338 while (p < end) {
339 if (*p < '0' || *p > '9')
340 break;
341 min *= 10;
342 min += (*p - '0');
343 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100345
346 /* check Minor number */
347 if (min > 999)
348 goto out;
349
350 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100351 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100352 p++;
353 if (p != end)
354 goto out;
355
356 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 out:
358 return vsn;
359}
360
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800362 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100365spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366{
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100368 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100369 char *p, *end;
370 unsigned int flags = SPOE_FRM_FL_FIN;
371 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 p = frame;
374 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 /* Set Frame type */
377 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100379 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200380 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100381 memcpy(p, (char *)&flags, 4);
382 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200383
384 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100385 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
388 * and "capabilities" */
389
390 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 sz = SLEN(SUPPORTED_VERSIONS_KEY);
392 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
393 goto too_big;
394
395 *p++ = SPOE_DATA_T_STR;
396 sz = SLEN(SUPPORTED_VERSIONS_VAL);
397 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
398 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200399
400 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100401 sz = SLEN(MAX_FRAME_SIZE_KEY);
402 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
403 goto too_big;
404
405 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200406 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 sz = SLEN(CAPABILITIES_KEY);
411 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
412 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
Christopher Faulet8ef75252017-02-20 22:56:03 +0100414 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100415 chk = get_trash_chunk();
416 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 memcpy(chk->area, "pipelining", 10);
418 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100419 }
420 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 if (chk->data) chk->area[chk->data++] = ',';
422 memcpy(chk->area+chk->data, "async", 5);
423 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100424 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100425 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 if (chk->data) chk->area[chk->data++] = ',';
427 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100428 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100429 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200430 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 goto too_big;
432
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500433 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200434 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100435 sz = SLEN(ENGINE_ID_KEY);
436 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
437 goto too_big;
438
439 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200440 sz = strlen(agent->rt[tid].engine_id);
441 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100442 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100443 }
444
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 return (p - frame);
446
447 too_big:
448 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
449 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200450}
451
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
453 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
454 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200455static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200457{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 const char *reason;
459 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100460 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 p = frame;
464 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 /* Set Frame type */
467 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100469 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200470 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100471 memcpy(p, (char *)&flags, 4);
472 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473
474 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 *p++ = 0; *p++ = 0;
476
477 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
478 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
480 /* There are 2 mandatory items: "status-code" and "message" */
481
482 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 sz = SLEN(STATUS_CODE_KEY);
484 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
485 goto too_big;
486
487 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200488 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 sz = SLEN(MSG_KEY);
493 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
494 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 /*Get the message corresponding to the status code */
497 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
498
499 *p++ = SPOE_DATA_T_STR;
500 sz = strlen(reason);
501 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
502 goto too_big;
503
504 return (p - frame);
505
506 too_big:
507 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
508 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509}
510
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
512 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
513 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100515spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100516 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 char *p, *end;
519 unsigned int stream_id, frame_id;
520 unsigned int flags = SPOE_FRM_FL_FIN;
521 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 p = frame;
524 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100526 stream_id = ctx->stream_id;
527 frame_id = ctx->frame_id;
528
529 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
530 /* The fragmentation is not supported by the applet */
531 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
532 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
533 return -1;
534 }
535 flags = ctx->frag_ctx.flags;
536 }
537
538 /* Set Frame type */
539 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
540
541 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200542 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100543 memcpy(p, (char *)&flags, 4);
544 p += 4;
545
546 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200547 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200549 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100550 goto too_big;
551
552 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200553 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100554 if (p + sz >= end)
555 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 p += sz;
558
559 return (p - frame);
560
561 too_big:
562 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
563 return 0;
564}
565
566/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
567 * returns the number of encoded bytes in the frame on success, 0 if an encoding
568 * error occurred and -1 if a fatal error occurred. */
569static int
570spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
571 char *frame, size_t size)
572{
573 char *p, *end;
574 unsigned int stream_id, frame_id;
575 unsigned int flags;
576 size_t sz;
577
578 p = frame;
579 end = frame+size;
580
Christopher Faulet8ef75252017-02-20 22:56:03 +0100581 /* <ctx> is null when the stream has aborted the processing of a
582 * fragmented frame. In this case, we must notify the corresponding
583 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100584 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100585 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100586 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
587 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
588 }
589 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100590 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100591 stream_id = ctx->stream_id;
592 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100593 }
594
Christopher Faulet8ef75252017-02-20 22:56:03 +0100595 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100596 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100598 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200599 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 memcpy(p, (char *)&flags, 4);
601 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200602
603 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200604 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200606 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200608
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 if (ctx == NULL)
610 goto end;
611
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200613 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 if (p + sz >= end)
615 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200616 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100618
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100619 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 return (p - frame);
621
622 too_big:
623 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
624 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200625}
626
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627/* Decode and process the HELLO frame sent by an agent. It returns the number of
628 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
629 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632{
Christopher Faulet305c6072017-02-23 16:17:53 +0100633 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
634 char *p, *end;
635 int vsn, max_frame_size;
636 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100637
638 p = frame;
639 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200640
641 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
643 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 if (size < 7 /* TYPE + METADATA */) {
648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
649 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650 }
651
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100652 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200654 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200656
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100657 /* Fragmentation is not supported for HELLO frame */
658 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100660 return -1;
661 }
662
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (*p != 0 || *(p+1) != 0) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
666 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669
670 /* There are 3 mandatory items: "version", "max-frame-size" and
671 * "capabilities" */
672
673 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100674 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 while (p < end) {
676 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200677 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200679
680 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 ret = spoe_decode_buffer(&p, end, &str, &sz);
682 if (ret == -1 || !sz) {
683 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
684 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200688 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 int i, type = *p++;
690
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
693 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
694 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
697 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
698 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 }
700
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 return -1;
705 }
706 for (i = 0; supported_versions[i].str != NULL; ++i) {
707 if (vsn >= supported_versions[i].min &&
708 vsn <= supported_versions[i].max)
709 break;
710 }
711 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 return -1;
714 }
715 }
716 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200717 else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100718 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719
720 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
722 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
723 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
724 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
726 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200728 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
730 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200731 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 if (sz < MIN_FRAME_SIZE ||
733 sz > SPOE_APPCTX(appctx)->max_frame_size) {
734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 max_frame_size = sz;
738 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200740 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100741 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742
743 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100744 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
745 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
746 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
749 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
750 return 0;
751 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752
Christopher Faulet8ef75252017-02-20 22:56:03 +0100753 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100754 char *delim;
755
756 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100757 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100758
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
760 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100761 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 flags |= SPOE_APPCTX_FL_PIPELINING;
763 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 else if (sz >= 5 && !strncmp(str, "async", 5)) {
765 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100766 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767 flags |= SPOE_APPCTX_FL_ASYNC;
768 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100769 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
770 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100771 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100772 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 /* Get the next comma or break */
776 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 delim++;
779 sz -= (delim - str);
780 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781 }
782 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200783 else {
784 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100785 if (spoe_skip_data(&p, end) == -1) {
786 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
787 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200788 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200789 }
790 }
791
792 /* Final checks */
793 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200795 return -1;
796 }
797 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200799 return -1;
800 }
Christopher Faulet11389012019-02-07 16:13:26 +0100801 if (!agent)
802 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
803 else {
804 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
805 flags &= ~SPOE_APPCTX_FL_PIPELINING;
806 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
807 flags &= ~SPOE_APPCTX_FL_ASYNC;
808 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809
Christopher Faulet42bfa462017-01-04 14:14:19 +0100810 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
811 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
812 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100813
814 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815}
816
817/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
818 * bytes on success, 0 if the frame can be ignored and -1 if an error
819 * occurred. */
820static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100824 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825
826 p = frame;
827 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828
829 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
831 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834
Christopher Faulet8ef75252017-02-20 22:56:03 +0100835 if (size < 7 /* TYPE + METADATA */) {
836 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
837 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838 }
839
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100840 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200842 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200844
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100845 /* Fragmentation is not supported for DISCONNECT frame */
846 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100847 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 return -1;
849 }
850
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 if (*p != 0 || *(p+1) != 0) {
853 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
854 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857
858 /* There are 2 mandatory items: "status-code" and "message" */
859
860 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 while (p < end) {
862 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200863 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
866 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 ret = spoe_decode_buffer(&p, end, &str, &sz);
868 if (ret == -1 || !sz) {
869 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
870 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871 }
872
873 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200874 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876
877 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
879 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
880 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
881 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
883 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200885 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
887 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
891
892 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200893 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 int type = *p++;
895
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
898 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
899 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 ret = spoe_decode_buffer(&p, end, &str, &sz);
902 if (ret == -1 || sz > 255) {
903 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
904 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
907 SPOE_APPCTX(appctx)->reason = str;
908 SPOE_APPCTX(appctx)->rlen = sz;
909#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910 }
911 else {
912 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 if (spoe_skip_data(&p, end) == -1) {
914 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
915 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
918 }
919
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921}
922
923
Christopher Fauleta1cda022016-12-21 08:58:06 +0100924/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 * success, 0 if the frame can be ignored and -1 if an error occurred. */
926static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100928 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100930 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100934 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100935
936 p = frame;
937 end = frame + size;
938 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939
940 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
942 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200945
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946 if (size < 7 /* TYPE + METADATA */) {
947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
948 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200949 }
950
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200953 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 /* Fragmentation is not supported for now */
957 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 return -1;
960 }
961
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200963 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100965 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200967 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100971
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100973 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200974 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100975 if ((*ctx)->stream_id == (unsigned int)stream_id &&
976 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100977 goto found;
978 }
979 }
980 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100981 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
982 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100984 goto found;
985 }
986 }
987
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100988 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
989 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
990 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
991
992 /* ABRT bit is set for an unfinished fragmented frame */
993 if (flags & SPOE_FRM_FL_ABRT) {
994 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100995 (*ctx)->state = SPOE_CTX_ST_ERROR;
996 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
997 /* Ignore the payload */
998 goto end;
999 }
1000 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1001 /* For now, we ignore the ack */
1002 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1003 return 0;
1004 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001005
Christopher Fauleta1cda022016-12-21 08:58:06 +01001006 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001007 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1008 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001009 " - stream-id=%u - frame-id=%u\n",
1010 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1011 __FUNCTION__, appctx,
1012 (unsigned int)stream_id, (unsigned int)frame_id);
1013
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001014 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001015 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1016 /* Report an error if we are waiting the ack for another frame,
1017 * but not if there is no longer frame waiting for a ack
1018 * (timeout)
1019 */
1020 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1021 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1022 return -1;
1023 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1024 SPOE_APPCTX(appctx)->cur_fpa = 0;
1025 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001026 return 0;
1027
1028 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1030 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001032 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001034
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001035 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001037 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1038 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001040
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 /* Transfer the buffer ownership to the SPOE context */
1042 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001043 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 (*ctx)->state = SPOE_CTX_ST_DONE;
1046
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001047 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 " - ACK frame received"
1050 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1053 (*ctx)->frame_id, flags);
1054 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001055}
1056
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057/* This function is used in cfgparse.c and declared in proto/checks.h. It
1058 * prepare the request to send to agents during a healthcheck. It returns 0 on
1059 * success and -1 if an error occurred. */
1060int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 struct appctx appctx;
1064 struct spoe_appctx spoe_appctx;
1065 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1066 size_t sz;
1067 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069 memset(&appctx, 0, sizeof(appctx));
1070 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072
1073 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001074 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1077 end = frame + MAX_FRAME_SIZE;
1078
1079 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1080 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 /* Add "healthcheck" K/V item */
1085 sz = SLEN(HEALTHCHECK_KEY);
1086 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1087 return -1;
1088 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 *len = frame - buf;
1091 sz = htonl(*len - 4);
1092 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097 return 0;
1098}
1099
1100/* This function is used in checks.c and declared in proto/checks.h. It decode
1101 * the response received from an agent during a healthcheck. It returns 0 on
1102 * success and -1 if an error occurred. */
1103int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 struct appctx appctx;
1107 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 memset(&appctx, 0, sizeof(appctx));
1110 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001113 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1116 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117 goto error;
1118 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001119 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1120 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121
1122 return 0;
1123
1124 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1126 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1127 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return -1;
1129}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001130
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001132 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133 * success. */
1134static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136{
1137 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138 int ret;
1139 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1142 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 netint = htonl(framesz);
1144 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001145 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001147 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001148 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 }
1151 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return -1; /* error */
1153 }
1154 return framesz;
1155}
1156
1157/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1158 * when the frame can be ignored, 1 to retry later and the frame length on
1159 * success. */
1160static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162{
1163 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 int ret;
1165 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166
Willy Tarreau06d80a92017-10-19 14:32:15 +02001167 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 if (ret > 0) {
1169 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001170 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 return -1;
1173 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 }
1176 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001179 }
1180 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001181 return -1; /* error */
1182 }
1183 return framesz;
1184}
1185
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001186/********************************************************************
1187 * Functions that manage the SPOE applet
1188 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001189static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001190spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001191{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001192 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001193 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001194 appctx_wakeup(appctx);
1195 return 1;
1196}
1197
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198/* Callback function that catches applet timeouts. If a timeout occurred, we set
1199 * <appctx->st1> flag and the SPOE applet is woken up. */
1200static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001201spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001203 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204
1205 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1206 if (tick_is_expired(task->expire, now_ms)) {
1207 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211 return task;
1212}
1213
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214/* Callback function that releases a SPOE applet. This happens when the
1215 * connection with the agent is closed. */
1216static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219 struct stream_interface *si = appctx->owner;
1220 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1221 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001222 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223
1224 if (spoe_appctx == NULL)
1225 return;
1226
1227 appctx->ctx.spoe.ptr = NULL;
1228 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229
1230 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1231 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1232 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233
Christopher Faulet8ef75252017-02-20 22:56:03 +01001234 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001235 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001236 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001238 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001241 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242
Christopher Faulet8ef75252017-02-20 22:56:03 +01001243 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001244 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001245 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1246 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001247 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001248 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001249
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001250 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1252 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001253
1254 si_shutw(si);
1255 si_shutr(si);
1256 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001257 }
1258
Christopher Faulet8ef75252017-02-20 22:56:03 +01001259 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001260 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001261
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001262 /* Report an error to all streams in the appctx waiting queue */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001263 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001264 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001265 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001266 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001267 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001268 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001269 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001270 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001271 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001272 }
1273
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001274 /* If the applet was processing a fragmented frame, report an error to
1275 * the corresponding stream. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001276 if (spoe_appctx->frag_ctx.ctx) {
1277 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001278 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001279 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001280 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001281 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1282 }
1283
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001284 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1285 /* If there are still some running applets, remove reference on
1286 * the current one from streams in the async waiting queue. In
1287 * async mode, the ACK may be received from another appctx.
1288 */
Christopher Fauletcf181c72020-11-10 18:45:34 +01001289 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1290 if (ctx->spoe_appctx == spoe_appctx)
1291 ctx->spoe_appctx = NULL;
1292 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001293 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001294 }
Christopher Faulet4d350af2021-08-02 17:53:56 +02001295 else {
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001296 /* It is the last running applet and the sending and async
1297 * waiting queues are not empty. So try to start a new applet if
1298 * HAproxy is not stopping. On success, we remove reference on
1299 * the current appctx from streams in the async waiting queue.
1300 * In async mode, the ACK may be received from another appctx.
Christopher Faulet4d350af2021-08-02 17:53:56 +02001301 */
1302 if (!stopping &&
1303 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001304 spoe_create_appctx(agent->spoe_conf)) {
1305 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1306 if (ctx->spoe_appctx == spoe_appctx)
1307 ctx->spoe_appctx = NULL;
1308 }
Christopher Faulet4d350af2021-08-02 17:53:56 +02001309 goto end;
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001310 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311
Christopher Fauletee9b4a62022-08-25 18:50:18 +02001312 /* Otherwise, report an error to all streams in the sending and
1313 * async waiting queues.
1314 */
Christopher Faulet4d350af2021-08-02 17:53:56 +02001315 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1316 LIST_DELETE(&ctx->list);
1317 LIST_INIT(&ctx->list);
1318 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
1319 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1320 ctx->spoe_appctx = NULL;
1321 ctx->state = SPOE_CTX_ST_ERROR;
1322 ctx->status_code = (spoe_appctx->status_code + 0x100);
1323 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1324 }
1325 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1326 LIST_DELETE(&ctx->list);
1327 LIST_INIT(&ctx->list);
1328 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
1329 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1330 ctx->spoe_appctx = NULL;
1331 ctx->state = SPOE_CTX_ST_ERROR;
1332 ctx->status_code = (spoe_appctx->status_code + 0x100);
1333 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1334 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001336
1337 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001338 /* Release allocated memory */
1339 spoe_release_buffer(&spoe_appctx->buffer,
1340 &spoe_appctx->buffer_wait);
1341 pool_free(pool_head_spoe_appctx, spoe_appctx);
1342
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001343 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001344 agent->rt[tid].frame_size = agent->max_frame_size;
1345 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1346 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001347}
1348
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001349static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001350spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001351{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001353 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001357 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1358 /* closed */
1359 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1360 goto exit;
1361 }
1362
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001363 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001364 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001365 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1367 goto stop;
1368 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001369
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001371 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1372 " - Connection timed out\n",
1373 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1374 __FUNCTION__, appctx);
1375 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001376 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001377 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378
Christopher Faulet42bfa462017-01-04 14:14:19 +01001379 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001380 SPOE_APPCTX(appctx)->task->expire =
1381 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001382
Christopher Faulet8ef75252017-02-20 22:56:03 +01001383 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1384 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001385 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 ret = spoe_prepare_hahello_frame(appctx, frame,
1387 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001390
1391 switch (ret) {
1392 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001393 case 0: /* ignore => an error, cannot be ignored */
1394 goto exit;
1395
1396 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001397 goto stop;
1398
Christopher Faulet8ef75252017-02-20 22:56:03 +01001399 default:
1400 /* HELLO frame successfully sent, now wait for the
1401 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1403 goto next;
1404 }
1405
1406 next:
1407 return 0;
1408 stop:
1409 return 1;
1410 exit:
1411 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1412 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413}
1414
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001416spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001417{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001419 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 char *frame;
1421 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001422
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001423
Christopher Fauletb067b062017-01-04 16:39:11 +01001424 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001426 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001427 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001428
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001430 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1431 " - Connection timed out\n",
1432 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1433 __FUNCTION__, appctx);
1434 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001435 goto exit;
1436 }
1437
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001438 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 ret = spoe_recv_frame(appctx, frame,
1440 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001441 if (ret > 1) {
1442 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1443 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1444 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001445 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001446 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001447 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001448 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449
Christopher Fauleta1cda022016-12-21 08:58:06 +01001450 switch (ret) {
1451 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1454 goto next;
1455
1456 case 1: /* retry later */
1457 goto stop;
1458
1459 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001460 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001461 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001462 SPOE_APPCTX(appctx)->node.key = 0;
1463 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001464
1465 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001466 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001467 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001468 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001469
Christopher Fauleta1cda022016-12-21 08:58:06 +01001470 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001471 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001472 if (trash.data)
1473 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474
1475 SPOE_APPCTX(appctx)->task->expire =
1476 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001477 return 0;
1478 stop:
1479 return 1;
1480 exit:
1481 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1482 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001483}
1484
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485
Christopher Fauleta1cda022016-12-21 08:58:06 +01001486static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001488{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001489 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1490 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 char *frame, *buf;
1492 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001493
Christopher Faulet8ef75252017-02-20 22:56:03 +01001494 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1495 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001496 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001497
1498 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1499 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1500 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1501 SPOE_APPCTX(appctx)->max_frame_size);
1502 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001503 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001504 *skip = 1;
1505 ret = 1;
1506 goto end;
1507 }
1508 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001509 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001510 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1511 SPOE_APPCTX(appctx)->max_frame_size);
1512
1513 }
1514
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 if (ret > 1)
1516 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001517
Christopher Faulet8ef75252017-02-20 22:56:03 +01001518 switch (ret) {
1519 case -1: /* error */
1520 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1521 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001522
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 case 0: /* ignore */
1524 if (ctx == NULL)
1525 goto abort_frag_frame;
1526
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001527 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001528 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001529 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001530 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001531 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001532 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 ctx->state = SPOE_CTX_ST_ERROR;
1534 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1535 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001536 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001537 break;
1538
1539 case 1: /* retry */
1540 *skip = 1;
1541 break;
1542
1543 default:
1544 if (ctx == NULL)
1545 goto abort_frag_frame;
1546
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001547 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001548 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001550 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001551 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001552 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001553 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1554 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1555 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001556 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001557 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558 }
1559 goto end;
1560
1561 frag_frame_sent:
1562 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001563 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1565 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1566 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1568 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1569 goto end;
1570
1571 no_frag_frame_sent:
1572 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1573 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001574 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 }
1576 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1577 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001578 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579 }
1580 else {
1581 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001582 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001583 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001584 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001585 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001586 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1588 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1589 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001590 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591
Christopher Faulet8ef75252017-02-20 22:56:03 +01001592 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1593 goto end;
1594
1595 abort_frag_frame:
1596 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1597 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1598 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1599 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1600 goto end;
1601
1602 end:
1603 return ret;
1604}
1605
1606static int
1607spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1608{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001609 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 struct spoe_context *ctx = NULL;
1611 char *frame;
1612 int ret;
1613
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001614 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001615 ret = spoe_recv_frame(appctx, frame,
1616 SPOE_APPCTX(appctx)->max_frame_size);
1617 if (ret > 1) {
1618 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1619 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001620 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001621 goto end;
1622 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001623 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1625 }
1626 switch (ret) {
1627 case -1: /* error */
1628 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1629 break;
1630
1631 case 0: /* ignore */
1632 break;
1633
1634 case 1: /* retry */
1635 *skip = 1;
1636 break;
1637
1638 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001639 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001640 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001641 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001642 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1643 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001644 if (ctx->spoe_appctx) {
1645 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001646 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001647 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001648 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1649 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1650 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1651 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1652 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1653 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1654 }
1655 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1656 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001657 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1658 break;
1659 }
1660
1661 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001662 if (trash.data)
1663 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001664 end:
1665 return ret;
1666}
1667
1668static int
1669spoe_handle_processing_appctx(struct appctx *appctx)
1670{
1671 struct stream_interface *si = appctx->owner;
Christopher Fauletf028bb62021-08-02 18:13:27 +02001672 struct server *srv = objt_server(si_strm(si)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001673 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletf028bb62021-08-02 18:13:27 +02001674 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001675
1676 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1677 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1678 goto exit;
1679 }
1680
1681 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1682 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1683 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1684 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1685 goto next;
1686 }
1687
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001688 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001689 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001690 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1692 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001693 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001694
Christopher Fauletf028bb62021-08-02 18:13:27 +02001695
1696 /* Close the applet ASAP because some sessions are waiting for a free
1697 * connection slot. It is only an issue in multithreaded mode.
1698 */
1699 close_asap = (global.nbthread > 1 &&
1700 (agent->b.be->nbpend ||
1701 (srv && (srv->nbpend || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1702
1703 /* Don"t try to send new frame we are waiting for at lease a ack, in
1704 * sync mode or if applet must be closed ASAP
1705 */
1706 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 /* receiving_frame loop */
1710 while (!skip_receiving) {
1711 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1712 switch (ret) {
1713 case -1: /* error */
1714 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 case 0: /* ignore */
1717 active_r = 1;
1718 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001719
Christopher Faulet8f82b202018-01-24 16:23:03 +01001720 case 1: /* retry */
1721 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001722
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 default:
1724 active_r = 1;
1725 break;
1726 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001727 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 /* send_frame loop */
1730 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1731 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1732 switch (ret) {
1733 case -1: /* error */
1734 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001735
Christopher Faulet8f82b202018-01-24 16:23:03 +01001736 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001737 if (SPOE_APPCTX(appctx)->node.key)
1738 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001739 active_s++;
1740 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001741
Christopher Faulet8f82b202018-01-24 16:23:03 +01001742 case 1: /* retry */
1743 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744
Christopher Faulet8f82b202018-01-24 16:23:03 +01001745 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001746 if (SPOE_APPCTX(appctx)->node.key)
1747 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001748 active_s++;
1749 break;
1750 }
Christopher Fauletf028bb62021-08-02 18:13:27 +02001751
1752 /* if applet must be close ASAP, don't send more than a frame */
1753 if (close_asap)
1754 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001755 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756
Christopher Faulet8f82b202018-01-24 16:23:03 +01001757 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001758 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1759 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1760 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001761
Christopher Faulet8f82b202018-01-24 16:23:03 +01001762 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletf028bb62021-08-02 18:13:27 +02001763 /* If applet must be closed, don't switch it in IDLE state and
1764 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001765 */
Christopher Fauletf028bb62021-08-02 18:13:27 +02001766 if (close_asap) {
1767 if (SPOE_APPCTX(appctx)->cur_fpa)
1768 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001769 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1770 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1771 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1772 goto next;
1773 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001774 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001775 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001776 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001777 }
Christopher Fauletf028bb62021-08-02 18:13:27 +02001778
1779 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780 return 1;
1781
Christopher Faulet8f82b202018-01-24 16:23:03 +01001782 next:
1783 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1784 return 0;
1785
Christopher Fauleta1cda022016-12-21 08:58:06 +01001786 exit:
1787 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1788 return 0;
1789}
1790
1791static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001792spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793{
1794 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001795 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796 char *frame, *buf;
1797 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001798
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1800 goto exit;
1801
1802 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1803 goto exit;
1804
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1806 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001807 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1809 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812
1813 switch (ret) {
1814 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 goto exit;
1817
1818 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 goto stop;
1820
1821 default:
1822 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1823 " - disconnected by HAProxy (%d): %s\n",
1824 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 __FUNCTION__, appctx,
1826 SPOE_APPCTX(appctx)->status_code,
1827 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828
1829 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1830 goto next;
1831 }
1832
1833 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834 SPOE_APPCTX(appctx)->task->expire =
1835 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836 return 0;
1837 stop:
1838 return 1;
1839 exit:
1840 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1841 return 0;
1842}
1843
1844static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846{
1847 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 char *frame;
1849 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001850
Christopher Fauletb067b062017-01-04 16:39:11 +01001851 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001854 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855
Christopher Fauletb067b062017-01-04 16:39:11 +01001856 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001859 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001861 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862 ret = spoe_recv_frame(appctx, frame,
1863 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001865 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 }
1868
1869 switch (ret) {
1870 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1872 " - error on frame (%s)\n",
1873 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001874 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001875 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001876 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001877 goto exit;
1878
1879 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880 goto next;
1881
1882 case 1: /* retry */
1883 goto stop;
1884
1885 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001886 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001887 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001889 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1891 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 goto exit;
1893 }
1894
1895 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001896 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001897 if (trash.data)
1898 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 return 0;
1901 stop:
1902 return 1;
1903 exit:
1904 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1905 return 0;
1906}
1907
1908/* I/O Handler processing messages exchanged with the agent */
1909static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001910spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001911{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001912 struct stream_interface *si = appctx->owner;
1913 struct spoe_agent *agent;
1914
1915 if (SPOE_APPCTX(appctx) == NULL)
1916 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917
Christopher Faulet8ef75252017-02-20 22:56:03 +01001918 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1919 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001920
Christopher Fauleta1cda022016-12-21 08:58:06 +01001921 switchstate:
1922 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1923 " - appctx-state=%s\n",
1924 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1925 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1926
1927 switch (appctx->st0) {
1928 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001929 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001930 goto out;
1931 goto switchstate;
1932
1933 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001934 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001935 goto out;
1936 goto switchstate;
1937
1938 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001939 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001940 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001941 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001942 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001943 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001944 SPOE_APPCTX(appctx)->task->expire =
1945 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001946 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001947 goto switchstate;
1948 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001949 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1950 /* fall through */
1951
1952 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001953 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1954 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001955 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001956 goto out;
1957 goto switchstate;
1958
1959 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001960 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001961 goto out;
1962 goto switchstate;
1963
1964 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001965 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001966 goto out;
1967 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001968
1969 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001970 appctx->st0 = SPOE_APPCTX_ST_END;
1971 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1972
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973 si_shutw(si);
1974 si_shutr(si);
1975 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001976 /* fall through */
1977
1978 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001979 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001980 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001981 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001982 if (stopping)
1983 spoe_wakeup_appctx(appctx);
1984
Christopher Faulet42bfa462017-01-04 14:14:19 +01001985 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1986 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987}
1988
1989struct applet spoe_applet = {
1990 .obj_type = OBJ_TYPE_APPLET,
1991 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001992 .fct = spoe_handle_appctx,
1993 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994};
1995
1996/* Create a SPOE applet. On success, the created applet is returned, else
1997 * NULL. */
1998static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001999spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000{
2001 struct appctx *appctx;
2002 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004
Emeric Brun1138fd02017-06-19 12:38:55 +02002005 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006 goto out_error;
2007
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002008 appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002009 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002010 goto out_free_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002011
Christopher Faulet42bfa462017-01-04 14:14:19 +01002012 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002013 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002014 goto out_free_spoe_appctx;
2015
2016 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002017 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002018 SPOE_APPCTX(appctx)->task->context = appctx;
2019 SPOE_APPCTX(appctx)->agent = conf->agent;
2020 SPOE_APPCTX(appctx)->version = 0;
2021 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
2022 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01002023 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002024 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01002025 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01002026
Willy Tarreau90f366b2021-02-20 11:49:49 +01002027 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002028 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002029 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002030
2031 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2032 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033
Willy Tarreau5820a362016-12-22 15:59:02 +01002034 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035 if (!sess)
2036 goto out_free_spoe;
2037
Christopher Faulet26256f82020-09-14 11:40:13 +02002038 if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002039 goto out_free_sess;
2040
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041 stream_set_backend(strm, conf->agent->b.be);
2042
2043 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002044 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045 appctx_wakeup(appctx);
2046
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002047 strm->do_log = NULL;
2048 strm->res.flags |= CF_READ_DONTWAIT;
2049
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002050 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02002051 LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002052 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau4781b152021-04-06 13:53:36 +02002053 _HA_ATOMIC_INC(&conf->agent->counters.applets);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002054
Emeric Brunc60def82017-09-27 14:59:38 +02002055 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002056 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002057 return appctx;
2058
2059 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060 out_free_sess:
2061 session_free(sess);
2062 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002063 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002064 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002065 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066 out_free_appctx:
2067 appctx_free(appctx);
2068 out_error:
2069 return NULL;
2070}
2071
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002073spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074{
2075 struct spoe_config *conf = FLT_CONF(ctx->filter);
2076 struct spoe_agent *agent = conf->agent;
2077 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002078 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002081 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002082 (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 +01002083 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084
2085 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002086 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2088 ctx->strm);
2089
Christopher Fauleta1cda022016-12-21 08:58:06 +01002090 /* Do not try to create a new applet if there is no server up for the
2091 * agent's backend. */
2092 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2093 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2094 " - cannot create SPOE appctx: no server up\n",
2095 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2096 __FUNCTION__, ctx->strm);
2097 goto end;
2098 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099
Christopher Fauleta1cda022016-12-21 08:58:06 +01002100 /* Do not try to create a new applet if we have reached the maximum of
2101 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002102 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002103 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002104 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2105 " - cannot create SPOE appctx: max CPS reached\n",
2106 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2107 __FUNCTION__, ctx->strm);
2108 goto end;
2109 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002110 }
2111
Christopher Faulet8ef75252017-02-20 22:56:03 +01002112 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002113 if (appctx == NULL) {
2114 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2115 " - failed to create SPOE appctx\n",
2116 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2117 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002118 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002119 "SPOE: [%s] failed to create SPOE applet\n",
2120 agent->id);
2121
Christopher Fauleta1cda022016-12-21 08:58:06 +01002122 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002123 }
2124
Christopher Fauleta1cda022016-12-21 08:58:06 +01002125 /* Increase the per-process number of cumulated connections */
2126 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002127 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002128
Christopher Fauleta1cda022016-12-21 08:58:06 +01002129 end:
2130 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002131 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002132 ctx->status_code = SPOE_CTX_ERR_RES;
2133 return -1;
2134 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002135
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002136 /* Add the SPOE context in the sending queue if the stream has no applet
2137 * already assigned and wakeup all idle applets. Otherwise, don't queue
2138 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002139 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002140 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2141 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002142 if (ctx->spoe_appctx)
2143 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002144 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002145
2146 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002147 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002148 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002149 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002150 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002151 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002152
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002153 /* Finally try to wakeup an IDLE applet. */
2154 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2155 struct eb32_node *node;
2156
2157 node = eb32_first(&agent->rt[tid].idle_applets);
2158 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2159 if (node && spoe_appctx) {
2160 eb32_delete(&spoe_appctx->node);
2161 spoe_appctx->node.key++;
2162 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2163 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002164 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002165 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002166 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002167}
2168
2169/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002170 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002171 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002172/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2173 * fragmented_content. If the next message can be processed, it returns 0. If
2174 * the message is too big, it returns -1.*/
2175static int
2176spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2177 struct spoe_message *msg, int dir,
2178 char **buf, char *end)
2179{
2180 struct sample *smp;
2181 struct spoe_arg *arg;
2182 int ret;
2183
2184 if (msg->cond) {
2185 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2186 ret = acl_pass(ret);
2187 if (msg->cond->pol == ACL_COND_UNLESS)
2188 ret = !ret;
2189
2190 /* the rule does not match */
2191 if (!ret)
2192 goto next;
2193 }
2194
2195 /* Resume encoding of a SPOE argument */
2196 if (ctx->frag_ctx.curarg != NULL) {
2197 arg = ctx->frag_ctx.curarg;
2198 goto encode_argument;
2199 }
2200
2201 if (ctx->frag_ctx.curoff != UINT_MAX)
2202 goto encode_msg_payload;
2203
2204 /* Check if there is enough space for the message name and the
2205 * number of arguments. It implies <msg->id_len> is encoded on 2
2206 * bytes, at most (< 2288). */
2207 if (*buf + 2 + msg->id_len + 1 > end)
2208 goto too_big;
2209
2210 /* Encode the message name */
2211 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2212 goto too_big;
2213
2214 /* Set the number of arguments for this message */
2215 **buf = msg->nargs;
2216 (*buf)++;
2217
2218 ctx->frag_ctx.curoff = 0;
2219 encode_msg_payload:
2220
2221 /* Loop on arguments */
2222 list_for_each_entry(arg, &msg->args, list) {
2223 ctx->frag_ctx.curarg = arg;
2224 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002225 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002226
2227 encode_argument:
2228 if (ctx->frag_ctx.curoff != UINT_MAX)
2229 goto encode_arg_value;
2230
Joseph Herlantf7f60312018-11-15 13:46:49 -08002231 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002232 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2233 goto too_big;
2234
2235 ctx->frag_ctx.curoff = 0;
2236 encode_arg_value:
2237
Joseph Herlantf7f60312018-11-15 13:46:49 -08002238 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002239 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002240 if (smp) {
2241 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2242 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2243 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002244 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002245 if (ret == -1 || ctx->frag_ctx.curoff)
2246 goto too_big;
2247 }
2248
2249 next:
2250 return 0;
2251
2252 too_big:
2253 return -1;
2254}
2255
Christopher Fauletc718b822017-09-21 16:50:56 +02002256/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2257 * handle fragmented content. On success it returns 1. If an error occurred, -1
2258 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2259 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002260static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002261spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002262 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002263{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002264 struct spoe_config *conf = FLT_CONF(ctx->filter);
2265 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002266 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002267 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002268
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002269 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002270 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002271
Christopher Fauletc718b822017-09-21 16:50:56 +02002272 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2273 /* Resume encoding of a SPOE message */
2274 if (ctx->frag_ctx.curmsg != NULL) {
2275 msg = ctx->frag_ctx.curmsg;
2276 goto encode_evt_message;
2277 }
2278
2279 list_for_each_entry(msg, messages, by_evt) {
2280 ctx->frag_ctx.curmsg = msg;
2281 ctx->frag_ctx.curarg = NULL;
2282 ctx->frag_ctx.curoff = UINT_MAX;
2283
2284 encode_evt_message:
2285 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2286 goto too_big;
2287 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002289 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2290 /* Resume encoding of a SPOE message */
2291 if (ctx->frag_ctx.curmsg != NULL) {
2292 msg = ctx->frag_ctx.curmsg;
2293 goto encode_grp_message;
2294 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002295
Christopher Fauletc718b822017-09-21 16:50:56 +02002296 list_for_each_entry(msg, messages, by_grp) {
2297 ctx->frag_ctx.curmsg = msg;
2298 ctx->frag_ctx.curarg = NULL;
2299 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002300
Christopher Fauletc718b822017-09-21 16:50:56 +02002301 encode_grp_message:
2302 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2303 goto too_big;
2304 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002305 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002306 else
2307 goto skip;
2308
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002309
Christopher Faulet57583e42017-09-04 15:41:09 +02002310 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002311 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002312 goto skip;
2313
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002314 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315 " - encode %s messages - spoe_appctx=%p"
2316 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002317 (int)now.tv_sec, (int)now.tv_usec,
2318 agent->id, __FUNCTION__, s,
2319 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002320 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002321 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002322
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002323 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002324 ctx->frag_ctx.curmsg = NULL;
2325 ctx->frag_ctx.curarg = NULL;
2326 ctx->frag_ctx.curoff = 0;
2327 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002328
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329 return 1;
2330
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002331 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002332 /* Return an error if fragmentation is unsupported or if nothing has
2333 * been encoded because its too big and not splittable. */
2334 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002335 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2336 return -1;
2337 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002338
2339 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340 " - encode fragmented messages - spoe_appctx=%p"
2341 " - curmsg=%p - curarg=%p - curoff=%u"
2342 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002343 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002344 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002345 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002346 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002347
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002348 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002349 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2350 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2351 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002352
2353 skip:
2354 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2355 " - skip the frame because nothing has been encoded\n",
2356 (int)now.tv_sec, (int)now.tv_usec,
2357 agent->id, __FUNCTION__, s);
2358 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359}
2360
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002361
2362/***************************************************************************
2363 * Functions that handle SPOE actions
2364 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002365/* Helper function to set a variable */
2366static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368 struct sample *smp)
2369{
2370 struct spoe_config *conf = FLT_CONF(ctx->filter);
2371 struct spoe_agent *agent = conf->agent;
2372 char varname[64];
2373
2374 memset(varname, 0, sizeof(varname));
2375 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2376 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002377 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2378 vars_set_by_name(varname, len, smp);
2379 else
2380 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002381}
2382
2383/* Helper function to unset a variable */
2384static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002386 struct sample *smp)
2387{
2388 struct spoe_config *conf = FLT_CONF(ctx->filter);
2389 struct spoe_agent *agent = conf->agent;
2390 char varname[64];
2391
2392 memset(varname, 0, sizeof(varname));
2393 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2394 scope, agent->var_pfx, len, name);
2395 vars_unset_by_name_ifexist(varname, len, smp);
2396}
2397
2398
Christopher Faulet8ef75252017-02-20 22:56:03 +01002399static inline int
2400spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2401 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 char *str, *scope, *p = *buf;
2404 struct sample smp;
2405 uint64_t sz;
2406 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002407
Christopher Faulet8ef75252017-02-20 22:56:03 +01002408 if (p + 2 >= end)
2409 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002410
Christopher Faulet8ef75252017-02-20 22:56:03 +01002411 /* SET-VAR requires 3 arguments */
2412 if (*p++ != 3)
2413 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
Christopher Faulet8ef75252017-02-20 22:56:03 +01002415 switch (*p++) {
2416 case SPOE_SCOPE_PROC: scope = "proc"; break;
2417 case SPOE_SCOPE_SESS: scope = "sess"; break;
2418 case SPOE_SCOPE_TXN : scope = "txn"; break;
2419 case SPOE_SCOPE_REQ : scope = "req"; break;
2420 case SPOE_SCOPE_RES : scope = "res"; break;
2421 default: goto skip;
2422 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002423
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2425 goto skip;
2426 memset(&smp, 0, sizeof(smp));
2427 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet8ef75252017-02-20 22:56:03 +01002429 if (spoe_decode_data(&p, end, &smp) == -1)
2430 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002431
Christopher Faulet8ef75252017-02-20 22:56:03 +01002432 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2433 " - set-var '%s.%s.%.*s'\n",
2434 (int)now.tv_sec, (int)now.tv_usec,
2435 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2436 __FUNCTION__, s, scope,
2437 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2438 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002439
Christopher Faulet2469eba2020-10-15 16:08:30 +02002440 if (smp.data.type == SMP_T_ANY)
2441 spoe_unset_var(ctx, scope, str, sz, &smp);
2442 else
2443 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002444
Christopher Faulet8ef75252017-02-20 22:56:03 +01002445 ret = (p - *buf);
2446 *buf = p;
2447 return ret;
2448 skip:
2449 return 0;
2450}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002451
Christopher Faulet8ef75252017-02-20 22:56:03 +01002452static inline int
2453spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2454 char **buf, char *end, int dir)
2455{
2456 char *str, *scope, *p = *buf;
2457 struct sample smp;
2458 uint64_t sz;
2459 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460
Christopher Faulet8ef75252017-02-20 22:56:03 +01002461 if (p + 2 >= end)
2462 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002463
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464 /* UNSET-VAR requires 2 arguments */
2465 if (*p++ != 2)
2466 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002467
Christopher Faulet8ef75252017-02-20 22:56:03 +01002468 switch (*p++) {
2469 case SPOE_SCOPE_PROC: scope = "proc"; break;
2470 case SPOE_SCOPE_SESS: scope = "sess"; break;
2471 case SPOE_SCOPE_TXN : scope = "txn"; break;
2472 case SPOE_SCOPE_REQ : scope = "req"; break;
2473 case SPOE_SCOPE_RES : scope = "res"; break;
2474 default: goto skip;
2475 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002476
Christopher Faulet8ef75252017-02-20 22:56:03 +01002477 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2478 goto skip;
2479 memset(&smp, 0, sizeof(smp));
2480 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Faulet8ef75252017-02-20 22:56:03 +01002482 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2483 " - unset-var '%s.%s.%.*s'\n",
2484 (int)now.tv_sec, (int)now.tv_usec,
2485 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2486 __FUNCTION__, s, scope,
2487 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2488 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002489
Christopher Faulet8ef75252017-02-20 22:56:03 +01002490 spoe_unset_var(ctx, scope, str, sz, &smp);
2491
2492 ret = (p - *buf);
2493 *buf = p;
2494 return ret;
2495 skip:
2496 return 0;
2497}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002498
Christopher Faulet8ef75252017-02-20 22:56:03 +01002499/* Process SPOE actions for a specific event. It returns 1 on success. If an
2500 * error occurred, 0 is returned. */
2501static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002502spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002503{
2504 char *p, *end;
2505 int ret;
2506
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002507 p = b_head(&ctx->buffer);
2508 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002509
2510 while (p < end) {
2511 enum spoe_action_type type;
2512
2513 type = *p++;
2514 switch (type) {
2515 case SPOE_ACT_T_SET_VAR:
2516 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2517 if (!ret)
2518 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002519 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002520
Christopher Faulet8ef75252017-02-20 22:56:03 +01002521 case SPOE_ACT_T_UNSET_VAR:
2522 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2523 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002524 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002525 break;
2526
2527 default:
2528 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002529 }
2530 }
2531
2532 return 1;
2533 skip:
2534 return 0;
2535}
2536
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002537/***************************************************************************
2538 * Functions that process SPOE events
2539 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002540static void
2541spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2542 struct spoe_context *ctx, int dir)
2543{
2544 if (!tv_iszero(&ctx->stats.tv_start)) {
2545 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2546 ctx->stats.t_total += ctx->stats.t_process;
2547 tv_zero(&ctx->stats.tv_request);
2548 tv_zero(&ctx->stats.tv_queue);
2549 tv_zero(&ctx->stats.tv_wait);
2550 tv_zero(&ctx->stats.tv_response);
2551 }
2552
2553 if (agent->var_t_process) {
2554 struct sample smp;
2555
2556 memset(&smp, 0, sizeof(smp));
2557 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2558 smp.data.u.sint = ctx->stats.t_process;
2559 smp.data.type = SMP_T_SINT;
2560
2561 spoe_set_var(ctx, "txn", agent->var_t_process,
2562 strlen(agent->var_t_process), &smp);
2563 }
2564
2565 if (agent->var_t_total) {
2566 struct sample smp;
2567
2568 memset(&smp, 0, sizeof(smp));
2569 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2570 smp.data.u.sint = ctx->stats.t_total;
2571 smp.data.type = SMP_T_SINT;
2572
2573 spoe_set_var(ctx, "txn", agent->var_t_total,
2574 strlen(agent->var_t_total), &smp);
2575 }
2576}
2577
2578static void
2579spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2580 struct spoe_context *ctx, int dir)
2581{
2582 if (agent->eps_max > 0)
2583 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2584
2585 if (agent->var_on_error) {
2586 struct sample smp;
2587
2588 memset(&smp, 0, sizeof(smp));
2589 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2590 smp.data.u.sint = ctx->status_code;
2591 smp.data.type = SMP_T_BOOL;
2592
2593 spoe_set_var(ctx, "txn", agent->var_on_error,
2594 strlen(agent->var_on_error), &smp);
2595 }
2596
2597 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2598 ? SPOE_CTX_ST_READY
2599 : SPOE_CTX_ST_NONE);
2600}
2601
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002602static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002603spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002604{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 /* If a process is already started for this SPOE context, retry
2606 * later. */
2607 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002608 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002609
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002610 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002611 ctx->stats.tv_start = now;
2612 ctx->stats.tv_request = now;
2613 ctx->stats.t_request = -1;
2614 ctx->stats.t_queue = -1;
2615 ctx->stats.t_waiting = -1;
2616 ctx->stats.t_response = -1;
2617 ctx->stats.t_process = -1;
2618
2619 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002620
Christopher Fauleta1cda022016-12-21 08:58:06 +01002621 /* Set the right flag to prevent request and response processing
2622 * in same time. */
2623 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2624 ? SPOE_CTX_FL_REQ_PROCESS
2625 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002626 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002627}
2628
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002629static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002630spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002631{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002632 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002633
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002634 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2635 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002636 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002637 if (sa) {
2638 if (sa->frag_ctx.ctx == ctx) {
2639 sa->frag_ctx.ctx = NULL;
2640 spoe_wakeup_appctx(sa->owner);
2641 }
2642 else
2643 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002644 }
2645
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002647 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002648 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002649
2650 /* Reset processing timer */
2651 ctx->process_exp = TICK_ETERNITY;
2652
Christopher Faulet8ef75252017-02-20 22:56:03 +01002653 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002654
Christopher Fauletfce747b2018-01-24 15:59:32 +01002655 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002656 ctx->frag_ctx.curmsg = NULL;
2657 ctx->frag_ctx.curarg = NULL;
2658 ctx->frag_ctx.curoff = 0;
2659 ctx->frag_ctx.flags = 0;
2660
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002662 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002663 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002664 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002665 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002666
Willy Tarreau2b718102021-04-21 07:32:39 +02002667 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002668 LIST_INIT(&ctx->list);
2669 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002670}
2671
Christopher Faulet58d03682017-09-21 16:57:24 +02002672/* Process a list of SPOE messages. First, this functions will process messages
2673 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2674 * to process corresponding actions. During all the processing, it returns 0
2675 * and it returns 1 when the processing is finished. If an error occurred, -1
2676 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002678spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2679 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002680{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002681 struct spoe_config *conf = FLT_CONF(ctx->filter);
2682 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002683 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684
2685 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002686 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002687
2688 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2689 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002690 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002691 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002692 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002693 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002694 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002695 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696
2697 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002699 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002700 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002701 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002703 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002704 goto skip;
2705 }
2706 }
2707
Christopher Fauletf7a30922016-11-10 15:04:51 +01002708 if (!tick_isset(ctx->process_exp)) {
2709 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2710 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2711 ctx->process_exp);
2712 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002713 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002714 if (!ret)
2715 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002716
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002717 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002718 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002719 }
2720
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002721 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002722 if (tv_iszero(&ctx->stats.tv_request))
2723 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002724 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002725 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002726 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002727 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002728 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002729 if (!ret)
2730 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002731 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002732 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002733 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2734 }
2735
2736 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002737 if (ctx->spoe_appctx)
2738 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002739 ret = 0;
2740 goto out;
2741 }
2742
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002743 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2744 ret = 0;
2745 goto out;
2746 }
2747
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002748 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002749 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002750 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002751 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002752 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002753 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002754 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002755 }
2756
2757 out:
2758 return ret;
2759
Christopher Fauleta1cda022016-12-21 08:58:06 +01002760 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002761 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002762 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002763 spoe_stop_processing(agent, ctx);
2764 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002765
Christopher Fauleta1cda022016-12-21 08:58:06 +01002766 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002767 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002768 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002769 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002770 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002771 spoe_handle_processing_error(s, agent, ctx, dir);
2772 ret = 1;
2773 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002774 return ret;
2775}
2776
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002777/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2778 * See spoe_process_message for details. */
2779static int
2780spoe_process_group(struct stream *s, struct spoe_context *ctx,
2781 struct spoe_group *group, int dir)
2782{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002783 struct spoe_config *conf = FLT_CONF(ctx->filter);
2784 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002785 int ret;
2786
2787 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2788 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002789 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002790 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2791 group->id);
2792
2793 if (LIST_ISEMPTY(&group->messages))
2794 return 1;
2795
2796 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002797 if (ret && ctx->stats.t_process != -1) {
2798 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002799 " - <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 +01002800 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002801 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002802 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002803 ctx->stats.t_response, ctx->stats.t_process,
2804 agent->counters.idles, agent->counters.applets,
2805 agent->counters.nb_sending, agent->counters.nb_waiting,
2806 agent->counters.nb_errors, agent->counters.nb_processed,
2807 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2808 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2809 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2810 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2811 agent->id, group->id, s->uniq_id, ctx->status_code,
2812 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2813 ctx->stats.t_response, ctx->stats.t_process,
2814 agent->counters.idles, agent->counters.applets,
2815 agent->counters.nb_sending, agent->counters.nb_waiting,
2816 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002817 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002818 return ret;
2819}
2820
Christopher Faulet58d03682017-09-21 16:57:24 +02002821/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2822 * See spoe_process_message for details. */
2823static int
2824spoe_process_event(struct stream *s, struct spoe_context *ctx,
2825 enum spoe_event ev)
2826{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002827 struct spoe_config *conf = FLT_CONF(ctx->filter);
2828 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002829 int dir, ret;
2830
2831 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002832 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002833 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002834 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2835 spoe_event_str[ev]);
2836
2837 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2838
2839 if (LIST_ISEMPTY(&(ctx->events[ev])))
2840 return 1;
2841
2842 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002843 if (ret && ctx->stats.t_process != -1) {
2844 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002845 " - <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 +01002846 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2847 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2848 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002849 ctx->stats.t_response, ctx->stats.t_process,
2850 agent->counters.idles, agent->counters.applets,
2851 agent->counters.nb_sending, agent->counters.nb_waiting,
2852 agent->counters.nb_errors, agent->counters.nb_processed,
2853 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2854 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2855 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2856 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2857 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2858 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2859 ctx->stats.t_response, ctx->stats.t_process,
2860 agent->counters.idles, agent->counters.applets,
2861 agent->counters.nb_sending, agent->counters.nb_waiting,
2862 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002863 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002864 return ret;
2865}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002866
2867/***************************************************************************
2868 * Functions that create/destroy SPOE contexts
2869 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002870static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002871spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002872{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002873 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002874 return 1;
2875
Willy Tarreau2b718102021-04-21 07:32:39 +02002876 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002877 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002878
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002879 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002880 return 1;
2881
Willy Tarreau2b718102021-04-21 07:32:39 +02002882 LIST_APPEND(&ti->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002883 return 0;
2884}
2885
2886static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002887spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002888{
Willy Tarreau2b718102021-04-21 07:32:39 +02002889 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002890 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002891
2892 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002894 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002895 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002896 }
2897}
2898
Christopher Faulet4596fb72017-01-11 14:05:19 +01002899static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002900spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002901{
2902 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2903 return 1;
2904}
2905
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002906static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002907spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908{
2909 struct spoe_config *conf = FLT_CONF(filter);
2910 struct spoe_context *ctx;
2911
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002912 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913 if (ctx == NULL) {
2914 return NULL;
2915 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002916 ctx->filter = filter;
2917 ctx->state = SPOE_CTX_ST_NONE;
2918 ctx->status_code = SPOE_CTX_ERR_NONE;
2919 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002920 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002921 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002922 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002923 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002924 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002925 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002926 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002927
Christopher Fauletf7a30922016-11-10 15:04:51 +01002928 ctx->stream_id = 0;
2929 ctx->frame_id = 1;
2930 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002932 tv_zero(&ctx->stats.tv_start);
2933 tv_zero(&ctx->stats.tv_request);
2934 tv_zero(&ctx->stats.tv_queue);
2935 tv_zero(&ctx->stats.tv_wait);
2936 tv_zero(&ctx->stats.tv_response);
2937 ctx->stats.t_request = -1;
2938 ctx->stats.t_queue = -1;
2939 ctx->stats.t_waiting = -1;
2940 ctx->stats.t_response = -1;
2941 ctx->stats.t_process = -1;
2942 ctx->stats.t_total = 0;
2943
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002944 ctx->strm = s;
2945 ctx->state = SPOE_CTX_ST_READY;
2946 filter->ctx = ctx;
2947
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002948 return ctx;
2949}
2950
2951static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002952spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002954 struct spoe_config *conf = FLT_CONF(filter);
2955 struct spoe_context *ctx = filter->ctx;
2956
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957 if (!ctx)
2958 return;
2959
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002960 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002961 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002962 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963}
2964
2965static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002966spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967{
2968 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002969 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002970
2971 tv_zero(&ctx->stats.tv_start);
2972 tv_zero(&ctx->stats.tv_request);
2973 tv_zero(&ctx->stats.tv_queue);
2974 tv_zero(&ctx->stats.tv_wait);
2975 tv_zero(&ctx->stats.tv_response);
2976 ctx->stats.t_request = -1;
2977 ctx->stats.t_queue = -1;
2978 ctx->stats.t_waiting = -1;
2979 ctx->stats.t_response = -1;
2980 ctx->stats.t_process = -1;
2981 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982}
2983
2984
2985/***************************************************************************
2986 * Hooks that manage the filter lifecycle (init/check/deinit)
2987 **************************************************************************/
2988/* Signal handler: Do a soft stop, wakeup SPOE applet */
2989static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002990spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991{
2992 struct proxy *p;
2993
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002994 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995 while (p) {
2996 struct flt_conf *fconf;
2997
2998 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002999 struct spoe_config *conf;
3000 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01003001 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02003002 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003003
Christopher Faulet3b386a32017-02-23 10:17:15 +01003004 if (fconf->id != spoe_filter_id)
3005 continue;
3006
3007 conf = fconf->conf;
3008 agent = conf->agent;
3009
Christopher Faulet24289f22017-09-25 14:48:02 +02003010 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003011 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003012 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3013 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003014 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003015 }
3016 }
3017 p = p->next;
3018 }
3019}
3020
3021
3022/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3023static int
3024spoe_init(struct proxy *px, struct flt_conf *fconf)
3025{
3026 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003027
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003028 /* conf->agent_fe was already initialized during the config
3029 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003030 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031 conf->agent_fe.cap = PR_CAP_FE;
3032 conf->agent_fe.mode = PR_MODE_TCP;
3033 conf->agent_fe.maxconn = 0;
3034 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3035 conf->agent_fe.conn_retries = CONN_RETRIES;
3036 conf->agent_fe.accept = frontend_accept;
3037 conf->agent_fe.srv = NULL;
3038 conf->agent_fe.timeout.client = TICK_ETERNITY;
3039 conf->agent_fe.default_target = &spoe_applet.obj_type;
3040 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003043 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044 sighandler_registered = 1;
3045 }
3046
Christopher Faulet00292352019-01-09 15:05:10 +01003047 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049}
3050
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003051/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052static void
3053spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3054{
3055 struct spoe_config *conf = fconf->conf;
3056
3057 if (conf) {
3058 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059
Christopher Faulet8ef75252017-02-20 22:56:03 +01003060 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003061 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062 free(conf);
3063 }
3064 fconf->conf = NULL;
3065}
3066
3067/* Check configuration of a SPOE filter for a specified proxy.
3068 * Return 1 on error, else 0. */
3069static int
3070spoe_check(struct proxy *px, struct flt_conf *fconf)
3071{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003072 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003073 struct spoe_config *conf = fconf->conf;
3074 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003075 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003076 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003077
Christopher Faulet7ee86672017-09-19 11:08:28 +02003078 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3079 * are uniq */
3080 list_for_each_entry(f, &px->filter_configs, list) {
3081 struct spoe_config *c = f->conf;
3082
3083 /* This is not an SPOE filter */
3084 if (f->id != spoe_filter_id)
3085 continue;
3086 /* This is the current SPOE filter */
3087 if (f == fconf)
3088 continue;
3089
3090 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003091 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003092 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3093 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003094 return 1;
3095 }
3096 }
3097
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003098 target = proxy_be_by_name(conf->agent->b.name);
3099 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003100 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3101 " declared at %s:%d.\n",
3102 px->id, conf->agent->b.name, conf->agent->id,
3103 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104 return 1;
3105 }
3106 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003107 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3108 " at %s:%d does not support HTTP mode.\n",
3109 px->id, target->id, conf->agent->id,
3110 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003111 return 1;
3112 }
3113
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003114 if (px->bind_proc & ~target->bind_proc) {
3115 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3116 " at %s:%d does not cover all of its processes.\n",
3117 px->id, target->id, conf->agent->id,
3118 conf->agent->conf.file, conf->agent->conf.line);
3119 return 1;
3120 }
3121
Christopher Fauletfe261552019-03-18 13:57:42 +01003122 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003123 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3124 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3125 return 1;
3126 }
3127 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003128 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003129 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3130 conf->agent->rt[i].processing = 0;
3131 LIST_INIT(&conf->agent->rt[i].applets);
3132 LIST_INIT(&conf->agent->rt[i].sending_queue);
3133 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3134 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003135 }
3136
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003137 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3138 if (logsrv->type == LOG_TARGET_BUFFER) {
3139 struct sink *sink = sink_find(logsrv->ring_name);
3140
3141 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3142 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003143 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003144 px->id, conf->agent->id, conf->agent->conf.file,
3145 conf->agent->conf.line, logsrv->ring_name);
3146 return 1;
3147 }
3148 logsrv->sink = sink;
3149 }
3150 }
3151
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003152 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003153 conf->agent->b.be = target;
3154 return 0;
3155}
3156
Kevin Zhud87b1a52019-09-17 15:05:45 +02003157/* Initializes the SPOE filter for a proxy for a specific thread.
3158 * Returns a negative value if an error occurs. */
3159static int
3160spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3161{
3162 struct spoe_config *conf = fconf->conf;
3163 struct spoe_agent *agent = conf->agent;
3164
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003165 agent->rt[tid].engine_id = generate_pseudo_uuid();
3166 if (agent->rt[tid].engine_id == NULL)
3167 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003168 return 0;
3169}
3170
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003171/**************************************************************************
3172 * Hooks attached to a stream
3173 *************************************************************************/
3174/* Called when a filter instance is created and attach to a stream. It creates
3175 * the context that will be used to process this stream. */
3176static int
3177spoe_start(struct stream *s, struct filter *filter)
3178{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003179 struct spoe_config *conf = FLT_CONF(filter);
3180 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003181 struct spoe_context *ctx;
3182
3183 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003184 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 __FUNCTION__, s);
3186
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003187 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003188 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3189 " - failed to create SPOE context\n",
3190 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003191 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003192 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003193 "SPOE: [%s] failed to create SPOE context\n",
3194 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 return 0;
3196 }
3197
Christopher Faulet11610f32017-09-21 10:23:10 +02003198 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003199 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3200
Christopher Faulet11610f32017-09-21 10:23:10 +02003201 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003202 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3203
Christopher Faulet11610f32017-09-21 10:23:10 +02003204 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205 filter->pre_analyzers |= AN_RES_INSPECT;
3206
Christopher Faulet11610f32017-09-21 10:23:10 +02003207 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3209
Christopher Faulet11610f32017-09-21 10:23:10 +02003210 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3212
Christopher Faulet11610f32017-09-21 10:23:10 +02003213 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3215
3216 return 1;
3217}
3218
3219/* Called when a filter instance is detached from a stream. It release the
3220 * attached SPOE context. */
3221static void
3222spoe_stop(struct stream *s, struct filter *filter)
3223{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3225 (int)now.tv_sec, (int)now.tv_usec,
3226 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3227 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003228 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229}
3230
Christopher Fauletf7a30922016-11-10 15:04:51 +01003231
3232/*
3233 * Called when the stream is woken up because of expired timer.
3234 */
3235static void
3236spoe_check_timeouts(struct stream *s, struct filter *filter)
3237{
3238 struct spoe_context *ctx = filter->ctx;
3239
Christopher Fauletac580602018-03-20 16:09:20 +01003240 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003241 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003242}
3243
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003244/* Called when we are ready to filter data on a channel */
3245static int
3246spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3247{
3248 struct spoe_context *ctx = filter->ctx;
3249 int ret = 1;
3250
3251 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3252 " - ctx-flags=0x%08x\n",
3253 (int)now.tv_sec, (int)now.tv_usec,
3254 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3255 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3256
Christopher Fauletb067b062017-01-04 16:39:11 +01003257 if (ctx->state == SPOE_CTX_ST_NONE)
3258 goto out;
3259
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 if (!(chn->flags & CF_ISRESP)) {
3261 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3262 chn->analysers |= AN_REQ_INSPECT_FE;
3263 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3264 chn->analysers |= AN_REQ_INSPECT_BE;
3265
3266 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3267 goto out;
3268
3269 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003270 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003271 if (!ret)
3272 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3274 }
3275 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003276 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003277 chn->analysers |= AN_RES_INSPECT;
3278
3279 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3280 goto out;
3281
Christopher Faulet8ef75252017-02-20 22:56:03 +01003282 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003283 if (!ret) {
3284 channel_dont_read(chn);
3285 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003286 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003287 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003288 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 }
3290
3291 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 return ret;
3293}
3294
3295/* Called before a processing happens on a given channel */
3296static int
3297spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3298 struct channel *chn, unsigned an_bit)
3299{
3300 struct spoe_context *ctx = filter->ctx;
3301 int ret = 1;
3302
3303 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3304 " - ctx-flags=0x%08x - ana=0x%08x\n",
3305 (int)now.tv_sec, (int)now.tv_usec,
3306 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3307 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3308 ctx->flags, an_bit);
3309
Christopher Fauletb067b062017-01-04 16:39:11 +01003310 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 goto out;
3312
3313 switch (an_bit) {
3314 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003315 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003316 break;
3317 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003318 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003319 break;
3320 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003321 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 break;
3323 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003324 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 break;
3326 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003327 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003328 break;
3329 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003330 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331 break;
3332 }
3333
3334 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003335 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003336 channel_dont_read(chn);
3337 channel_dont_close(chn);
3338 }
3339 return ret;
3340}
3341
3342/* Called when the filtering on the channel ends. */
3343static int
3344spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3345{
3346 struct spoe_context *ctx = filter->ctx;
3347
3348 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3349 " - ctx-flags=0x%08x\n",
3350 (int)now.tv_sec, (int)now.tv_usec,
3351 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3352 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3353
3354 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003355 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003356 }
3357
3358 return 1;
3359}
3360
3361/********************************************************************
3362 * Functions that manage the filter initialization
3363 ********************************************************************/
3364struct flt_ops spoe_ops = {
3365 /* Manage SPOE filter, called for each filter declaration */
3366 .init = spoe_init,
3367 .deinit = spoe_deinit,
3368 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003369 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003370
3371 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003372 .attach = spoe_start,
3373 .detach = spoe_stop,
3374 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003375
3376 /* Handle channels activity */
3377 .channel_start_analyze = spoe_start_analyze,
3378 .channel_pre_analyze = spoe_chn_pre_analyze,
3379 .channel_end_analyze = spoe_end_analyze,
3380};
3381
3382
3383static int
3384cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3385{
3386 const char *err;
3387 int i, err_code = 0;
3388
3389 if ((cfg_scope == NULL && curengine != NULL) ||
3390 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003391 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003392 goto out;
3393
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003394 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003395 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3397 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 err_code |= ERR_ALERT | ERR_ABORT;
3399 goto out;
3400 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003401 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3402 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003403 goto out;
3404 }
3405
3406 err = invalid_char(args[1]);
3407 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003408 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3409 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 err_code |= ERR_ALERT | ERR_ABORT;
3411 goto out;
3412 }
3413
3414 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3416 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 err_code |= ERR_ALERT | ERR_ABORT;
3418 goto out;
3419 }
3420 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003421 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 err_code |= ERR_ALERT | ERR_ABORT;
3423 goto out;
3424 }
3425
3426 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003427
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003428 curagent->conf.file = strdup(file);
3429 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003430
3431 curagent->timeout.hello = TICK_ETERNITY;
3432 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003433 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003434
Christopher Fauleta1cda022016-12-21 08:58:06 +01003435 curagent->var_pfx = NULL;
3436 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003437 curagent->var_t_process = NULL;
3438 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003439 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003440 curagent->cps_max = 0;
3441 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003442 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003443 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444
3445 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003446 LIST_INIT(&curagent->events[i]);
3447 LIST_INIT(&curagent->groups);
3448 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003450 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3453 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 err_code |= ERR_ALERT | ERR_FATAL;
3455 goto out;
3456 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003457 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003458 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 free(curagent->b.name);
3460 curagent->b.name = strdup(args[1]);
3461 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003462 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003463 int cur_arg = 1;
3464 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003465 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003466
Christopher Faulet11610f32017-09-21 10:23:10 +02003467 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003468 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003469 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3470 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003471 err_code |= ERR_ALERT | ERR_FATAL;
3472 goto out;
3473 }
3474 }
3475
Christopher Faulet11610f32017-09-21 10:23:10 +02003476 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 err_code |= ERR_ALERT | ERR_ABORT;
3479 goto out;
3480 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003481 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003482 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003483 cur_arg++;
3484 }
3485 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003486 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003487 int cur_arg = 1;
3488 while (*args[cur_arg]) {
3489 struct spoe_placeholder *ph = NULL;
3490
3491 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003492 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003493 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3494 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003495 err_code |= ERR_ALERT | ERR_FATAL;
3496 goto out;
3497 }
3498 }
3499
3500 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003501 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003502 err_code |= ERR_ALERT | ERR_ABORT;
3503 goto out;
3504 }
3505 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003506 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003507 cur_arg++;
3508 }
3509 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003510 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003511 unsigned int *tv = NULL;
3512 const char *res;
3513 unsigned timeout;
3514
3515 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3517 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003518 err_code |= ERR_ALERT | ERR_FATAL;
3519 goto out;
3520 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003521 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3522 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003523 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003524 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003525 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003526 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003527 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003528 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003529 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003530 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3531 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003532 err_code |= ERR_ALERT | ERR_FATAL;
3533 goto out;
3534 }
3535 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003536 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3537 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003538 err_code |= ERR_ALERT | ERR_FATAL;
3539 goto out;
3540 }
3541 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003542 if (res == PARSE_TIME_OVER) {
3543 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3544 file, linenum, args[2], args[0], args[1]);
3545 err_code |= ERR_ALERT | ERR_FATAL;
3546 goto out;
3547 }
3548 else if (res == PARSE_TIME_UNDER) {
3549 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3550 file, linenum, args[2], args[0], args[1]);
3551 err_code |= ERR_ALERT | ERR_FATAL;
3552 goto out;
3553 }
3554 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003555 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3556 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003557 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003558 goto out;
3559 }
3560 *tv = MS_TO_TICKS(timeout);
3561 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003562 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003563 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003564 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3565 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003566 err_code |= ERR_ALERT | ERR_FATAL;
3567 goto out;
3568 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003569
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003570 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003571 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003572 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003573 if (kwm == 1)
3574 curagent->flags &= ~SPOE_FL_PIPELINING;
3575 else
3576 curagent->flags |= SPOE_FL_PIPELINING;
3577 goto out;
3578 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003579 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003580 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003581 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003582 if (kwm == 1)
3583 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003584 else
3585 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003586 goto out;
3587 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003588 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003589 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3590 goto out;
3591 if (kwm == 1)
3592 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3593 else
3594 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3595 goto out;
3596 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003597 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003598 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3599 goto out;
3600 if (kwm == 1)
3601 curpxopts2 &= ~PR_O2_NOLOGNORM;
3602 else
3603 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003604 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003605 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003606
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003607 /* Following options does not support negation */
3608 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003609 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3610 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003611 err_code |= ERR_ALERT | ERR_FATAL;
3612 goto out;
3613 }
3614
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003615 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003616 char *tmp;
3617
3618 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003619 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3620 file, linenum, args[0],
3621 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003622 err_code |= ERR_ALERT | ERR_FATAL;
3623 goto out;
3624 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003625 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3626 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003627 tmp = args[2];
3628 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003629 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003630 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003631 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003632 err_code |= ERR_ALERT | ERR_FATAL;
3633 goto out;
3634 }
3635 tmp++;
3636 }
3637 curagent->var_pfx = strdup(args[2]);
3638 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003639 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003640 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3641 goto out;
3642 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3643 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003644 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003645 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003646 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003647 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3648 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003649 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003650 char *tmp;
3651
3652 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003653 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3654 file, linenum, args[0],
3655 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003656 err_code |= ERR_ALERT | ERR_FATAL;
3657 goto out;
3658 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003659 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3660 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003661 tmp = args[2];
3662 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003663 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003664 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003665 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003666 err_code |= ERR_ALERT | ERR_FATAL;
3667 goto out;
3668 }
3669 tmp++;
3670 }
3671 curagent->var_on_error = strdup(args[2]);
3672 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003673 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003674 char *tmp;
3675
3676 if (!*args[2]) {
3677 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3678 file, linenum, args[0],
3679 args[1]);
3680 err_code |= ERR_ALERT | ERR_FATAL;
3681 goto out;
3682 }
3683 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3684 goto out;
3685 tmp = args[2];
3686 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003687 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003688 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003689 file, linenum, args[0], args[1]);
3690 err_code |= ERR_ALERT | ERR_FATAL;
3691 goto out;
3692 }
3693 tmp++;
3694 }
3695 curagent->var_t_process = strdup(args[2]);
3696 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003697 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003698 char *tmp;
3699
3700 if (!*args[2]) {
3701 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3702 file, linenum, args[0],
3703 args[1]);
3704 err_code |= ERR_ALERT | ERR_FATAL;
3705 goto out;
3706 }
3707 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3708 goto out;
3709 tmp = args[2];
3710 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003711 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003712 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003713 file, linenum, args[0], args[1]);
3714 err_code |= ERR_ALERT | ERR_FATAL;
3715 goto out;
3716 }
3717 tmp++;
3718 }
3719 curagent->var_t_total = strdup(args[2]);
3720 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003721 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003722 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3723 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003724 err_code |= ERR_ALERT | ERR_FATAL;
3725 goto out;
3726 }
Christopher Faulet48026722016-11-16 15:01:12 +01003727 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003728 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003729 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003730 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3731 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003735 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003736 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003737 curagent->cps_max = atol(args[1]);
3738 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003739 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003740 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003741 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3742 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003743 err_code |= ERR_ALERT | ERR_FATAL;
3744 goto out;
3745 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003746 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003747 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003748 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003749 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003750 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003751 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003752 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3753 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003754 err_code |= ERR_ALERT | ERR_FATAL;
3755 goto out;
3756 }
3757 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3758 goto out;
3759 curagent->max_frame_size = atol(args[1]);
3760 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3761 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003762 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3763 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003764 err_code |= ERR_ALERT | ERR_FATAL;
3765 goto out;
3766 }
3767 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003768 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003769 if (!*args[1]) {
3770 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3771 file, linenum, args[0]);
3772 err_code |= ERR_ALERT | ERR_FATAL;
3773 goto out;
3774 }
3775 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3776 goto out;
3777 curagent->max_fpa = atol(args[1]);
3778 if (curagent->max_fpa < 1) {
3779 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3780 file, linenum, args[0]);
3781 err_code |= ERR_ALERT | ERR_FATAL;
3782 goto out;
3783 }
3784 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003785 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003786 int cur_arg;
3787
3788 if (!*args[1]) {
3789 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3790 file, linenum, args[0]);
3791 err_code |= ERR_ALERT | ERR_FATAL;
3792 goto out;
3793 }
3794 cur_arg = 1;
3795 while (*args[cur_arg]) {
3796 struct spoe_var_placeholder *vph;
3797
3798 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3799 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3800 err_code |= ERR_ALERT | ERR_ABORT;
3801 goto out;
3802 }
3803 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003804 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003805 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3806 err_code |= ERR_ALERT | ERR_ABORT;
3807 goto out;
3808 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003809 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003810 cur_arg++;
3811 }
3812 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003813 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003814 char *errmsg = NULL;
3815
Emeric Brun9533a702021-04-02 10:13:43 +02003816 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003817 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3818 err_code |= ERR_ALERT | ERR_FATAL;
3819 goto out;
3820 }
3821 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003822 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003823 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3824 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003825 err_code |= ERR_ALERT | ERR_FATAL;
3826 goto out;
3827 }
3828 out:
3829 return err_code;
3830}
Christopher Faulet11610f32017-09-21 10:23:10 +02003831static int
3832cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3833{
3834 struct spoe_group *grp;
3835 const char *err;
3836 int err_code = 0;
3837
3838 if ((cfg_scope == NULL && curengine != NULL) ||
3839 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003840 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003841 goto out;
3842
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003843 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003844 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003845 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3846 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003847 err_code |= ERR_ALERT | ERR_ABORT;
3848 goto out;
3849 }
3850 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3851 err_code |= ERR_ABORT;
3852 goto out;
3853 }
3854
3855 err = invalid_char(args[1]);
3856 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003857 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3858 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003859 err_code |= ERR_ALERT | ERR_ABORT;
3860 goto out;
3861 }
3862
3863 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003864 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003865 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3866 " name as another one declared at %s:%d.\n",
3867 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003868 err_code |= ERR_ALERT | ERR_FATAL;
3869 goto out;
3870 }
3871 }
3872
3873 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003874 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003875 err_code |= ERR_ALERT | ERR_ABORT;
3876 goto out;
3877 }
3878
3879 curgrp->id = strdup(args[1]);
3880 curgrp->conf.file = strdup(file);
3881 curgrp->conf.line = linenum;
3882 LIST_INIT(&curgrp->phs);
3883 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003884 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003885 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003886 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003887 int cur_arg = 1;
3888 while (*args[cur_arg]) {
3889 struct spoe_placeholder *ph = NULL;
3890
3891 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003892 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003893 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3894 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003895 err_code |= ERR_ALERT | ERR_FATAL;
3896 goto out;
3897 }
3898 }
3899
3900 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003901 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003902 err_code |= ERR_ALERT | ERR_ABORT;
3903 goto out;
3904 }
3905 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003906 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003907 cur_arg++;
3908 }
3909 }
3910 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003911 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3912 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003913 err_code |= ERR_ALERT | ERR_FATAL;
3914 goto out;
3915 }
3916 out:
3917 return err_code;
3918}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919
3920static int
3921cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3922{
3923 struct spoe_message *msg;
3924 struct spoe_arg *arg;
3925 const char *err;
3926 char *errmsg = NULL;
3927 int err_code = 0;
3928
3929 if ((cfg_scope == NULL && curengine != NULL) ||
3930 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003931 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003932 goto out;
3933
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003934 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003935 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003936 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3937 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003938 err_code |= ERR_ALERT | ERR_ABORT;
3939 goto out;
3940 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003941 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3942 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003943 goto out;
3944 }
3945
3946 err = invalid_char(args[1]);
3947 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003948 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3949 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 err_code |= ERR_ALERT | ERR_ABORT;
3951 goto out;
3952 }
3953
3954 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003955 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003956 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3957 " name as another one declared at %s:%d.\n",
3958 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003959 err_code |= ERR_ALERT | ERR_FATAL;
3960 goto out;
3961 }
3962 }
3963
3964 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003965 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003966 err_code |= ERR_ALERT | ERR_ABORT;
3967 goto out;
3968 }
3969
3970 curmsg->id = strdup(args[1]);
3971 curmsg->id_len = strlen(curmsg->id);
3972 curmsg->event = SPOE_EV_NONE;
3973 curmsg->conf.file = strdup(file);
3974 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003975 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003976 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003977 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003978 LIST_INIT(&curmsg->by_evt);
3979 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003980 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003982 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003983 int cur_arg = 1;
3984
3985 curproxy->conf.args.ctx = ARGC_SPOE;
3986 curproxy->conf.args.file = file;
3987 curproxy->conf.args.line = linenum;
3988 while (*args[cur_arg]) {
3989 char *delim = strchr(args[cur_arg], '=');
3990 int idx = 0;
3991
3992 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003993 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003994 err_code |= ERR_ALERT | ERR_ABORT;
3995 goto out;
3996 }
3997
3998 if (!delim) {
3999 arg->name = NULL;
4000 arg->name_len = 0;
4001 delim = args[cur_arg];
4002 }
4003 else {
4004 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
4005 arg->name_len = delim - args[cur_arg];
4006 delim++;
4007 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01004008 arg->expr = sample_parse_expr((char*[]){delim, NULL},
4009 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01004010 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004011 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004012 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004013 err_code |= ERR_ALERT | ERR_FATAL;
4014 free(arg->name);
4015 free(arg);
4016 goto out;
4017 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004018 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004019 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004020 cur_arg++;
4021 }
4022 curproxy->conf.args.file = NULL;
4023 curproxy->conf.args.line = 0;
4024 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004025 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004026 err = invalid_char(args[1]);
4027 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004028 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4029 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004030 err_code |= ERR_ALERT | ERR_FATAL;
4031 goto out;
4032 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004033 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004034 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004035 "logical disjunction within a condition.\n",
4036 file, linenum, args[1]);
4037 err_code |= ERR_ALERT | ERR_FATAL;
4038 goto out;
4039 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004040 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004041 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4042 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004043 err_code |= ERR_ALERT | ERR_FATAL;
4044 goto out;
4045 }
4046 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004047 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004048 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004049 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004050 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004051 goto out;
4052 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004053 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4054 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004055
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004056 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004058 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004059 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4060
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004061 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004062 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004063 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004064 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004065 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004066 curmsg->event = SPOE_EV_ON_TCP_RSP;
4067
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004068 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004069 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004070 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004071 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004072 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004073 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4074 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004075 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004076 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004077 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004078 goto out;
4079 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004080
4081 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4082 struct acl_cond *cond;
4083
4084 cond = build_acl_cond(file, linenum, &curmsg->acls,
4085 curproxy, (const char **)args+2,
4086 &errmsg);
4087 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004088 ha_alert("parsing [%s:%d] : error detected while "
4089 "parsing an 'event %s' condition : %s.\n",
4090 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004091 err_code |= ERR_ALERT | ERR_FATAL;
4092 goto out;
4093 }
4094 curmsg->cond = cond;
4095 }
4096 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004097 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4098 "or 'unless' followed by a condition but found '%s'.\n",
4099 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004100 err_code |= ERR_ALERT | ERR_FATAL;
4101 goto out;
4102 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004103 }
4104 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004105 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4106 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004107 err_code |= ERR_ALERT | ERR_FATAL;
4108 goto out;
4109 }
4110 out:
4111 free(errmsg);
4112 return err_code;
4113}
4114
4115/* Return -1 on error, else 0 */
4116static int
4117parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4118 struct flt_conf *fconf, char **err, void *private)
4119{
4120 struct list backup_sections;
4121 struct spoe_config *conf;
4122 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004123 struct spoe_group *grp, *grpback;
4124 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004125 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004126 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004127 char *file = NULL, *engine = NULL;
4128 int ret, pos = *cur_arg + 1;
4129
Christopher Faulet84c844e2018-03-23 14:37:14 +01004130 LIST_INIT(&curmsgs);
4131 LIST_INIT(&curgrps);
4132 LIST_INIT(&curmphs);
4133 LIST_INIT(&curgphs);
4134 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004135 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004136 curpxopts = 0;
4137 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004138
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004139 conf = calloc(1, sizeof(*conf));
4140 if (conf == NULL) {
4141 memprintf(err, "%s: out of memory", args[*cur_arg]);
4142 goto error;
4143 }
4144 conf->proxy = px;
4145
4146 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004147 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004148 if (!*args[pos+1]) {
4149 memprintf(err, "'%s' : '%s' option without value",
4150 args[*cur_arg], args[pos]);
4151 goto error;
4152 }
4153 file = args[pos+1];
4154 pos += 2;
4155 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004156 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004157 if (!*args[pos+1]) {
4158 memprintf(err, "'%s' : '%s' option without value",
4159 args[*cur_arg], args[pos]);
4160 goto error;
4161 }
4162 engine = args[pos+1];
4163 pos += 2;
4164 }
4165 else {
4166 memprintf(err, "unknown keyword '%s'", args[pos]);
4167 goto error;
4168 }
4169 }
4170 if (file == NULL) {
4171 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4172 goto error;
4173 }
4174
4175 /* backup sections and register SPOE sections */
4176 LIST_INIT(&backup_sections);
4177 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004178 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4179 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004180 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004181
4182 /* Parse SPOE filter configuration file */
4183 curengine = engine;
4184 curproxy = px;
4185 curagent = NULL;
4186 curmsg = NULL;
4187 ret = readcfgfile(file);
4188 curproxy = NULL;
4189
4190 /* unregister SPOE sections and restore previous sections */
4191 cfg_unregister_sections();
4192 cfg_restore_sections(&backup_sections);
4193
4194 if (ret == -1) {
4195 memprintf(err, "Could not open configuration file %s : %s",
4196 file, strerror(errno));
4197 goto error;
4198 }
4199 if (ret & (ERR_ABORT|ERR_FATAL)) {
4200 memprintf(err, "Error(s) found in configuration file %s", file);
4201 goto error;
4202 }
4203
4204 /* Check SPOE agent */
4205 if (curagent == NULL) {
4206 memprintf(err, "No SPOE agent found in file %s", file);
4207 goto error;
4208 }
4209 if (curagent->b.name == NULL) {
4210 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4211 curagent->id, curagent->conf.file, curagent->conf.line);
4212 goto error;
4213 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004214 if (curagent->timeout.hello == TICK_ETERNITY ||
4215 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004216 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004217 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4218 " | While not properly invalid, you will certainly encounter various problems\n"
4219 " | with such a configuration. To fix this, please ensure that all following\n"
4220 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4221 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004222 }
4223 if (curagent->var_pfx == NULL) {
4224 char *tmp = curagent->id;
4225
4226 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004227 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004228 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4229 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4230 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4231 goto error;
4232 }
4233 tmp++;
4234 }
4235 curagent->var_pfx = strdup(curagent->id);
4236 }
4237
Christopher Fauletb7426d12018-03-21 14:12:17 +01004238 if (curagent->var_on_error) {
4239 struct arg arg;
4240
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004241 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004242 curagent->var_pfx, curagent->var_on_error);
4243
4244 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004245 arg.data.str.area = trash.area;
4246 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004247 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004248 if (!vars_check_arg(&arg, err)) {
4249 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4250 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4251 goto error;
4252 }
4253 }
4254
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004255 if (curagent->var_t_process) {
4256 struct arg arg;
4257
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004258 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004259 curagent->var_pfx, curagent->var_t_process);
4260
4261 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004262 arg.data.str.area = trash.area;
4263 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004264 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004265 if (!vars_check_arg(&arg, err)) {
4266 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4267 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4268 goto error;
4269 }
4270 }
4271
4272 if (curagent->var_t_total) {
4273 struct arg arg;
4274
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004275 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004276 curagent->var_pfx, curagent->var_t_total);
4277
4278 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004279 arg.data.str.area = trash.area;
4280 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004281 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004282 if (!vars_check_arg(&arg, err)) {
4283 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4284 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4285 goto error;
4286 }
4287 }
4288
Christopher Faulet11610f32017-09-21 10:23:10 +02004289 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004290 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4291 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004292 goto finish;
4293 }
4294
Christopher Faulet11610f32017-09-21 10:23:10 +02004295 /* Replace placeholders by the corresponding messages for the SPOE
4296 * agent */
4297 list_for_each_entry(ph, &curmphs, list) {
4298 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004299 struct spoe_arg *arg;
4300 unsigned int where;
4301
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004302 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004303 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4304 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4305 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4306 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4307 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4308 }
4309 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4310 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4311 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004312 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4313 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004314 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004315 }
4316 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004317 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4318 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004319 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004320 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004321
4322 where = 0;
4323 switch (msg->event) {
4324 case SPOE_EV_ON_CLIENT_SESS:
4325 where |= SMP_VAL_FE_CON_ACC;
4326 break;
4327
4328 case SPOE_EV_ON_TCP_REQ_FE:
4329 where |= SMP_VAL_FE_REQ_CNT;
4330 break;
4331
4332 case SPOE_EV_ON_HTTP_REQ_FE:
4333 where |= SMP_VAL_FE_HRQ_HDR;
4334 break;
4335
4336 case SPOE_EV_ON_TCP_REQ_BE:
4337 if (px->cap & PR_CAP_FE)
4338 where |= SMP_VAL_FE_REQ_CNT;
4339 if (px->cap & PR_CAP_BE)
4340 where |= SMP_VAL_BE_REQ_CNT;
4341 break;
4342
4343 case SPOE_EV_ON_HTTP_REQ_BE:
4344 if (px->cap & PR_CAP_FE)
4345 where |= SMP_VAL_FE_HRQ_HDR;
4346 if (px->cap & PR_CAP_BE)
4347 where |= SMP_VAL_BE_HRQ_HDR;
4348 break;
4349
4350 case SPOE_EV_ON_SERVER_SESS:
4351 where |= SMP_VAL_BE_SRV_CON;
4352 break;
4353
4354 case SPOE_EV_ON_TCP_RSP:
4355 if (px->cap & PR_CAP_FE)
4356 where |= SMP_VAL_FE_RES_CNT;
4357 if (px->cap & PR_CAP_BE)
4358 where |= SMP_VAL_BE_RES_CNT;
4359 break;
4360
4361 case SPOE_EV_ON_HTTP_RSP:
4362 if (px->cap & PR_CAP_FE)
4363 where |= SMP_VAL_FE_HRS_HDR;
4364 if (px->cap & PR_CAP_BE)
4365 where |= SMP_VAL_BE_HRS_HDR;
4366 break;
4367
4368 default:
4369 break;
4370 }
4371
4372 list_for_each_entry(arg, &msg->args, list) {
4373 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004374 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004375 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004376 "none of which is available here ('%s')",
4377 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004378 sample_ckp_names(arg->expr->fetch->use),
4379 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004380 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004381 }
4382 }
4383
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004384 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004385 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004386 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004387 }
4388 }
4389 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004390 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004391 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004392 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004393 continue;
4394 }
4395
Christopher Faulet11610f32017-09-21 10:23:10 +02004396 /* Replace placeholders by the corresponding groups for the SPOE
4397 * agent */
4398 list_for_each_entry(ph, &curgphs, list) {
4399 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004400 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004401 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004402 LIST_DELETE(&grp->list);
4403 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004404 goto next_aph;
4405 }
4406 }
4407 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4408 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4409 goto error;
4410 next_aph:
4411 continue;
4412 }
4413
4414 /* Replace placeholders by the corresponding message for each SPOE
4415 * group of the SPOE agent */
4416 list_for_each_entry(grp, &curagent->groups, list) {
4417 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4418 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004419 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004420 if (msg->group != NULL) {
4421 memprintf(err, "SPOE message '%s' already belongs to "
4422 "the SPOE group '%s' declare at %s:%d",
4423 msg->id, msg->group->id,
4424 msg->group->conf.file,
4425 msg->group->conf.line);
4426 goto error;
4427 }
4428
4429 /* Scope for arguments are not checked for now. We will check
4430 * them only if a rule use the corresponding SPOE group. */
4431 msg->agent = curagent;
4432 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004433 LIST_DELETE(&ph->list);
4434 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004435 goto next_mph_grp;
4436 }
4437 }
4438 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4439 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4440 goto error;
4441 next_mph_grp:
4442 continue;
4443 }
4444 }
4445
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004446 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004447 /* move curmsgs to the agent message list */
4448 curmsgs.n->p = &curagent->messages;
4449 curmsgs.p->n = &curagent->messages;
4450 curagent->messages = curmsgs;
4451 LIST_INIT(&curmsgs);
4452
Christopher Faulet7ee86672017-09-19 11:08:28 +02004453 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004454 conf->agent = curagent;
Christopher Faulet541e2612021-08-02 17:51:01 +02004455 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004456
4457 /* Start agent's proxy initialization here. It will be finished during
4458 * the filter init. */
4459 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4460 init_new_proxy(&conf->agent_fe);
4461 conf->agent_fe.id = conf->agent->id;
4462 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004463 conf->agent_fe.options |= curpxopts;
4464 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004465
4466 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004467 LIST_DELETE(&logsrv->list);
4468 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004469 }
4470
Christopher Faulet11610f32017-09-21 10:23:10 +02004471 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004472 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004473 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004474 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004475 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004476 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004477 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004478 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004479 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4480 struct arg arg;
4481
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004482 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004483 curagent->var_pfx, vph->name);
4484
4485 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004486 arg.data.str.area = trash.area;
4487 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004488 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004489 if (!vars_check_arg(&arg, err)) {
4490 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4491 curagent->id, curagent->var_pfx, vph->name, *err);
4492 goto error;
4493 }
4494
Willy Tarreau2b718102021-04-21 07:32:39 +02004495 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004496 free(vph->name);
4497 free(vph);
4498 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004499 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004500 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004501 spoe_release_group(grp);
4502 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004503 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004504 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004505 fconf->ops = &spoe_ops;
4506 fconf->conf = conf;
4507 return 0;
4508
4509 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004510 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004511 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004512 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004513 spoe_release_placeholder(ph);
4514 }
4515 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004516 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004517 spoe_release_placeholder(ph);
4518 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004519 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004520 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004521 free(vph->name);
4522 free(vph);
4523 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004524 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004525 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004526 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004527 }
4528 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004529 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004530 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004531 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004532 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004533 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004534 free(logsrv);
4535 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004536 free(conf);
4537 return -1;
4538}
4539
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004540/* Send message of a SPOE group. This is the action_ptr callback of a rule
4541 * associated to a "send-spoe-group" action.
4542 *
Christopher Faulet13403762019-12-13 09:01:57 +01004543 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4544 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004545static enum act_return
4546spoe_send_group(struct act_rule *rule, struct proxy *px,
4547 struct session *sess, struct stream *s, int flags)
4548{
4549 struct filter *filter;
4550 struct spoe_agent *agent = NULL;
4551 struct spoe_group *group = NULL;
4552 struct spoe_context *ctx = NULL;
4553 int ret, dir;
4554
4555 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4556 if (filter->config == rule->arg.act.p[0]) {
4557 agent = rule->arg.act.p[2];
4558 group = rule->arg.act.p[3];
4559 ctx = filter->ctx;
4560 break;
4561 }
4562 }
4563 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004564 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004565 if (ctx->state == SPOE_CTX_ST_NONE)
4566 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004567
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004568 switch (rule->from) {
4569 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4570 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4571 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4572 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4573 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4574 default:
4575 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4576 " - internal error while execute spoe-send-group\n",
4577 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4578 __FUNCTION__, s);
4579 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4580 agent->id);
4581 return ACT_RET_CONT;
4582 }
4583
4584 ret = spoe_process_group(s, ctx, group, dir);
4585 if (ret == 1)
4586 return ACT_RET_CONT;
4587 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004588 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004589 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4590 " - failed to process group '%s': interrupted by caller\n",
4591 (int)now.tv_sec, (int)now.tv_usec,
4592 agent->id, __FUNCTION__, s, group->id);
4593 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004594 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004595 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004596 return ACT_RET_CONT;
4597 }
4598 return ACT_RET_YIELD;
4599 }
4600 else
Christopher Faulet13403762019-12-13 09:01:57 +01004601 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004602}
4603
4604/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4605 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4606 * action should be:
4607 *
4608 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4609 *
4610 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4611 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4612 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4613 * group.
4614 *
4615 * The function returns 1 in success case, otherwise, it returns 0 and err is
4616 * filled.
4617 */
4618static int
4619check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4620{
4621 struct flt_conf *fconf;
4622 struct spoe_config *conf;
4623 struct spoe_agent *agent = NULL;
4624 struct spoe_group *group;
4625 struct spoe_message *msg;
4626 char *engine_id = rule->arg.act.p[0];
4627 char *group_id = rule->arg.act.p[1];
4628 unsigned int where = 0;
4629
4630 switch (rule->from) {
4631 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4632 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4633 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4634 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4635 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4636 default:
4637 memprintf(err,
4638 "internal error, unexpected rule->from=%d, please report this bug!",
4639 rule->from);
4640 goto error;
4641 }
4642
4643 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4644 * <px> */
4645 list_for_each_entry(fconf, &px->filter_configs, list) {
4646 conf = fconf->conf;
4647
4648 /* This is not an SPOE filter */
4649 if (fconf->id != spoe_filter_id)
4650 continue;
4651
4652 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004653 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004654 agent = conf->agent;
4655 break;
4656 }
4657 }
4658 if (agent == NULL) {
4659 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4660 engine_id, group_id);
4661 goto error;
4662 }
4663
4664 /* Try to find the right group */
4665 list_for_each_entry(group, &agent->groups, list) {
4666 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004667 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004668 break;
4669 }
4670 if (&group->list == &agent->groups) {
4671 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4672 group_id, engine_id);
4673 goto error;
4674 }
4675
4676 /* Ok, we found the group, we need to check messages and their
4677 * arguments */
4678 list_for_each_entry(msg, &group->messages, by_grp) {
4679 struct spoe_arg *arg;
4680
4681 list_for_each_entry(arg, &msg->args, list) {
4682 if (!(arg->expr->fetch->val & where)) {
4683 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4684 "some args extract information from '%s',"
4685 "none of which is available here ('%s')",
4686 msg->id, group->id, msg->conf.file, msg->conf.line,
4687 sample_ckp_names(arg->expr->fetch->use),
4688 sample_ckp_names(where));
4689 goto error;
4690 }
4691 }
4692 }
4693
4694 free(engine_id);
4695 free(group_id);
4696 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4697 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4698 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4699 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4700 return 1;
4701
4702 error:
4703 free(engine_id);
4704 free(group_id);
4705 return 0;
4706}
4707
4708/* Parse 'send-spoe-group' action following the format:
4709 *
4710 * ... send-spoe-group <engine-id> <group-id>
4711 *
4712 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4713 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4714 * ids are saved and used later, when the rule will be checked.
4715 */
4716static enum act_parse_ret
4717parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4718 struct act_rule *rule, char **err)
4719{
4720 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4721 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4722 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4723 return ACT_RET_PRS_ERR;
4724 }
4725 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4726 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4727
4728 (*orig_arg) += 2;
4729
4730 rule->action = ACT_CUSTOM;
4731 rule->action_ptr = spoe_send_group;
4732 rule->check_ptr = check_send_spoe_group;
4733 return ACT_RET_PRS_OK;
4734}
4735
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004736
4737/* Declare the filter parser for "spoe" keyword */
4738static struct flt_kw_list flt_kws = { "SPOE", { }, {
4739 { "spoe", parse_spoe_flt, NULL },
4740 { NULL, NULL, NULL },
4741 }
4742};
4743
Willy Tarreau0108d902018-11-25 19:14:37 +01004744INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4745
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004746/* Delcate the action parser for "spoe-action" keyword */
4747static struct action_kw_list tcp_req_action_kws = { { }, {
4748 { "send-spoe-group", parse_send_spoe_group },
4749 { /* END */ },
4750 }
4751};
Willy Tarreau0108d902018-11-25 19:14:37 +01004752
4753INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4754
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004755static struct action_kw_list tcp_res_action_kws = { { }, {
4756 { "send-spoe-group", parse_send_spoe_group },
4757 { /* END */ },
4758 }
4759};
Willy Tarreau0108d902018-11-25 19:14:37 +01004760
4761INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4762
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004763static struct action_kw_list http_req_action_kws = { { }, {
4764 { "send-spoe-group", parse_send_spoe_group },
4765 { /* END */ },
4766 }
4767};
Willy Tarreau0108d902018-11-25 19:14:37 +01004768
4769INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4770
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004771static struct action_kw_list http_res_action_kws = { { }, {
4772 { "send-spoe-group", parse_send_spoe_group },
4773 { /* END */ },
4774 }
4775};
4776
Willy Tarreau0108d902018-11-25 19:14:37 +01004777INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);