blob: 92a5af425a039d2a35fd31187c071d269fa0cce7 [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 Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020040
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041
42#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
43#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010044#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#else
46#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010047#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020048#endif
49
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010050/* Reserved 4 bytes to the frame size. So a frame and its size can be written
51 * together in a buffer */
52#define MAX_FRAME_SIZE global.tune.bufsize - 4
53
54/* The minimum size for a frame */
55#define MIN_FRAME_SIZE 256
56
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010057/* Reserved for the metadata and the frame type.
58 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010059#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020060
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010061/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010062#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
63
Christopher Faulet3b386a32017-02-23 10:17:15 +010064/* SPOE filter id. Used to identify SPOE filters */
65const char *spoe_filter_id = "SPOE filter";
66
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067/* Set if the handle on SIGUSR1 is registered */
68static int sighandler_registered = 0;
69
70/* proxy used during the parsing */
71struct proxy *curproxy = NULL;
72
73/* The name of the SPOE engine, used during the parsing */
74char *curengine = NULL;
75
76/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020077/* SPOE agent/group/message used during the parsing */
78struct spoe_agent *curagent = NULL;
79struct spoe_group *curgrp = NULL;
80struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020081
82/* list of SPOE messages and placeholders used during the parsing */
83struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020084struct list curgrps;
85struct list curmphs;
86struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010087struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
Christopher Faulet7250b8f2018-03-26 17:19:01 +020089/* list of log servers used during the parsing */
90struct list curlogsrvs;
91
Christopher Faulet0e0f0852018-03-26 17:20:36 +020092/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
93int curpxopts;
94int curpxopts2;
95
Christopher Faulet42bfa462017-01-04 14:14:19 +010096/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +010097DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
98DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020099
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100struct flt_ops spoe_ops;
101
Christopher Faulet8ef75252017-02-20 22:56:03 +0100102static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200103static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
104static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105
106/********************************************************************
107 * helper functions/globals
108 ********************************************************************/
109static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200110spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111{
Christopher Faulet11610f32017-09-21 10:23:10 +0200112 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200114 free(ph->id);
115 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116}
117
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100119spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120{
Christopher Faulet57583e42017-09-04 15:41:09 +0200121 struct spoe_arg *arg, *argback;
122 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123
124 if (!msg)
125 return;
126 free(msg->id);
127 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200129 release_sample_expr(arg->expr);
130 free(arg->name);
131 LIST_DEL(&arg->list);
132 free(arg);
133 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200134 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
135 LIST_DEL(&acl->list);
136 prune_acl(acl);
137 free(acl);
138 }
139 if (msg->cond) {
140 prune_acl_cond(msg->cond);
141 free(msg->cond);
142 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200143 free(msg);
144}
145
146static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200147spoe_release_group(struct spoe_group *grp)
148{
149 if (!grp)
150 return;
151 free(grp->id);
152 free(grp->conf.file);
153 free(grp);
154}
155
156static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100157spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200158{
Christopher Faulet11610f32017-09-21 10:23:10 +0200159 struct spoe_message *msg, *msgback;
160 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200161 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200162
163 if (!agent)
164 return;
165 free(agent->id);
166 free(agent->conf.file);
167 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100168 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100169 free(agent->var_t_process);
170 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200171 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
172 LIST_DEL(&msg->list);
173 spoe_release_message(msg);
174 }
175 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
176 LIST_DEL(&grp->list);
177 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200178 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100179 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200180 for (i = 0; i < global.nbthread; ++i) {
181 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100182 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200183 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200185 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200186 free(agent);
187}
188
189static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100190 [SPOE_FRM_ERR_NONE] = "normal",
191 [SPOE_FRM_ERR_IO] = "I/O error",
192 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
193 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
194 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
195 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
196 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
197 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
198 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
199 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
200 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
201 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100202 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100203 [SPOE_FRM_ERR_RES] = "resource allocation error",
204 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200205};
206
207static const char *spoe_event_str[SPOE_EV_EVENTS] = {
208 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
209 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
210 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
211 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
212 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
213
214 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
215 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
216 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
217};
218
219
220#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
221
222static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100223 [SPOE_CTX_ST_NONE] = "NONE",
224 [SPOE_CTX_ST_READY] = "READY",
225 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
226 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
227 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
228 [SPOE_CTX_ST_DONE] = "DONE",
229 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200230};
231
232static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100233 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
234 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
235 [SPOE_APPCTX_ST_IDLE] = "IDLE",
236 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
237 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
238 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
239 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
240 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
241 [SPOE_APPCTX_ST_EXIT] = "EXIT",
242 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200243};
244
245#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100246
Christopher Faulet8ef75252017-02-20 22:56:03 +0100247/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500248 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100250static char *
251generate_pseudo_uuid()
252{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100253 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800254 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100255}
256
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100257
258static inline void
259spoe_update_stat_time(struct timeval *tv, long *t)
260{
261 if (*t == -1)
262 *t = tv_ms_elapsed(tv, &now);
263 else
264 *t += tv_ms_elapsed(tv, &now);
265 tv_zero(tv);
266}
267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200268/********************************************************************
269 * Functions that encode/decode SPOE frames
270 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/* Helper to get static string length, excluding the terminating null byte */
272#define SLEN(str) (sizeof(str)-1)
273
274/* Predefined key used in HELLO/DISCONNECT frames */
275#define SUPPORTED_VERSIONS_KEY "supported-versions"
276#define VERSION_KEY "version"
277#define MAX_FRAME_SIZE_KEY "max-frame-size"
278#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100280#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281#define STATUS_CODE_KEY "status-code"
282#define MSG_KEY "message"
283
284struct spoe_version {
285 char *str;
286 int min;
287 int max;
288};
289
290/* All supported versions */
291static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200292 /* 1.0 is now unsupported because of a bug about frame's flags*/
293 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200294 {NULL, 0, 0}
295};
296
297/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200298#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299
Christopher Faulet8ef75252017-02-20 22:56:03 +0100300/* Convert a string to a SPOE version value. The string must follow the format
301 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
302 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200303static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100304spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306 const char *p, *end;
307 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309 p = str;
310 end = str+len;
311 maj = min = 0;
312 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200313
Christopher Faulet8ef75252017-02-20 22:56:03 +0100314 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100315 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200317
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 /* parse Major number, until the '.' */
319 while (*p != '.') {
320 if (p >= end || *p < '0' || *p > '9')
321 goto out;
322 maj *= 10;
323 maj += (*p - '0');
324 p++;
325 }
326
327 /* check Major version */
328 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329 goto out;
330
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331 p++; /* skip the '.' */
332 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
333 goto out;
334
335 /* Parse Minor number */
336 while (p < end) {
337 if (*p < '0' || *p > '9')
338 break;
339 min *= 10;
340 min += (*p - '0');
341 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200342 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100343
344 /* check Minor number */
345 if (min > 999)
346 goto out;
347
348 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100349 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100350 p++;
351 if (p != end)
352 goto out;
353
354 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200355 out:
356 return vsn;
357}
358
Christopher Faulet8ef75252017-02-20 22:56:03 +0100359/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800360 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364{
Willy Tarreau83061a82018-07-13 11:56:34 +0200365 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100366 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367 char *p, *end;
368 unsigned int flags = SPOE_FRM_FL_FIN;
369 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200370
Christopher Faulet8ef75252017-02-20 22:56:03 +0100371 p = frame;
372 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200373
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374 /* Set Frame type */
375 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200376
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100377 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200378 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379 memcpy(p, (char *)&flags, 4);
380 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381
382 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
386 * and "capabilities" */
387
388 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100389 sz = SLEN(SUPPORTED_VERSIONS_KEY);
390 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
391 goto too_big;
392
393 *p++ = SPOE_DATA_T_STR;
394 sz = SLEN(SUPPORTED_VERSIONS_VAL);
395 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
396 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
398 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 sz = SLEN(MAX_FRAME_SIZE_KEY);
400 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
401 goto too_big;
402
403 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200404 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100405 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200406
407 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 sz = SLEN(CAPABILITIES_KEY);
409 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100413 chk = get_trash_chunk();
414 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200415 memcpy(chk->area, "pipelining", 10);
416 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100417 }
418 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 if (chk->data) chk->area[chk->data++] = ',';
420 memcpy(chk->area+chk->data, "async", 5);
421 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100422 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100423 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 if (chk->data) chk->area[chk->data++] = ',';
425 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100426 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100427 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200428 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100429 goto too_big;
430
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500431 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200432 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100433 sz = SLEN(ENGINE_ID_KEY);
434 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
435 goto too_big;
436
437 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200438 sz = strlen(agent->rt[tid].engine_id);
439 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100440 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100441 }
442
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 return (p - frame);
444
445 too_big:
446 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
447 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200448}
449
Christopher Faulet8ef75252017-02-20 22:56:03 +0100450/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
451 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
452 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100454spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200455{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456 const char *reason;
457 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100458 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200460
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 p = frame;
462 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 /* Set Frame type */
465 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200466
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100467 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200468 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 memcpy(p, (char *)&flags, 4);
470 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
472 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 *p++ = 0; *p++ = 0;
474
475 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
476 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477
478 /* There are 2 mandatory items: "status-code" and "message" */
479
480 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481 sz = SLEN(STATUS_CODE_KEY);
482 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
483 goto too_big;
484
485 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200486 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488
489 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 sz = SLEN(MSG_KEY);
491 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 /*Get the message corresponding to the status code */
495 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
496
497 *p++ = SPOE_DATA_T_STR;
498 sz = strlen(reason);
499 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
500 goto too_big;
501
502 return (p - frame);
503
504 too_big:
505 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
506 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200507}
508
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
510 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
511 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100514 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 char *p, *end;
517 unsigned int stream_id, frame_id;
518 unsigned int flags = SPOE_FRM_FL_FIN;
519 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 p = frame;
522 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200523
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100524 stream_id = ctx->stream_id;
525 frame_id = ctx->frame_id;
526
527 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
528 /* The fragmentation is not supported by the applet */
529 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
530 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
531 return -1;
532 }
533 flags = ctx->frag_ctx.flags;
534 }
535
536 /* Set Frame type */
537 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
538
539 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200540 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100541 memcpy(p, (char *)&flags, 4);
542 p += 4;
543
544 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200545 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200547 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 goto too_big;
549
550 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200551 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100552 if (p + sz >= end)
553 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200554 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100555 p += sz;
556
557 return (p - frame);
558
559 too_big:
560 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
561 return 0;
562}
563
564/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
565 * returns the number of encoded bytes in the frame on success, 0 if an encoding
566 * error occurred and -1 if a fatal error occurred. */
567static int
568spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
569 char *frame, size_t size)
570{
571 char *p, *end;
572 unsigned int stream_id, frame_id;
573 unsigned int flags;
574 size_t sz;
575
576 p = frame;
577 end = frame+size;
578
Christopher Faulet8ef75252017-02-20 22:56:03 +0100579 /* <ctx> is null when the stream has aborted the processing of a
580 * fragmented frame. In this case, we must notify the corresponding
581 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100582 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100583 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100584 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
585 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
586 }
587 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100589 stream_id = ctx->stream_id;
590 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100591 }
592
Christopher Faulet8ef75252017-02-20 22:56:03 +0100593 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100594 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100595
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200597 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 memcpy(p, (char *)&flags, 4);
599 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200600
601 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200602 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200604 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200606
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100607 if (ctx == NULL)
608 goto end;
609
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200611 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 if (p + sz >= end)
613 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200614 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100616
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100617 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 return (p - frame);
619
620 too_big:
621 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
622 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200623}
624
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625/* Decode and process the HELLO frame sent by an agent. It returns the number of
626 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
627 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630{
Christopher Faulet305c6072017-02-23 16:17:53 +0100631 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
632 char *p, *end;
633 int vsn, max_frame_size;
634 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635
636 p = frame;
637 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200638
639 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200642 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (size < 7 /* TYPE + METADATA */) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
647 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648 }
649
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100650 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200652 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 /* Fragmentation is not supported for HELLO frame */
656 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100658 return -1;
659 }
660
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 if (*p != 0 || *(p+1) != 0) {
663 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
664 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200665 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100666 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667
668 /* There are 3 mandatory items: "version", "max-frame-size" and
669 * "capabilities" */
670
671 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100672 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 while (p < end) {
674 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200675 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677
678 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100679 ret = spoe_decode_buffer(&p, end, &str, &sz);
680 if (ret == -1 || !sz) {
681 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
682 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200686 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687 int i, type = *p++;
688
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
691 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
692 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
695 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
696 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 }
698
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 return -1;
703 }
704 for (i = 0; supported_versions[i].str != NULL; ++i) {
705 if (vsn >= supported_versions[i].min &&
706 vsn <= supported_versions[i].max)
707 break;
708 }
709 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200711 return -1;
712 }
713 }
714 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200715 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 +0100716 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717
718 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
720 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
721 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
722 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
724 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200725 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200726 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
728 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 if (sz < MIN_FRAME_SIZE ||
731 sz > SPOE_APPCTX(appctx)->max_frame_size) {
732 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200733 return -1;
734 }
735 max_frame_size = sz;
736 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100737 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200738 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100740
741 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
743 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
744 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100745 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
748 return 0;
749 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752 char *delim;
753
754 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100755 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100756
Christopher Faulet8ef75252017-02-20 22:56:03 +0100757 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
758 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100759 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760 flags |= SPOE_APPCTX_FL_PIPELINING;
761 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 else if (sz >= 5 && !strncmp(str, "async", 5)) {
763 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100764 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765 flags |= SPOE_APPCTX_FL_ASYNC;
766 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
768 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100769 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100770 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
771 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773 /* Get the next comma or break */
774 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 delim++;
777 sz -= (delim - str);
778 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779 }
780 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200781 else {
782 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 if (spoe_skip_data(&p, end) == -1) {
784 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
785 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200787 }
788 }
789
790 /* Final checks */
791 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100792 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200793 return -1;
794 }
795 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100796 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 return -1;
798 }
Christopher Faulet11389012019-02-07 16:13:26 +0100799 if (!agent)
800 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
801 else {
802 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
803 flags &= ~SPOE_APPCTX_FL_PIPELINING;
804 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
805 flags &= ~SPOE_APPCTX_FL_ASYNC;
806 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200807
Christopher Faulet42bfa462017-01-04 14:14:19 +0100808 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
809 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
810 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811
812 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813}
814
815/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
816 * bytes on success, 0 if the frame can be ignored and -1 if an error
817 * occurred. */
818static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100819spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100822 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823
824 p = frame;
825 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826
827 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 if (size < 7 /* TYPE + METADATA */) {
834 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
835 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836 }
837
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100838 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200840 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 /* Fragmentation is not supported for DISCONNECT frame */
844 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100846 return -1;
847 }
848
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 if (*p != 0 || *(p+1) != 0) {
851 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
852 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
856 /* There are 2 mandatory items: "status-code" and "message" */
857
858 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 while (p < end) {
860 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200861 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863
864 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 ret = spoe_decode_buffer(&p, end, &str, &sz);
866 if (ret == -1 || !sz) {
867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
868 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 }
870
871 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200872 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874
875 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
877 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
878 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
879 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
881 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200883 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
885 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 }
889
890 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200891 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 int type = *p++;
893
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200894 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
896 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
897 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100899 ret = spoe_decode_buffer(&p, end, &str, &sz);
900 if (ret == -1 || sz > 255) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
905 SPOE_APPCTX(appctx)->reason = str;
906 SPOE_APPCTX(appctx)->rlen = sz;
907#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
909 else {
910 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911 if (spoe_skip_data(&p, end) == -1) {
912 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
913 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
916 }
917
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919}
920
921
Christopher Fauleta1cda022016-12-21 08:58:06 +0100922/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 * success, 0 if the frame can be ignored and -1 if an error occurred. */
924static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100926 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100928 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100929 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100930 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933
934 p = frame;
935 end = frame + size;
936 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937
938 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
940 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (size < 7 /* TYPE + METADATA */) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
946 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947 }
948
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100949 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200951 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Fragmentation is not supported for now */
955 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 return -1;
958 }
959
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200961 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100963 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200965 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200967 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100969
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100971 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200972 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100973 if ((*ctx)->stream_id == (unsigned int)stream_id &&
974 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100975 goto found;
976 }
977 }
978 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100979 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
980 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100982 goto found;
983 }
984 }
985
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100986 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
987 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
988 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
989
990 /* ABRT bit is set for an unfinished fragmented frame */
991 if (flags & SPOE_FRM_FL_ABRT) {
992 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100993 (*ctx)->state = SPOE_CTX_ST_ERROR;
994 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
995 /* Ignore the payload */
996 goto end;
997 }
998 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
999 /* For now, we ignore the ack */
1000 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1001 return 0;
1002 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001003
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001005 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1006 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001007 " - stream-id=%u - frame-id=%u\n",
1008 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1009 __FUNCTION__, appctx,
1010 (unsigned int)stream_id, (unsigned int)frame_id);
1011
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001012 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001013 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1014 /* Report an error if we are waiting the ack for another frame,
1015 * but not if there is no longer frame waiting for a ack
1016 * (timeout)
1017 */
1018 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1019 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1020 return -1;
1021 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1022 SPOE_APPCTX(appctx)->cur_fpa = 0;
1023 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 return 0;
1025
1026 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1028 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001030 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001032
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001033 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001034 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001035 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1036 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001038
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001039 /* Transfer the buffer ownership to the SPOE context */
1040 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001041 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001042
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 (*ctx)->state = SPOE_CTX_ST_DONE;
1044
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001045 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 " - ACK frame received"
1048 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001049 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1051 (*ctx)->frame_id, flags);
1052 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001053}
1054
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055/* This function is used in cfgparse.c and declared in proto/checks.h. It
1056 * prepare the request to send to agents during a healthcheck. It returns 0 on
1057 * success and -1 if an error occurred. */
1058int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 struct appctx appctx;
1062 struct spoe_appctx spoe_appctx;
1063 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1064 size_t sz;
1065 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001066
Christopher Faulet42bfa462017-01-04 14:14:19 +01001067 memset(&appctx, 0, sizeof(appctx));
1068 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001070
1071 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001072 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1075 end = frame + MAX_FRAME_SIZE;
1076
1077 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1078 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001079 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 /* Add "healthcheck" K/V item */
1083 sz = SLEN(HEALTHCHECK_KEY);
1084 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1085 return -1;
1086 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087
Christopher Faulet8ef75252017-02-20 22:56:03 +01001088 *len = frame - buf;
1089 sz = htonl(*len - 4);
1090 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001091
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return 0;
1096}
1097
1098/* This function is used in checks.c and declared in proto/checks.h. It decode
1099 * the response received from an agent during a healthcheck. It returns 0 on
1100 * success and -1 if an error occurred. */
1101int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001104 struct appctx appctx;
1105 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106
Christopher Faulet42bfa462017-01-04 14:14:19 +01001107 memset(&appctx, 0, sizeof(appctx));
1108 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109
Christopher Faulet42bfa462017-01-04 14:14:19 +01001110 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001111 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1114 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115 goto error;
1116 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1118 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001119
1120 return 0;
1121
1122 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001123 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1124 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1125 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126 return -1;
1127}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001128
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001130 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131 * success. */
1132static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134{
1135 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 int ret;
1137 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1140 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141 netint = htonl(framesz);
1142 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001143 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001144 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001145 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001146 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 }
1149 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 return -1; /* error */
1151 }
1152 return framesz;
1153}
1154
1155/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1156 * when the frame can be ignored, 1 to retry later and the frame length on
1157 * success. */
1158static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001160{
1161 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001162 int ret;
1163 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164
Willy Tarreau06d80a92017-10-19 14:32:15 +02001165 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166 if (ret > 0) {
1167 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001168 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001169 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170 return -1;
1171 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001172 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 }
1174 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 }
1178 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return -1; /* error */
1180 }
1181 return framesz;
1182}
1183
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001184/********************************************************************
1185 * Functions that manage the SPOE applet
1186 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001187static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001189{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001190 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001191 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001192 appctx_wakeup(appctx);
1193 return 1;
1194}
1195
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001196/* Callback function that catches applet timeouts. If a timeout occurred, we set
1197 * <appctx->st1> flag and the SPOE applet is woken up. */
1198static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001199spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001200{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001201 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202
1203 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1204 if (tick_is_expired(task->expire, now_ms)) {
1205 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001206 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209 return task;
1210}
1211
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212/* Callback function that releases a SPOE applet. This happens when the
1213 * connection with the agent is closed. */
1214static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 struct stream_interface *si = appctx->owner;
1218 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1219 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221
1222 if (spoe_appctx == NULL)
1223 return;
1224
1225 appctx->ctx.spoe.ptr = NULL;
1226 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001227
1228 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1229 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1230 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001231
Christopher Faulet8ef75252017-02-20 22:56:03 +01001232 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001233 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001234 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1236 LIST_DEL(&spoe_appctx->list);
1237 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001238 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001239 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001243 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1244 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001245 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001246 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001249 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1250 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001251
1252 si_shutw(si);
1253 si_shutr(si);
1254 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001255 }
1256
Christopher Faulet8ef75252017-02-20 22:56:03 +01001257 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001258 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001259
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 /* Notify all waiting streams */
1261 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001262 LIST_DEL(&ctx->list);
1263 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001264 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001265 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001266 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001267 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001269 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001270 }
1271
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 /* If the applet was processing a fragmented frame, notify the
1273 * corresponding stream. */
1274 if (spoe_appctx->frag_ctx.ctx) {
1275 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001276 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001277 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001279 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1280 }
1281
Christopher Fauletcf181c72020-11-10 18:45:34 +01001282 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1283 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1284 if (ctx->spoe_appctx == spoe_appctx)
1285 ctx->spoe_appctx = NULL;
1286 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001287 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001288 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001289
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001291 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292 LIST_DEL(&ctx->list);
1293 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001294 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001295 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001296 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001301 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001302 LIST_DEL(&ctx->list);
1303 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001304 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001305 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001306 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001308 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1310 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001311
1312 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001313 /* Release allocated memory */
1314 spoe_release_buffer(&spoe_appctx->buffer,
1315 &spoe_appctx->buffer_wait);
1316 pool_free(pool_head_spoe_appctx, spoe_appctx);
1317
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001318 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001319 agent->rt[tid].frame_size = agent->max_frame_size;
1320 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1321 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001322}
1323
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001324static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001325spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001326{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001328 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001329 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001330 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001331
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001332 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1333 /* closed */
1334 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1335 goto exit;
1336 }
1337
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001338 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001339 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001340 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1342 goto stop;
1343 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001344
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001346 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1347 " - Connection timed out\n",
1348 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1349 __FUNCTION__, appctx);
1350 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001351 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001352 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001353
Christopher Faulet42bfa462017-01-04 14:14:19 +01001354 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355 SPOE_APPCTX(appctx)->task->expire =
1356 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001357
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1359 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001360 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 ret = spoe_prepare_hahello_frame(appctx, frame,
1362 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001365
1366 switch (ret) {
1367 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 case 0: /* ignore => an error, cannot be ignored */
1369 goto exit;
1370
1371 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372 goto stop;
1373
Christopher Faulet8ef75252017-02-20 22:56:03 +01001374 default:
1375 /* HELLO frame successfully sent, now wait for the
1376 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001377 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1378 goto next;
1379 }
1380
1381 next:
1382 return 0;
1383 stop:
1384 return 1;
1385 exit:
1386 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1387 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388}
1389
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001390static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001392{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001393 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001394 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 char *frame;
1396 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001397
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398
Christopher Fauletb067b062017-01-04 16:39:11 +01001399 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001400 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001401 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001402 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001405 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1406 " - Connection timed out\n",
1407 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1408 __FUNCTION__, appctx);
1409 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 goto exit;
1411 }
1412
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001413 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 ret = spoe_recv_frame(appctx, frame,
1415 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 if (ret > 1) {
1417 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1418 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1419 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001420 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001421 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001423 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001424
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 switch (ret) {
1426 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001427 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1429 goto next;
1430
1431 case 1: /* retry later */
1432 goto stop;
1433
1434 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 /* HELLO handshake is finished, set the idle timeout and
1436 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001437 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001438 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001439 SPOE_APPCTX(appctx)->node.key = 0;
1440 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001441
1442 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001443 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001445 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001448 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001449 if (trash.data)
1450 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001451
1452 SPOE_APPCTX(appctx)->task->expire =
1453 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001454 return 0;
1455 stop:
1456 return 1;
1457 exit:
1458 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1459 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001460}
1461
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001462
Christopher Fauleta1cda022016-12-21 08:58:06 +01001463static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001464spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001465{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001466 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1467 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001468 char *frame, *buf;
1469 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001470
Christopher Faulet8ef75252017-02-20 22:56:03 +01001471 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1472 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001473 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001474
1475 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1476 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1477 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1478 SPOE_APPCTX(appctx)->max_frame_size);
1479 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001480 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001481 *skip = 1;
1482 ret = 1;
1483 goto end;
1484 }
1485 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001486 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1488 SPOE_APPCTX(appctx)->max_frame_size);
1489
1490 }
1491
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 if (ret > 1)
1493 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001494
Christopher Faulet8ef75252017-02-20 22:56:03 +01001495 switch (ret) {
1496 case -1: /* error */
1497 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1498 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001499
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 case 0: /* ignore */
1501 if (ctx == NULL)
1502 goto abort_frag_frame;
1503
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001504 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505 LIST_DEL(&ctx->list);
1506 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001507 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001508 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001509 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001510 ctx->state = SPOE_CTX_ST_ERROR;
1511 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1512 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001513 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 break;
1515
1516 case 1: /* retry */
1517 *skip = 1;
1518 break;
1519
1520 default:
1521 if (ctx == NULL)
1522 goto abort_frag_frame;
1523
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001524 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 LIST_DEL(&ctx->list);
1526 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001527 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001528 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001529 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001530 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1531 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1532 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001533 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001535 }
1536 goto end;
1537
1538 frag_frame_sent:
1539 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001540 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1542 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1543 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001544 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1545 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1546 goto end;
1547
1548 no_frag_frame_sent:
1549 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1550 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001551 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001552 }
1553 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1554 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1555 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1556 }
1557 else {
1558 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001559 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001560 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1561 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001562 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001563 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1565 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1566 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001567 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568
Christopher Faulet8ef75252017-02-20 22:56:03 +01001569 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1570 goto end;
1571
1572 abort_frag_frame:
1573 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1574 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1575 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1576 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1577 goto end;
1578
1579 end:
1580 return ret;
1581}
1582
1583static int
1584spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1585{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001586 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 struct spoe_context *ctx = NULL;
1588 char *frame;
1589 int ret;
1590
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001591 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001592 ret = spoe_recv_frame(appctx, frame,
1593 SPOE_APPCTX(appctx)->max_frame_size);
1594 if (ret > 1) {
1595 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1596 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001597 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 goto end;
1599 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001600 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1602 }
1603 switch (ret) {
1604 case -1: /* error */
1605 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1606 break;
1607
1608 case 0: /* ignore */
1609 break;
1610
1611 case 1: /* retry */
1612 *skip = 1;
1613 break;
1614
1615 default:
1616 LIST_DEL(&ctx->list);
1617 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001618 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001619 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1620 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001621 if (ctx->spoe_appctx) {
1622 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001623 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001624 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001625 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1626 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1627 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1628 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1629 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1630 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1631 }
1632 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1633 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001634 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1635 break;
1636 }
1637
1638 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001639 if (trash.data)
1640 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001641 end:
1642 return ret;
1643}
1644
1645static int
1646spoe_handle_processing_appctx(struct appctx *appctx)
1647{
1648 struct stream_interface *si = appctx->owner;
1649 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001650 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001651
1652 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1653 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1654 goto exit;
1655 }
1656
1657 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1658 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1659 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1660 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1661 goto next;
1662 }
1663
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001664 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001665 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001666 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001667 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1668 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001669 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1672 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001673
Christopher Faulet8f82b202018-01-24 16:23:03 +01001674 /* receiving_frame loop */
1675 while (!skip_receiving) {
1676 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1677 switch (ret) {
1678 case -1: /* error */
1679 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001680
Christopher Faulet8f82b202018-01-24 16:23:03 +01001681 case 0: /* ignore */
1682 active_r = 1;
1683 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001684
Christopher Faulet8f82b202018-01-24 16:23:03 +01001685 case 1: /* retry */
1686 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001687
Christopher Faulet8f82b202018-01-24 16:23:03 +01001688 default:
1689 active_r = 1;
1690 break;
1691 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 /* send_frame loop */
1695 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1696 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1697 switch (ret) {
1698 case -1: /* error */
1699 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001700
Christopher Faulet8f82b202018-01-24 16:23:03 +01001701 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001702 if (SPOE_APPCTX(appctx)->node.key)
1703 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 active_s++;
1705 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 case 1: /* retry */
1708 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001709
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001711 if (SPOE_APPCTX(appctx)->node.key)
1712 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 active_s++;
1714 break;
1715 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001716 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717
Christopher Faulet8f82b202018-01-24 16:23:03 +01001718 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1720 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1721 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001722
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001724 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001725 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001726 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001727 }
1728 return 1;
1729
Christopher Faulet8f82b202018-01-24 16:23:03 +01001730 next:
1731 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1732 return 0;
1733
Christopher Fauleta1cda022016-12-21 08:58:06 +01001734 exit:
1735 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1736 return 0;
1737}
1738
1739static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001740spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001741{
1742 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001743 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001744 char *frame, *buf;
1745 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001746
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1748 goto exit;
1749
1750 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1751 goto exit;
1752
Christopher Faulet8ef75252017-02-20 22:56:03 +01001753 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1754 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001755 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001756 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1757 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001760
1761 switch (ret) {
1762 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764 goto exit;
1765
1766 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 goto stop;
1768
1769 default:
1770 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1771 " - disconnected by HAProxy (%d): %s\n",
1772 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001773 __FUNCTION__, appctx,
1774 SPOE_APPCTX(appctx)->status_code,
1775 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776
1777 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1778 goto next;
1779 }
1780
1781 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 SPOE_APPCTX(appctx)->task->expire =
1783 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784 return 0;
1785 stop:
1786 return 1;
1787 exit:
1788 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1789 return 0;
1790}
1791
1792static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001793spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001794{
1795 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796 char *frame;
1797 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798
Christopher Fauletb067b062017-01-04 16:39:11 +01001799 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001800 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001802 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803
Christopher Fauletb067b062017-01-04 16:39:11 +01001804 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001807 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001808
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001809 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 ret = spoe_recv_frame(appctx, frame,
1811 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001813 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 }
1816
1817 switch (ret) {
1818 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1820 " - error on frame (%s)\n",
1821 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001822 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 goto exit;
1826
1827 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 goto next;
1829
1830 case 1: /* retry */
1831 goto stop;
1832
1833 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001835 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001837 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001838 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1839 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840 goto exit;
1841 }
1842
1843 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001845 if (trash.data)
1846 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 return 0;
1849 stop:
1850 return 1;
1851 exit:
1852 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1853 return 0;
1854}
1855
1856/* I/O Handler processing messages exchanged with the agent */
1857static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001858spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001860 struct stream_interface *si = appctx->owner;
1861 struct spoe_agent *agent;
1862
1863 if (SPOE_APPCTX(appctx) == NULL)
1864 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1867 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001868
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869 switchstate:
1870 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1871 " - appctx-state=%s\n",
1872 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1873 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1874
1875 switch (appctx->st0) {
1876 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001877 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878 goto out;
1879 goto switchstate;
1880
1881 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 goto out;
1884 goto switchstate;
1885
1886 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001887 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001888 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001890 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001891 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001892 SPOE_APPCTX(appctx)->task->expire =
1893 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001895 goto switchstate;
1896 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1898 /* fall through */
1899
1900 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001901 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1902 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 goto out;
1905 goto switchstate;
1906
1907 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909 goto out;
1910 goto switchstate;
1911
1912 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001913 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001914 goto out;
1915 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916
1917 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001918 appctx->st0 = SPOE_APPCTX_ST_END;
1919 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1920
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001921 si_shutw(si);
1922 si_shutr(si);
1923 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001924 /* fall through */
1925
1926 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001927 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001928 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001930 if (stopping)
1931 spoe_wakeup_appctx(appctx);
1932
Christopher Faulet42bfa462017-01-04 14:14:19 +01001933 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1934 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001935}
1936
1937struct applet spoe_applet = {
1938 .obj_type = OBJ_TYPE_APPLET,
1939 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001940 .fct = spoe_handle_appctx,
1941 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942};
1943
1944/* Create a SPOE applet. On success, the created applet is returned, else
1945 * NULL. */
1946static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948{
1949 struct appctx *appctx;
1950 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001952
Emeric Brun1138fd02017-06-19 12:38:55 +02001953 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001954 goto out_error;
1955
Willy Tarreaubafbe012017-11-24 17:34:44 +01001956 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001957 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001959 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001960
Christopher Faulet42bfa462017-01-04 14:14:19 +01001961 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001962 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001963 goto out_free_spoe_appctx;
1964
1965 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001966 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001967 SPOE_APPCTX(appctx)->task->context = appctx;
1968 SPOE_APPCTX(appctx)->agent = conf->agent;
1969 SPOE_APPCTX(appctx)->version = 0;
1970 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1971 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001972 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001973 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001974 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001975
Willy Tarreau90f366b2021-02-20 11:49:49 +01001976 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001977 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001978 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001979
1980 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1981 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001982
Willy Tarreau5820a362016-12-22 15:59:02 +01001983 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984 if (!sess)
1985 goto out_free_spoe;
1986
Christopher Faulet26256f82020-09-14 11:40:13 +02001987 if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001988 goto out_free_sess;
1989
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990 stream_set_backend(strm, conf->agent->b.be);
1991
1992 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001993 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 appctx_wakeup(appctx);
1995
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 strm->do_log = NULL;
1997 strm->res.flags |= CF_READ_DONTWAIT;
1998
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001999 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002000 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002001 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002002 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002003
Emeric Brunc60def82017-09-27 14:59:38 +02002004 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002005 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006 return appctx;
2007
2008 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009 out_free_sess:
2010 session_free(sess);
2011 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002012 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002013 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002014 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015 out_free_appctx:
2016 appctx_free(appctx);
2017 out_error:
2018 return NULL;
2019}
2020
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002022spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023{
2024 struct spoe_config *conf = FLT_CONF(ctx->filter);
2025 struct spoe_agent *agent = conf->agent;
2026 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002027 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028
Christopher Fauleta1cda022016-12-21 08:58:06 +01002029 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002030 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002031 (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 +01002032 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033
2034 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2037 ctx->strm);
2038
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 /* Do not try to create a new applet if there is no server up for the
2040 * agent's backend. */
2041 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2042 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2043 " - cannot create SPOE appctx: no server up\n",
2044 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2045 __FUNCTION__, ctx->strm);
2046 goto end;
2047 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048
Christopher Fauleta1cda022016-12-21 08:58:06 +01002049 /* Do not try to create a new applet if we have reached the maximum of
2050 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002051 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002052 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002053 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2054 " - cannot create SPOE appctx: max CPS reached\n",
2055 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2056 __FUNCTION__, ctx->strm);
2057 goto end;
2058 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002059 }
2060
Christopher Faulet8ef75252017-02-20 22:56:03 +01002061 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 if (appctx == NULL) {
2063 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2064 " - failed to create SPOE appctx\n",
2065 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2066 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002067 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002068 "SPOE: [%s] failed to create SPOE applet\n",
2069 agent->id);
2070
Christopher Fauleta1cda022016-12-21 08:58:06 +01002071 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072 }
2073
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 /* Increase the per-process number of cumulated connections */
2075 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002076 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077
Christopher Fauleta1cda022016-12-21 08:58:06 +01002078 end:
2079 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002080 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002081 ctx->status_code = SPOE_CTX_ERR_RES;
2082 return -1;
2083 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002085 /* Add the SPOE context in the sending queue if the stream has no applet
2086 * already assigned and wakeup all idle applets. Otherwise, don't queue
2087 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002088 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002089 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2090 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002091 if (ctx->spoe_appctx)
2092 return 1;
2093 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002094
2095 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002096 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002097 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002098 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002099 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002100 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002101
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002102 /* Finally try to wakeup an IDLE applet. */
2103 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2104 struct eb32_node *node;
2105
2106 node = eb32_first(&agent->rt[tid].idle_applets);
2107 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2108 if (node && spoe_appctx) {
2109 eb32_delete(&spoe_appctx->node);
2110 spoe_appctx->node.key++;
2111 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2112 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002113 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002114 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002115 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002116}
2117
2118/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002119 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002120 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002121/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2122 * fragmented_content. If the next message can be processed, it returns 0. If
2123 * the message is too big, it returns -1.*/
2124static int
2125spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2126 struct spoe_message *msg, int dir,
2127 char **buf, char *end)
2128{
2129 struct sample *smp;
2130 struct spoe_arg *arg;
2131 int ret;
2132
2133 if (msg->cond) {
2134 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2135 ret = acl_pass(ret);
2136 if (msg->cond->pol == ACL_COND_UNLESS)
2137 ret = !ret;
2138
2139 /* the rule does not match */
2140 if (!ret)
2141 goto next;
2142 }
2143
2144 /* Resume encoding of a SPOE argument */
2145 if (ctx->frag_ctx.curarg != NULL) {
2146 arg = ctx->frag_ctx.curarg;
2147 goto encode_argument;
2148 }
2149
2150 if (ctx->frag_ctx.curoff != UINT_MAX)
2151 goto encode_msg_payload;
2152
2153 /* Check if there is enough space for the message name and the
2154 * number of arguments. It implies <msg->id_len> is encoded on 2
2155 * bytes, at most (< 2288). */
2156 if (*buf + 2 + msg->id_len + 1 > end)
2157 goto too_big;
2158
2159 /* Encode the message name */
2160 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2161 goto too_big;
2162
2163 /* Set the number of arguments for this message */
2164 **buf = msg->nargs;
2165 (*buf)++;
2166
2167 ctx->frag_ctx.curoff = 0;
2168 encode_msg_payload:
2169
2170 /* Loop on arguments */
2171 list_for_each_entry(arg, &msg->args, list) {
2172 ctx->frag_ctx.curarg = arg;
2173 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002174 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002175
2176 encode_argument:
2177 if (ctx->frag_ctx.curoff != UINT_MAX)
2178 goto encode_arg_value;
2179
Joseph Herlantf7f60312018-11-15 13:46:49 -08002180 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002181 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2182 goto too_big;
2183
2184 ctx->frag_ctx.curoff = 0;
2185 encode_arg_value:
2186
Joseph Herlantf7f60312018-11-15 13:46:49 -08002187 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002188 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002189 if (smp) {
2190 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2191 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2192 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002193 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002194 if (ret == -1 || ctx->frag_ctx.curoff)
2195 goto too_big;
2196 }
2197
2198 next:
2199 return 0;
2200
2201 too_big:
2202 return -1;
2203}
2204
Christopher Fauletc718b822017-09-21 16:50:56 +02002205/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2206 * handle fragmented content. On success it returns 1. If an error occurred, -1
2207 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2208 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002209static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002210spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002211 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002212{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002213 struct spoe_config *conf = FLT_CONF(ctx->filter);
2214 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002215 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002216 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002217
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002218 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002219 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002220
Christopher Fauletc718b822017-09-21 16:50:56 +02002221 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2222 /* Resume encoding of a SPOE message */
2223 if (ctx->frag_ctx.curmsg != NULL) {
2224 msg = ctx->frag_ctx.curmsg;
2225 goto encode_evt_message;
2226 }
2227
2228 list_for_each_entry(msg, messages, by_evt) {
2229 ctx->frag_ctx.curmsg = msg;
2230 ctx->frag_ctx.curarg = NULL;
2231 ctx->frag_ctx.curoff = UINT_MAX;
2232
2233 encode_evt_message:
2234 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2235 goto too_big;
2236 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002237 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002238 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2239 /* Resume encoding of a SPOE message */
2240 if (ctx->frag_ctx.curmsg != NULL) {
2241 msg = ctx->frag_ctx.curmsg;
2242 goto encode_grp_message;
2243 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244
Christopher Fauletc718b822017-09-21 16:50:56 +02002245 list_for_each_entry(msg, messages, by_grp) {
2246 ctx->frag_ctx.curmsg = msg;
2247 ctx->frag_ctx.curarg = NULL;
2248 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249
Christopher Fauletc718b822017-09-21 16:50:56 +02002250 encode_grp_message:
2251 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2252 goto too_big;
2253 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002254 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002255 else
2256 goto skip;
2257
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002258
Christopher Faulet57583e42017-09-04 15:41:09 +02002259 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002260 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002261 goto skip;
2262
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002264 " - encode %s messages - spoe_appctx=%p"
2265 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266 (int)now.tv_sec, (int)now.tv_usec,
2267 agent->id, __FUNCTION__, s,
2268 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002269 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002270 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002271
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002272 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002273 ctx->frag_ctx.curmsg = NULL;
2274 ctx->frag_ctx.curarg = NULL;
2275 ctx->frag_ctx.curoff = 0;
2276 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002277
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002278 return 1;
2279
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002281 /* Return an error if fragmentation is unsupported or if nothing has
2282 * been encoded because its too big and not splittable. */
2283 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002284 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2285 return -1;
2286 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002287
2288 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002289 " - encode fragmented messages - spoe_appctx=%p"
2290 " - curmsg=%p - curarg=%p - curoff=%u"
2291 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002293 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002295 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002296
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002297 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002298 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2299 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2300 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002301
2302 skip:
2303 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2304 " - skip the frame because nothing has been encoded\n",
2305 (int)now.tv_sec, (int)now.tv_usec,
2306 agent->id, __FUNCTION__, s);
2307 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002308}
2309
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002310
2311/***************************************************************************
2312 * Functions that handle SPOE actions
2313 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002314/* Helper function to set a variable */
2315static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002316spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002317 struct sample *smp)
2318{
2319 struct spoe_config *conf = FLT_CONF(ctx->filter);
2320 struct spoe_agent *agent = conf->agent;
2321 char varname[64];
2322
2323 memset(varname, 0, sizeof(varname));
2324 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2325 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002326 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2327 vars_set_by_name(varname, len, smp);
2328 else
2329 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330}
2331
2332/* Helper function to unset a variable */
2333static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002334spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002335 struct sample *smp)
2336{
2337 struct spoe_config *conf = FLT_CONF(ctx->filter);
2338 struct spoe_agent *agent = conf->agent;
2339 char varname[64];
2340
2341 memset(varname, 0, sizeof(varname));
2342 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2343 scope, agent->var_pfx, len, name);
2344 vars_unset_by_name_ifexist(varname, len, smp);
2345}
2346
2347
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348static inline int
2349spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2350 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002351{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002352 char *str, *scope, *p = *buf;
2353 struct sample smp;
2354 uint64_t sz;
2355 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002356
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357 if (p + 2 >= end)
2358 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 /* SET-VAR requires 3 arguments */
2361 if (*p++ != 3)
2362 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002363
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364 switch (*p++) {
2365 case SPOE_SCOPE_PROC: scope = "proc"; break;
2366 case SPOE_SCOPE_SESS: scope = "sess"; break;
2367 case SPOE_SCOPE_TXN : scope = "txn"; break;
2368 case SPOE_SCOPE_REQ : scope = "req"; break;
2369 case SPOE_SCOPE_RES : scope = "res"; break;
2370 default: goto skip;
2371 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372
Christopher Faulet8ef75252017-02-20 22:56:03 +01002373 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2374 goto skip;
2375 memset(&smp, 0, sizeof(smp));
2376 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002377
Christopher Faulet8ef75252017-02-20 22:56:03 +01002378 if (spoe_decode_data(&p, end, &smp) == -1)
2379 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2382 " - set-var '%s.%s.%.*s'\n",
2383 (int)now.tv_sec, (int)now.tv_usec,
2384 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2385 __FUNCTION__, s, scope,
2386 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2387 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002388
Christopher Faulet2469eba2020-10-15 16:08:30 +02002389 if (smp.data.type == SMP_T_ANY)
2390 spoe_unset_var(ctx, scope, str, sz, &smp);
2391 else
2392 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002393
Christopher Faulet8ef75252017-02-20 22:56:03 +01002394 ret = (p - *buf);
2395 *buf = p;
2396 return ret;
2397 skip:
2398 return 0;
2399}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401static inline int
2402spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2403 char **buf, char *end, int dir)
2404{
2405 char *str, *scope, *p = *buf;
2406 struct sample smp;
2407 uint64_t sz;
2408 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002409
Christopher Faulet8ef75252017-02-20 22:56:03 +01002410 if (p + 2 >= end)
2411 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413 /* UNSET-VAR requires 2 arguments */
2414 if (*p++ != 2)
2415 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002416
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417 switch (*p++) {
2418 case SPOE_SCOPE_PROC: scope = "proc"; break;
2419 case SPOE_SCOPE_SESS: scope = "sess"; break;
2420 case SPOE_SCOPE_TXN : scope = "txn"; break;
2421 case SPOE_SCOPE_REQ : scope = "req"; break;
2422 case SPOE_SCOPE_RES : scope = "res"; break;
2423 default: goto skip;
2424 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002425
Christopher Faulet8ef75252017-02-20 22:56:03 +01002426 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2427 goto skip;
2428 memset(&smp, 0, sizeof(smp));
2429 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002430
Christopher Faulet8ef75252017-02-20 22:56:03 +01002431 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2432 " - unset-var '%s.%s.%.*s'\n",
2433 (int)now.tv_sec, (int)now.tv_usec,
2434 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2435 __FUNCTION__, s, scope,
2436 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2437 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002438
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439 spoe_unset_var(ctx, scope, str, sz, &smp);
2440
2441 ret = (p - *buf);
2442 *buf = p;
2443 return ret;
2444 skip:
2445 return 0;
2446}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002447
Christopher Faulet8ef75252017-02-20 22:56:03 +01002448/* Process SPOE actions for a specific event. It returns 1 on success. If an
2449 * error occurred, 0 is returned. */
2450static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002451spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002452{
2453 char *p, *end;
2454 int ret;
2455
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002456 p = b_head(&ctx->buffer);
2457 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002458
2459 while (p < end) {
2460 enum spoe_action_type type;
2461
2462 type = *p++;
2463 switch (type) {
2464 case SPOE_ACT_T_SET_VAR:
2465 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2466 if (!ret)
2467 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002468 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002469
Christopher Faulet8ef75252017-02-20 22:56:03 +01002470 case SPOE_ACT_T_UNSET_VAR:
2471 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2472 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002473 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002474 break;
2475
2476 default:
2477 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002478 }
2479 }
2480
2481 return 1;
2482 skip:
2483 return 0;
2484}
2485
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002486/***************************************************************************
2487 * Functions that process SPOE events
2488 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002489static void
2490spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2491 struct spoe_context *ctx, int dir)
2492{
2493 if (!tv_iszero(&ctx->stats.tv_start)) {
2494 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2495 ctx->stats.t_total += ctx->stats.t_process;
2496 tv_zero(&ctx->stats.tv_request);
2497 tv_zero(&ctx->stats.tv_queue);
2498 tv_zero(&ctx->stats.tv_wait);
2499 tv_zero(&ctx->stats.tv_response);
2500 }
2501
2502 if (agent->var_t_process) {
2503 struct sample smp;
2504
2505 memset(&smp, 0, sizeof(smp));
2506 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2507 smp.data.u.sint = ctx->stats.t_process;
2508 smp.data.type = SMP_T_SINT;
2509
2510 spoe_set_var(ctx, "txn", agent->var_t_process,
2511 strlen(agent->var_t_process), &smp);
2512 }
2513
2514 if (agent->var_t_total) {
2515 struct sample smp;
2516
2517 memset(&smp, 0, sizeof(smp));
2518 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2519 smp.data.u.sint = ctx->stats.t_total;
2520 smp.data.type = SMP_T_SINT;
2521
2522 spoe_set_var(ctx, "txn", agent->var_t_total,
2523 strlen(agent->var_t_total), &smp);
2524 }
2525}
2526
2527static void
2528spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2529 struct spoe_context *ctx, int dir)
2530{
2531 if (agent->eps_max > 0)
2532 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2533
2534 if (agent->var_on_error) {
2535 struct sample smp;
2536
2537 memset(&smp, 0, sizeof(smp));
2538 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2539 smp.data.u.sint = ctx->status_code;
2540 smp.data.type = SMP_T_BOOL;
2541
2542 spoe_set_var(ctx, "txn", agent->var_on_error,
2543 strlen(agent->var_on_error), &smp);
2544 }
2545
2546 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2547 ? SPOE_CTX_ST_READY
2548 : SPOE_CTX_ST_NONE);
2549}
2550
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002551static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002552spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002553{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002554 /* If a process is already started for this SPOE context, retry
2555 * later. */
2556 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002557 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002558
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002559 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002560 ctx->stats.tv_start = now;
2561 ctx->stats.tv_request = now;
2562 ctx->stats.t_request = -1;
2563 ctx->stats.t_queue = -1;
2564 ctx->stats.t_waiting = -1;
2565 ctx->stats.t_response = -1;
2566 ctx->stats.t_process = -1;
2567
2568 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002569
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570 /* Set the right flag to prevent request and response processing
2571 * in same time. */
2572 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2573 ? SPOE_CTX_FL_REQ_PROCESS
2574 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002575 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576}
2577
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002578static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002579spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002580{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002581 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002582
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002583 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2584 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002585 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002586 if (sa) {
2587 if (sa->frag_ctx.ctx == ctx) {
2588 sa->frag_ctx.ctx = NULL;
2589 spoe_wakeup_appctx(sa->owner);
2590 }
2591 else
2592 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002593 }
2594
Christopher Fauleta1cda022016-12-21 08:58:06 +01002595 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002596 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002597 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598
2599 /* Reset processing timer */
2600 ctx->process_exp = TICK_ETERNITY;
2601
Christopher Faulet8ef75252017-02-20 22:56:03 +01002602 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002603
Christopher Fauletfce747b2018-01-24 15:59:32 +01002604 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605 ctx->frag_ctx.curmsg = NULL;
2606 ctx->frag_ctx.curarg = NULL;
2607 ctx->frag_ctx.curoff = 0;
2608 ctx->frag_ctx.flags = 0;
2609
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002611 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002612 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002613 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002614 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002615
Christopher Fauleta1cda022016-12-21 08:58:06 +01002616 LIST_DEL(&ctx->list);
2617 LIST_INIT(&ctx->list);
2618 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002619}
2620
Christopher Faulet58d03682017-09-21 16:57:24 +02002621/* Process a list of SPOE messages. First, this functions will process messages
2622 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2623 * to process corresponding actions. During all the processing, it returns 0
2624 * and it returns 1 when the processing is finished. If an error occurred, -1
2625 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002626static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002627spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2628 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002629{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002630 struct spoe_config *conf = FLT_CONF(ctx->filter);
2631 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002632 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002633
2634 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002635 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002636
2637 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2638 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002639 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002640 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002641 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002642 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002643 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002644 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002645
2646 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002647 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002648 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002649 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002650 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002651 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002652 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002653 goto skip;
2654 }
2655 }
2656
Christopher Fauletf7a30922016-11-10 15:04:51 +01002657 if (!tick_isset(ctx->process_exp)) {
2658 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2659 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2660 ctx->process_exp);
2661 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002662 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002663 if (!ret)
2664 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002666 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002667 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002668 }
2669
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002670 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002671 if (tv_iszero(&ctx->stats.tv_request))
2672 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002673 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002674 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002675 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002676 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002677 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002678 if (!ret)
2679 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002680 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002681 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002682 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2683 }
2684
2685 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002686 if (ctx->spoe_appctx)
2687 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688 ret = 0;
2689 goto out;
2690 }
2691
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002692 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2693 ret = 0;
2694 goto out;
2695 }
2696
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002697 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002698 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002699 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002701 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002702 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002704 }
2705
2706 out:
2707 return ret;
2708
Christopher Fauleta1cda022016-12-21 08:58:06 +01002709 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002710 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002711 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002712 spoe_stop_processing(agent, ctx);
2713 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002714
Christopher Fauleta1cda022016-12-21 08:58:06 +01002715 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002716 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002717 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002718 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002719 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002720 spoe_handle_processing_error(s, agent, ctx, dir);
2721 ret = 1;
2722 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002723 return ret;
2724}
2725
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002726/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2727 * See spoe_process_message for details. */
2728static int
2729spoe_process_group(struct stream *s, struct spoe_context *ctx,
2730 struct spoe_group *group, int dir)
2731{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002732 struct spoe_config *conf = FLT_CONF(ctx->filter);
2733 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002734 int ret;
2735
2736 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2737 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002738 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002739 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2740 group->id);
2741
2742 if (LIST_ISEMPTY(&group->messages))
2743 return 1;
2744
2745 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002746 if (ret && ctx->stats.t_process != -1) {
2747 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002748 " - <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 +01002749 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002750 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002751 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002752 ctx->stats.t_response, ctx->stats.t_process,
2753 agent->counters.idles, agent->counters.applets,
2754 agent->counters.nb_sending, agent->counters.nb_waiting,
2755 agent->counters.nb_errors, agent->counters.nb_processed,
2756 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2757 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2758 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2759 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2760 agent->id, group->id, s->uniq_id, ctx->status_code,
2761 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2762 ctx->stats.t_response, ctx->stats.t_process,
2763 agent->counters.idles, agent->counters.applets,
2764 agent->counters.nb_sending, agent->counters.nb_waiting,
2765 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002766 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002767 return ret;
2768}
2769
Christopher Faulet58d03682017-09-21 16:57:24 +02002770/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2771 * See spoe_process_message for details. */
2772static int
2773spoe_process_event(struct stream *s, struct spoe_context *ctx,
2774 enum spoe_event ev)
2775{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002776 struct spoe_config *conf = FLT_CONF(ctx->filter);
2777 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002778 int dir, ret;
2779
2780 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002781 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002782 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002783 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2784 spoe_event_str[ev]);
2785
2786 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2787
2788 if (LIST_ISEMPTY(&(ctx->events[ev])))
2789 return 1;
2790
2791 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002792 if (ret && ctx->stats.t_process != -1) {
2793 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002794 " - <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 +01002795 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2796 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2797 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002798 ctx->stats.t_response, ctx->stats.t_process,
2799 agent->counters.idles, agent->counters.applets,
2800 agent->counters.nb_sending, agent->counters.nb_waiting,
2801 agent->counters.nb_errors, agent->counters.nb_processed,
2802 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2803 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2804 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2805 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2806 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2807 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2808 ctx->stats.t_response, ctx->stats.t_process,
2809 agent->counters.idles, agent->counters.applets,
2810 agent->counters.nb_sending, agent->counters.nb_waiting,
2811 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002812 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002813 return ret;
2814}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002815
2816/***************************************************************************
2817 * Functions that create/destroy SPOE contexts
2818 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002820spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002821{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002822 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002823 return 1;
2824
Willy Tarreau90f366b2021-02-20 11:49:49 +01002825 if (LIST_ADDED(&buffer_wait->list))
2826 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827
Christopher Faulet4596fb72017-01-11 14:05:19 +01002828 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002829 return 1;
2830
Willy Tarreau90f366b2021-02-20 11:49:49 +01002831 LIST_ADDQ(&ti->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002832 return 0;
2833}
2834
2835static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002836spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002837{
Willy Tarreau90f366b2021-02-20 11:49:49 +01002838 if (LIST_ADDED(&buffer_wait->list))
2839 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002840
2841 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002842 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002843 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002844 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002845 }
2846}
2847
Christopher Faulet4596fb72017-01-11 14:05:19 +01002848static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002849spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002850{
2851 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2852 return 1;
2853}
2854
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002855static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002856spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002857{
2858 struct spoe_config *conf = FLT_CONF(filter);
2859 struct spoe_context *ctx;
2860
Willy Tarreaubafbe012017-11-24 17:34:44 +01002861 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002862 if (ctx == NULL) {
2863 return NULL;
2864 }
2865 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002866 ctx->filter = filter;
2867 ctx->state = SPOE_CTX_ST_NONE;
2868 ctx->status_code = SPOE_CTX_ERR_NONE;
2869 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002870 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002871 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002873 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002874 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002875 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002876 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002877
Christopher Fauletf7a30922016-11-10 15:04:51 +01002878 ctx->stream_id = 0;
2879 ctx->frame_id = 1;
2880 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002882 tv_zero(&ctx->stats.tv_start);
2883 tv_zero(&ctx->stats.tv_request);
2884 tv_zero(&ctx->stats.tv_queue);
2885 tv_zero(&ctx->stats.tv_wait);
2886 tv_zero(&ctx->stats.tv_response);
2887 ctx->stats.t_request = -1;
2888 ctx->stats.t_queue = -1;
2889 ctx->stats.t_waiting = -1;
2890 ctx->stats.t_response = -1;
2891 ctx->stats.t_process = -1;
2892 ctx->stats.t_total = 0;
2893
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002894 ctx->strm = s;
2895 ctx->state = SPOE_CTX_ST_READY;
2896 filter->ctx = ctx;
2897
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002898 return ctx;
2899}
2900
2901static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002902spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002903{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002904 struct spoe_config *conf = FLT_CONF(filter);
2905 struct spoe_context *ctx = filter->ctx;
2906
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002907 if (!ctx)
2908 return;
2909
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002910 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002911 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002912 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913}
2914
2915static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002916spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917{
2918 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002919 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002920
2921 tv_zero(&ctx->stats.tv_start);
2922 tv_zero(&ctx->stats.tv_request);
2923 tv_zero(&ctx->stats.tv_queue);
2924 tv_zero(&ctx->stats.tv_wait);
2925 tv_zero(&ctx->stats.tv_response);
2926 ctx->stats.t_request = -1;
2927 ctx->stats.t_queue = -1;
2928 ctx->stats.t_waiting = -1;
2929 ctx->stats.t_response = -1;
2930 ctx->stats.t_process = -1;
2931 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002932}
2933
2934
2935/***************************************************************************
2936 * Hooks that manage the filter lifecycle (init/check/deinit)
2937 **************************************************************************/
2938/* Signal handler: Do a soft stop, wakeup SPOE applet */
2939static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002941{
2942 struct proxy *p;
2943
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002944 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945 while (p) {
2946 struct flt_conf *fconf;
2947
2948 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002949 struct spoe_config *conf;
2950 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002951 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002952 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953
Christopher Faulet3b386a32017-02-23 10:17:15 +01002954 if (fconf->id != spoe_filter_id)
2955 continue;
2956
2957 conf = fconf->conf;
2958 agent = conf->agent;
2959
Christopher Faulet24289f22017-09-25 14:48:02 +02002960 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002961 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002962 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2963 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002964 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965 }
2966 }
2967 p = p->next;
2968 }
2969}
2970
2971
2972/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2973static int
2974spoe_init(struct proxy *px, struct flt_conf *fconf)
2975{
2976 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002977
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002978 /* conf->agent_fe was already initialized during the config
2979 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981 conf->agent_fe.cap = PR_CAP_FE;
2982 conf->agent_fe.mode = PR_MODE_TCP;
2983 conf->agent_fe.maxconn = 0;
2984 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2985 conf->agent_fe.conn_retries = CONN_RETRIES;
2986 conf->agent_fe.accept = frontend_accept;
2987 conf->agent_fe.srv = NULL;
2988 conf->agent_fe.timeout.client = TICK_ETERNITY;
2989 conf->agent_fe.default_target = &spoe_applet.obj_type;
2990 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2991
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002993 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002994 sighandler_registered = 1;
2995 }
2996
Christopher Faulet00292352019-01-09 15:05:10 +01002997 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002998 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002999}
3000
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003001/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002static void
3003spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3004{
3005 struct spoe_config *conf = fconf->conf;
3006
3007 if (conf) {
3008 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009
Christopher Faulet8ef75252017-02-20 22:56:03 +01003010 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003011 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012 free(conf);
3013 }
3014 fconf->conf = NULL;
3015}
3016
3017/* Check configuration of a SPOE filter for a specified proxy.
3018 * Return 1 on error, else 0. */
3019static int
3020spoe_check(struct proxy *px, struct flt_conf *fconf)
3021{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003022 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 struct spoe_config *conf = fconf->conf;
3024 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003025 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003026 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003027
Christopher Faulet7ee86672017-09-19 11:08:28 +02003028 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3029 * are uniq */
3030 list_for_each_entry(f, &px->filter_configs, list) {
3031 struct spoe_config *c = f->conf;
3032
3033 /* This is not an SPOE filter */
3034 if (f->id != spoe_filter_id)
3035 continue;
3036 /* This is the current SPOE filter */
3037 if (f == fconf)
3038 continue;
3039
3040 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003041 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003042 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3043 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003044 return 1;
3045 }
3046 }
3047
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 target = proxy_be_by_name(conf->agent->b.name);
3049 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003050 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3051 " declared at %s:%d.\n",
3052 px->id, conf->agent->b.name, conf->agent->id,
3053 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003054 return 1;
3055 }
3056 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003057 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3058 " at %s:%d does not support HTTP mode.\n",
3059 px->id, target->id, conf->agent->id,
3060 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003061 return 1;
3062 }
3063
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003064 if (px->bind_proc & ~target->bind_proc) {
3065 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3066 " at %s:%d does not cover all of its processes.\n",
3067 px->id, target->id, conf->agent->id,
3068 conf->agent->conf.file, conf->agent->conf.line);
3069 return 1;
3070 }
3071
Christopher Fauletfe261552019-03-18 13:57:42 +01003072 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003073 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3074 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3075 return 1;
3076 }
3077 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003078 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003079 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3080 conf->agent->rt[i].processing = 0;
3081 LIST_INIT(&conf->agent->rt[i].applets);
3082 LIST_INIT(&conf->agent->rt[i].sending_queue);
3083 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3084 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003085 }
3086
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003087 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3088 if (logsrv->type == LOG_TARGET_BUFFER) {
3089 struct sink *sink = sink_find(logsrv->ring_name);
3090
3091 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3092 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
3093 " at %s:%d uses unkown ring named '%s'.\n",
3094 px->id, conf->agent->id, conf->agent->conf.file,
3095 conf->agent->conf.line, logsrv->ring_name);
3096 return 1;
3097 }
3098 logsrv->sink = sink;
3099 }
3100 }
3101
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003102 free(conf->agent->b.name);
3103 conf->agent->b.name = NULL;
3104 conf->agent->b.be = target;
3105 return 0;
3106}
3107
Kevin Zhud87b1a52019-09-17 15:05:45 +02003108/* Initializes the SPOE filter for a proxy for a specific thread.
3109 * Returns a negative value if an error occurs. */
3110static int
3111spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3112{
3113 struct spoe_config *conf = fconf->conf;
3114 struct spoe_agent *agent = conf->agent;
3115
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003116 agent->rt[tid].engine_id = generate_pseudo_uuid();
3117 if (agent->rt[tid].engine_id == NULL)
3118 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003119 return 0;
3120}
3121
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003122/**************************************************************************
3123 * Hooks attached to a stream
3124 *************************************************************************/
3125/* Called when a filter instance is created and attach to a stream. It creates
3126 * the context that will be used to process this stream. */
3127static int
3128spoe_start(struct stream *s, struct filter *filter)
3129{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003130 struct spoe_config *conf = FLT_CONF(filter);
3131 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132 struct spoe_context *ctx;
3133
3134 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003135 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003136 __FUNCTION__, s);
3137
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003138 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003139 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3140 " - failed to create SPOE context\n",
3141 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003142 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003143 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003144 "SPOE: [%s] failed to create SPOE context\n",
3145 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146 return 0;
3147 }
3148
Christopher Faulet11610f32017-09-21 10:23:10 +02003149 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003150 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3151
Christopher Faulet11610f32017-09-21 10:23:10 +02003152 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003153 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3154
Christopher Faulet11610f32017-09-21 10:23:10 +02003155 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003156 filter->pre_analyzers |= AN_RES_INSPECT;
3157
Christopher Faulet11610f32017-09-21 10:23:10 +02003158 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003159 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3160
Christopher Faulet11610f32017-09-21 10:23:10 +02003161 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3163
Christopher Faulet11610f32017-09-21 10:23:10 +02003164 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003165 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3166
3167 return 1;
3168}
3169
3170/* Called when a filter instance is detached from a stream. It release the
3171 * attached SPOE context. */
3172static void
3173spoe_stop(struct stream *s, struct filter *filter)
3174{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003175 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3176 (int)now.tv_sec, (int)now.tv_usec,
3177 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3178 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003179 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003180}
3181
Christopher Fauletf7a30922016-11-10 15:04:51 +01003182
3183/*
3184 * Called when the stream is woken up because of expired timer.
3185 */
3186static void
3187spoe_check_timeouts(struct stream *s, struct filter *filter)
3188{
3189 struct spoe_context *ctx = filter->ctx;
3190
Christopher Fauletac580602018-03-20 16:09:20 +01003191 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003192 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003193}
3194
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195/* Called when we are ready to filter data on a channel */
3196static int
3197spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3198{
3199 struct spoe_context *ctx = filter->ctx;
3200 int ret = 1;
3201
3202 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3203 " - ctx-flags=0x%08x\n",
3204 (int)now.tv_sec, (int)now.tv_usec,
3205 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3206 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3207
Christopher Fauletb067b062017-01-04 16:39:11 +01003208 if (ctx->state == SPOE_CTX_ST_NONE)
3209 goto out;
3210
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 if (!(chn->flags & CF_ISRESP)) {
3212 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3213 chn->analysers |= AN_REQ_INSPECT_FE;
3214 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3215 chn->analysers |= AN_REQ_INSPECT_BE;
3216
3217 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3218 goto out;
3219
3220 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003221 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003222 if (!ret)
3223 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3225 }
3226 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003227 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 chn->analysers |= AN_RES_INSPECT;
3229
3230 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3231 goto out;
3232
Christopher Faulet8ef75252017-02-20 22:56:03 +01003233 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003234 if (!ret) {
3235 channel_dont_read(chn);
3236 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003237 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003238 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003239 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003240 }
3241
3242 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003243 return ret;
3244}
3245
3246/* Called before a processing happens on a given channel */
3247static int
3248spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3249 struct channel *chn, unsigned an_bit)
3250{
3251 struct spoe_context *ctx = filter->ctx;
3252 int ret = 1;
3253
3254 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3255 " - ctx-flags=0x%08x - ana=0x%08x\n",
3256 (int)now.tv_sec, (int)now.tv_usec,
3257 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3258 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3259 ctx->flags, an_bit);
3260
Christopher Fauletb067b062017-01-04 16:39:11 +01003261 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003262 goto out;
3263
3264 switch (an_bit) {
3265 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003266 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003267 break;
3268 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003269 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003270 break;
3271 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003272 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 break;
3274 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003275 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 break;
3277 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003278 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 break;
3280 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003281 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003282 break;
3283 }
3284
3285 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003286 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003287 channel_dont_read(chn);
3288 channel_dont_close(chn);
3289 }
3290 return ret;
3291}
3292
3293/* Called when the filtering on the channel ends. */
3294static int
3295spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3296{
3297 struct spoe_context *ctx = filter->ctx;
3298
3299 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3300 " - ctx-flags=0x%08x\n",
3301 (int)now.tv_sec, (int)now.tv_usec,
3302 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3303 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3304
3305 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003306 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003307 }
3308
3309 return 1;
3310}
3311
3312/********************************************************************
3313 * Functions that manage the filter initialization
3314 ********************************************************************/
3315struct flt_ops spoe_ops = {
3316 /* Manage SPOE filter, called for each filter declaration */
3317 .init = spoe_init,
3318 .deinit = spoe_deinit,
3319 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003320 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003321
3322 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003323 .attach = spoe_start,
3324 .detach = spoe_stop,
3325 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003326
3327 /* Handle channels activity */
3328 .channel_start_analyze = spoe_start_analyze,
3329 .channel_pre_analyze = spoe_chn_pre_analyze,
3330 .channel_end_analyze = spoe_end_analyze,
3331};
3332
3333
3334static int
3335cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3336{
3337 const char *err;
3338 int i, err_code = 0;
3339
3340 if ((cfg_scope == NULL && curengine != NULL) ||
3341 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003342 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003343 goto out;
3344
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003345 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003346 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003347 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3348 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003349 err_code |= ERR_ALERT | ERR_ABORT;
3350 goto out;
3351 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003352 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3353 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003354 goto out;
3355 }
3356
3357 err = invalid_char(args[1]);
3358 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003359 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3360 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361 err_code |= ERR_ALERT | ERR_ABORT;
3362 goto out;
3363 }
3364
3365 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003366 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3367 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003368 err_code |= ERR_ALERT | ERR_ABORT;
3369 goto out;
3370 }
3371 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003372 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003373 err_code |= ERR_ALERT | ERR_ABORT;
3374 goto out;
3375 }
3376
3377 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003378
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379 curagent->conf.file = strdup(file);
3380 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003381
3382 curagent->timeout.hello = TICK_ETERNITY;
3383 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003384 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003385
Christopher Fauleta1cda022016-12-21 08:58:06 +01003386 curagent->var_pfx = NULL;
3387 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003388 curagent->var_t_process = NULL;
3389 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003390 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003391 curagent->cps_max = 0;
3392 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003393 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003394 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003395
3396 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003397 LIST_INIT(&curagent->events[i]);
3398 LIST_INIT(&curagent->groups);
3399 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003400 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003401 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003402 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003403 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3404 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405 err_code |= ERR_ALERT | ERR_FATAL;
3406 goto out;
3407 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003408 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003409 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 free(curagent->b.name);
3411 curagent->b.name = strdup(args[1]);
3412 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003413 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003414 int cur_arg = 1;
3415 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003416 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417
Christopher Faulet11610f32017-09-21 10:23:10 +02003418 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003419 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003420 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3421 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 err_code |= ERR_ALERT | ERR_FATAL;
3423 goto out;
3424 }
3425 }
3426
Christopher Faulet11610f32017-09-21 10:23:10 +02003427 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003428 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003429 err_code |= ERR_ALERT | ERR_ABORT;
3430 goto out;
3431 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003432 ph->id = strdup(args[cur_arg]);
3433 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 cur_arg++;
3435 }
3436 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003437 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003438 int cur_arg = 1;
3439 while (*args[cur_arg]) {
3440 struct spoe_placeholder *ph = NULL;
3441
3442 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003443 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003444 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3445 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003446 err_code |= ERR_ALERT | ERR_FATAL;
3447 goto out;
3448 }
3449 }
3450
3451 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003453 err_code |= ERR_ALERT | ERR_ABORT;
3454 goto out;
3455 }
3456 ph->id = strdup(args[cur_arg]);
3457 LIST_ADDQ(&curgphs, &ph->list);
3458 cur_arg++;
3459 }
3460 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003461 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003462 unsigned int *tv = NULL;
3463 const char *res;
3464 unsigned timeout;
3465
3466 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003467 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3468 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003469 err_code |= ERR_ALERT | ERR_FATAL;
3470 goto out;
3471 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003472 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3473 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003474 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003475 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003476 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003477 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003478 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003479 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003480 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003481 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3482 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003483 err_code |= ERR_ALERT | ERR_FATAL;
3484 goto out;
3485 }
3486 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003487 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3488 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003489 err_code |= ERR_ALERT | ERR_FATAL;
3490 goto out;
3491 }
3492 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003493 if (res == PARSE_TIME_OVER) {
3494 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3495 file, linenum, args[2], args[0], args[1]);
3496 err_code |= ERR_ALERT | ERR_FATAL;
3497 goto out;
3498 }
3499 else if (res == PARSE_TIME_UNDER) {
3500 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3501 file, linenum, args[2], args[0], args[1]);
3502 err_code |= ERR_ALERT | ERR_FATAL;
3503 goto out;
3504 }
3505 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003506 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3507 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003508 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003509 goto out;
3510 }
3511 *tv = MS_TO_TICKS(timeout);
3512 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003513 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003514 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003515 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3516 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003517 err_code |= ERR_ALERT | ERR_FATAL;
3518 goto out;
3519 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003520
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003521 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003522 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003523 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003524 if (kwm == 1)
3525 curagent->flags &= ~SPOE_FL_PIPELINING;
3526 else
3527 curagent->flags |= SPOE_FL_PIPELINING;
3528 goto out;
3529 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003530 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003531 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003532 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003533 if (kwm == 1)
3534 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003535 else
3536 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003537 goto out;
3538 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003539 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003540 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3541 goto out;
3542 if (kwm == 1)
3543 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3544 else
3545 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3546 goto out;
3547 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003548 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003549 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3550 goto out;
3551 if (kwm == 1)
3552 curpxopts2 &= ~PR_O2_NOLOGNORM;
3553 else
3554 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003555 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003556 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003557
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003558 /* Following options does not support negation */
3559 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003560 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3561 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003562 err_code |= ERR_ALERT | ERR_FATAL;
3563 goto out;
3564 }
3565
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003566 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003567 char *tmp;
3568
3569 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003570 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3571 file, linenum, args[0],
3572 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003573 err_code |= ERR_ALERT | ERR_FATAL;
3574 goto out;
3575 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003576 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3577 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003578 tmp = args[2];
3579 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003580 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003581 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003582 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003583 err_code |= ERR_ALERT | ERR_FATAL;
3584 goto out;
3585 }
3586 tmp++;
3587 }
3588 curagent->var_pfx = strdup(args[2]);
3589 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003590 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003591 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3592 goto out;
3593 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3594 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003595 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003596 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003597 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003598 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3599 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003600 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003601 char *tmp;
3602
3603 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003604 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3605 file, linenum, args[0],
3606 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003607 err_code |= ERR_ALERT | ERR_FATAL;
3608 goto out;
3609 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003610 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3611 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003612 tmp = args[2];
3613 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003614 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003615 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003616 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003617 err_code |= ERR_ALERT | ERR_FATAL;
3618 goto out;
3619 }
3620 tmp++;
3621 }
3622 curagent->var_on_error = strdup(args[2]);
3623 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003624 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003625 char *tmp;
3626
3627 if (!*args[2]) {
3628 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3629 file, linenum, args[0],
3630 args[1]);
3631 err_code |= ERR_ALERT | ERR_FATAL;
3632 goto out;
3633 }
3634 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3635 goto out;
3636 tmp = args[2];
3637 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003638 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003639 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003640 file, linenum, args[0], args[1]);
3641 err_code |= ERR_ALERT | ERR_FATAL;
3642 goto out;
3643 }
3644 tmp++;
3645 }
3646 curagent->var_t_process = strdup(args[2]);
3647 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003648 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003649 char *tmp;
3650
3651 if (!*args[2]) {
3652 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3653 file, linenum, args[0],
3654 args[1]);
3655 err_code |= ERR_ALERT | ERR_FATAL;
3656 goto out;
3657 }
3658 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3659 goto out;
3660 tmp = args[2];
3661 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003662 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003663 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003664 file, linenum, args[0], args[1]);
3665 err_code |= ERR_ALERT | ERR_FATAL;
3666 goto out;
3667 }
3668 tmp++;
3669 }
3670 curagent->var_t_total = strdup(args[2]);
3671 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003672 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003673 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3674 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003675 err_code |= ERR_ALERT | ERR_FATAL;
3676 goto out;
3677 }
Christopher Faulet48026722016-11-16 15:01:12 +01003678 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003679 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003680 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003681 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3682 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003683 err_code |= ERR_ALERT | ERR_FATAL;
3684 goto out;
3685 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003686 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003687 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003688 curagent->cps_max = atol(args[1]);
3689 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003690 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003691 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003692 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3693 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003697 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003698 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003699 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003700 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003701 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003702 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003703 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3704 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003705 err_code |= ERR_ALERT | ERR_FATAL;
3706 goto out;
3707 }
3708 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3709 goto out;
3710 curagent->max_frame_size = atol(args[1]);
3711 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3712 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003713 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3714 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003715 err_code |= ERR_ALERT | ERR_FATAL;
3716 goto out;
3717 }
3718 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003719 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003720 if (!*args[1]) {
3721 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3722 file, linenum, args[0]);
3723 err_code |= ERR_ALERT | ERR_FATAL;
3724 goto out;
3725 }
3726 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3727 goto out;
3728 curagent->max_fpa = atol(args[1]);
3729 if (curagent->max_fpa < 1) {
3730 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3731 file, linenum, args[0]);
3732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
3735 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003736 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003737 int cur_arg;
3738
3739 if (!*args[1]) {
3740 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3741 file, linenum, args[0]);
3742 err_code |= ERR_ALERT | ERR_FATAL;
3743 goto out;
3744 }
3745 cur_arg = 1;
3746 while (*args[cur_arg]) {
3747 struct spoe_var_placeholder *vph;
3748
3749 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3750 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3751 err_code |= ERR_ALERT | ERR_ABORT;
3752 goto out;
3753 }
3754 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003755 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003756 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3757 err_code |= ERR_ALERT | ERR_ABORT;
3758 goto out;
3759 }
3760 LIST_ADDQ(&curvars, &vph->list);
3761 cur_arg++;
3762 }
3763 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003764 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003765 char *errmsg = NULL;
3766
3767 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3768 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3769 err_code |= ERR_ALERT | ERR_FATAL;
3770 goto out;
3771 }
3772 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003773 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003774 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3775 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003776 err_code |= ERR_ALERT | ERR_FATAL;
3777 goto out;
3778 }
3779 out:
3780 return err_code;
3781}
Christopher Faulet11610f32017-09-21 10:23:10 +02003782static int
3783cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3784{
3785 struct spoe_group *grp;
3786 const char *err;
3787 int err_code = 0;
3788
3789 if ((cfg_scope == NULL && curengine != NULL) ||
3790 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003791 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003792 goto out;
3793
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003794 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003795 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003796 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3797 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003798 err_code |= ERR_ALERT | ERR_ABORT;
3799 goto out;
3800 }
3801 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3802 err_code |= ERR_ABORT;
3803 goto out;
3804 }
3805
3806 err = invalid_char(args[1]);
3807 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003808 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3809 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003810 err_code |= ERR_ALERT | ERR_ABORT;
3811 goto out;
3812 }
3813
3814 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003815 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003816 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3817 " name as another one declared at %s:%d.\n",
3818 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003819 err_code |= ERR_ALERT | ERR_FATAL;
3820 goto out;
3821 }
3822 }
3823
3824 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003825 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003826 err_code |= ERR_ALERT | ERR_ABORT;
3827 goto out;
3828 }
3829
3830 curgrp->id = strdup(args[1]);
3831 curgrp->conf.file = strdup(file);
3832 curgrp->conf.line = linenum;
3833 LIST_INIT(&curgrp->phs);
3834 LIST_INIT(&curgrp->messages);
3835 LIST_ADDQ(&curgrps, &curgrp->list);
3836 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003837 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003838 int cur_arg = 1;
3839 while (*args[cur_arg]) {
3840 struct spoe_placeholder *ph = NULL;
3841
3842 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003843 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003844 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3845 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003846 err_code |= ERR_ALERT | ERR_FATAL;
3847 goto out;
3848 }
3849 }
3850
3851 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003852 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003853 err_code |= ERR_ALERT | ERR_ABORT;
3854 goto out;
3855 }
3856 ph->id = strdup(args[cur_arg]);
3857 LIST_ADDQ(&curgrp->phs, &ph->list);
3858 cur_arg++;
3859 }
3860 }
3861 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3863 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003864 err_code |= ERR_ALERT | ERR_FATAL;
3865 goto out;
3866 }
3867 out:
3868 return err_code;
3869}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003870
3871static int
3872cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3873{
3874 struct spoe_message *msg;
3875 struct spoe_arg *arg;
3876 const char *err;
3877 char *errmsg = NULL;
3878 int err_code = 0;
3879
3880 if ((cfg_scope == NULL && curengine != NULL) ||
3881 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003882 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003883 goto out;
3884
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003885 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003886 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003887 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3888 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003889 err_code |= ERR_ALERT | ERR_ABORT;
3890 goto out;
3891 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003892 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3893 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003894 goto out;
3895 }
3896
3897 err = invalid_char(args[1]);
3898 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003899 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3900 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003901 err_code |= ERR_ALERT | ERR_ABORT;
3902 goto out;
3903 }
3904
3905 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003906 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003907 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3908 " name as another one declared at %s:%d.\n",
3909 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003910 err_code |= ERR_ALERT | ERR_FATAL;
3911 goto out;
3912 }
3913 }
3914
3915 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003916 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003917 err_code |= ERR_ALERT | ERR_ABORT;
3918 goto out;
3919 }
3920
3921 curmsg->id = strdup(args[1]);
3922 curmsg->id_len = strlen(curmsg->id);
3923 curmsg->event = SPOE_EV_NONE;
3924 curmsg->conf.file = strdup(file);
3925 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003926 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003928 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003929 LIST_INIT(&curmsg->by_evt);
3930 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003931 LIST_ADDQ(&curmsgs, &curmsg->list);
3932 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003933 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934 int cur_arg = 1;
3935
3936 curproxy->conf.args.ctx = ARGC_SPOE;
3937 curproxy->conf.args.file = file;
3938 curproxy->conf.args.line = linenum;
3939 while (*args[cur_arg]) {
3940 char *delim = strchr(args[cur_arg], '=');
3941 int idx = 0;
3942
3943 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003944 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003945 err_code |= ERR_ALERT | ERR_ABORT;
3946 goto out;
3947 }
3948
3949 if (!delim) {
3950 arg->name = NULL;
3951 arg->name_len = 0;
3952 delim = args[cur_arg];
3953 }
3954 else {
3955 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3956 arg->name_len = delim - args[cur_arg];
3957 delim++;
3958 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003959 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3960 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003961 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003962 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003963 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003964 err_code |= ERR_ALERT | ERR_FATAL;
3965 free(arg->name);
3966 free(arg);
3967 goto out;
3968 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003969 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003970 LIST_ADDQ(&curmsg->args, &arg->list);
3971 cur_arg++;
3972 }
3973 curproxy->conf.args.file = NULL;
3974 curproxy->conf.args.line = 0;
3975 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003976 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02003977 err = invalid_char(args[1]);
3978 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003979 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3980 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003981 err_code |= ERR_ALERT | ERR_FATAL;
3982 goto out;
3983 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003984 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003985 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003986 "logical disjunction within a condition.\n",
3987 file, linenum, args[1]);
3988 err_code |= ERR_ALERT | ERR_FATAL;
3989 goto out;
3990 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003991 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003992 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3993 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003994 err_code |= ERR_ALERT | ERR_FATAL;
3995 goto out;
3996 }
3997 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003998 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003999 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004000 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004001 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004002 goto out;
4003 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004004 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4005 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004006
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004007 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004008 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004009 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004010 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4011
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004012 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004013 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004014 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004015 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004016 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004017 curmsg->event = SPOE_EV_ON_TCP_RSP;
4018
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004019 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004020 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004021 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004022 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004023 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004024 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4025 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004026 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004027 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004028 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004029 goto out;
4030 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004031
4032 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4033 struct acl_cond *cond;
4034
4035 cond = build_acl_cond(file, linenum, &curmsg->acls,
4036 curproxy, (const char **)args+2,
4037 &errmsg);
4038 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004039 ha_alert("parsing [%s:%d] : error detected while "
4040 "parsing an 'event %s' condition : %s.\n",
4041 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004042 err_code |= ERR_ALERT | ERR_FATAL;
4043 goto out;
4044 }
4045 curmsg->cond = cond;
4046 }
4047 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004048 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4049 "or 'unless' followed by a condition but found '%s'.\n",
4050 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004051 err_code |= ERR_ALERT | ERR_FATAL;
4052 goto out;
4053 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004054 }
4055 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004056 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4057 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 err_code |= ERR_ALERT | ERR_FATAL;
4059 goto out;
4060 }
4061 out:
4062 free(errmsg);
4063 return err_code;
4064}
4065
4066/* Return -1 on error, else 0 */
4067static int
4068parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4069 struct flt_conf *fconf, char **err, void *private)
4070{
4071 struct list backup_sections;
4072 struct spoe_config *conf;
4073 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004074 struct spoe_group *grp, *grpback;
4075 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004076 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004077 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004078 char *file = NULL, *engine = NULL;
4079 int ret, pos = *cur_arg + 1;
4080
Christopher Faulet84c844e2018-03-23 14:37:14 +01004081 LIST_INIT(&curmsgs);
4082 LIST_INIT(&curgrps);
4083 LIST_INIT(&curmphs);
4084 LIST_INIT(&curgphs);
4085 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004086 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004087 curpxopts = 0;
4088 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004089
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004090 conf = calloc(1, sizeof(*conf));
4091 if (conf == NULL) {
4092 memprintf(err, "%s: out of memory", args[*cur_arg]);
4093 goto error;
4094 }
4095 conf->proxy = px;
4096
4097 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004098 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004099 if (!*args[pos+1]) {
4100 memprintf(err, "'%s' : '%s' option without value",
4101 args[*cur_arg], args[pos]);
4102 goto error;
4103 }
4104 file = args[pos+1];
4105 pos += 2;
4106 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004107 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004108 if (!*args[pos+1]) {
4109 memprintf(err, "'%s' : '%s' option without value",
4110 args[*cur_arg], args[pos]);
4111 goto error;
4112 }
4113 engine = args[pos+1];
4114 pos += 2;
4115 }
4116 else {
4117 memprintf(err, "unknown keyword '%s'", args[pos]);
4118 goto error;
4119 }
4120 }
4121 if (file == NULL) {
4122 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4123 goto error;
4124 }
4125
4126 /* backup sections and register SPOE sections */
4127 LIST_INIT(&backup_sections);
4128 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004129 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4130 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004131 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004132
4133 /* Parse SPOE filter configuration file */
4134 curengine = engine;
4135 curproxy = px;
4136 curagent = NULL;
4137 curmsg = NULL;
4138 ret = readcfgfile(file);
4139 curproxy = NULL;
4140
4141 /* unregister SPOE sections and restore previous sections */
4142 cfg_unregister_sections();
4143 cfg_restore_sections(&backup_sections);
4144
4145 if (ret == -1) {
4146 memprintf(err, "Could not open configuration file %s : %s",
4147 file, strerror(errno));
4148 goto error;
4149 }
4150 if (ret & (ERR_ABORT|ERR_FATAL)) {
4151 memprintf(err, "Error(s) found in configuration file %s", file);
4152 goto error;
4153 }
4154
4155 /* Check SPOE agent */
4156 if (curagent == NULL) {
4157 memprintf(err, "No SPOE agent found in file %s", file);
4158 goto error;
4159 }
4160 if (curagent->b.name == NULL) {
4161 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4162 curagent->id, curagent->conf.file, curagent->conf.line);
4163 goto error;
4164 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004165 if (curagent->timeout.hello == TICK_ETERNITY ||
4166 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004167 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004168 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4169 " | While not properly invalid, you will certainly encounter various problems\n"
4170 " | with such a configuration. To fix this, please ensure that all following\n"
4171 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4172 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004173 }
4174 if (curagent->var_pfx == NULL) {
4175 char *tmp = curagent->id;
4176
4177 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004178 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004179 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4180 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4181 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4182 goto error;
4183 }
4184 tmp++;
4185 }
4186 curagent->var_pfx = strdup(curagent->id);
4187 }
4188
Christopher Fauletb7426d12018-03-21 14:12:17 +01004189 if (curagent->var_on_error) {
4190 struct arg arg;
4191
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004192 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004193 curagent->var_pfx, curagent->var_on_error);
4194
4195 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004196 arg.data.str.area = trash.area;
4197 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004198 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004199 if (!vars_check_arg(&arg, err)) {
4200 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4201 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4202 goto error;
4203 }
4204 }
4205
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004206 if (curagent->var_t_process) {
4207 struct arg arg;
4208
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004209 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004210 curagent->var_pfx, curagent->var_t_process);
4211
4212 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004213 arg.data.str.area = trash.area;
4214 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004215 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004216 if (!vars_check_arg(&arg, err)) {
4217 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4218 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4219 goto error;
4220 }
4221 }
4222
4223 if (curagent->var_t_total) {
4224 struct arg arg;
4225
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004226 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004227 curagent->var_pfx, curagent->var_t_total);
4228
4229 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004230 arg.data.str.area = trash.area;
4231 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004232 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004233 if (!vars_check_arg(&arg, err)) {
4234 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4235 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4236 goto error;
4237 }
4238 }
4239
Christopher Faulet11610f32017-09-21 10:23:10 +02004240 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004241 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4242 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004243 goto finish;
4244 }
4245
Christopher Faulet11610f32017-09-21 10:23:10 +02004246 /* Replace placeholders by the corresponding messages for the SPOE
4247 * agent */
4248 list_for_each_entry(ph, &curmphs, list) {
4249 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004250 struct spoe_arg *arg;
4251 unsigned int where;
4252
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004253 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004254 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4255 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4256 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4257 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4258 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4259 }
4260 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4261 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4262 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004263 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4264 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004265 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004266 }
4267 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004268 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4269 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004270 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004271 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004272
4273 where = 0;
4274 switch (msg->event) {
4275 case SPOE_EV_ON_CLIENT_SESS:
4276 where |= SMP_VAL_FE_CON_ACC;
4277 break;
4278
4279 case SPOE_EV_ON_TCP_REQ_FE:
4280 where |= SMP_VAL_FE_REQ_CNT;
4281 break;
4282
4283 case SPOE_EV_ON_HTTP_REQ_FE:
4284 where |= SMP_VAL_FE_HRQ_HDR;
4285 break;
4286
4287 case SPOE_EV_ON_TCP_REQ_BE:
4288 if (px->cap & PR_CAP_FE)
4289 where |= SMP_VAL_FE_REQ_CNT;
4290 if (px->cap & PR_CAP_BE)
4291 where |= SMP_VAL_BE_REQ_CNT;
4292 break;
4293
4294 case SPOE_EV_ON_HTTP_REQ_BE:
4295 if (px->cap & PR_CAP_FE)
4296 where |= SMP_VAL_FE_HRQ_HDR;
4297 if (px->cap & PR_CAP_BE)
4298 where |= SMP_VAL_BE_HRQ_HDR;
4299 break;
4300
4301 case SPOE_EV_ON_SERVER_SESS:
4302 where |= SMP_VAL_BE_SRV_CON;
4303 break;
4304
4305 case SPOE_EV_ON_TCP_RSP:
4306 if (px->cap & PR_CAP_FE)
4307 where |= SMP_VAL_FE_RES_CNT;
4308 if (px->cap & PR_CAP_BE)
4309 where |= SMP_VAL_BE_RES_CNT;
4310 break;
4311
4312 case SPOE_EV_ON_HTTP_RSP:
4313 if (px->cap & PR_CAP_FE)
4314 where |= SMP_VAL_FE_HRS_HDR;
4315 if (px->cap & PR_CAP_BE)
4316 where |= SMP_VAL_BE_HRS_HDR;
4317 break;
4318
4319 default:
4320 break;
4321 }
4322
4323 list_for_each_entry(arg, &msg->args, list) {
4324 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004325 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004326 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004327 "none of which is available here ('%s')",
4328 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004329 sample_ckp_names(arg->expr->fetch->use),
4330 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004331 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004332 }
4333 }
4334
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004335 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004336 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4337 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004338 }
4339 }
4340 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004341 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004342 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004343 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004344 continue;
4345 }
4346
Christopher Faulet11610f32017-09-21 10:23:10 +02004347 /* Replace placeholders by the corresponding groups for the SPOE
4348 * agent */
4349 list_for_each_entry(ph, &curgphs, list) {
4350 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004351 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004352 grp->agent = curagent;
4353 LIST_DEL(&grp->list);
4354 LIST_ADDQ(&curagent->groups, &grp->list);
4355 goto next_aph;
4356 }
4357 }
4358 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4359 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4360 goto error;
4361 next_aph:
4362 continue;
4363 }
4364
4365 /* Replace placeholders by the corresponding message for each SPOE
4366 * group of the SPOE agent */
4367 list_for_each_entry(grp, &curagent->groups, list) {
4368 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4369 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004370 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004371 if (msg->group != NULL) {
4372 memprintf(err, "SPOE message '%s' already belongs to "
4373 "the SPOE group '%s' declare at %s:%d",
4374 msg->id, msg->group->id,
4375 msg->group->conf.file,
4376 msg->group->conf.line);
4377 goto error;
4378 }
4379
4380 /* Scope for arguments are not checked for now. We will check
4381 * them only if a rule use the corresponding SPOE group. */
4382 msg->agent = curagent;
4383 msg->group = grp;
4384 LIST_DEL(&ph->list);
4385 LIST_ADDQ(&grp->messages, &msg->by_grp);
4386 goto next_mph_grp;
4387 }
4388 }
4389 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4390 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4391 goto error;
4392 next_mph_grp:
4393 continue;
4394 }
4395 }
4396
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004397 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004398 /* move curmsgs to the agent message list */
4399 curmsgs.n->p = &curagent->messages;
4400 curmsgs.p->n = &curagent->messages;
4401 curagent->messages = curmsgs;
4402 LIST_INIT(&curmsgs);
4403
Christopher Faulet7ee86672017-09-19 11:08:28 +02004404 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004405 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004406
4407 /* Start agent's proxy initialization here. It will be finished during
4408 * the filter init. */
4409 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4410 init_new_proxy(&conf->agent_fe);
4411 conf->agent_fe.id = conf->agent->id;
4412 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004413 conf->agent_fe.options |= curpxopts;
4414 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004415
4416 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4417 LIST_DEL(&logsrv->list);
4418 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4419 }
4420
Christopher Faulet11610f32017-09-21 10:23:10 +02004421 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4422 LIST_DEL(&ph->list);
4423 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004424 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004425 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4426 LIST_DEL(&ph->list);
4427 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004428 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004429 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4430 struct arg arg;
4431
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004432 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004433 curagent->var_pfx, vph->name);
4434
4435 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004436 arg.data.str.area = trash.area;
4437 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004438 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004439 if (!vars_check_arg(&arg, err)) {
4440 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4441 curagent->id, curagent->var_pfx, vph->name, *err);
4442 goto error;
4443 }
4444
4445 LIST_DEL(&vph->list);
4446 free(vph->name);
4447 free(vph);
4448 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004449 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4450 LIST_DEL(&grp->list);
4451 spoe_release_group(grp);
4452 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004453 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004454 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004455 fconf->ops = &spoe_ops;
4456 fconf->conf = conf;
4457 return 0;
4458
4459 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004460 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004461 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4462 LIST_DEL(&ph->list);
4463 spoe_release_placeholder(ph);
4464 }
4465 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4466 LIST_DEL(&ph->list);
4467 spoe_release_placeholder(ph);
4468 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004469 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4470 LIST_DEL(&vph->list);
4471 free(vph->name);
4472 free(vph);
4473 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004474 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4475 LIST_DEL(&grp->list);
4476 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004477 }
4478 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4479 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004480 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004481 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004482 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4483 LIST_DEL(&logsrv->list);
4484 free(logsrv);
4485 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004486 free(conf);
4487 return -1;
4488}
4489
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004490/* Send message of a SPOE group. This is the action_ptr callback of a rule
4491 * associated to a "send-spoe-group" action.
4492 *
Christopher Faulet13403762019-12-13 09:01:57 +01004493 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4494 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004495static enum act_return
4496spoe_send_group(struct act_rule *rule, struct proxy *px,
4497 struct session *sess, struct stream *s, int flags)
4498{
4499 struct filter *filter;
4500 struct spoe_agent *agent = NULL;
4501 struct spoe_group *group = NULL;
4502 struct spoe_context *ctx = NULL;
4503 int ret, dir;
4504
4505 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4506 if (filter->config == rule->arg.act.p[0]) {
4507 agent = rule->arg.act.p[2];
4508 group = rule->arg.act.p[3];
4509 ctx = filter->ctx;
4510 break;
4511 }
4512 }
4513 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004514 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004515 if (ctx->state == SPOE_CTX_ST_NONE)
4516 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004517
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004518 switch (rule->from) {
4519 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4520 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4521 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4522 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4523 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4524 default:
4525 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4526 " - internal error while execute spoe-send-group\n",
4527 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4528 __FUNCTION__, s);
4529 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4530 agent->id);
4531 return ACT_RET_CONT;
4532 }
4533
4534 ret = spoe_process_group(s, ctx, group, dir);
4535 if (ret == 1)
4536 return ACT_RET_CONT;
4537 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004538 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004539 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4540 " - failed to process group '%s': interrupted by caller\n",
4541 (int)now.tv_sec, (int)now.tv_usec,
4542 agent->id, __FUNCTION__, s, group->id);
4543 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004544 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004545 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004546 return ACT_RET_CONT;
4547 }
4548 return ACT_RET_YIELD;
4549 }
4550 else
Christopher Faulet13403762019-12-13 09:01:57 +01004551 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004552}
4553
4554/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4555 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4556 * action should be:
4557 *
4558 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4559 *
4560 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4561 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4562 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4563 * group.
4564 *
4565 * The function returns 1 in success case, otherwise, it returns 0 and err is
4566 * filled.
4567 */
4568static int
4569check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4570{
4571 struct flt_conf *fconf;
4572 struct spoe_config *conf;
4573 struct spoe_agent *agent = NULL;
4574 struct spoe_group *group;
4575 struct spoe_message *msg;
4576 char *engine_id = rule->arg.act.p[0];
4577 char *group_id = rule->arg.act.p[1];
4578 unsigned int where = 0;
4579
4580 switch (rule->from) {
4581 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4582 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4583 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4584 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4585 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4586 default:
4587 memprintf(err,
4588 "internal error, unexpected rule->from=%d, please report this bug!",
4589 rule->from);
4590 goto error;
4591 }
4592
4593 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4594 * <px> */
4595 list_for_each_entry(fconf, &px->filter_configs, list) {
4596 conf = fconf->conf;
4597
4598 /* This is not an SPOE filter */
4599 if (fconf->id != spoe_filter_id)
4600 continue;
4601
4602 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004603 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004604 agent = conf->agent;
4605 break;
4606 }
4607 }
4608 if (agent == NULL) {
4609 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4610 engine_id, group_id);
4611 goto error;
4612 }
4613
4614 /* Try to find the right group */
4615 list_for_each_entry(group, &agent->groups, list) {
4616 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004617 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004618 break;
4619 }
4620 if (&group->list == &agent->groups) {
4621 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4622 group_id, engine_id);
4623 goto error;
4624 }
4625
4626 /* Ok, we found the group, we need to check messages and their
4627 * arguments */
4628 list_for_each_entry(msg, &group->messages, by_grp) {
4629 struct spoe_arg *arg;
4630
4631 list_for_each_entry(arg, &msg->args, list) {
4632 if (!(arg->expr->fetch->val & where)) {
4633 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4634 "some args extract information from '%s',"
4635 "none of which is available here ('%s')",
4636 msg->id, group->id, msg->conf.file, msg->conf.line,
4637 sample_ckp_names(arg->expr->fetch->use),
4638 sample_ckp_names(where));
4639 goto error;
4640 }
4641 }
4642 }
4643
4644 free(engine_id);
4645 free(group_id);
4646 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4647 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4648 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4649 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4650 return 1;
4651
4652 error:
4653 free(engine_id);
4654 free(group_id);
4655 return 0;
4656}
4657
4658/* Parse 'send-spoe-group' action following the format:
4659 *
4660 * ... send-spoe-group <engine-id> <group-id>
4661 *
4662 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4663 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4664 * ids are saved and used later, when the rule will be checked.
4665 */
4666static enum act_parse_ret
4667parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4668 struct act_rule *rule, char **err)
4669{
4670 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4671 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4672 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4673 return ACT_RET_PRS_ERR;
4674 }
4675 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4676 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4677
4678 (*orig_arg) += 2;
4679
4680 rule->action = ACT_CUSTOM;
4681 rule->action_ptr = spoe_send_group;
4682 rule->check_ptr = check_send_spoe_group;
4683 return ACT_RET_PRS_OK;
4684}
4685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004686
4687/* Declare the filter parser for "spoe" keyword */
4688static struct flt_kw_list flt_kws = { "SPOE", { }, {
4689 { "spoe", parse_spoe_flt, NULL },
4690 { NULL, NULL, NULL },
4691 }
4692};
4693
Willy Tarreau0108d902018-11-25 19:14:37 +01004694INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4695
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004696/* Delcate the action parser for "spoe-action" keyword */
4697static struct action_kw_list tcp_req_action_kws = { { }, {
4698 { "send-spoe-group", parse_send_spoe_group },
4699 { /* END */ },
4700 }
4701};
Willy Tarreau0108d902018-11-25 19:14:37 +01004702
4703INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4704
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004705static struct action_kw_list tcp_res_action_kws = { { }, {
4706 { "send-spoe-group", parse_send_spoe_group },
4707 { /* END */ },
4708 }
4709};
Willy Tarreau0108d902018-11-25 19:14:37 +01004710
4711INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4712
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004713static struct action_kw_list http_req_action_kws = { { }, {
4714 { "send-spoe-group", parse_send_spoe_group },
4715 { /* END */ },
4716 }
4717};
Willy Tarreau0108d902018-11-25 19:14:37 +01004718
4719INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4720
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004721static struct action_kw_list http_res_action_kws = { { }, {
4722 { "send-spoe-group", parse_send_spoe_group },
4723 { /* END */ },
4724 }
4725};
4726
Willy Tarreau0108d902018-11-25 19:14:37 +01004727INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);