blob: 3920cc52359959101367067af19c634c536fd6ac [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 Faulet8ef75252017-02-20 22:56:03 +01001262 /* Notify all waiting streams */
1263 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 Faulet8ef75252017-02-20 22:56:03 +01001274 /* If the applet was processing a fragmented frame, notify the
1275 * corresponding stream. */
1276 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 Fauletcf181c72020-11-10 18:45:34 +01001284 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1285 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1286 if (ctx->spoe_appctx == spoe_appctx)
1287 ctx->spoe_appctx = NULL;
1288 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001289 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001290 }
Christopher Faulet4d350af2021-08-02 17:53:56 +02001291 else {
1292 /* It is the last running applet and the sending and waiting
1293 * queues are not empty. Try to start a new one if HAproxy is
1294 * not stopping.
1295 */
1296 if (!stopping &&
1297 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
1298 spoe_create_appctx(agent->spoe_conf))
1299 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300
Christopher Faulet4d350af2021-08-02 17:53:56 +02001301 /* otherwise, notify all waiting streams */
1302 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1303 LIST_DELETE(&ctx->list);
1304 LIST_INIT(&ctx->list);
1305 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
1306 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1307 ctx->spoe_appctx = NULL;
1308 ctx->state = SPOE_CTX_ST_ERROR;
1309 ctx->status_code = (spoe_appctx->status_code + 0x100);
1310 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1311 }
1312 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1313 LIST_DELETE(&ctx->list);
1314 LIST_INIT(&ctx->list);
1315 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
1316 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1317 ctx->spoe_appctx = NULL;
1318 ctx->state = SPOE_CTX_ST_ERROR;
1319 ctx->status_code = (spoe_appctx->status_code + 0x100);
1320 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1321 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001323
1324 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001325 /* Release allocated memory */
1326 spoe_release_buffer(&spoe_appctx->buffer,
1327 &spoe_appctx->buffer_wait);
1328 pool_free(pool_head_spoe_appctx, spoe_appctx);
1329
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001330 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001331 agent->rt[tid].frame_size = agent->max_frame_size;
1332 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1333 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001334}
1335
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001336static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001339 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001340 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001341 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001344 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1345 /* closed */
1346 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1347 goto exit;
1348 }
1349
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001350 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001351 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001352 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1354 goto stop;
1355 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001356
Christopher Fauleta1cda022016-12-21 08:58:06 +01001357 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1359 " - Connection timed out\n",
1360 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1361 __FUNCTION__, appctx);
1362 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001364 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001365
Christopher Faulet42bfa462017-01-04 14:14:19 +01001366 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001367 SPOE_APPCTX(appctx)->task->expire =
1368 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1371 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001372 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001373 ret = spoe_prepare_hahello_frame(appctx, frame,
1374 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001375 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001376 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001377
1378 switch (ret) {
1379 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 case 0: /* ignore => an error, cannot be ignored */
1381 goto exit;
1382
1383 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 goto stop;
1385
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 default:
1387 /* HELLO frame successfully sent, now wait for the
1388 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001389 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1390 goto next;
1391 }
1392
1393 next:
1394 return 0;
1395 stop:
1396 return 1;
1397 exit:
1398 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1399 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001400}
1401
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001402static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001404{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001405 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001406 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001407 char *frame;
1408 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001409
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410
Christopher Fauletb067b062017-01-04 16:39:11 +01001411 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001412 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001413 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001414 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1418 " - Connection timed out\n",
1419 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1420 __FUNCTION__, appctx);
1421 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001422 goto exit;
1423 }
1424
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001425 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 ret = spoe_recv_frame(appctx, frame,
1427 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 if (ret > 1) {
1429 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1430 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1431 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001433 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001434 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 switch (ret) {
1438 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001440 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1441 goto next;
1442
1443 case 1: /* retry later */
1444 goto stop;
1445
1446 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001447 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001448 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001449 SPOE_APPCTX(appctx)->node.key = 0;
1450 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001451
1452 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001453 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001454 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001455 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001456
Christopher Fauleta1cda022016-12-21 08:58:06 +01001457 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001459 if (trash.data)
1460 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001461
1462 SPOE_APPCTX(appctx)->task->expire =
1463 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001464 return 0;
1465 stop:
1466 return 1;
1467 exit:
1468 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1469 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001470}
1471
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001472
Christopher Fauleta1cda022016-12-21 08:58:06 +01001473static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001474spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001475{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001476 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1477 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001478 char *frame, *buf;
1479 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001480
Christopher Faulet8ef75252017-02-20 22:56:03 +01001481 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1482 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001483 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001484
1485 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1486 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1487 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1488 SPOE_APPCTX(appctx)->max_frame_size);
1489 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001490 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001491 *skip = 1;
1492 ret = 1;
1493 goto end;
1494 }
1495 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001496 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001497 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1498 SPOE_APPCTX(appctx)->max_frame_size);
1499
1500 }
1501
Christopher Faulet8ef75252017-02-20 22:56:03 +01001502 if (ret > 1)
1503 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001504
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505 switch (ret) {
1506 case -1: /* error */
1507 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1508 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001509
Christopher Faulet8ef75252017-02-20 22:56:03 +01001510 case 0: /* ignore */
1511 if (ctx == NULL)
1512 goto abort_frag_frame;
1513
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001514 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001515 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001516 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001517 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001518 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001519 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 ctx->state = SPOE_CTX_ST_ERROR;
1521 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1522 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001523 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001524 break;
1525
1526 case 1: /* retry */
1527 *skip = 1;
1528 break;
1529
1530 default:
1531 if (ctx == NULL)
1532 goto abort_frag_frame;
1533
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001534 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001535 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001537 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001538 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001539 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1541 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1542 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001543 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001544 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 }
1546 goto end;
1547
1548 frag_frame_sent:
1549 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001550 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001551 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1552 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1553 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001554 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1555 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1556 goto end;
1557
1558 no_frag_frame_sent:
1559 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1560 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001561 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001562 }
1563 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1564 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001565 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566 }
1567 else {
1568 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001569 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001570 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001571 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001572 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001573 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001574 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1575 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1576 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001577 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001578
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1580 goto end;
1581
1582 abort_frag_frame:
1583 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1584 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1585 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1586 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1587 goto end;
1588
1589 end:
1590 return ret;
1591}
1592
1593static int
1594spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1595{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001596 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001597 struct spoe_context *ctx = NULL;
1598 char *frame;
1599 int ret;
1600
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001601 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 ret = spoe_recv_frame(appctx, frame,
1603 SPOE_APPCTX(appctx)->max_frame_size);
1604 if (ret > 1) {
1605 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1606 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001607 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001608 goto end;
1609 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001610 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001611 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1612 }
1613 switch (ret) {
1614 case -1: /* error */
1615 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1616 break;
1617
1618 case 0: /* ignore */
1619 break;
1620
1621 case 1: /* retry */
1622 *skip = 1;
1623 break;
1624
1625 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001626 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001627 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001628 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001629 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1630 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001631 if (ctx->spoe_appctx) {
1632 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001633 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001634 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001635 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1636 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1637 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1638 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1639 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1640 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1641 }
1642 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1643 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001644 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1645 break;
1646 }
1647
1648 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001649 if (trash.data)
1650 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001651 end:
1652 return ret;
1653}
1654
1655static int
1656spoe_handle_processing_appctx(struct appctx *appctx)
1657{
1658 struct stream_interface *si = appctx->owner;
Christopher Fauletf028bb62021-08-02 18:13:27 +02001659 struct server *srv = objt_server(si_strm(si)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001660 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletf028bb62021-08-02 18:13:27 +02001661 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001662
1663 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1664 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1665 goto exit;
1666 }
1667
1668 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1669 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1670 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1671 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1672 goto next;
1673 }
1674
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001675 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001676 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001677 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1679 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001680 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001681
Christopher Fauletf028bb62021-08-02 18:13:27 +02001682
1683 /* Close the applet ASAP because some sessions are waiting for a free
1684 * connection slot. It is only an issue in multithreaded mode.
1685 */
1686 close_asap = (global.nbthread > 1 &&
1687 (agent->b.be->nbpend ||
1688 (srv && (srv->nbpend || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1689
1690 /* Don"t try to send new frame we are waiting for at lease a ack, in
1691 * sync mode or if applet must be closed ASAP
1692 */
1693 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001695
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 /* receiving_frame loop */
1697 while (!skip_receiving) {
1698 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1699 switch (ret) {
1700 case -1: /* error */
1701 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001702
Christopher Faulet8f82b202018-01-24 16:23:03 +01001703 case 0: /* ignore */
1704 active_r = 1;
1705 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001706
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 case 1: /* retry */
1708 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001709
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 default:
1711 active_r = 1;
1712 break;
1713 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 /* send_frame loop */
1717 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1718 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1719 switch (ret) {
1720 case -1: /* error */
1721 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001724 if (SPOE_APPCTX(appctx)->node.key)
1725 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 active_s++;
1727 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 case 1: /* retry */
1730 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731
Christopher Faulet8f82b202018-01-24 16:23:03 +01001732 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001733 if (SPOE_APPCTX(appctx)->node.key)
1734 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001735 active_s++;
1736 break;
1737 }
Christopher Fauletf028bb62021-08-02 18:13:27 +02001738
1739 /* if applet must be close ASAP, don't send more than a frame */
1740 if (close_asap)
1741 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001743
Christopher Faulet8f82b202018-01-24 16:23:03 +01001744 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001745 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1746 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1747 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001748
Christopher Faulet8f82b202018-01-24 16:23:03 +01001749 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletf028bb62021-08-02 18:13:27 +02001750 /* If applet must be closed, don't switch it in IDLE state and
1751 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001752 */
Christopher Fauletf028bb62021-08-02 18:13:27 +02001753 if (close_asap) {
1754 if (SPOE_APPCTX(appctx)->cur_fpa)
1755 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1757 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1758 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1759 goto next;
1760 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001761 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001762 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001763 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764 }
Christopher Fauletf028bb62021-08-02 18:13:27 +02001765
1766 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 return 1;
1768
Christopher Faulet8f82b202018-01-24 16:23:03 +01001769 next:
1770 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1771 return 0;
1772
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 exit:
1774 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1775 return 0;
1776}
1777
1778static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780{
1781 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001782 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783 char *frame, *buf;
1784 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001785
Christopher Fauleta1cda022016-12-21 08:58:06 +01001786 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1787 goto exit;
1788
1789 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1790 goto exit;
1791
Christopher Faulet8ef75252017-02-20 22:56:03 +01001792 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1793 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001794 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1796 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799
1800 switch (ret) {
1801 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803 goto exit;
1804
1805 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 goto stop;
1807
1808 default:
1809 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1810 " - disconnected by HAProxy (%d): %s\n",
1811 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001812 __FUNCTION__, appctx,
1813 SPOE_APPCTX(appctx)->status_code,
1814 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815
1816 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1817 goto next;
1818 }
1819
1820 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 SPOE_APPCTX(appctx)->task->expire =
1822 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 return 0;
1824 stop:
1825 return 1;
1826 exit:
1827 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1828 return 0;
1829}
1830
1831static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001832spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833{
1834 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001835 char *frame;
1836 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837
Christopher Fauletb067b062017-01-04 16:39:11 +01001838 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001839 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001841 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842
Christopher Fauletb067b062017-01-04 16:39:11 +01001843 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001846 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001848 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001849 ret = spoe_recv_frame(appctx, frame,
1850 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001852 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001854 }
1855
1856 switch (ret) {
1857 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1859 " - error on frame (%s)\n",
1860 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001861 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001862 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864 goto exit;
1865
1866 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 goto next;
1868
1869 case 1: /* retry */
1870 goto stop;
1871
1872 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001874 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001875 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001876 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001877 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1878 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001879 goto exit;
1880 }
1881
1882 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001884 if (trash.data)
1885 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001886
Christopher Fauleta1cda022016-12-21 08:58:06 +01001887 return 0;
1888 stop:
1889 return 1;
1890 exit:
1891 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1892 return 0;
1893}
1894
1895/* I/O Handler processing messages exchanged with the agent */
1896static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001897spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899 struct stream_interface *si = appctx->owner;
1900 struct spoe_agent *agent;
1901
1902 if (SPOE_APPCTX(appctx) == NULL)
1903 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1906 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001907
Christopher Fauleta1cda022016-12-21 08:58:06 +01001908 switchstate:
1909 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1910 " - appctx-state=%s\n",
1911 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1912 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1913
1914 switch (appctx->st0) {
1915 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001916 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 goto out;
1918 goto switchstate;
1919
1920 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001921 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001922 goto out;
1923 goto switchstate;
1924
1925 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001926 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001927 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001928 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001929 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001930 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001931 SPOE_APPCTX(appctx)->task->expire =
1932 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001933 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934 goto switchstate;
1935 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001936 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1937 /* fall through */
1938
1939 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001940 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1941 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001942 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001943 goto out;
1944 goto switchstate;
1945
1946 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001948 goto out;
1949 goto switchstate;
1950
1951 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001952 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001953 goto out;
1954 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955
1956 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001957 appctx->st0 = SPOE_APPCTX_ST_END;
1958 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1959
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960 si_shutw(si);
1961 si_shutr(si);
1962 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963 /* fall through */
1964
1965 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001966 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001968 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001969 if (stopping)
1970 spoe_wakeup_appctx(appctx);
1971
Christopher Faulet42bfa462017-01-04 14:14:19 +01001972 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1973 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001974}
1975
1976struct applet spoe_applet = {
1977 .obj_type = OBJ_TYPE_APPLET,
1978 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001979 .fct = spoe_handle_appctx,
1980 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001981};
1982
1983/* Create a SPOE applet. On success, the created applet is returned, else
1984 * NULL. */
1985static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001986spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987{
1988 struct appctx *appctx;
1989 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991
Emeric Brun1138fd02017-06-19 12:38:55 +02001992 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993 goto out_error;
1994
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01001995 appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001996 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 goto out_free_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001998
Christopher Faulet42bfa462017-01-04 14:14:19 +01001999 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002000 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002001 goto out_free_spoe_appctx;
2002
2003 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002004 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002005 SPOE_APPCTX(appctx)->task->context = appctx;
2006 SPOE_APPCTX(appctx)->agent = conf->agent;
2007 SPOE_APPCTX(appctx)->version = 0;
2008 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
2009 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01002010 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002011 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01002012 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01002013
Willy Tarreau90f366b2021-02-20 11:49:49 +01002014 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002015 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002016 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002017
2018 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2019 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020
Willy Tarreau5820a362016-12-22 15:59:02 +01002021 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022 if (!sess)
2023 goto out_free_spoe;
2024
Christopher Faulet26256f82020-09-14 11:40:13 +02002025 if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026 goto out_free_sess;
2027
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028 stream_set_backend(strm, conf->agent->b.be);
2029
2030 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002031 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002032 appctx_wakeup(appctx);
2033
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002034 strm->do_log = NULL;
2035 strm->res.flags |= CF_READ_DONTWAIT;
2036
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002037 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02002038 LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002039 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau4781b152021-04-06 13:53:36 +02002040 _HA_ATOMIC_INC(&conf->agent->counters.applets);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002041
Emeric Brunc60def82017-09-27 14:59:38 +02002042 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002043 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002044 return appctx;
2045
2046 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002047 out_free_sess:
2048 session_free(sess);
2049 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002050 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002051 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002052 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053 out_free_appctx:
2054 appctx_free(appctx);
2055 out_error:
2056 return NULL;
2057}
2058
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002059static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002060spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002061{
2062 struct spoe_config *conf = FLT_CONF(ctx->filter);
2063 struct spoe_agent *agent = conf->agent;
2064 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002065 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066
Christopher Fauleta1cda022016-12-21 08:58:06 +01002067 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002068 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002069 (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 +01002070 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071
2072 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002073 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2075 ctx->strm);
2076
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 /* Do not try to create a new applet if there is no server up for the
2078 * agent's backend. */
2079 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2080 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2081 " - cannot create SPOE appctx: no server up\n",
2082 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2083 __FUNCTION__, ctx->strm);
2084 goto end;
2085 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086
Christopher Fauleta1cda022016-12-21 08:58:06 +01002087 /* Do not try to create a new applet if we have reached the maximum of
2088 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002089 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002090 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002091 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2092 " - cannot create SPOE appctx: max CPS reached\n",
2093 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2094 __FUNCTION__, ctx->strm);
2095 goto end;
2096 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002097 }
2098
Christopher Faulet8ef75252017-02-20 22:56:03 +01002099 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002100 if (appctx == NULL) {
2101 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2102 " - failed to create SPOE appctx\n",
2103 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2104 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002105 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002106 "SPOE: [%s] failed to create SPOE applet\n",
2107 agent->id);
2108
Christopher Fauleta1cda022016-12-21 08:58:06 +01002109 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002110 }
2111
Christopher Fauleta1cda022016-12-21 08:58:06 +01002112 /* Increase the per-process number of cumulated connections */
2113 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002114 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002115
Christopher Fauleta1cda022016-12-21 08:58:06 +01002116 end:
2117 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002118 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002119 ctx->status_code = SPOE_CTX_ERR_RES;
2120 return -1;
2121 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002122
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002123 /* Add the SPOE context in the sending queue if the stream has no applet
2124 * already assigned and wakeup all idle applets. Otherwise, don't queue
2125 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002126 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002127 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2128 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002129 if (ctx->spoe_appctx)
2130 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002131 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002132
2133 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002134 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002135 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002136 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002137 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002138 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002139
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002140 /* Finally try to wakeup an IDLE applet. */
2141 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2142 struct eb32_node *node;
2143
2144 node = eb32_first(&agent->rt[tid].idle_applets);
2145 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2146 if (node && spoe_appctx) {
2147 eb32_delete(&spoe_appctx->node);
2148 spoe_appctx->node.key++;
2149 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2150 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002151 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002152 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002153 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002154}
2155
2156/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002157 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002158 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002159/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2160 * fragmented_content. If the next message can be processed, it returns 0. If
2161 * the message is too big, it returns -1.*/
2162static int
2163spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2164 struct spoe_message *msg, int dir,
2165 char **buf, char *end)
2166{
2167 struct sample *smp;
2168 struct spoe_arg *arg;
2169 int ret;
2170
2171 if (msg->cond) {
2172 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2173 ret = acl_pass(ret);
2174 if (msg->cond->pol == ACL_COND_UNLESS)
2175 ret = !ret;
2176
2177 /* the rule does not match */
2178 if (!ret)
2179 goto next;
2180 }
2181
2182 /* Resume encoding of a SPOE argument */
2183 if (ctx->frag_ctx.curarg != NULL) {
2184 arg = ctx->frag_ctx.curarg;
2185 goto encode_argument;
2186 }
2187
2188 if (ctx->frag_ctx.curoff != UINT_MAX)
2189 goto encode_msg_payload;
2190
2191 /* Check if there is enough space for the message name and the
2192 * number of arguments. It implies <msg->id_len> is encoded on 2
2193 * bytes, at most (< 2288). */
2194 if (*buf + 2 + msg->id_len + 1 > end)
2195 goto too_big;
2196
2197 /* Encode the message name */
2198 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2199 goto too_big;
2200
2201 /* Set the number of arguments for this message */
2202 **buf = msg->nargs;
2203 (*buf)++;
2204
2205 ctx->frag_ctx.curoff = 0;
2206 encode_msg_payload:
2207
2208 /* Loop on arguments */
2209 list_for_each_entry(arg, &msg->args, list) {
2210 ctx->frag_ctx.curarg = arg;
2211 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002212 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002213
2214 encode_argument:
2215 if (ctx->frag_ctx.curoff != UINT_MAX)
2216 goto encode_arg_value;
2217
Joseph Herlantf7f60312018-11-15 13:46:49 -08002218 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002219 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2220 goto too_big;
2221
2222 ctx->frag_ctx.curoff = 0;
2223 encode_arg_value:
2224
Joseph Herlantf7f60312018-11-15 13:46:49 -08002225 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002226 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002227 if (smp) {
2228 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2229 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2230 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002231 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002232 if (ret == -1 || ctx->frag_ctx.curoff)
2233 goto too_big;
2234 }
2235
2236 next:
2237 return 0;
2238
2239 too_big:
2240 return -1;
2241}
2242
Christopher Fauletc718b822017-09-21 16:50:56 +02002243/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2244 * handle fragmented content. On success it returns 1. If an error occurred, -1
2245 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2246 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002247static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002248spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002249 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002250{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002251 struct spoe_config *conf = FLT_CONF(ctx->filter);
2252 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002253 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002254 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002255
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002256 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002257 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002258
Christopher Fauletc718b822017-09-21 16:50:56 +02002259 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2260 /* Resume encoding of a SPOE message */
2261 if (ctx->frag_ctx.curmsg != NULL) {
2262 msg = ctx->frag_ctx.curmsg;
2263 goto encode_evt_message;
2264 }
2265
2266 list_for_each_entry(msg, messages, by_evt) {
2267 ctx->frag_ctx.curmsg = msg;
2268 ctx->frag_ctx.curarg = NULL;
2269 ctx->frag_ctx.curoff = UINT_MAX;
2270
2271 encode_evt_message:
2272 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2273 goto too_big;
2274 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002275 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002276 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2277 /* Resume encoding of a SPOE message */
2278 if (ctx->frag_ctx.curmsg != NULL) {
2279 msg = ctx->frag_ctx.curmsg;
2280 goto encode_grp_message;
2281 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282
Christopher Fauletc718b822017-09-21 16:50:56 +02002283 list_for_each_entry(msg, messages, by_grp) {
2284 ctx->frag_ctx.curmsg = msg;
2285 ctx->frag_ctx.curarg = NULL;
2286 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002287
Christopher Fauletc718b822017-09-21 16:50:56 +02002288 encode_grp_message:
2289 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2290 goto too_big;
2291 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002292 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002293 else
2294 goto skip;
2295
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002296
Christopher Faulet57583e42017-09-04 15:41:09 +02002297 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002298 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002299 goto skip;
2300
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002301 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002302 " - encode %s messages - spoe_appctx=%p"
2303 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002304 (int)now.tv_sec, (int)now.tv_usec,
2305 agent->id, __FUNCTION__, s,
2306 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002307 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002308 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002309
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002310 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002311 ctx->frag_ctx.curmsg = NULL;
2312 ctx->frag_ctx.curarg = NULL;
2313 ctx->frag_ctx.curoff = 0;
2314 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002315
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002316 return 1;
2317
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002318 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002319 /* Return an error if fragmentation is unsupported or if nothing has
2320 * been encoded because its too big and not splittable. */
2321 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002322 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2323 return -1;
2324 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002325
2326 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002327 " - encode fragmented messages - spoe_appctx=%p"
2328 " - curmsg=%p - curarg=%p - curoff=%u"
2329 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002330 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002331 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002332 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002333 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002334
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002335 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002336 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2337 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2338 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002339
2340 skip:
2341 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2342 " - skip the frame because nothing has been encoded\n",
2343 (int)now.tv_sec, (int)now.tv_usec,
2344 agent->id, __FUNCTION__, s);
2345 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346}
2347
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002348
2349/***************************************************************************
2350 * Functions that handle SPOE actions
2351 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002352/* Helper function to set a variable */
2353static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355 struct sample *smp)
2356{
2357 struct spoe_config *conf = FLT_CONF(ctx->filter);
2358 struct spoe_agent *agent = conf->agent;
2359 char varname[64];
2360
2361 memset(varname, 0, sizeof(varname));
2362 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2363 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002364 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2365 vars_set_by_name(varname, len, smp);
2366 else
2367 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368}
2369
2370/* Helper function to unset a variable */
2371static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373 struct sample *smp)
2374{
2375 struct spoe_config *conf = FLT_CONF(ctx->filter);
2376 struct spoe_agent *agent = conf->agent;
2377 char varname[64];
2378
2379 memset(varname, 0, sizeof(varname));
2380 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2381 scope, agent->var_pfx, len, name);
2382 vars_unset_by_name_ifexist(varname, len, smp);
2383}
2384
2385
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386static inline int
2387spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2388 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 char *str, *scope, *p = *buf;
2391 struct sample smp;
2392 uint64_t sz;
2393 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 if (p + 2 >= end)
2396 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002397
Christopher Faulet8ef75252017-02-20 22:56:03 +01002398 /* SET-VAR requires 3 arguments */
2399 if (*p++ != 3)
2400 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401
Christopher Faulet8ef75252017-02-20 22:56:03 +01002402 switch (*p++) {
2403 case SPOE_SCOPE_PROC: scope = "proc"; break;
2404 case SPOE_SCOPE_SESS: scope = "sess"; break;
2405 case SPOE_SCOPE_TXN : scope = "txn"; break;
2406 case SPOE_SCOPE_REQ : scope = "req"; break;
2407 case SPOE_SCOPE_RES : scope = "res"; break;
2408 default: goto skip;
2409 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002410
Christopher Faulet8ef75252017-02-20 22:56:03 +01002411 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2412 goto skip;
2413 memset(&smp, 0, sizeof(smp));
2414 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002415
Christopher Faulet8ef75252017-02-20 22:56:03 +01002416 if (spoe_decode_data(&p, end, &smp) == -1)
2417 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2420 " - set-var '%s.%s.%.*s'\n",
2421 (int)now.tv_sec, (int)now.tv_usec,
2422 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2423 __FUNCTION__, s, scope,
2424 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2425 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002426
Christopher Faulet2469eba2020-10-15 16:08:30 +02002427 if (smp.data.type == SMP_T_ANY)
2428 spoe_unset_var(ctx, scope, str, sz, &smp);
2429 else
2430 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002431
Christopher Faulet8ef75252017-02-20 22:56:03 +01002432 ret = (p - *buf);
2433 *buf = p;
2434 return ret;
2435 skip:
2436 return 0;
2437}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002438
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439static inline int
2440spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2441 char **buf, char *end, int dir)
2442{
2443 char *str, *scope, *p = *buf;
2444 struct sample smp;
2445 uint64_t sz;
2446 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002447
Christopher Faulet8ef75252017-02-20 22:56:03 +01002448 if (p + 2 >= end)
2449 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451 /* UNSET-VAR requires 2 arguments */
2452 if (*p++ != 2)
2453 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002454
Christopher Faulet8ef75252017-02-20 22:56:03 +01002455 switch (*p++) {
2456 case SPOE_SCOPE_PROC: scope = "proc"; break;
2457 case SPOE_SCOPE_SESS: scope = "sess"; break;
2458 case SPOE_SCOPE_TXN : scope = "txn"; break;
2459 case SPOE_SCOPE_REQ : scope = "req"; break;
2460 case SPOE_SCOPE_RES : scope = "res"; break;
2461 default: goto skip;
2462 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002463
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2465 goto skip;
2466 memset(&smp, 0, sizeof(smp));
2467 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002468
Christopher Faulet8ef75252017-02-20 22:56:03 +01002469 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2470 " - unset-var '%s.%s.%.*s'\n",
2471 (int)now.tv_sec, (int)now.tv_usec,
2472 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2473 __FUNCTION__, s, scope,
2474 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2475 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002476
Christopher Faulet8ef75252017-02-20 22:56:03 +01002477 spoe_unset_var(ctx, scope, str, sz, &smp);
2478
2479 ret = (p - *buf);
2480 *buf = p;
2481 return ret;
2482 skip:
2483 return 0;
2484}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002485
Christopher Faulet8ef75252017-02-20 22:56:03 +01002486/* Process SPOE actions for a specific event. It returns 1 on success. If an
2487 * error occurred, 0 is returned. */
2488static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002489spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002490{
2491 char *p, *end;
2492 int ret;
2493
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002494 p = b_head(&ctx->buffer);
2495 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002496
2497 while (p < end) {
2498 enum spoe_action_type type;
2499
2500 type = *p++;
2501 switch (type) {
2502 case SPOE_ACT_T_SET_VAR:
2503 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2504 if (!ret)
2505 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002506 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002507
Christopher Faulet8ef75252017-02-20 22:56:03 +01002508 case SPOE_ACT_T_UNSET_VAR:
2509 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2510 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002511 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002512 break;
2513
2514 default:
2515 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002516 }
2517 }
2518
2519 return 1;
2520 skip:
2521 return 0;
2522}
2523
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002524/***************************************************************************
2525 * Functions that process SPOE events
2526 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002527static void
2528spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2529 struct spoe_context *ctx, int dir)
2530{
2531 if (!tv_iszero(&ctx->stats.tv_start)) {
2532 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2533 ctx->stats.t_total += ctx->stats.t_process;
2534 tv_zero(&ctx->stats.tv_request);
2535 tv_zero(&ctx->stats.tv_queue);
2536 tv_zero(&ctx->stats.tv_wait);
2537 tv_zero(&ctx->stats.tv_response);
2538 }
2539
2540 if (agent->var_t_process) {
2541 struct sample smp;
2542
2543 memset(&smp, 0, sizeof(smp));
2544 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2545 smp.data.u.sint = ctx->stats.t_process;
2546 smp.data.type = SMP_T_SINT;
2547
2548 spoe_set_var(ctx, "txn", agent->var_t_process,
2549 strlen(agent->var_t_process), &smp);
2550 }
2551
2552 if (agent->var_t_total) {
2553 struct sample smp;
2554
2555 memset(&smp, 0, sizeof(smp));
2556 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2557 smp.data.u.sint = ctx->stats.t_total;
2558 smp.data.type = SMP_T_SINT;
2559
2560 spoe_set_var(ctx, "txn", agent->var_t_total,
2561 strlen(agent->var_t_total), &smp);
2562 }
2563}
2564
2565static void
2566spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2567 struct spoe_context *ctx, int dir)
2568{
2569 if (agent->eps_max > 0)
2570 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2571
2572 if (agent->var_on_error) {
2573 struct sample smp;
2574
2575 memset(&smp, 0, sizeof(smp));
2576 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2577 smp.data.u.sint = ctx->status_code;
2578 smp.data.type = SMP_T_BOOL;
2579
2580 spoe_set_var(ctx, "txn", agent->var_on_error,
2581 strlen(agent->var_on_error), &smp);
2582 }
2583
2584 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2585 ? SPOE_CTX_ST_READY
2586 : SPOE_CTX_ST_NONE);
2587}
2588
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002589static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002590spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002591{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592 /* If a process is already started for this SPOE context, retry
2593 * later. */
2594 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002595 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002597 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002598 ctx->stats.tv_start = now;
2599 ctx->stats.tv_request = now;
2600 ctx->stats.t_request = -1;
2601 ctx->stats.t_queue = -1;
2602 ctx->stats.t_waiting = -1;
2603 ctx->stats.t_response = -1;
2604 ctx->stats.t_process = -1;
2605
2606 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002607
Christopher Fauleta1cda022016-12-21 08:58:06 +01002608 /* Set the right flag to prevent request and response processing
2609 * in same time. */
2610 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2611 ? SPOE_CTX_FL_REQ_PROCESS
2612 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002613 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002614}
2615
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002616static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002617spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002618{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002619 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002620
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002621 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2622 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002623 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002624 if (sa) {
2625 if (sa->frag_ctx.ctx == ctx) {
2626 sa->frag_ctx.ctx = NULL;
2627 spoe_wakeup_appctx(sa->owner);
2628 }
2629 else
2630 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002631 }
2632
Christopher Fauleta1cda022016-12-21 08:58:06 +01002633 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002634 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002635 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002636
2637 /* Reset processing timer */
2638 ctx->process_exp = TICK_ETERNITY;
2639
Christopher Faulet8ef75252017-02-20 22:56:03 +01002640 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641
Christopher Fauletfce747b2018-01-24 15:59:32 +01002642 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002643 ctx->frag_ctx.curmsg = NULL;
2644 ctx->frag_ctx.curarg = NULL;
2645 ctx->frag_ctx.curoff = 0;
2646 ctx->frag_ctx.flags = 0;
2647
Christopher Fauleta1cda022016-12-21 08:58:06 +01002648 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002649 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002650 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002651 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002652 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002653
Willy Tarreau2b718102021-04-21 07:32:39 +02002654 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002655 LIST_INIT(&ctx->list);
2656 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002657}
2658
Christopher Faulet58d03682017-09-21 16:57:24 +02002659/* Process a list of SPOE messages. First, this functions will process messages
2660 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2661 * to process corresponding actions. During all the processing, it returns 0
2662 * and it returns 1 when the processing is finished. If an error occurred, -1
2663 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002664static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002665spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2666 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002667{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002668 struct spoe_config *conf = FLT_CONF(ctx->filter);
2669 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002670 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002671
2672 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002673 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002674
2675 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2676 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002677 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002678 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002679 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002680 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002681 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002682 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002683
2684 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002685 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002686 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002687 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002688 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002689 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002690 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002691 goto skip;
2692 }
2693 }
2694
Christopher Fauletf7a30922016-11-10 15:04:51 +01002695 if (!tick_isset(ctx->process_exp)) {
2696 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2697 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2698 ctx->process_exp);
2699 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002700 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002701 if (!ret)
2702 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002704 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002705 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002706 }
2707
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002708 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002709 if (tv_iszero(&ctx->stats.tv_request))
2710 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002711 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002712 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002713 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002714 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002715 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002716 if (!ret)
2717 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002718 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002719 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002720 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2721 }
2722
2723 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002724 if (ctx->spoe_appctx)
2725 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002726 ret = 0;
2727 goto out;
2728 }
2729
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002730 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2731 ret = 0;
2732 goto out;
2733 }
2734
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002735 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002736 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002737 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002738 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002739 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002740 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002741 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002742 }
2743
2744 out:
2745 return ret;
2746
Christopher Fauleta1cda022016-12-21 08:58:06 +01002747 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002748 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002749 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002750 spoe_stop_processing(agent, ctx);
2751 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002752
Christopher Fauleta1cda022016-12-21 08:58:06 +01002753 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002754 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002755 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002756 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002757 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002758 spoe_handle_processing_error(s, agent, ctx, dir);
2759 ret = 1;
2760 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002761 return ret;
2762}
2763
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002764/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2765 * See spoe_process_message for details. */
2766static int
2767spoe_process_group(struct stream *s, struct spoe_context *ctx,
2768 struct spoe_group *group, int dir)
2769{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002770 struct spoe_config *conf = FLT_CONF(ctx->filter);
2771 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002772 int ret;
2773
2774 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2775 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002776 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002777 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2778 group->id);
2779
2780 if (LIST_ISEMPTY(&group->messages))
2781 return 1;
2782
2783 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002784 if (ret && ctx->stats.t_process != -1) {
2785 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002786 " - <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 +01002787 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002788 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002789 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002790 ctx->stats.t_response, ctx->stats.t_process,
2791 agent->counters.idles, agent->counters.applets,
2792 agent->counters.nb_sending, agent->counters.nb_waiting,
2793 agent->counters.nb_errors, agent->counters.nb_processed,
2794 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2795 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2796 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2797 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2798 agent->id, group->id, s->uniq_id, ctx->status_code,
2799 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2800 ctx->stats.t_response, ctx->stats.t_process,
2801 agent->counters.idles, agent->counters.applets,
2802 agent->counters.nb_sending, agent->counters.nb_waiting,
2803 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002804 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002805 return ret;
2806}
2807
Christopher Faulet58d03682017-09-21 16:57:24 +02002808/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2809 * See spoe_process_message for details. */
2810static int
2811spoe_process_event(struct stream *s, struct spoe_context *ctx,
2812 enum spoe_event ev)
2813{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002814 struct spoe_config *conf = FLT_CONF(ctx->filter);
2815 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002816 int dir, ret;
2817
2818 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002819 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002820 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002821 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2822 spoe_event_str[ev]);
2823
2824 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2825
2826 if (LIST_ISEMPTY(&(ctx->events[ev])))
2827 return 1;
2828
2829 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002830 if (ret && ctx->stats.t_process != -1) {
2831 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002832 " - <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 +01002833 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2834 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2835 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002836 ctx->stats.t_response, ctx->stats.t_process,
2837 agent->counters.idles, agent->counters.applets,
2838 agent->counters.nb_sending, agent->counters.nb_waiting,
2839 agent->counters.nb_errors, agent->counters.nb_processed,
2840 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2841 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2842 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2843 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2844 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2845 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2846 ctx->stats.t_response, ctx->stats.t_process,
2847 agent->counters.idles, agent->counters.applets,
2848 agent->counters.nb_sending, agent->counters.nb_waiting,
2849 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002850 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002851 return ret;
2852}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853
2854/***************************************************************************
2855 * Functions that create/destroy SPOE contexts
2856 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002857static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002859{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002860 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002861 return 1;
2862
Willy Tarreau2b718102021-04-21 07:32:39 +02002863 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002864 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002865
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002866 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002867 return 1;
2868
Willy Tarreau2b718102021-04-21 07:32:39 +02002869 LIST_APPEND(&ti->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002870 return 0;
2871}
2872
2873static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002874spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002875{
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
2879 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002880 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002881 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002882 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002883 }
2884}
2885
Christopher Faulet4596fb72017-01-11 14:05:19 +01002886static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002887spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002888{
2889 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2890 return 1;
2891}
2892
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002893static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002894spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002895{
2896 struct spoe_config *conf = FLT_CONF(filter);
2897 struct spoe_context *ctx;
2898
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002899 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900 if (ctx == NULL) {
2901 return NULL;
2902 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002903 ctx->filter = filter;
2904 ctx->state = SPOE_CTX_ST_NONE;
2905 ctx->status_code = SPOE_CTX_ERR_NONE;
2906 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002907 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002908 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002909 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002910 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002911 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002912 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002913 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914
Christopher Fauletf7a30922016-11-10 15:04:51 +01002915 ctx->stream_id = 0;
2916 ctx->frame_id = 1;
2917 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002918
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002919 tv_zero(&ctx->stats.tv_start);
2920 tv_zero(&ctx->stats.tv_request);
2921 tv_zero(&ctx->stats.tv_queue);
2922 tv_zero(&ctx->stats.tv_wait);
2923 tv_zero(&ctx->stats.tv_response);
2924 ctx->stats.t_request = -1;
2925 ctx->stats.t_queue = -1;
2926 ctx->stats.t_waiting = -1;
2927 ctx->stats.t_response = -1;
2928 ctx->stats.t_process = -1;
2929 ctx->stats.t_total = 0;
2930
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002931 ctx->strm = s;
2932 ctx->state = SPOE_CTX_ST_READY;
2933 filter->ctx = ctx;
2934
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935 return ctx;
2936}
2937
2938static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002939spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002941 struct spoe_config *conf = FLT_CONF(filter);
2942 struct spoe_context *ctx = filter->ctx;
2943
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944 if (!ctx)
2945 return;
2946
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002947 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002948 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002949 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002950}
2951
2952static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002953spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002954{
2955 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002956 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002957
2958 tv_zero(&ctx->stats.tv_start);
2959 tv_zero(&ctx->stats.tv_request);
2960 tv_zero(&ctx->stats.tv_queue);
2961 tv_zero(&ctx->stats.tv_wait);
2962 tv_zero(&ctx->stats.tv_response);
2963 ctx->stats.t_request = -1;
2964 ctx->stats.t_queue = -1;
2965 ctx->stats.t_waiting = -1;
2966 ctx->stats.t_response = -1;
2967 ctx->stats.t_process = -1;
2968 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002969}
2970
2971
2972/***************************************************************************
2973 * Hooks that manage the filter lifecycle (init/check/deinit)
2974 **************************************************************************/
2975/* Signal handler: Do a soft stop, wakeup SPOE applet */
2976static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002977spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978{
2979 struct proxy *p;
2980
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002981 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 while (p) {
2983 struct flt_conf *fconf;
2984
2985 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002986 struct spoe_config *conf;
2987 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002988 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002989 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002990
Christopher Faulet3b386a32017-02-23 10:17:15 +01002991 if (fconf->id != spoe_filter_id)
2992 continue;
2993
2994 conf = fconf->conf;
2995 agent = conf->agent;
2996
Christopher Faulet24289f22017-09-25 14:48:02 +02002997 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002998 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002999 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3000 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003001 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002 }
3003 }
3004 p = p->next;
3005 }
3006}
3007
3008
3009/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3010static int
3011spoe_init(struct proxy *px, struct flt_conf *fconf)
3012{
3013 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003015 /* conf->agent_fe was already initialized during the config
3016 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003017 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018 conf->agent_fe.cap = PR_CAP_FE;
3019 conf->agent_fe.mode = PR_MODE_TCP;
3020 conf->agent_fe.maxconn = 0;
3021 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3022 conf->agent_fe.conn_retries = CONN_RETRIES;
3023 conf->agent_fe.accept = frontend_accept;
3024 conf->agent_fe.srv = NULL;
3025 conf->agent_fe.timeout.client = TICK_ETERNITY;
3026 conf->agent_fe.default_target = &spoe_applet.obj_type;
3027 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3028
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003029 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003030 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031 sighandler_registered = 1;
3032 }
3033
Christopher Faulet00292352019-01-09 15:05:10 +01003034 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003035 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003036}
3037
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003038/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003039static void
3040spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3041{
3042 struct spoe_config *conf = fconf->conf;
3043
3044 if (conf) {
3045 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046
Christopher Faulet8ef75252017-02-20 22:56:03 +01003047 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003048 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049 free(conf);
3050 }
3051 fconf->conf = NULL;
3052}
3053
3054/* Check configuration of a SPOE filter for a specified proxy.
3055 * Return 1 on error, else 0. */
3056static int
3057spoe_check(struct proxy *px, struct flt_conf *fconf)
3058{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003059 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003060 struct spoe_config *conf = fconf->conf;
3061 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003062 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003063 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003064
Christopher Faulet7ee86672017-09-19 11:08:28 +02003065 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3066 * are uniq */
3067 list_for_each_entry(f, &px->filter_configs, list) {
3068 struct spoe_config *c = f->conf;
3069
3070 /* This is not an SPOE filter */
3071 if (f->id != spoe_filter_id)
3072 continue;
3073 /* This is the current SPOE filter */
3074 if (f == fconf)
3075 continue;
3076
3077 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003078 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003079 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3080 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003081 return 1;
3082 }
3083 }
3084
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003085 target = proxy_be_by_name(conf->agent->b.name);
3086 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003087 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3088 " declared at %s:%d.\n",
3089 px->id, conf->agent->b.name, conf->agent->id,
3090 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091 return 1;
3092 }
3093 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003094 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3095 " at %s:%d does not support HTTP mode.\n",
3096 px->id, target->id, conf->agent->id,
3097 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003098 return 1;
3099 }
3100
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003101 if (px->bind_proc & ~target->bind_proc) {
3102 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3103 " at %s:%d does not cover all of its processes.\n",
3104 px->id, target->id, conf->agent->id,
3105 conf->agent->conf.file, conf->agent->conf.line);
3106 return 1;
3107 }
3108
Christopher Fauletfe261552019-03-18 13:57:42 +01003109 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003110 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3111 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3112 return 1;
3113 }
3114 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003115 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003116 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3117 conf->agent->rt[i].processing = 0;
3118 LIST_INIT(&conf->agent->rt[i].applets);
3119 LIST_INIT(&conf->agent->rt[i].sending_queue);
3120 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3121 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003122 }
3123
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003124 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3125 if (logsrv->type == LOG_TARGET_BUFFER) {
3126 struct sink *sink = sink_find(logsrv->ring_name);
3127
3128 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3129 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003130 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003131 px->id, conf->agent->id, conf->agent->conf.file,
3132 conf->agent->conf.line, logsrv->ring_name);
3133 return 1;
3134 }
3135 logsrv->sink = sink;
3136 }
3137 }
3138
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003139 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003140 conf->agent->b.be = target;
3141 return 0;
3142}
3143
Kevin Zhud87b1a52019-09-17 15:05:45 +02003144/* Initializes the SPOE filter for a proxy for a specific thread.
3145 * Returns a negative value if an error occurs. */
3146static int
3147spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3148{
3149 struct spoe_config *conf = fconf->conf;
3150 struct spoe_agent *agent = conf->agent;
3151
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003152 agent->rt[tid].engine_id = generate_pseudo_uuid();
3153 if (agent->rt[tid].engine_id == NULL)
3154 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003155 return 0;
3156}
3157
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003158/**************************************************************************
3159 * Hooks attached to a stream
3160 *************************************************************************/
3161/* Called when a filter instance is created and attach to a stream. It creates
3162 * the context that will be used to process this stream. */
3163static int
3164spoe_start(struct stream *s, struct filter *filter)
3165{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003166 struct spoe_config *conf = FLT_CONF(filter);
3167 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168 struct spoe_context *ctx;
3169
3170 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003171 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003172 __FUNCTION__, s);
3173
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003174 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003175 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3176 " - failed to create SPOE context\n",
3177 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003178 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003179 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003180 "SPOE: [%s] failed to create SPOE context\n",
3181 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003182 return 0;
3183 }
3184
Christopher Faulet11610f32017-09-21 10:23:10 +02003185 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003186 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3187
Christopher Faulet11610f32017-09-21 10:23:10 +02003188 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003189 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3190
Christopher Faulet11610f32017-09-21 10:23:10 +02003191 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003192 filter->pre_analyzers |= AN_RES_INSPECT;
3193
Christopher Faulet11610f32017-09-21 10:23:10 +02003194 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3196
Christopher Faulet11610f32017-09-21 10:23:10 +02003197 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003198 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3199
Christopher Faulet11610f32017-09-21 10:23:10 +02003200 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003201 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3202
3203 return 1;
3204}
3205
3206/* Called when a filter instance is detached from a stream. It release the
3207 * attached SPOE context. */
3208static void
3209spoe_stop(struct stream *s, struct filter *filter)
3210{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3212 (int)now.tv_sec, (int)now.tv_usec,
3213 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3214 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003215 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003216}
3217
Christopher Fauletf7a30922016-11-10 15:04:51 +01003218
3219/*
3220 * Called when the stream is woken up because of expired timer.
3221 */
3222static void
3223spoe_check_timeouts(struct stream *s, struct filter *filter)
3224{
3225 struct spoe_context *ctx = filter->ctx;
3226
Christopher Fauletac580602018-03-20 16:09:20 +01003227 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003228 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003229}
3230
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003231/* Called when we are ready to filter data on a channel */
3232static int
3233spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3234{
3235 struct spoe_context *ctx = filter->ctx;
3236 int ret = 1;
3237
3238 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3239 " - ctx-flags=0x%08x\n",
3240 (int)now.tv_sec, (int)now.tv_usec,
3241 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3242 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3243
Christopher Fauletb067b062017-01-04 16:39:11 +01003244 if (ctx->state == SPOE_CTX_ST_NONE)
3245 goto out;
3246
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247 if (!(chn->flags & CF_ISRESP)) {
3248 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3249 chn->analysers |= AN_REQ_INSPECT_FE;
3250 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3251 chn->analysers |= AN_REQ_INSPECT_BE;
3252
3253 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3254 goto out;
3255
3256 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003257 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003258 if (!ret)
3259 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3261 }
3262 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003263 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003264 chn->analysers |= AN_RES_INSPECT;
3265
3266 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3267 goto out;
3268
Christopher Faulet8ef75252017-02-20 22:56:03 +01003269 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003270 if (!ret) {
3271 channel_dont_read(chn);
3272 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003273 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003274 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003275 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 }
3277
3278 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 return ret;
3280}
3281
3282/* Called before a processing happens on a given channel */
3283static int
3284spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3285 struct channel *chn, unsigned an_bit)
3286{
3287 struct spoe_context *ctx = filter->ctx;
3288 int ret = 1;
3289
3290 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3291 " - ctx-flags=0x%08x - ana=0x%08x\n",
3292 (int)now.tv_sec, (int)now.tv_usec,
3293 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3294 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3295 ctx->flags, an_bit);
3296
Christopher Fauletb067b062017-01-04 16:39:11 +01003297 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003298 goto out;
3299
3300 switch (an_bit) {
3301 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003302 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003303 break;
3304 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003305 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003306 break;
3307 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003308 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003309 break;
3310 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003311 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003312 break;
3313 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003314 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003315 break;
3316 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003317 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003318 break;
3319 }
3320
3321 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003322 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003323 channel_dont_read(chn);
3324 channel_dont_close(chn);
3325 }
3326 return ret;
3327}
3328
3329/* Called when the filtering on the channel ends. */
3330static int
3331spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3332{
3333 struct spoe_context *ctx = filter->ctx;
3334
3335 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3336 " - ctx-flags=0x%08x\n",
3337 (int)now.tv_sec, (int)now.tv_usec,
3338 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3339 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3340
3341 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003342 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003343 }
3344
3345 return 1;
3346}
3347
3348/********************************************************************
3349 * Functions that manage the filter initialization
3350 ********************************************************************/
3351struct flt_ops spoe_ops = {
3352 /* Manage SPOE filter, called for each filter declaration */
3353 .init = spoe_init,
3354 .deinit = spoe_deinit,
3355 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003356 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003357
3358 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003359 .attach = spoe_start,
3360 .detach = spoe_stop,
3361 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003362
3363 /* Handle channels activity */
3364 .channel_start_analyze = spoe_start_analyze,
3365 .channel_pre_analyze = spoe_chn_pre_analyze,
3366 .channel_end_analyze = spoe_end_analyze,
3367};
3368
3369
3370static int
3371cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3372{
3373 const char *err;
3374 int i, err_code = 0;
3375
3376 if ((cfg_scope == NULL && curengine != NULL) ||
3377 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003378 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379 goto out;
3380
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003381 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003382 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003383 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3384 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385 err_code |= ERR_ALERT | ERR_ABORT;
3386 goto out;
3387 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003388 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3389 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003390 goto out;
3391 }
3392
3393 err = invalid_char(args[1]);
3394 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003395 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3396 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003397 err_code |= ERR_ALERT | ERR_ABORT;
3398 goto out;
3399 }
3400
3401 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003402 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3403 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003404 err_code |= ERR_ALERT | ERR_ABORT;
3405 goto out;
3406 }
3407 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003408 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003409 err_code |= ERR_ALERT | ERR_ABORT;
3410 goto out;
3411 }
3412
3413 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003414
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003415 curagent->conf.file = strdup(file);
3416 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003417
3418 curagent->timeout.hello = TICK_ETERNITY;
3419 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003420 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003421
Christopher Fauleta1cda022016-12-21 08:58:06 +01003422 curagent->var_pfx = NULL;
3423 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003424 curagent->var_t_process = NULL;
3425 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003426 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003427 curagent->cps_max = 0;
3428 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003429 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003430 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003431
3432 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003433 LIST_INIT(&curagent->events[i]);
3434 LIST_INIT(&curagent->groups);
3435 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003436 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003437 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003438 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003439 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3440 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003441 err_code |= ERR_ALERT | ERR_FATAL;
3442 goto out;
3443 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003444 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003446 free(curagent->b.name);
3447 curagent->b.name = strdup(args[1]);
3448 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003449 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450 int cur_arg = 1;
3451 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003452 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003453
Christopher Faulet11610f32017-09-21 10:23:10 +02003454 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003455 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003456 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3457 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003458 err_code |= ERR_ALERT | ERR_FATAL;
3459 goto out;
3460 }
3461 }
3462
Christopher Faulet11610f32017-09-21 10:23:10 +02003463 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003464 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003465 err_code |= ERR_ALERT | ERR_ABORT;
3466 goto out;
3467 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003468 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003469 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003470 cur_arg++;
3471 }
3472 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003473 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003474 int cur_arg = 1;
3475 while (*args[cur_arg]) {
3476 struct spoe_placeholder *ph = NULL;
3477
3478 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003479 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003480 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3481 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003482 err_code |= ERR_ALERT | ERR_FATAL;
3483 goto out;
3484 }
3485 }
3486
3487 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003488 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003489 err_code |= ERR_ALERT | ERR_ABORT;
3490 goto out;
3491 }
3492 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003493 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003494 cur_arg++;
3495 }
3496 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003497 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003498 unsigned int *tv = NULL;
3499 const char *res;
3500 unsigned timeout;
3501
3502 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003503 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3504 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003505 err_code |= ERR_ALERT | ERR_FATAL;
3506 goto out;
3507 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003508 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3509 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003510 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003511 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003512 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003513 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003514 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003515 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003516 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003517 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3518 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003519 err_code |= ERR_ALERT | ERR_FATAL;
3520 goto out;
3521 }
3522 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003523 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3524 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003525 err_code |= ERR_ALERT | ERR_FATAL;
3526 goto out;
3527 }
3528 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003529 if (res == PARSE_TIME_OVER) {
3530 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3531 file, linenum, args[2], args[0], args[1]);
3532 err_code |= ERR_ALERT | ERR_FATAL;
3533 goto out;
3534 }
3535 else if (res == PARSE_TIME_UNDER) {
3536 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3537 file, linenum, args[2], args[0], args[1]);
3538 err_code |= ERR_ALERT | ERR_FATAL;
3539 goto out;
3540 }
3541 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003542 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3543 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003544 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003545 goto out;
3546 }
3547 *tv = MS_TO_TICKS(timeout);
3548 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003549 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003550 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003551 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3552 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003553 err_code |= ERR_ALERT | ERR_FATAL;
3554 goto out;
3555 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003556
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003557 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003558 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003559 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003560 if (kwm == 1)
3561 curagent->flags &= ~SPOE_FL_PIPELINING;
3562 else
3563 curagent->flags |= SPOE_FL_PIPELINING;
3564 goto out;
3565 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003566 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003567 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003568 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003569 if (kwm == 1)
3570 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003571 else
3572 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003573 goto out;
3574 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003575 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003576 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3577 goto out;
3578 if (kwm == 1)
3579 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3580 else
3581 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3582 goto out;
3583 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003584 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003585 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3586 goto out;
3587 if (kwm == 1)
3588 curpxopts2 &= ~PR_O2_NOLOGNORM;
3589 else
3590 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003591 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003592 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003593
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003594 /* Following options does not support negation */
3595 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003596 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3597 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003598 err_code |= ERR_ALERT | ERR_FATAL;
3599 goto out;
3600 }
3601
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003602 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003603 char *tmp;
3604
3605 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003606 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3607 file, linenum, args[0],
3608 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003609 err_code |= ERR_ALERT | ERR_FATAL;
3610 goto out;
3611 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003612 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3613 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003614 tmp = args[2];
3615 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003616 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003617 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003618 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003619 err_code |= ERR_ALERT | ERR_FATAL;
3620 goto out;
3621 }
3622 tmp++;
3623 }
3624 curagent->var_pfx = strdup(args[2]);
3625 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003626 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003627 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3628 goto out;
3629 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3630 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003631 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003632 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003633 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003634 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3635 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003636 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003637 char *tmp;
3638
3639 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003640 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3641 file, linenum, args[0],
3642 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003643 err_code |= ERR_ALERT | ERR_FATAL;
3644 goto out;
3645 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003646 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3647 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003648 tmp = args[2];
3649 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003650 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003651 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003652 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003653 err_code |= ERR_ALERT | ERR_FATAL;
3654 goto out;
3655 }
3656 tmp++;
3657 }
3658 curagent->var_on_error = strdup(args[2]);
3659 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003660 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003661 char *tmp;
3662
3663 if (!*args[2]) {
3664 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3665 file, linenum, args[0],
3666 args[1]);
3667 err_code |= ERR_ALERT | ERR_FATAL;
3668 goto out;
3669 }
3670 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3671 goto out;
3672 tmp = args[2];
3673 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003674 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003675 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003676 file, linenum, args[0], args[1]);
3677 err_code |= ERR_ALERT | ERR_FATAL;
3678 goto out;
3679 }
3680 tmp++;
3681 }
3682 curagent->var_t_process = strdup(args[2]);
3683 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003684 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003685 char *tmp;
3686
3687 if (!*args[2]) {
3688 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3689 file, linenum, args[0],
3690 args[1]);
3691 err_code |= ERR_ALERT | ERR_FATAL;
3692 goto out;
3693 }
3694 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3695 goto out;
3696 tmp = args[2];
3697 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003698 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003699 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003700 file, linenum, args[0], args[1]);
3701 err_code |= ERR_ALERT | ERR_FATAL;
3702 goto out;
3703 }
3704 tmp++;
3705 }
3706 curagent->var_t_total = strdup(args[2]);
3707 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003708 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003709 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3710 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003711 err_code |= ERR_ALERT | ERR_FATAL;
3712 goto out;
3713 }
Christopher Faulet48026722016-11-16 15:01:12 +01003714 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003715 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003716 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003717 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3718 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003719 err_code |= ERR_ALERT | ERR_FATAL;
3720 goto out;
3721 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003722 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003723 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003724 curagent->cps_max = atol(args[1]);
3725 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003726 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003727 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003728 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3729 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003730 err_code |= ERR_ALERT | ERR_FATAL;
3731 goto out;
3732 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003733 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003734 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003735 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003736 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003737 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003738 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003739 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3740 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003741 err_code |= ERR_ALERT | ERR_FATAL;
3742 goto out;
3743 }
3744 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3745 goto out;
3746 curagent->max_frame_size = atol(args[1]);
3747 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3748 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003749 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3750 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003751 err_code |= ERR_ALERT | ERR_FATAL;
3752 goto out;
3753 }
3754 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003755 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003756 if (!*args[1]) {
3757 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3758 file, linenum, args[0]);
3759 err_code |= ERR_ALERT | ERR_FATAL;
3760 goto out;
3761 }
3762 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3763 goto out;
3764 curagent->max_fpa = atol(args[1]);
3765 if (curagent->max_fpa < 1) {
3766 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3767 file, linenum, args[0]);
3768 err_code |= ERR_ALERT | ERR_FATAL;
3769 goto out;
3770 }
3771 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003772 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003773 int cur_arg;
3774
3775 if (!*args[1]) {
3776 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3777 file, linenum, args[0]);
3778 err_code |= ERR_ALERT | ERR_FATAL;
3779 goto out;
3780 }
3781 cur_arg = 1;
3782 while (*args[cur_arg]) {
3783 struct spoe_var_placeholder *vph;
3784
3785 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3786 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3787 err_code |= ERR_ALERT | ERR_ABORT;
3788 goto out;
3789 }
3790 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003791 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003792 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3793 err_code |= ERR_ALERT | ERR_ABORT;
3794 goto out;
3795 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003796 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003797 cur_arg++;
3798 }
3799 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003800 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003801 char *errmsg = NULL;
3802
Emeric Brun9533a702021-04-02 10:13:43 +02003803 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003804 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3805 err_code |= ERR_ALERT | ERR_FATAL;
3806 goto out;
3807 }
3808 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003809 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003810 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3811 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003812 err_code |= ERR_ALERT | ERR_FATAL;
3813 goto out;
3814 }
3815 out:
3816 return err_code;
3817}
Christopher Faulet11610f32017-09-21 10:23:10 +02003818static int
3819cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3820{
3821 struct spoe_group *grp;
3822 const char *err;
3823 int err_code = 0;
3824
3825 if ((cfg_scope == NULL && curengine != NULL) ||
3826 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003827 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003828 goto out;
3829
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003830 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003831 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003832 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3833 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003834 err_code |= ERR_ALERT | ERR_ABORT;
3835 goto out;
3836 }
3837 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3838 err_code |= ERR_ABORT;
3839 goto out;
3840 }
3841
3842 err = invalid_char(args[1]);
3843 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003844 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3845 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003846 err_code |= ERR_ALERT | ERR_ABORT;
3847 goto out;
3848 }
3849
3850 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003851 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003852 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3853 " name as another one declared at %s:%d.\n",
3854 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003855 err_code |= ERR_ALERT | ERR_FATAL;
3856 goto out;
3857 }
3858 }
3859
3860 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003862 err_code |= ERR_ALERT | ERR_ABORT;
3863 goto out;
3864 }
3865
3866 curgrp->id = strdup(args[1]);
3867 curgrp->conf.file = strdup(file);
3868 curgrp->conf.line = linenum;
3869 LIST_INIT(&curgrp->phs);
3870 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003871 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003872 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003873 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003874 int cur_arg = 1;
3875 while (*args[cur_arg]) {
3876 struct spoe_placeholder *ph = NULL;
3877
3878 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003879 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003880 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3881 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003882 err_code |= ERR_ALERT | ERR_FATAL;
3883 goto out;
3884 }
3885 }
3886
3887 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003888 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003889 err_code |= ERR_ALERT | ERR_ABORT;
3890 goto out;
3891 }
3892 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003893 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003894 cur_arg++;
3895 }
3896 }
3897 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003898 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3899 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003900 err_code |= ERR_ALERT | ERR_FATAL;
3901 goto out;
3902 }
3903 out:
3904 return err_code;
3905}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003906
3907static int
3908cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3909{
3910 struct spoe_message *msg;
3911 struct spoe_arg *arg;
3912 const char *err;
3913 char *errmsg = NULL;
3914 int err_code = 0;
3915
3916 if ((cfg_scope == NULL && curengine != NULL) ||
3917 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003918 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919 goto out;
3920
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003921 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003922 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003923 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3924 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003925 err_code |= ERR_ALERT | ERR_ABORT;
3926 goto out;
3927 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003928 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3929 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003930 goto out;
3931 }
3932
3933 err = invalid_char(args[1]);
3934 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003935 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3936 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003937 err_code |= ERR_ALERT | ERR_ABORT;
3938 goto out;
3939 }
3940
3941 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003942 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003943 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3944 " name as another one declared at %s:%d.\n",
3945 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 err_code |= ERR_ALERT | ERR_FATAL;
3947 goto out;
3948 }
3949 }
3950
3951 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003952 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003953 err_code |= ERR_ALERT | ERR_ABORT;
3954 goto out;
3955 }
3956
3957 curmsg->id = strdup(args[1]);
3958 curmsg->id_len = strlen(curmsg->id);
3959 curmsg->event = SPOE_EV_NONE;
3960 curmsg->conf.file = strdup(file);
3961 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003962 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003963 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003964 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003965 LIST_INIT(&curmsg->by_evt);
3966 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003967 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003968 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003969 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003970 int cur_arg = 1;
3971
3972 curproxy->conf.args.ctx = ARGC_SPOE;
3973 curproxy->conf.args.file = file;
3974 curproxy->conf.args.line = linenum;
3975 while (*args[cur_arg]) {
3976 char *delim = strchr(args[cur_arg], '=');
3977 int idx = 0;
3978
3979 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003980 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 err_code |= ERR_ALERT | ERR_ABORT;
3982 goto out;
3983 }
3984
3985 if (!delim) {
3986 arg->name = NULL;
3987 arg->name_len = 0;
3988 delim = args[cur_arg];
3989 }
3990 else {
3991 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3992 arg->name_len = delim - args[cur_arg];
3993 delim++;
3994 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003995 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3996 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003997 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003998 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003999 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004000 err_code |= ERR_ALERT | ERR_FATAL;
4001 free(arg->name);
4002 free(arg);
4003 goto out;
4004 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004005 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004006 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004007 cur_arg++;
4008 }
4009 curproxy->conf.args.file = NULL;
4010 curproxy->conf.args.line = 0;
4011 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004012 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004013 err = invalid_char(args[1]);
4014 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004015 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4016 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004017 err_code |= ERR_ALERT | ERR_FATAL;
4018 goto out;
4019 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004020 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004021 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004022 "logical disjunction within a condition.\n",
4023 file, linenum, args[1]);
4024 err_code |= ERR_ALERT | ERR_FATAL;
4025 goto out;
4026 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004027 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004028 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4029 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004030 err_code |= ERR_ALERT | ERR_FATAL;
4031 goto out;
4032 }
4033 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004034 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004035 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004036 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004037 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004038 goto out;
4039 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004040 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4041 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004042
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004043 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004044 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004045 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004046 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4047
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004048 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004049 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004050 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004051 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004052 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 curmsg->event = SPOE_EV_ON_TCP_RSP;
4054
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004055 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004056 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004057 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004059 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004060 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4061 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004062 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004063 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004064 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004065 goto out;
4066 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004067
4068 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4069 struct acl_cond *cond;
4070
4071 cond = build_acl_cond(file, linenum, &curmsg->acls,
4072 curproxy, (const char **)args+2,
4073 &errmsg);
4074 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004075 ha_alert("parsing [%s:%d] : error detected while "
4076 "parsing an 'event %s' condition : %s.\n",
4077 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004078 err_code |= ERR_ALERT | ERR_FATAL;
4079 goto out;
4080 }
4081 curmsg->cond = cond;
4082 }
4083 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004084 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4085 "or 'unless' followed by a condition but found '%s'.\n",
4086 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004087 err_code |= ERR_ALERT | ERR_FATAL;
4088 goto out;
4089 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004090 }
4091 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004092 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4093 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004094 err_code |= ERR_ALERT | ERR_FATAL;
4095 goto out;
4096 }
4097 out:
4098 free(errmsg);
4099 return err_code;
4100}
4101
4102/* Return -1 on error, else 0 */
4103static int
4104parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4105 struct flt_conf *fconf, char **err, void *private)
4106{
4107 struct list backup_sections;
4108 struct spoe_config *conf;
4109 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004110 struct spoe_group *grp, *grpback;
4111 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004112 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004113 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004114 char *file = NULL, *engine = NULL;
4115 int ret, pos = *cur_arg + 1;
4116
Christopher Faulet84c844e2018-03-23 14:37:14 +01004117 LIST_INIT(&curmsgs);
4118 LIST_INIT(&curgrps);
4119 LIST_INIT(&curmphs);
4120 LIST_INIT(&curgphs);
4121 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004122 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004123 curpxopts = 0;
4124 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004125
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004126 conf = calloc(1, sizeof(*conf));
4127 if (conf == NULL) {
4128 memprintf(err, "%s: out of memory", args[*cur_arg]);
4129 goto error;
4130 }
4131 conf->proxy = px;
4132
4133 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004134 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004135 if (!*args[pos+1]) {
4136 memprintf(err, "'%s' : '%s' option without value",
4137 args[*cur_arg], args[pos]);
4138 goto error;
4139 }
4140 file = args[pos+1];
4141 pos += 2;
4142 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004143 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004144 if (!*args[pos+1]) {
4145 memprintf(err, "'%s' : '%s' option without value",
4146 args[*cur_arg], args[pos]);
4147 goto error;
4148 }
4149 engine = args[pos+1];
4150 pos += 2;
4151 }
4152 else {
4153 memprintf(err, "unknown keyword '%s'", args[pos]);
4154 goto error;
4155 }
4156 }
4157 if (file == NULL) {
4158 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4159 goto error;
4160 }
4161
4162 /* backup sections and register SPOE sections */
4163 LIST_INIT(&backup_sections);
4164 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004165 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4166 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004167 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004168
4169 /* Parse SPOE filter configuration file */
4170 curengine = engine;
4171 curproxy = px;
4172 curagent = NULL;
4173 curmsg = NULL;
4174 ret = readcfgfile(file);
4175 curproxy = NULL;
4176
4177 /* unregister SPOE sections and restore previous sections */
4178 cfg_unregister_sections();
4179 cfg_restore_sections(&backup_sections);
4180
4181 if (ret == -1) {
4182 memprintf(err, "Could not open configuration file %s : %s",
4183 file, strerror(errno));
4184 goto error;
4185 }
4186 if (ret & (ERR_ABORT|ERR_FATAL)) {
4187 memprintf(err, "Error(s) found in configuration file %s", file);
4188 goto error;
4189 }
4190
4191 /* Check SPOE agent */
4192 if (curagent == NULL) {
4193 memprintf(err, "No SPOE agent found in file %s", file);
4194 goto error;
4195 }
4196 if (curagent->b.name == NULL) {
4197 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4198 curagent->id, curagent->conf.file, curagent->conf.line);
4199 goto error;
4200 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004201 if (curagent->timeout.hello == TICK_ETERNITY ||
4202 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004203 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004204 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4205 " | While not properly invalid, you will certainly encounter various problems\n"
4206 " | with such a configuration. To fix this, please ensure that all following\n"
4207 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4208 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004209 }
4210 if (curagent->var_pfx == NULL) {
4211 char *tmp = curagent->id;
4212
4213 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004214 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004215 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4216 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4217 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4218 goto error;
4219 }
4220 tmp++;
4221 }
4222 curagent->var_pfx = strdup(curagent->id);
4223 }
4224
Christopher Fauletb7426d12018-03-21 14:12:17 +01004225 if (curagent->var_on_error) {
4226 struct arg arg;
4227
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004228 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004229 curagent->var_pfx, curagent->var_on_error);
4230
4231 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004232 arg.data.str.area = trash.area;
4233 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004234 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004235 if (!vars_check_arg(&arg, err)) {
4236 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4237 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4238 goto error;
4239 }
4240 }
4241
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004242 if (curagent->var_t_process) {
4243 struct arg arg;
4244
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004245 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004246 curagent->var_pfx, curagent->var_t_process);
4247
4248 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004249 arg.data.str.area = trash.area;
4250 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004251 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004252 if (!vars_check_arg(&arg, err)) {
4253 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4254 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4255 goto error;
4256 }
4257 }
4258
4259 if (curagent->var_t_total) {
4260 struct arg arg;
4261
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004262 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004263 curagent->var_pfx, curagent->var_t_total);
4264
4265 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004266 arg.data.str.area = trash.area;
4267 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004268 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004269 if (!vars_check_arg(&arg, err)) {
4270 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4271 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4272 goto error;
4273 }
4274 }
4275
Christopher Faulet11610f32017-09-21 10:23:10 +02004276 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004277 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4278 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004279 goto finish;
4280 }
4281
Christopher Faulet11610f32017-09-21 10:23:10 +02004282 /* Replace placeholders by the corresponding messages for the SPOE
4283 * agent */
4284 list_for_each_entry(ph, &curmphs, list) {
4285 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004286 struct spoe_arg *arg;
4287 unsigned int where;
4288
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004289 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004290 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4291 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4292 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4293 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4294 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4295 }
4296 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4297 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4298 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004299 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4300 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004301 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004302 }
4303 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004304 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4305 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004306 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004307 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004308
4309 where = 0;
4310 switch (msg->event) {
4311 case SPOE_EV_ON_CLIENT_SESS:
4312 where |= SMP_VAL_FE_CON_ACC;
4313 break;
4314
4315 case SPOE_EV_ON_TCP_REQ_FE:
4316 where |= SMP_VAL_FE_REQ_CNT;
4317 break;
4318
4319 case SPOE_EV_ON_HTTP_REQ_FE:
4320 where |= SMP_VAL_FE_HRQ_HDR;
4321 break;
4322
4323 case SPOE_EV_ON_TCP_REQ_BE:
4324 if (px->cap & PR_CAP_FE)
4325 where |= SMP_VAL_FE_REQ_CNT;
4326 if (px->cap & PR_CAP_BE)
4327 where |= SMP_VAL_BE_REQ_CNT;
4328 break;
4329
4330 case SPOE_EV_ON_HTTP_REQ_BE:
4331 if (px->cap & PR_CAP_FE)
4332 where |= SMP_VAL_FE_HRQ_HDR;
4333 if (px->cap & PR_CAP_BE)
4334 where |= SMP_VAL_BE_HRQ_HDR;
4335 break;
4336
4337 case SPOE_EV_ON_SERVER_SESS:
4338 where |= SMP_VAL_BE_SRV_CON;
4339 break;
4340
4341 case SPOE_EV_ON_TCP_RSP:
4342 if (px->cap & PR_CAP_FE)
4343 where |= SMP_VAL_FE_RES_CNT;
4344 if (px->cap & PR_CAP_BE)
4345 where |= SMP_VAL_BE_RES_CNT;
4346 break;
4347
4348 case SPOE_EV_ON_HTTP_RSP:
4349 if (px->cap & PR_CAP_FE)
4350 where |= SMP_VAL_FE_HRS_HDR;
4351 if (px->cap & PR_CAP_BE)
4352 where |= SMP_VAL_BE_HRS_HDR;
4353 break;
4354
4355 default:
4356 break;
4357 }
4358
4359 list_for_each_entry(arg, &msg->args, list) {
4360 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004361 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004362 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004363 "none of which is available here ('%s')",
4364 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004365 sample_ckp_names(arg->expr->fetch->use),
4366 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004367 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004368 }
4369 }
4370
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004371 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004372 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004373 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004374 }
4375 }
4376 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004377 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004378 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004379 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004380 continue;
4381 }
4382
Christopher Faulet11610f32017-09-21 10:23:10 +02004383 /* Replace placeholders by the corresponding groups for the SPOE
4384 * agent */
4385 list_for_each_entry(ph, &curgphs, list) {
4386 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004387 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004388 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004389 LIST_DELETE(&grp->list);
4390 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004391 goto next_aph;
4392 }
4393 }
4394 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4395 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4396 goto error;
4397 next_aph:
4398 continue;
4399 }
4400
4401 /* Replace placeholders by the corresponding message for each SPOE
4402 * group of the SPOE agent */
4403 list_for_each_entry(grp, &curagent->groups, list) {
4404 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4405 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004406 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004407 if (msg->group != NULL) {
4408 memprintf(err, "SPOE message '%s' already belongs to "
4409 "the SPOE group '%s' declare at %s:%d",
4410 msg->id, msg->group->id,
4411 msg->group->conf.file,
4412 msg->group->conf.line);
4413 goto error;
4414 }
4415
4416 /* Scope for arguments are not checked for now. We will check
4417 * them only if a rule use the corresponding SPOE group. */
4418 msg->agent = curagent;
4419 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004420 LIST_DELETE(&ph->list);
4421 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004422 goto next_mph_grp;
4423 }
4424 }
4425 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4426 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4427 goto error;
4428 next_mph_grp:
4429 continue;
4430 }
4431 }
4432
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004433 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004434 /* move curmsgs to the agent message list */
4435 curmsgs.n->p = &curagent->messages;
4436 curmsgs.p->n = &curagent->messages;
4437 curagent->messages = curmsgs;
4438 LIST_INIT(&curmsgs);
4439
Christopher Faulet7ee86672017-09-19 11:08:28 +02004440 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004441 conf->agent = curagent;
Christopher Faulet541e2612021-08-02 17:51:01 +02004442 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004443
4444 /* Start agent's proxy initialization here. It will be finished during
4445 * the filter init. */
4446 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4447 init_new_proxy(&conf->agent_fe);
4448 conf->agent_fe.id = conf->agent->id;
4449 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004450 conf->agent_fe.options |= curpxopts;
4451 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004452
4453 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004454 LIST_DELETE(&logsrv->list);
4455 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004456 }
4457
Christopher Faulet11610f32017-09-21 10:23:10 +02004458 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004459 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004460 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004461 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004462 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004463 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004464 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004465 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004466 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4467 struct arg arg;
4468
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004469 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004470 curagent->var_pfx, vph->name);
4471
4472 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004473 arg.data.str.area = trash.area;
4474 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004475 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004476 if (!vars_check_arg(&arg, err)) {
4477 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4478 curagent->id, curagent->var_pfx, vph->name, *err);
4479 goto error;
4480 }
4481
Willy Tarreau2b718102021-04-21 07:32:39 +02004482 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004483 free(vph->name);
4484 free(vph);
4485 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004486 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004487 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004488 spoe_release_group(grp);
4489 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004490 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004491 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004492 fconf->ops = &spoe_ops;
4493 fconf->conf = conf;
4494 return 0;
4495
4496 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004497 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004498 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004499 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004500 spoe_release_placeholder(ph);
4501 }
4502 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004503 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004504 spoe_release_placeholder(ph);
4505 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004506 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004507 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004508 free(vph->name);
4509 free(vph);
4510 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004511 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004512 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004513 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004514 }
4515 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004516 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004517 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004518 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004519 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004520 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004521 free(logsrv);
4522 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004523 free(conf);
4524 return -1;
4525}
4526
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004527/* Send message of a SPOE group. This is the action_ptr callback of a rule
4528 * associated to a "send-spoe-group" action.
4529 *
Christopher Faulet13403762019-12-13 09:01:57 +01004530 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4531 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004532static enum act_return
4533spoe_send_group(struct act_rule *rule, struct proxy *px,
4534 struct session *sess, struct stream *s, int flags)
4535{
4536 struct filter *filter;
4537 struct spoe_agent *agent = NULL;
4538 struct spoe_group *group = NULL;
4539 struct spoe_context *ctx = NULL;
4540 int ret, dir;
4541
4542 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4543 if (filter->config == rule->arg.act.p[0]) {
4544 agent = rule->arg.act.p[2];
4545 group = rule->arg.act.p[3];
4546 ctx = filter->ctx;
4547 break;
4548 }
4549 }
4550 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004551 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004552 if (ctx->state == SPOE_CTX_ST_NONE)
4553 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004554
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004555 switch (rule->from) {
4556 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4557 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4558 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4559 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4560 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4561 default:
4562 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4563 " - internal error while execute spoe-send-group\n",
4564 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4565 __FUNCTION__, s);
4566 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4567 agent->id);
4568 return ACT_RET_CONT;
4569 }
4570
4571 ret = spoe_process_group(s, ctx, group, dir);
4572 if (ret == 1)
4573 return ACT_RET_CONT;
4574 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004575 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004576 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4577 " - failed to process group '%s': interrupted by caller\n",
4578 (int)now.tv_sec, (int)now.tv_usec,
4579 agent->id, __FUNCTION__, s, group->id);
4580 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004581 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004582 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004583 return ACT_RET_CONT;
4584 }
4585 return ACT_RET_YIELD;
4586 }
4587 else
Christopher Faulet13403762019-12-13 09:01:57 +01004588 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004589}
4590
4591/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4592 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4593 * action should be:
4594 *
4595 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4596 *
4597 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4598 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4599 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4600 * group.
4601 *
4602 * The function returns 1 in success case, otherwise, it returns 0 and err is
4603 * filled.
4604 */
4605static int
4606check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4607{
4608 struct flt_conf *fconf;
4609 struct spoe_config *conf;
4610 struct spoe_agent *agent = NULL;
4611 struct spoe_group *group;
4612 struct spoe_message *msg;
4613 char *engine_id = rule->arg.act.p[0];
4614 char *group_id = rule->arg.act.p[1];
4615 unsigned int where = 0;
4616
4617 switch (rule->from) {
4618 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4619 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4620 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4621 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4622 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4623 default:
4624 memprintf(err,
4625 "internal error, unexpected rule->from=%d, please report this bug!",
4626 rule->from);
4627 goto error;
4628 }
4629
4630 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4631 * <px> */
4632 list_for_each_entry(fconf, &px->filter_configs, list) {
4633 conf = fconf->conf;
4634
4635 /* This is not an SPOE filter */
4636 if (fconf->id != spoe_filter_id)
4637 continue;
4638
4639 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004640 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004641 agent = conf->agent;
4642 break;
4643 }
4644 }
4645 if (agent == NULL) {
4646 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4647 engine_id, group_id);
4648 goto error;
4649 }
4650
4651 /* Try to find the right group */
4652 list_for_each_entry(group, &agent->groups, list) {
4653 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004654 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004655 break;
4656 }
4657 if (&group->list == &agent->groups) {
4658 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4659 group_id, engine_id);
4660 goto error;
4661 }
4662
4663 /* Ok, we found the group, we need to check messages and their
4664 * arguments */
4665 list_for_each_entry(msg, &group->messages, by_grp) {
4666 struct spoe_arg *arg;
4667
4668 list_for_each_entry(arg, &msg->args, list) {
4669 if (!(arg->expr->fetch->val & where)) {
4670 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4671 "some args extract information from '%s',"
4672 "none of which is available here ('%s')",
4673 msg->id, group->id, msg->conf.file, msg->conf.line,
4674 sample_ckp_names(arg->expr->fetch->use),
4675 sample_ckp_names(where));
4676 goto error;
4677 }
4678 }
4679 }
4680
4681 free(engine_id);
4682 free(group_id);
4683 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4684 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4685 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4686 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4687 return 1;
4688
4689 error:
4690 free(engine_id);
4691 free(group_id);
4692 return 0;
4693}
4694
4695/* Parse 'send-spoe-group' action following the format:
4696 *
4697 * ... send-spoe-group <engine-id> <group-id>
4698 *
4699 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4700 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4701 * ids are saved and used later, when the rule will be checked.
4702 */
4703static enum act_parse_ret
4704parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4705 struct act_rule *rule, char **err)
4706{
4707 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4708 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4709 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4710 return ACT_RET_PRS_ERR;
4711 }
4712 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4713 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4714
4715 (*orig_arg) += 2;
4716
4717 rule->action = ACT_CUSTOM;
4718 rule->action_ptr = spoe_send_group;
4719 rule->check_ptr = check_send_spoe_group;
4720 return ACT_RET_PRS_OK;
4721}
4722
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004723
4724/* Declare the filter parser for "spoe" keyword */
4725static struct flt_kw_list flt_kws = { "SPOE", { }, {
4726 { "spoe", parse_spoe_flt, NULL },
4727 { NULL, NULL, NULL },
4728 }
4729};
4730
Willy Tarreau0108d902018-11-25 19:14:37 +01004731INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4732
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004733/* Delcate the action parser for "spoe-action" keyword */
4734static struct action_kw_list tcp_req_action_kws = { { }, {
4735 { "send-spoe-group", parse_send_spoe_group },
4736 { /* END */ },
4737 }
4738};
Willy Tarreau0108d902018-11-25 19:14:37 +01004739
4740INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4741
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004742static struct action_kw_list tcp_res_action_kws = { { }, {
4743 { "send-spoe-group", parse_send_spoe_group },
4744 { /* END */ },
4745 }
4746};
Willy Tarreau0108d902018-11-25 19:14:37 +01004747
4748INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4749
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004750static struct action_kw_list http_req_action_kws = { { }, {
4751 { "send-spoe-group", parse_send_spoe_group },
4752 { /* END */ },
4753 }
4754};
Willy Tarreau0108d902018-11-25 19:14:37 +01004755
4756INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4757
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004758static struct action_kw_list http_res_action_kws = { { }, {
4759 { "send-spoe-group", parse_send_spoe_group },
4760 { /* END */ },
4761 }
4762};
4763
Willy Tarreau0108d902018-11-25 19:14:37 +01004764INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);