blob: f5094c02ffb12046cd8302e31d420a0db4d051e2 [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>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020018#include <common/cfgparse.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020019#include <haproxy/filters.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020020#include <haproxy/frontend.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020021#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020022#include <haproxy/log.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020023#include <haproxy/sample.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020024#include <haproxy/signal.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020025#include <haproxy/thread.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020027#include <haproxy/session.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020029#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020030#include <haproxy/tcp_rules.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020031#include <haproxy/time.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020032#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020034#include <haproxy/arg-t.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020035#include <haproxy/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010036#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020037
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020038#include <haproxy/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020039#include <proto/backend.h>
Willy Tarreau66347942020-06-01 12:18:08 +020040#include <haproxy/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/proxy.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010042#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043#include <proto/stream.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020044
45#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
46#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010047#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020048#else
49#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010050#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020051#endif
52
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010053/* Reserved 4 bytes to the frame size. So a frame and its size can be written
54 * together in a buffer */
55#define MAX_FRAME_SIZE global.tune.bufsize - 4
56
57/* The minimum size for a frame */
58#define MIN_FRAME_SIZE 256
59
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Reserved for the metadata and the frame type.
61 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010062#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020063
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010065#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
66
Christopher Faulet3b386a32017-02-23 10:17:15 +010067/* SPOE filter id. Used to identify SPOE filters */
68const char *spoe_filter_id = "SPOE filter";
69
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020070/* Set if the handle on SIGUSR1 is registered */
71static int sighandler_registered = 0;
72
73/* proxy used during the parsing */
74struct proxy *curproxy = NULL;
75
76/* The name of the SPOE engine, used during the parsing */
77char *curengine = NULL;
78
79/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020080/* SPOE agent/group/message used during the parsing */
81struct spoe_agent *curagent = NULL;
82struct spoe_group *curgrp = NULL;
83struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020084
85/* list of SPOE messages and placeholders used during the parsing */
86struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020087struct list curgrps;
88struct list curmphs;
89struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010090struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020091
Christopher Faulet7250b8f2018-03-26 17:19:01 +020092/* list of log servers used during the parsing */
93struct list curlogsrvs;
94
Christopher Faulet0e0f0852018-03-26 17:20:36 +020095/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
96int curpxopts;
97int curpxopts2;
98
Christopher Faulet42bfa462017-01-04 14:14:19 +010099/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100100DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
101DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200102
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200103struct flt_ops spoe_ops;
104
Christopher Faulet8ef75252017-02-20 22:56:03 +0100105static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200106static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
107static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200108
109/********************************************************************
110 * helper functions/globals
111 ********************************************************************/
112static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200113spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114{
Christopher Faulet11610f32017-09-21 10:23:10 +0200115 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 free(ph->id);
118 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119}
120
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100122spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123{
Christopher Faulet57583e42017-09-04 15:41:09 +0200124 struct spoe_arg *arg, *argback;
125 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200126
127 if (!msg)
128 return;
129 free(msg->id);
130 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200131 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200132 release_sample_expr(arg->expr);
133 free(arg->name);
134 LIST_DEL(&arg->list);
135 free(arg);
136 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200137 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
138 LIST_DEL(&acl->list);
139 prune_acl(acl);
140 free(acl);
141 }
142 if (msg->cond) {
143 prune_acl_cond(msg->cond);
144 free(msg->cond);
145 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200146 free(msg);
147}
148
149static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200150spoe_release_group(struct spoe_group *grp)
151{
152 if (!grp)
153 return;
154 free(grp->id);
155 free(grp->conf.file);
156 free(grp);
157}
158
159static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100160spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200161{
Christopher Faulet11610f32017-09-21 10:23:10 +0200162 struct spoe_message *msg, *msgback;
163 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200164 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200165
166 if (!agent)
167 return;
168 free(agent->id);
169 free(agent->conf.file);
170 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100171 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100172 free(agent->var_t_process);
173 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200174 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
175 LIST_DEL(&msg->list);
176 spoe_release_message(msg);
177 }
178 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
179 LIST_DEL(&grp->list);
180 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200181 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100182 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200183 for (i = 0; i < global.nbthread; ++i) {
184 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100185 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200186 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200188 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200189 free(agent);
190}
191
192static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100193 [SPOE_FRM_ERR_NONE] = "normal",
194 [SPOE_FRM_ERR_IO] = "I/O error",
195 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
196 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
197 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
198 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
199 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
200 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
201 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
202 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
203 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
204 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100205 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100206 [SPOE_FRM_ERR_RES] = "resource allocation error",
207 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200208};
209
210static const char *spoe_event_str[SPOE_EV_EVENTS] = {
211 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
212 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
213 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
214 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
215 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
216
217 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
218 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
219 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
220};
221
222
223#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
224
225static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100226 [SPOE_CTX_ST_NONE] = "NONE",
227 [SPOE_CTX_ST_READY] = "READY",
228 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
229 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
230 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
231 [SPOE_CTX_ST_DONE] = "DONE",
232 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200233};
234
235static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100236 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
237 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
238 [SPOE_APPCTX_ST_IDLE] = "IDLE",
239 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
240 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
241 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
242 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
243 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
244 [SPOE_APPCTX_ST_EXIT] = "EXIT",
245 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200246};
247
248#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100249
Christopher Faulet8ef75252017-02-20 22:56:03 +0100250/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500251 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100252 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100253static char *
254generate_pseudo_uuid()
255{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100256 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800257 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100258}
259
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100260
261static inline void
262spoe_update_stat_time(struct timeval *tv, long *t)
263{
264 if (*t == -1)
265 *t = tv_ms_elapsed(tv, &now);
266 else
267 *t += tv_ms_elapsed(tv, &now);
268 tv_zero(tv);
269}
270
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/********************************************************************
272 * Functions that encode/decode SPOE frames
273 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200274/* Helper to get static string length, excluding the terminating null byte */
275#define SLEN(str) (sizeof(str)-1)
276
277/* Predefined key used in HELLO/DISCONNECT frames */
278#define SUPPORTED_VERSIONS_KEY "supported-versions"
279#define VERSION_KEY "version"
280#define MAX_FRAME_SIZE_KEY "max-frame-size"
281#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100282#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100283#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200284#define STATUS_CODE_KEY "status-code"
285#define MSG_KEY "message"
286
287struct spoe_version {
288 char *str;
289 int min;
290 int max;
291};
292
293/* All supported versions */
294static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200295 /* 1.0 is now unsupported because of a bug about frame's flags*/
296 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297 {NULL, 0, 0}
298};
299
300/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200301#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200302
Christopher Faulet8ef75252017-02-20 22:56:03 +0100303/* Convert a string to a SPOE version value. The string must follow the format
304 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
305 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100307spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309 const char *p, *end;
310 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200311
Christopher Faulet8ef75252017-02-20 22:56:03 +0100312 p = str;
313 end = str+len;
314 maj = min = 0;
315 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200316
Christopher Faulet8ef75252017-02-20 22:56:03 +0100317 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100318 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 /* parse Major number, until the '.' */
322 while (*p != '.') {
323 if (p >= end || *p < '0' || *p > '9')
324 goto out;
325 maj *= 10;
326 maj += (*p - '0');
327 p++;
328 }
329
330 /* check Major version */
331 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332 goto out;
333
Christopher Faulet8ef75252017-02-20 22:56:03 +0100334 p++; /* skip the '.' */
335 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
336 goto out;
337
338 /* Parse Minor number */
339 while (p < end) {
340 if (*p < '0' || *p > '9')
341 break;
342 min *= 10;
343 min += (*p - '0');
344 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200345 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100346
347 /* check Minor number */
348 if (min > 999)
349 goto out;
350
351 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100352 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100353 p++;
354 if (p != end)
355 goto out;
356
357 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200358 out:
359 return vsn;
360}
361
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800363 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200365static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200367{
Willy Tarreau83061a82018-07-13 11:56:34 +0200368 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100369 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370 char *p, *end;
371 unsigned int flags = SPOE_FRM_FL_FIN;
372 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200373
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374 p = frame;
375 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200376
Christopher Faulet8ef75252017-02-20 22:56:03 +0100377 /* Set Frame type */
378 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100380 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200381 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100382 memcpy(p, (char *)&flags, 4);
383 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200387
388 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
389 * and "capabilities" */
390
391 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392 sz = SLEN(SUPPORTED_VERSIONS_KEY);
393 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
394 goto too_big;
395
396 *p++ = SPOE_DATA_T_STR;
397 sz = SLEN(SUPPORTED_VERSIONS_VAL);
398 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
399 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
401 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 sz = SLEN(MAX_FRAME_SIZE_KEY);
403 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
404 goto too_big;
405
406 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200407 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
410 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100411 sz = SLEN(CAPABILITIES_KEY);
412 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
413 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200414
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100416 chk = get_trash_chunk();
417 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200418 memcpy(chk->area, "pipelining", 10);
419 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100420 }
421 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 if (chk->data) chk->area[chk->data++] = ',';
423 memcpy(chk->area+chk->data, "async", 5);
424 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100425 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100426 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200427 if (chk->data) chk->area[chk->data++] = ',';
428 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100429 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100430 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200431 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 goto too_big;
433
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500434 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200435 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 sz = SLEN(ENGINE_ID_KEY);
437 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
438 goto too_big;
439
440 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200441 sz = strlen(agent->rt[tid].engine_id);
442 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100444 }
445
Christopher Faulet8ef75252017-02-20 22:56:03 +0100446 return (p - frame);
447
448 too_big:
449 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
450 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200451}
452
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
454 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
455 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200456static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 const char *reason;
460 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100461 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 p = frame;
465 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200466
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 /* Set Frame type */
468 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100470 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200471 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 memcpy(p, (char *)&flags, 4);
473 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
475 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 *p++ = 0; *p++ = 0;
477
478 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
479 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480
481 /* There are 2 mandatory items: "status-code" and "message" */
482
483 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 sz = SLEN(STATUS_CODE_KEY);
485 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
486 goto too_big;
487
488 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200489 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200491
492 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 sz = SLEN(MSG_KEY);
494 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
495 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 /*Get the message corresponding to the status code */
498 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
499
500 *p++ = SPOE_DATA_T_STR;
501 sz = strlen(reason);
502 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
503 goto too_big;
504
505 return (p - frame);
506
507 too_big:
508 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
509 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200510}
511
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
513 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
514 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100517 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200518{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100519 char *p, *end;
520 unsigned int stream_id, frame_id;
521 unsigned int flags = SPOE_FRM_FL_FIN;
522 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200523
Christopher Faulet8ef75252017-02-20 22:56:03 +0100524 p = frame;
525 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200526
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100527 stream_id = ctx->stream_id;
528 frame_id = ctx->frame_id;
529
530 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
531 /* The fragmentation is not supported by the applet */
532 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
533 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
534 return -1;
535 }
536 flags = ctx->frag_ctx.flags;
537 }
538
539 /* Set Frame type */
540 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
541
542 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200543 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100544 memcpy(p, (char *)&flags, 4);
545 p += 4;
546
547 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200548 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100549 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200550 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 goto too_big;
552
553 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200554 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100555 if (p + sz >= end)
556 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200557 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100558 p += sz;
559
560 return (p - frame);
561
562 too_big:
563 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
564 return 0;
565}
566
567/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
568 * returns the number of encoded bytes in the frame on success, 0 if an encoding
569 * error occurred and -1 if a fatal error occurred. */
570static int
571spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
572 char *frame, size_t size)
573{
574 char *p, *end;
575 unsigned int stream_id, frame_id;
576 unsigned int flags;
577 size_t sz;
578
579 p = frame;
580 end = frame+size;
581
Christopher Faulet8ef75252017-02-20 22:56:03 +0100582 /* <ctx> is null when the stream has aborted the processing of a
583 * fragmented frame. In this case, we must notify the corresponding
584 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100585 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100586 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100587 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
588 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
589 }
590 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100591 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100592 stream_id = ctx->stream_id;
593 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 }
595
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100597 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100599 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200600 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100601 memcpy(p, (char *)&flags, 4);
602 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200603
604 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200605 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100606 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200607 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200609
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100610 if (ctx == NULL)
611 goto end;
612
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200614 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 if (p + sz >= end)
616 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200617 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100619
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100620 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 return (p - frame);
622
623 too_big:
624 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
625 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626}
627
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628/* Decode and process the HELLO frame sent by an agent. It returns the number of
629 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
630 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200631static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633{
Christopher Faulet305c6072017-02-23 16:17:53 +0100634 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
635 char *p, *end;
636 int vsn, max_frame_size;
637 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638
639 p = frame;
640 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200641
642 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
644 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 if (size < 7 /* TYPE + METADATA */) {
649 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
650 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200651 }
652
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100653 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200655 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200657
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100658 /* Fragmentation is not supported for HELLO frame */
659 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100661 return -1;
662 }
663
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 if (*p != 0 || *(p+1) != 0) {
666 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
667 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670
671 /* There are 3 mandatory items: "version", "max-frame-size" and
672 * "capabilities" */
673
674 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100675 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 while (p < end) {
677 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200678 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100679 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680
681 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 ret = spoe_decode_buffer(&p, end, &str, &sz);
683 if (ret == -1 || !sz) {
684 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
685 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 /* Check "version" K/V item */
689 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 int i, type = *p++;
691
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
694 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
695 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
698 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
699 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 }
701
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 return -1;
706 }
707 for (i = 0; supported_versions[i].str != NULL; ++i) {
708 if (vsn >= supported_versions[i].min &&
709 vsn <= supported_versions[i].max)
710 break;
711 }
712 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714 return -1;
715 }
716 }
717 /* Check "max-frame-size" K/V item */
718 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100719 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200720
721 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
723 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
724 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
725 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
727 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200728 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200729 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
731 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 if (sz < MIN_FRAME_SIZE ||
734 sz > SPOE_APPCTX(appctx)->max_frame_size) {
735 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200736 return -1;
737 }
738 max_frame_size = sz;
739 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100740 /* Check "capabilities" K/V item */
741 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100743
744 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100748 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
750 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
751 return 0;
752 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755 char *delim;
756
757 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100758 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759
Christopher Faulet8ef75252017-02-20 22:56:03 +0100760 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
761 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100762 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100763 flags |= SPOE_APPCTX_FL_PIPELINING;
764 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 else if (sz >= 5 && !strncmp(str, "async", 5)) {
766 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100767 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100768 flags |= SPOE_APPCTX_FL_ASYNC;
769 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
771 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100772 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100773 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
774 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 /* Get the next comma or break */
777 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 delim++;
780 sz -= (delim - str);
781 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782 }
783 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200784 else {
785 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 if (spoe_skip_data(&p, end) == -1) {
787 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
788 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200789 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200790 }
791 }
792
793 /* Final checks */
794 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 return -1;
797 }
798 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100799 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800 return -1;
801 }
Christopher Faulet11389012019-02-07 16:13:26 +0100802 if (!agent)
803 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
804 else {
805 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
806 flags &= ~SPOE_APPCTX_FL_PIPELINING;
807 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
808 flags &= ~SPOE_APPCTX_FL_ASYNC;
809 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810
Christopher Faulet42bfa462017-01-04 14:14:19 +0100811 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
812 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
813 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100814
815 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816}
817
818/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
819 * bytes on success, 0 if the frame can be ignored and -1 if an error
820 * occurred. */
821static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200823{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100825 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826
827 p = frame;
828 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829
830 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
832 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 if (size < 7 /* TYPE + METADATA */) {
837 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
838 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200839 }
840
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100841 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200843 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100846 /* Fragmentation is not supported for DISCONNECT frame */
847 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100849 return -1;
850 }
851
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 if (*p != 0 || *(p+1) != 0) {
854 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
855 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858
859 /* There are 2 mandatory items: "status-code" and "message" */
860
861 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 while (p < end) {
863 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200864 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200866
867 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 ret = spoe_decode_buffer(&p, end, &str, &sz);
869 if (ret == -1 || !sz) {
870 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
871 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200872 }
873
874 /* Check "status-code" K/V item */
875 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877
878 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
880 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
881 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
884 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200885 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200886 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100890 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
892
893 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 else if (!memcmp(str, MSG_KEY, sz)) {
895 int type = *p++;
896
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
899 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
900 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 ret = spoe_decode_buffer(&p, end, &str, &sz);
903 if (ret == -1 || sz > 255) {
904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
908 SPOE_APPCTX(appctx)->reason = str;
909 SPOE_APPCTX(appctx)->rlen = sz;
910#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911 }
912 else {
913 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100914 if (spoe_skip_data(&p, end) == -1) {
915 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
916 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
919 }
920
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922}
923
924
Christopher Fauleta1cda022016-12-21 08:58:06 +0100925/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926 * success, 0 if the frame can be ignored and -1 if an error occurred. */
927static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100929 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936
937 p = frame;
938 end = frame + size;
939 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200940
941 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
943 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 if (size < 7 /* TYPE + METADATA */) {
948 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
949 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200950 }
951
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100952 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200954 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200956
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 /* Fragmentation is not supported for now */
958 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100960 return -1;
961 }
962
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200964 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100966 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200968 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100972
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100974 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200975 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100976 if ((*ctx)->stream_id == (unsigned int)stream_id &&
977 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100978 goto found;
979 }
980 }
981 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
983 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100985 goto found;
986 }
987 }
988
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100989 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
990 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
991 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
992
993 /* ABRT bit is set for an unfinished fragmented frame */
994 if (flags & SPOE_FRM_FL_ABRT) {
995 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100996 (*ctx)->state = SPOE_CTX_ST_ERROR;
997 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
998 /* Ignore the payload */
999 goto end;
1000 }
1001 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1002 /* For now, we ignore the ack */
1003 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1004 return 0;
1005 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001006
Christopher Fauleta1cda022016-12-21 08:58:06 +01001007 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001008 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1009 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001010 " - stream-id=%u - frame-id=%u\n",
1011 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1012 __FUNCTION__, appctx,
1013 (unsigned int)stream_id, (unsigned int)frame_id);
1014
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001015 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001016 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1017 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001018 return 0;
1019
1020 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001021 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1022 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001023 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001025 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001026
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001027 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001029 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1030 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001031 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001032
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 /* Transfer the buffer ownership to the SPOE context */
1034 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001035 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001036
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 (*ctx)->state = SPOE_CTX_ST_DONE;
1038
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001039 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041 " - ACK frame received"
1042 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001043 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001044 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1045 (*ctx)->frame_id, flags);
1046 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001047}
1048
Christopher Fauletba7bc162016-11-07 21:07:38 +01001049/* This function is used in cfgparse.c and declared in proto/checks.h. It
1050 * prepare the request to send to agents during a healthcheck. It returns 0 on
1051 * success and -1 if an error occurred. */
1052int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001053spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001054{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 struct appctx appctx;
1056 struct spoe_appctx spoe_appctx;
1057 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1058 size_t sz;
1059 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060
Christopher Faulet42bfa462017-01-04 14:14:19 +01001061 memset(&appctx, 0, sizeof(appctx));
1062 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001063 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001064
1065 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001066 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001067
Christopher Faulet8ef75252017-02-20 22:56:03 +01001068 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1069 end = frame + MAX_FRAME_SIZE;
1070
1071 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1072 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 /* Add "healthcheck" K/V item */
1077 sz = SLEN(HEALTHCHECK_KEY);
1078 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1079 return -1;
1080 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 *len = frame - buf;
1083 sz = htonl(*len - 4);
1084 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085
Christopher Faulet8ef75252017-02-20 22:56:03 +01001086 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001088 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089 return 0;
1090}
1091
1092/* This function is used in checks.c and declared in proto/checks.h. It decode
1093 * the response received from an agent during a healthcheck. It returns 0 on
1094 * success and -1 if an error occurred. */
1095int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001098 struct appctx appctx;
1099 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
Christopher Faulet42bfa462017-01-04 14:14:19 +01001101 memset(&appctx, 0, sizeof(appctx));
1102 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103
Christopher Faulet42bfa462017-01-04 14:14:19 +01001104 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001105 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1108 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109 goto error;
1110 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1112 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001113
1114 return 0;
1115
1116 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1118 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1119 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120 return -1;
1121}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001122
Christopher Fauleta1cda022016-12-21 08:58:06 +01001123/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001124 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001125 * success. */
1126static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128{
1129 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130 int ret;
1131 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1134 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001135 netint = htonl(framesz);
1136 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001137 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001139 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001140 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142 }
1143 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001144 return -1; /* error */
1145 }
1146 return framesz;
1147}
1148
1149/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1150 * when the frame can be ignored, 1 to retry later and the frame length on
1151 * success. */
1152static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154{
1155 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156 int ret;
1157 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158
Willy Tarreau06d80a92017-10-19 14:32:15 +02001159 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001160 if (ret > 0) {
1161 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001162 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001163 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164 return -1;
1165 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001166 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167 }
1168 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001169 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 }
1172 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 return -1; /* error */
1174 }
1175 return framesz;
1176}
1177
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001178/********************************************************************
1179 * Functions that manage the SPOE applet
1180 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001181static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001183{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001184 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001185 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001186 appctx_wakeup(appctx);
1187 return 1;
1188}
1189
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001190/* Callback function that catches applet timeouts. If a timeout occurred, we set
1191 * <appctx->st1> flag and the SPOE applet is woken up. */
1192static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001193spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001194{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001195 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001196
1197 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1198 if (tick_is_expired(task->expire, now_ms)) {
1199 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001200 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001201 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001202 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001203 return task;
1204}
1205
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001206/* Callback function that releases a SPOE applet. This happens when the
1207 * connection with the agent is closed. */
1208static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001209spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001211 struct stream_interface *si = appctx->owner;
1212 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1213 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001214 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215
1216 if (spoe_appctx == NULL)
1217 return;
1218
1219 appctx->ctx.spoe.ptr = NULL;
1220 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001221
1222 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1223 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1224 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001227 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001228 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1230 LIST_DEL(&spoe_appctx->list);
1231 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001232 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001233 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001234
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001236 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001237 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1238 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001239 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001240 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001241
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001243 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1244 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001245
1246 si_shutw(si);
1247 si_shutr(si);
1248 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249 }
1250
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001252 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001253
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254 /* Notify all waiting streams */
1255 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001256 LIST_DEL(&ctx->list);
1257 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001258 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001259 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001262 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001263 }
1264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 /* If the applet was processing a fragmented frame, notify the
1266 * corresponding stream. */
1267 if (spoe_appctx->frag_ctx.ctx) {
1268 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001269 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001270 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001271 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001272 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1273 }
1274
Christopher Faulet24289f22017-09-25 14:48:02 +02001275 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001276 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001277
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001279 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001280 LIST_DEL(&ctx->list);
1281 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001282 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001283 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001284 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001286 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001287 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001288 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001289 LIST_DEL(&ctx->list);
1290 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001291 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001292 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001293 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1296 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001297
1298 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001299 /* Release allocated memory */
1300 spoe_release_buffer(&spoe_appctx->buffer,
1301 &spoe_appctx->buffer_wait);
1302 pool_free(pool_head_spoe_appctx, spoe_appctx);
1303
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001304 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001305 agent->rt[tid].frame_size = agent->max_frame_size;
1306 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1307 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001308}
1309
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001310static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001313 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001314 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001315 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001317
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001318 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1319 /* closed */
1320 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1321 goto exit;
1322 }
1323
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001324 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001325 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001326 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1328 goto stop;
1329 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001330
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001332 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1333 " - Connection timed out\n",
1334 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1335 __FUNCTION__, appctx);
1336 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001337 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339
Christopher Faulet42bfa462017-01-04 14:14:19 +01001340 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001341 SPOE_APPCTX(appctx)->task->expire =
1342 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1345 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001346 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001347 ret = spoe_prepare_hahello_frame(appctx, frame,
1348 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001349 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001350 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001351
1352 switch (ret) {
1353 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 case 0: /* ignore => an error, cannot be ignored */
1355 goto exit;
1356
1357 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 goto stop;
1359
Christopher Faulet8ef75252017-02-20 22:56:03 +01001360 default:
1361 /* HELLO frame successfully sent, now wait for the
1362 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1364 goto next;
1365 }
1366
1367 next:
1368 return 0;
1369 stop:
1370 return 1;
1371 exit:
1372 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1373 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374}
1375
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001376static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001379 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001380 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381 char *frame;
1382 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001383
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001384
Christopher Fauletb067b062017-01-04 16:39:11 +01001385 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001387 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389
Christopher Fauleta1cda022016-12-21 08:58:06 +01001390 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1392 " - Connection timed out\n",
1393 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1394 __FUNCTION__, appctx);
1395 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001396 goto exit;
1397 }
1398
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001399 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001400 ret = spoe_recv_frame(appctx, frame,
1401 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 if (ret > 1) {
1403 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1404 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1405 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001407 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001409 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 switch (ret) {
1412 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001413 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001414 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1415 goto next;
1416
1417 case 1: /* retry later */
1418 goto stop;
1419
1420 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001421 /* HELLO handshake is finished, set the idle timeout and
1422 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001423 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001424 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001425 SPOE_APPCTX(appctx)->node.key = 0;
1426 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001427
1428 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001429 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001430 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432
Christopher Fauleta1cda022016-12-21 08:58:06 +01001433 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001434 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001435 if (trash.data)
1436 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001437
1438 SPOE_APPCTX(appctx)->task->expire =
1439 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001440 return 0;
1441 stop:
1442 return 1;
1443 exit:
1444 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1445 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446}
1447
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001448
Christopher Fauleta1cda022016-12-21 08:58:06 +01001449static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001450spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001451{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001452 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1453 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001454 char *frame, *buf;
1455 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001456
Christopher Faulet8ef75252017-02-20 22:56:03 +01001457 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1458 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001459 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001460
1461 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1462 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1463 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1464 SPOE_APPCTX(appctx)->max_frame_size);
1465 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001466 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001467 *skip = 1;
1468 ret = 1;
1469 goto end;
1470 }
1471 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001472 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1474 SPOE_APPCTX(appctx)->max_frame_size);
1475
1476 }
1477
Christopher Faulet8ef75252017-02-20 22:56:03 +01001478 if (ret > 1)
1479 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001480
Christopher Faulet8ef75252017-02-20 22:56:03 +01001481 switch (ret) {
1482 case -1: /* error */
1483 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1484 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001485
Christopher Faulet8ef75252017-02-20 22:56:03 +01001486 case 0: /* ignore */
1487 if (ctx == NULL)
1488 goto abort_frag_frame;
1489
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001490 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 LIST_DEL(&ctx->list);
1492 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001493 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001494 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001495 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 ctx->state = SPOE_CTX_ST_ERROR;
1497 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1498 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001499 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 break;
1501
1502 case 1: /* retry */
1503 *skip = 1;
1504 break;
1505
1506 default:
1507 if (ctx == NULL)
1508 goto abort_frag_frame;
1509
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001510 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 LIST_DEL(&ctx->list);
1512 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001513 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001514 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001515 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001516 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1517 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1518 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001519 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001521 }
1522 goto end;
1523
1524 frag_frame_sent:
1525 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001526 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001527 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1528 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1529 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001530 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1531 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1532 goto end;
1533
1534 no_frag_frame_sent:
1535 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1536 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001537 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001538 }
1539 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1540 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1541 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1542 }
1543 else {
1544 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001545 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1547 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001548 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001549 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1551 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1552 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001553 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001554
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1556 goto end;
1557
1558 abort_frag_frame:
1559 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1560 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1561 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1562 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1563 goto end;
1564
1565 end:
1566 return ret;
1567}
1568
1569static int
1570spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1571{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001572 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001573 struct spoe_context *ctx = NULL;
1574 char *frame;
1575 int ret;
1576
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001578 ret = spoe_recv_frame(appctx, frame,
1579 SPOE_APPCTX(appctx)->max_frame_size);
1580 if (ret > 1) {
1581 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1582 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001583 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001584 goto end;
1585 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001586 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1588 }
1589 switch (ret) {
1590 case -1: /* error */
1591 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1592 break;
1593
1594 case 0: /* ignore */
1595 break;
1596
1597 case 1: /* retry */
1598 *skip = 1;
1599 break;
1600
1601 default:
1602 LIST_DEL(&ctx->list);
1603 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001604 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001605 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1606 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001607 if (ctx->spoe_appctx) {
1608 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001609 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001610 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001611 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1612 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1613 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1614 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1615 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1616 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1617 }
1618 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1619 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001620 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1621 break;
1622 }
1623
1624 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001625 if (trash.data)
1626 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001627 end:
1628 return ret;
1629}
1630
1631static int
1632spoe_handle_processing_appctx(struct appctx *appctx)
1633{
1634 struct stream_interface *si = appctx->owner;
1635 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001636 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001637
1638 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1639 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1640 goto exit;
1641 }
1642
1643 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1644 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1645 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1646 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1647 goto next;
1648 }
1649
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001650 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001651 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001652 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001653 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1654 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001655 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001656
Christopher Faulet8f82b202018-01-24 16:23:03 +01001657 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1658 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001659
Christopher Faulet8f82b202018-01-24 16:23:03 +01001660 /* receiving_frame loop */
1661 while (!skip_receiving) {
1662 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1663 switch (ret) {
1664 case -1: /* error */
1665 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001666
Christopher Faulet8f82b202018-01-24 16:23:03 +01001667 case 0: /* ignore */
1668 active_r = 1;
1669 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 case 1: /* retry */
1672 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001673
Christopher Faulet8f82b202018-01-24 16:23:03 +01001674 default:
1675 active_r = 1;
1676 break;
1677 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001678 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679
Christopher Faulet8f82b202018-01-24 16:23:03 +01001680 /* send_frame loop */
1681 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1682 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1683 switch (ret) {
1684 case -1: /* error */
1685 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686
Christopher Faulet8f82b202018-01-24 16:23:03 +01001687 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001688 if (SPOE_APPCTX(appctx)->node.key)
1689 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001690 active_s++;
1691 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692
Christopher Faulet8f82b202018-01-24 16:23:03 +01001693 case 1: /* retry */
1694 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001697 if (SPOE_APPCTX(appctx)->node.key)
1698 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001699 active_s++;
1700 break;
1701 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001702 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001703
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1706 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1707 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001710 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001712 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 }
1714 return 1;
1715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 next:
1717 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1718 return 0;
1719
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720 exit:
1721 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1722 return 0;
1723}
1724
1725static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001726spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001727{
1728 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001729 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001730 char *frame, *buf;
1731 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001732
Christopher Fauleta1cda022016-12-21 08:58:06 +01001733 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1734 goto exit;
1735
1736 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1737 goto exit;
1738
Christopher Faulet8ef75252017-02-20 22:56:03 +01001739 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1740 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001741 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001742 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1743 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746
1747 switch (ret) {
1748 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750 goto exit;
1751
1752 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753 goto stop;
1754
1755 default:
1756 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1757 " - disconnected by HAProxy (%d): %s\n",
1758 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 __FUNCTION__, appctx,
1760 SPOE_APPCTX(appctx)->status_code,
1761 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762
1763 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1764 goto next;
1765 }
1766
1767 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 SPOE_APPCTX(appctx)->task->expire =
1769 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 return 0;
1771 stop:
1772 return 1;
1773 exit:
1774 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1775 return 0;
1776}
1777
1778static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780{
1781 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 char *frame;
1783 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784
Christopher Fauletb067b062017-01-04 16:39:11 +01001785 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001788 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789
Christopher Fauletb067b062017-01-04 16:39:11 +01001790 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001793 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001794
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001795 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796 ret = spoe_recv_frame(appctx, frame,
1797 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001799 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001800 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801 }
1802
1803 switch (ret) {
1804 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1806 " - error on frame (%s)\n",
1807 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001808 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 goto exit;
1812
1813 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 goto next;
1815
1816 case 1: /* retry */
1817 goto stop;
1818
1819 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001822 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001823 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1825 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 goto exit;
1827 }
1828
1829 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001830 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001831 if (trash.data)
1832 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 return 0;
1835 stop:
1836 return 1;
1837 exit:
1838 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1839 return 0;
1840}
1841
1842/* I/O Handler processing messages exchanged with the agent */
1843static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001846 struct stream_interface *si = appctx->owner;
1847 struct spoe_agent *agent;
1848
1849 if (SPOE_APPCTX(appctx) == NULL)
1850 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1853 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001854
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 switchstate:
1856 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1857 " - appctx-state=%s\n",
1858 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1859 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1860
1861 switch (appctx->st0) {
1862 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864 goto out;
1865 goto switchstate;
1866
1867 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001868 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869 goto out;
1870 goto switchstate;
1871
1872 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001873 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001874 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001875 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001876 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001877 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 SPOE_APPCTX(appctx)->task->expire =
1879 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001881 goto switchstate;
1882 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1884 /* fall through */
1885
1886 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001887 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1888 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 goto out;
1891 goto switchstate;
1892
1893 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto out;
1896 goto switchstate;
1897
1898 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 goto out;
1901 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001902
1903 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 appctx->st0 = SPOE_APPCTX_ST_END;
1905 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1906
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907 si_shutw(si);
1908 si_shutr(si);
1909 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001910 /* fall through */
1911
1912 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001913 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001916 if (stopping)
1917 spoe_wakeup_appctx(appctx);
1918
Christopher Faulet42bfa462017-01-04 14:14:19 +01001919 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1920 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001921}
1922
1923struct applet spoe_applet = {
1924 .obj_type = OBJ_TYPE_APPLET,
1925 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001926 .fct = spoe_handle_appctx,
1927 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001928};
1929
1930/* Create a SPOE applet. On success, the created applet is returned, else
1931 * NULL. */
1932static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001933spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934{
1935 struct appctx *appctx;
1936 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001937 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938
Emeric Brun1138fd02017-06-19 12:38:55 +02001939 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940 goto out_error;
1941
Willy Tarreaubafbe012017-11-24 17:34:44 +01001942 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001943 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001945 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001946
Christopher Faulet42bfa462017-01-04 14:14:19 +01001947 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001948 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001949 goto out_free_spoe_appctx;
1950
1951 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001952 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001953 SPOE_APPCTX(appctx)->task->context = appctx;
1954 SPOE_APPCTX(appctx)->agent = conf->agent;
1955 SPOE_APPCTX(appctx)->version = 0;
1956 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1957 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001958 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001959 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001960 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001961
Willy Tarreau21046592020-02-26 10:39:36 +01001962 MT_LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001963 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001964 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001965
1966 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1967 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001968
Willy Tarreau5820a362016-12-22 15:59:02 +01001969 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001970 if (!sess)
1971 goto out_free_spoe;
1972
Willy Tarreau87787ac2017-08-28 16:22:54 +02001973 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001974 goto out_free_sess;
1975
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001976 stream_set_backend(strm, conf->agent->b.be);
1977
1978 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001979 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001980 appctx_wakeup(appctx);
1981
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001982 strm->do_log = NULL;
1983 strm->res.flags |= CF_READ_DONTWAIT;
1984
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001985 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001986 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001987 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001988 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001989
Emeric Brunc60def82017-09-27 14:59:38 +02001990 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001991 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992 return appctx;
1993
1994 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995 out_free_sess:
1996 session_free(sess);
1997 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02001998 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001999 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002000 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002001 out_free_appctx:
2002 appctx_free(appctx);
2003 out_error:
2004 return NULL;
2005}
2006
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002007static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002008spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009{
2010 struct spoe_config *conf = FLT_CONF(ctx->filter);
2011 struct spoe_agent *agent = conf->agent;
2012 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002013 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002014
Christopher Fauleta1cda022016-12-21 08:58:06 +01002015 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002016 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002017 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002018 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002019
2020 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002021 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2023 ctx->strm);
2024
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 /* Do not try to create a new applet if there is no server up for the
2026 * agent's backend. */
2027 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2028 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2029 " - cannot create SPOE appctx: no server up\n",
2030 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2031 __FUNCTION__, ctx->strm);
2032 goto end;
2033 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002034
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 /* Do not try to create a new applet if we have reached the maximum of
2036 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002037 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002038 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2040 " - cannot create SPOE appctx: max CPS reached\n",
2041 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2042 __FUNCTION__, ctx->strm);
2043 goto end;
2044 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045 }
2046
Christopher Faulet8ef75252017-02-20 22:56:03 +01002047 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002048 if (appctx == NULL) {
2049 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2050 " - failed to create SPOE appctx\n",
2051 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2052 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002053 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002054 "SPOE: [%s] failed to create SPOE applet\n",
2055 agent->id);
2056
Christopher Fauleta1cda022016-12-21 08:58:06 +01002057 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002058 }
2059
Christopher Fauleta1cda022016-12-21 08:58:06 +01002060 /* Increase the per-process number of cumulated connections */
2061 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002062 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 end:
2065 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002066 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002067 ctx->status_code = SPOE_CTX_ERR_RES;
2068 return -1;
2069 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002070
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002071 /* Add the SPOE context in the sending queue if the stream has no applet
2072 * already assigned and wakeup all idle applets. Otherwise, don't queue
2073 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002074 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002075 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2076 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002077 if (ctx->spoe_appctx)
2078 return 1;
2079 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080
2081 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002082 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002083 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002084 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002085 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002086 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002087
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002088 /* Finally try to wakeup an IDLE applet. */
2089 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2090 struct eb32_node *node;
2091
2092 node = eb32_first(&agent->rt[tid].idle_applets);
2093 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2094 if (node && spoe_appctx) {
2095 eb32_delete(&spoe_appctx->node);
2096 spoe_appctx->node.key++;
2097 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2098 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002099 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002100 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002101 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002102}
2103
2104/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002105 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002107/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2108 * fragmented_content. If the next message can be processed, it returns 0. If
2109 * the message is too big, it returns -1.*/
2110static int
2111spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2112 struct spoe_message *msg, int dir,
2113 char **buf, char *end)
2114{
2115 struct sample *smp;
2116 struct spoe_arg *arg;
2117 int ret;
2118
2119 if (msg->cond) {
2120 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2121 ret = acl_pass(ret);
2122 if (msg->cond->pol == ACL_COND_UNLESS)
2123 ret = !ret;
2124
2125 /* the rule does not match */
2126 if (!ret)
2127 goto next;
2128 }
2129
2130 /* Resume encoding of a SPOE argument */
2131 if (ctx->frag_ctx.curarg != NULL) {
2132 arg = ctx->frag_ctx.curarg;
2133 goto encode_argument;
2134 }
2135
2136 if (ctx->frag_ctx.curoff != UINT_MAX)
2137 goto encode_msg_payload;
2138
2139 /* Check if there is enough space for the message name and the
2140 * number of arguments. It implies <msg->id_len> is encoded on 2
2141 * bytes, at most (< 2288). */
2142 if (*buf + 2 + msg->id_len + 1 > end)
2143 goto too_big;
2144
2145 /* Encode the message name */
2146 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2147 goto too_big;
2148
2149 /* Set the number of arguments for this message */
2150 **buf = msg->nargs;
2151 (*buf)++;
2152
2153 ctx->frag_ctx.curoff = 0;
2154 encode_msg_payload:
2155
2156 /* Loop on arguments */
2157 list_for_each_entry(arg, &msg->args, list) {
2158 ctx->frag_ctx.curarg = arg;
2159 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002160 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002161
2162 encode_argument:
2163 if (ctx->frag_ctx.curoff != UINT_MAX)
2164 goto encode_arg_value;
2165
Joseph Herlantf7f60312018-11-15 13:46:49 -08002166 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002167 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2168 goto too_big;
2169
2170 ctx->frag_ctx.curoff = 0;
2171 encode_arg_value:
2172
Joseph Herlantf7f60312018-11-15 13:46:49 -08002173 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002174 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002175 if (smp) {
2176 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2177 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2178 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002179 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002180 if (ret == -1 || ctx->frag_ctx.curoff)
2181 goto too_big;
2182 }
2183
2184 next:
2185 return 0;
2186
2187 too_big:
2188 return -1;
2189}
2190
Christopher Fauletc718b822017-09-21 16:50:56 +02002191/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2192 * handle fragmented content. On success it returns 1. If an error occurred, -1
2193 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2194 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002195static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002196spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002197 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002198{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002199 struct spoe_config *conf = FLT_CONF(ctx->filter);
2200 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002201 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002202 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002203
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002204 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002205 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206
Christopher Fauletc718b822017-09-21 16:50:56 +02002207 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2208 /* Resume encoding of a SPOE message */
2209 if (ctx->frag_ctx.curmsg != NULL) {
2210 msg = ctx->frag_ctx.curmsg;
2211 goto encode_evt_message;
2212 }
2213
2214 list_for_each_entry(msg, messages, by_evt) {
2215 ctx->frag_ctx.curmsg = msg;
2216 ctx->frag_ctx.curarg = NULL;
2217 ctx->frag_ctx.curoff = UINT_MAX;
2218
2219 encode_evt_message:
2220 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2221 goto too_big;
2222 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002223 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002224 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2225 /* Resume encoding of a SPOE message */
2226 if (ctx->frag_ctx.curmsg != NULL) {
2227 msg = ctx->frag_ctx.curmsg;
2228 goto encode_grp_message;
2229 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002230
Christopher Fauletc718b822017-09-21 16:50:56 +02002231 list_for_each_entry(msg, messages, by_grp) {
2232 ctx->frag_ctx.curmsg = msg;
2233 ctx->frag_ctx.curarg = NULL;
2234 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235
Christopher Fauletc718b822017-09-21 16:50:56 +02002236 encode_grp_message:
2237 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2238 goto too_big;
2239 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002240 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002241 else
2242 goto skip;
2243
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244
Christopher Faulet57583e42017-09-04 15:41:09 +02002245 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002246 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002247 goto skip;
2248
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002250 " - encode %s messages - spoe_appctx=%p"
2251 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252 (int)now.tv_sec, (int)now.tv_usec,
2253 agent->id, __FUNCTION__, s,
2254 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002255 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002256 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002258 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259 ctx->frag_ctx.curmsg = NULL;
2260 ctx->frag_ctx.curarg = NULL;
2261 ctx->frag_ctx.curoff = 0;
2262 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002263
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264 return 1;
2265
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002267 /* Return an error if fragmentation is unsupported or if nothing has
2268 * been encoded because its too big and not splittable. */
2269 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002270 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2271 return -1;
2272 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002273
2274 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002275 " - encode fragmented messages - spoe_appctx=%p"
2276 " - curmsg=%p - curarg=%p - curoff=%u"
2277 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002278 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002279 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002281 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002283 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2285 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2286 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002287
2288 skip:
2289 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2290 " - skip the frame because nothing has been encoded\n",
2291 (int)now.tv_sec, (int)now.tv_usec,
2292 agent->id, __FUNCTION__, s);
2293 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002294}
2295
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002296
2297/***************************************************************************
2298 * Functions that handle SPOE actions
2299 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002300/* Helper function to set a variable */
2301static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002302spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002303 struct sample *smp)
2304{
2305 struct spoe_config *conf = FLT_CONF(ctx->filter);
2306 struct spoe_agent *agent = conf->agent;
2307 char varname[64];
2308
2309 memset(varname, 0, sizeof(varname));
2310 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2311 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002312 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2313 vars_set_by_name(varname, len, smp);
2314 else
2315 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002316}
2317
2318/* Helper function to unset a variable */
2319static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002320spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002321 struct sample *smp)
2322{
2323 struct spoe_config *conf = FLT_CONF(ctx->filter);
2324 struct spoe_agent *agent = conf->agent;
2325 char varname[64];
2326
2327 memset(varname, 0, sizeof(varname));
2328 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2329 scope, agent->var_pfx, len, name);
2330 vars_unset_by_name_ifexist(varname, len, smp);
2331}
2332
2333
Christopher Faulet8ef75252017-02-20 22:56:03 +01002334static inline int
2335spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2336 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002337{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002338 char *str, *scope, *p = *buf;
2339 struct sample smp;
2340 uint64_t sz;
2341 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002342
Christopher Faulet8ef75252017-02-20 22:56:03 +01002343 if (p + 2 >= end)
2344 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002345
Christopher Faulet8ef75252017-02-20 22:56:03 +01002346 /* SET-VAR requires 3 arguments */
2347 if (*p++ != 3)
2348 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002349
Christopher Faulet8ef75252017-02-20 22:56:03 +01002350 switch (*p++) {
2351 case SPOE_SCOPE_PROC: scope = "proc"; break;
2352 case SPOE_SCOPE_SESS: scope = "sess"; break;
2353 case SPOE_SCOPE_TXN : scope = "txn"; break;
2354 case SPOE_SCOPE_REQ : scope = "req"; break;
2355 case SPOE_SCOPE_RES : scope = "res"; break;
2356 default: goto skip;
2357 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002358
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2360 goto skip;
2361 memset(&smp, 0, sizeof(smp));
2362 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002363
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364 if (spoe_decode_data(&p, end, &smp) == -1)
2365 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002366
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2368 " - set-var '%s.%s.%.*s'\n",
2369 (int)now.tv_sec, (int)now.tv_usec,
2370 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2371 __FUNCTION__, s, scope,
2372 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2373 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002376
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 ret = (p - *buf);
2378 *buf = p;
2379 return ret;
2380 skip:
2381 return 0;
2382}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002383
Christopher Faulet8ef75252017-02-20 22:56:03 +01002384static inline int
2385spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2386 char **buf, char *end, int dir)
2387{
2388 char *str, *scope, *p = *buf;
2389 struct sample smp;
2390 uint64_t sz;
2391 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002392
Christopher Faulet8ef75252017-02-20 22:56:03 +01002393 if (p + 2 >= end)
2394 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002395
Christopher Faulet8ef75252017-02-20 22:56:03 +01002396 /* UNSET-VAR requires 2 arguments */
2397 if (*p++ != 2)
2398 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 switch (*p++) {
2401 case SPOE_SCOPE_PROC: scope = "proc"; break;
2402 case SPOE_SCOPE_SESS: scope = "sess"; break;
2403 case SPOE_SCOPE_TXN : scope = "txn"; break;
2404 case SPOE_SCOPE_REQ : scope = "req"; break;
2405 case SPOE_SCOPE_RES : scope = "res"; break;
2406 default: goto skip;
2407 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2410 goto skip;
2411 memset(&smp, 0, sizeof(smp));
2412 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002413
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2415 " - unset-var '%s.%s.%.*s'\n",
2416 (int)now.tv_sec, (int)now.tv_usec,
2417 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2418 __FUNCTION__, s, scope,
2419 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2420 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 spoe_unset_var(ctx, scope, str, sz, &smp);
2423
2424 ret = (p - *buf);
2425 *buf = p;
2426 return ret;
2427 skip:
2428 return 0;
2429}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002430
Christopher Faulet8ef75252017-02-20 22:56:03 +01002431/* Process SPOE actions for a specific event. It returns 1 on success. If an
2432 * error occurred, 0 is returned. */
2433static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002434spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002435{
2436 char *p, *end;
2437 int ret;
2438
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002439 p = b_head(&ctx->buffer);
2440 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002441
2442 while (p < end) {
2443 enum spoe_action_type type;
2444
2445 type = *p++;
2446 switch (type) {
2447 case SPOE_ACT_T_SET_VAR:
2448 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2449 if (!ret)
2450 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002451 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002452
Christopher Faulet8ef75252017-02-20 22:56:03 +01002453 case SPOE_ACT_T_UNSET_VAR:
2454 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2455 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002456 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002457 break;
2458
2459 default:
2460 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002461 }
2462 }
2463
2464 return 1;
2465 skip:
2466 return 0;
2467}
2468
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002469/***************************************************************************
2470 * Functions that process SPOE events
2471 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002472static void
2473spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2474 struct spoe_context *ctx, int dir)
2475{
2476 if (!tv_iszero(&ctx->stats.tv_start)) {
2477 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2478 ctx->stats.t_total += ctx->stats.t_process;
2479 tv_zero(&ctx->stats.tv_request);
2480 tv_zero(&ctx->stats.tv_queue);
2481 tv_zero(&ctx->stats.tv_wait);
2482 tv_zero(&ctx->stats.tv_response);
2483 }
2484
2485 if (agent->var_t_process) {
2486 struct sample smp;
2487
2488 memset(&smp, 0, sizeof(smp));
2489 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2490 smp.data.u.sint = ctx->stats.t_process;
2491 smp.data.type = SMP_T_SINT;
2492
2493 spoe_set_var(ctx, "txn", agent->var_t_process,
2494 strlen(agent->var_t_process), &smp);
2495 }
2496
2497 if (agent->var_t_total) {
2498 struct sample smp;
2499
2500 memset(&smp, 0, sizeof(smp));
2501 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2502 smp.data.u.sint = ctx->stats.t_total;
2503 smp.data.type = SMP_T_SINT;
2504
2505 spoe_set_var(ctx, "txn", agent->var_t_total,
2506 strlen(agent->var_t_total), &smp);
2507 }
2508}
2509
2510static void
2511spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2512 struct spoe_context *ctx, int dir)
2513{
2514 if (agent->eps_max > 0)
2515 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2516
2517 if (agent->var_on_error) {
2518 struct sample smp;
2519
2520 memset(&smp, 0, sizeof(smp));
2521 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2522 smp.data.u.sint = ctx->status_code;
2523 smp.data.type = SMP_T_BOOL;
2524
2525 spoe_set_var(ctx, "txn", agent->var_on_error,
2526 strlen(agent->var_on_error), &smp);
2527 }
2528
2529 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2530 ? SPOE_CTX_ST_READY
2531 : SPOE_CTX_ST_NONE);
2532}
2533
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002534static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002535spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002536{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002537 /* If a process is already started for this SPOE context, retry
2538 * later. */
2539 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002540 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002541
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002542 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002543 ctx->stats.tv_start = now;
2544 ctx->stats.tv_request = now;
2545 ctx->stats.t_request = -1;
2546 ctx->stats.t_queue = -1;
2547 ctx->stats.t_waiting = -1;
2548 ctx->stats.t_response = -1;
2549 ctx->stats.t_process = -1;
2550
2551 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002552
Christopher Fauleta1cda022016-12-21 08:58:06 +01002553 /* Set the right flag to prevent request and response processing
2554 * in same time. */
2555 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2556 ? SPOE_CTX_FL_REQ_PROCESS
2557 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002558 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559}
2560
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002561static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002562spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002564 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002565
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002566 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2567 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002568 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002569 if (sa) {
2570 if (sa->frag_ctx.ctx == ctx) {
2571 sa->frag_ctx.ctx = NULL;
2572 spoe_wakeup_appctx(sa->owner);
2573 }
2574 else
2575 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002576 }
2577
Christopher Fauleta1cda022016-12-21 08:58:06 +01002578 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002579 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002580 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581
2582 /* Reset processing timer */
2583 ctx->process_exp = TICK_ETERNITY;
2584
Christopher Faulet8ef75252017-02-20 22:56:03 +01002585 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002586
Christopher Fauletfce747b2018-01-24 15:59:32 +01002587 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002588 ctx->frag_ctx.curmsg = NULL;
2589 ctx->frag_ctx.curarg = NULL;
2590 ctx->frag_ctx.curoff = 0;
2591 ctx->frag_ctx.flags = 0;
2592
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002594 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002595 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002596 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002597 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002598
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599 LIST_DEL(&ctx->list);
2600 LIST_INIT(&ctx->list);
2601 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002602}
2603
Christopher Faulet58d03682017-09-21 16:57:24 +02002604/* Process a list of SPOE messages. First, this functions will process messages
2605 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2606 * to process corresponding actions. During all the processing, it returns 0
2607 * and it returns 1 when the processing is finished. If an error occurred, -1
2608 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002609static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002610spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2611 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002612{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002613 struct spoe_config *conf = FLT_CONF(ctx->filter);
2614 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002615 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616
2617 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002618 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002619
2620 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2621 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002622 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002623 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002624 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002625 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002626 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002627 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002628
2629 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002630 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002631 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002632 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002633 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002634 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002635 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002636 goto skip;
2637 }
2638 }
2639
Christopher Fauletf7a30922016-11-10 15:04:51 +01002640 if (!tick_isset(ctx->process_exp)) {
2641 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2642 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2643 ctx->process_exp);
2644 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002645 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002646 if (!ret)
2647 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002648
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002649 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002650 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002651 }
2652
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002653 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002654 if (tv_iszero(&ctx->stats.tv_request))
2655 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002656 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002657 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002658 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002659 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002660 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002661 if (!ret)
2662 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002663 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002664 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002665 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2666 }
2667
2668 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002669 if (ctx->spoe_appctx)
2670 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002671 ret = 0;
2672 goto out;
2673 }
2674
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002675 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2676 ret = 0;
2677 goto out;
2678 }
2679
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002680 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002681 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002682 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002683 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002685 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002686 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002687 }
2688
2689 out:
2690 return ret;
2691
Christopher Fauleta1cda022016-12-21 08:58:06 +01002692 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002693 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002694 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002695 spoe_stop_processing(agent, ctx);
2696 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002697
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002699 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002700 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002701 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002702 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002703 spoe_handle_processing_error(s, agent, ctx, dir);
2704 ret = 1;
2705 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002706 return ret;
2707}
2708
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002709/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2710 * See spoe_process_message for details. */
2711static int
2712spoe_process_group(struct stream *s, struct spoe_context *ctx,
2713 struct spoe_group *group, int dir)
2714{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002715 struct spoe_config *conf = FLT_CONF(ctx->filter);
2716 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002717 int ret;
2718
2719 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2720 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002721 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002722 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2723 group->id);
2724
2725 if (LIST_ISEMPTY(&group->messages))
2726 return 1;
2727
2728 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002729 if (ret && ctx->stats.t_process != -1) {
2730 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002731 " - <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 +01002732 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002733 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002734 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002735 ctx->stats.t_response, ctx->stats.t_process,
2736 agent->counters.idles, agent->counters.applets,
2737 agent->counters.nb_sending, agent->counters.nb_waiting,
2738 agent->counters.nb_errors, agent->counters.nb_processed,
2739 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2740 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2741 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2742 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2743 agent->id, group->id, s->uniq_id, ctx->status_code,
2744 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2745 ctx->stats.t_response, ctx->stats.t_process,
2746 agent->counters.idles, agent->counters.applets,
2747 agent->counters.nb_sending, agent->counters.nb_waiting,
2748 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002749 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002750 return ret;
2751}
2752
Christopher Faulet58d03682017-09-21 16:57:24 +02002753/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2754 * See spoe_process_message for details. */
2755static int
2756spoe_process_event(struct stream *s, struct spoe_context *ctx,
2757 enum spoe_event ev)
2758{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002759 struct spoe_config *conf = FLT_CONF(ctx->filter);
2760 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002761 int dir, ret;
2762
2763 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002764 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002765 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002766 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2767 spoe_event_str[ev]);
2768
2769 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2770
2771 if (LIST_ISEMPTY(&(ctx->events[ev])))
2772 return 1;
2773
2774 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002775 if (ret && ctx->stats.t_process != -1) {
2776 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002777 " - <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 +01002778 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2779 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2780 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002781 ctx->stats.t_response, ctx->stats.t_process,
2782 agent->counters.idles, agent->counters.applets,
2783 agent->counters.nb_sending, agent->counters.nb_waiting,
2784 agent->counters.nb_errors, agent->counters.nb_processed,
2785 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2786 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2787 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2788 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2789 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2790 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2791 ctx->stats.t_response, ctx->stats.t_process,
2792 agent->counters.idles, agent->counters.applets,
2793 agent->counters.nb_sending, agent->counters.nb_waiting,
2794 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002795 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002796 return ret;
2797}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002798
2799/***************************************************************************
2800 * Functions that create/destroy SPOE contexts
2801 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002802static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002803spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002804{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002805 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002806 return 1;
2807
Willy Tarreau21046592020-02-26 10:39:36 +01002808 if (MT_LIST_ADDED(&buffer_wait->list))
2809 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002810
Christopher Faulet4596fb72017-01-11 14:05:19 +01002811 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002812 return 1;
2813
Willy Tarreau21046592020-02-26 10:39:36 +01002814 MT_LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002815 return 0;
2816}
2817
2818static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002819spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002820{
Willy Tarreau21046592020-02-26 10:39:36 +01002821 if (MT_LIST_ADDED(&buffer_wait->list))
2822 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002823
2824 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002825 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002826 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002827 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002828 }
2829}
2830
Christopher Faulet4596fb72017-01-11 14:05:19 +01002831static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002832spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002833{
2834 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2835 return 1;
2836}
2837
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002838static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002839spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002840{
2841 struct spoe_config *conf = FLT_CONF(filter);
2842 struct spoe_context *ctx;
2843
Willy Tarreaubafbe012017-11-24 17:34:44 +01002844 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002845 if (ctx == NULL) {
2846 return NULL;
2847 }
2848 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002849 ctx->filter = filter;
2850 ctx->state = SPOE_CTX_ST_NONE;
2851 ctx->status_code = SPOE_CTX_ERR_NONE;
2852 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002853 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002854 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002855 ctx->buffer = BUF_NULL;
Willy Tarreau21046592020-02-26 10:39:36 +01002856 MT_LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002857 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002858 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002859 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002860
Christopher Fauletf7a30922016-11-10 15:04:51 +01002861 ctx->stream_id = 0;
2862 ctx->frame_id = 1;
2863 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002864
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002865 tv_zero(&ctx->stats.tv_start);
2866 tv_zero(&ctx->stats.tv_request);
2867 tv_zero(&ctx->stats.tv_queue);
2868 tv_zero(&ctx->stats.tv_wait);
2869 tv_zero(&ctx->stats.tv_response);
2870 ctx->stats.t_request = -1;
2871 ctx->stats.t_queue = -1;
2872 ctx->stats.t_waiting = -1;
2873 ctx->stats.t_response = -1;
2874 ctx->stats.t_process = -1;
2875 ctx->stats.t_total = 0;
2876
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002877 ctx->strm = s;
2878 ctx->state = SPOE_CTX_ST_READY;
2879 filter->ctx = ctx;
2880
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881 return ctx;
2882}
2883
2884static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002885spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002886{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002887 struct spoe_config *conf = FLT_CONF(filter);
2888 struct spoe_context *ctx = filter->ctx;
2889
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002890 if (!ctx)
2891 return;
2892
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002893 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002894 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002895 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896}
2897
2898static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002899spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900{
2901 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002902 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002903
2904 tv_zero(&ctx->stats.tv_start);
2905 tv_zero(&ctx->stats.tv_request);
2906 tv_zero(&ctx->stats.tv_queue);
2907 tv_zero(&ctx->stats.tv_wait);
2908 tv_zero(&ctx->stats.tv_response);
2909 ctx->stats.t_request = -1;
2910 ctx->stats.t_queue = -1;
2911 ctx->stats.t_waiting = -1;
2912 ctx->stats.t_response = -1;
2913 ctx->stats.t_process = -1;
2914 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002915}
2916
2917
2918/***************************************************************************
2919 * Hooks that manage the filter lifecycle (init/check/deinit)
2920 **************************************************************************/
2921/* Signal handler: Do a soft stop, wakeup SPOE applet */
2922static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002923spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002924{
2925 struct proxy *p;
2926
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002927 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002928 while (p) {
2929 struct flt_conf *fconf;
2930
2931 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002932 struct spoe_config *conf;
2933 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002934 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002935 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936
Christopher Faulet3b386a32017-02-23 10:17:15 +01002937 if (fconf->id != spoe_filter_id)
2938 continue;
2939
2940 conf = fconf->conf;
2941 agent = conf->agent;
2942
Christopher Faulet24289f22017-09-25 14:48:02 +02002943 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002944 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002945 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2946 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002947 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002948 }
2949 }
2950 p = p->next;
2951 }
2952}
2953
2954
2955/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2956static int
2957spoe_init(struct proxy *px, struct flt_conf *fconf)
2958{
2959 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002960
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002961 /* conf->agent_fe was already initialized during the config
2962 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964 conf->agent_fe.cap = PR_CAP_FE;
2965 conf->agent_fe.mode = PR_MODE_TCP;
2966 conf->agent_fe.maxconn = 0;
2967 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2968 conf->agent_fe.conn_retries = CONN_RETRIES;
2969 conf->agent_fe.accept = frontend_accept;
2970 conf->agent_fe.srv = NULL;
2971 conf->agent_fe.timeout.client = TICK_ETERNITY;
2972 conf->agent_fe.default_target = &spoe_applet.obj_type;
2973 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002975 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002976 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002977 sighandler_registered = 1;
2978 }
2979
Christopher Faulet00292352019-01-09 15:05:10 +01002980 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982}
2983
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002984/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985static void
2986spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2987{
2988 struct spoe_config *conf = fconf->conf;
2989
2990 if (conf) {
2991 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992
Christopher Faulet8ef75252017-02-20 22:56:03 +01002993 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002994 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995 free(conf);
2996 }
2997 fconf->conf = NULL;
2998}
2999
3000/* Check configuration of a SPOE filter for a specified proxy.
3001 * Return 1 on error, else 0. */
3002static int
3003spoe_check(struct proxy *px, struct flt_conf *fconf)
3004{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003005 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003006 struct spoe_config *conf = fconf->conf;
3007 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003008 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009
Christopher Faulet7ee86672017-09-19 11:08:28 +02003010 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3011 * are uniq */
3012 list_for_each_entry(f, &px->filter_configs, list) {
3013 struct spoe_config *c = f->conf;
3014
3015 /* This is not an SPOE filter */
3016 if (f->id != spoe_filter_id)
3017 continue;
3018 /* This is the current SPOE filter */
3019 if (f == fconf)
3020 continue;
3021
3022 /* Check engine Id. It should be uniq */
3023 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003024 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3025 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003026 return 1;
3027 }
3028 }
3029
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003030 target = proxy_be_by_name(conf->agent->b.name);
3031 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003032 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3033 " declared at %s:%d.\n",
3034 px->id, conf->agent->b.name, conf->agent->id,
3035 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003036 return 1;
3037 }
3038 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003039 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3040 " at %s:%d does not support HTTP mode.\n",
3041 px->id, target->id, conf->agent->id,
3042 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003043 return 1;
3044 }
3045
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003046 if (px->bind_proc & ~target->bind_proc) {
3047 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3048 " at %s:%d does not cover all of its processes.\n",
3049 px->id, target->id, conf->agent->id,
3050 conf->agent->conf.file, conf->agent->conf.line);
3051 return 1;
3052 }
3053
Christopher Fauletfe261552019-03-18 13:57:42 +01003054 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003055 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3056 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3057 return 1;
3058 }
3059 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003060 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003061 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3062 conf->agent->rt[i].processing = 0;
3063 LIST_INIT(&conf->agent->rt[i].applets);
3064 LIST_INIT(&conf->agent->rt[i].sending_queue);
3065 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3066 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003067 }
3068
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003069 free(conf->agent->b.name);
3070 conf->agent->b.name = NULL;
3071 conf->agent->b.be = target;
3072 return 0;
3073}
3074
Kevin Zhud87b1a52019-09-17 15:05:45 +02003075/* Initializes the SPOE filter for a proxy for a specific thread.
3076 * Returns a negative value if an error occurs. */
3077static int
3078spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3079{
3080 struct spoe_config *conf = fconf->conf;
3081 struct spoe_agent *agent = conf->agent;
3082
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003083 agent->rt[tid].engine_id = generate_pseudo_uuid();
3084 if (agent->rt[tid].engine_id == NULL)
3085 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003086 return 0;
3087}
3088
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003089/**************************************************************************
3090 * Hooks attached to a stream
3091 *************************************************************************/
3092/* Called when a filter instance is created and attach to a stream. It creates
3093 * the context that will be used to process this stream. */
3094static int
3095spoe_start(struct stream *s, struct filter *filter)
3096{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003097 struct spoe_config *conf = FLT_CONF(filter);
3098 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003099 struct spoe_context *ctx;
3100
3101 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003102 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 __FUNCTION__, s);
3104
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003105 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003106 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3107 " - failed to create SPOE context\n",
3108 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003109 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003110 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003111 "SPOE: [%s] failed to create SPOE context\n",
3112 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003113 return 0;
3114 }
3115
Christopher Faulet11610f32017-09-21 10:23:10 +02003116 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003117 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3118
Christopher Faulet11610f32017-09-21 10:23:10 +02003119 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003120 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3121
Christopher Faulet11610f32017-09-21 10:23:10 +02003122 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003123 filter->pre_analyzers |= AN_RES_INSPECT;
3124
Christopher Faulet11610f32017-09-21 10:23:10 +02003125 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003126 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3127
Christopher Faulet11610f32017-09-21 10:23:10 +02003128 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003129 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3130
Christopher Faulet11610f32017-09-21 10:23:10 +02003131 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3133
3134 return 1;
3135}
3136
3137/* Called when a filter instance is detached from a stream. It release the
3138 * attached SPOE context. */
3139static void
3140spoe_stop(struct stream *s, struct filter *filter)
3141{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3143 (int)now.tv_sec, (int)now.tv_usec,
3144 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3145 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003146 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003147}
3148
Christopher Fauletf7a30922016-11-10 15:04:51 +01003149
3150/*
3151 * Called when the stream is woken up because of expired timer.
3152 */
3153static void
3154spoe_check_timeouts(struct stream *s, struct filter *filter)
3155{
3156 struct spoe_context *ctx = filter->ctx;
3157
Christopher Fauletac580602018-03-20 16:09:20 +01003158 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003159 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003160}
3161
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162/* Called when we are ready to filter data on a channel */
3163static int
3164spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3165{
3166 struct spoe_context *ctx = filter->ctx;
3167 int ret = 1;
3168
3169 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3170 " - ctx-flags=0x%08x\n",
3171 (int)now.tv_sec, (int)now.tv_usec,
3172 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3173 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3174
Christopher Fauletb067b062017-01-04 16:39:11 +01003175 if (ctx->state == SPOE_CTX_ST_NONE)
3176 goto out;
3177
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003178 if (!(chn->flags & CF_ISRESP)) {
3179 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3180 chn->analysers |= AN_REQ_INSPECT_FE;
3181 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3182 chn->analysers |= AN_REQ_INSPECT_BE;
3183
3184 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3185 goto out;
3186
3187 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003188 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003189 if (!ret)
3190 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3192 }
3193 else {
3194 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3195 chn->analysers |= AN_RES_INSPECT;
3196
3197 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3198 goto out;
3199
Christopher Faulet8ef75252017-02-20 22:56:03 +01003200 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003201 if (!ret) {
3202 channel_dont_read(chn);
3203 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003204 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003205 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003206 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003207 }
3208
3209 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003210 return ret;
3211}
3212
3213/* Called before a processing happens on a given channel */
3214static int
3215spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3216 struct channel *chn, unsigned an_bit)
3217{
3218 struct spoe_context *ctx = filter->ctx;
3219 int ret = 1;
3220
3221 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3222 " - ctx-flags=0x%08x - ana=0x%08x\n",
3223 (int)now.tv_sec, (int)now.tv_usec,
3224 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3225 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3226 ctx->flags, an_bit);
3227
Christopher Fauletb067b062017-01-04 16:39:11 +01003228 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 goto out;
3230
3231 switch (an_bit) {
3232 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003233 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003234 break;
3235 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003236 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003237 break;
3238 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003239 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003240 break;
3241 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003242 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003243 break;
3244 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003245 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003246 break;
3247 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003248 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003249 break;
3250 }
3251
3252 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003253 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003254 channel_dont_read(chn);
3255 channel_dont_close(chn);
3256 }
3257 return ret;
3258}
3259
3260/* Called when the filtering on the channel ends. */
3261static int
3262spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3263{
3264 struct spoe_context *ctx = filter->ctx;
3265
3266 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3267 " - ctx-flags=0x%08x\n",
3268 (int)now.tv_sec, (int)now.tv_usec,
3269 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3270 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3271
3272 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003273 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003274 }
3275
3276 return 1;
3277}
3278
3279/********************************************************************
3280 * Functions that manage the filter initialization
3281 ********************************************************************/
3282struct flt_ops spoe_ops = {
3283 /* Manage SPOE filter, called for each filter declaration */
3284 .init = spoe_init,
3285 .deinit = spoe_deinit,
3286 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003287 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003288
3289 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003290 .attach = spoe_start,
3291 .detach = spoe_stop,
3292 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003293
3294 /* Handle channels activity */
3295 .channel_start_analyze = spoe_start_analyze,
3296 .channel_pre_analyze = spoe_chn_pre_analyze,
3297 .channel_end_analyze = spoe_end_analyze,
3298};
3299
3300
3301static int
3302cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3303{
3304 const char *err;
3305 int i, err_code = 0;
3306
3307 if ((cfg_scope == NULL && curengine != NULL) ||
3308 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003309 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003310 goto out;
3311
3312 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3313 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003314 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3315 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003316 err_code |= ERR_ALERT | ERR_ABORT;
3317 goto out;
3318 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003319 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3320 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003321 goto out;
3322 }
3323
3324 err = invalid_char(args[1]);
3325 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003326 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3327 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003328 err_code |= ERR_ALERT | ERR_ABORT;
3329 goto out;
3330 }
3331
3332 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003333 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3334 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003335 err_code |= ERR_ALERT | ERR_ABORT;
3336 goto out;
3337 }
3338 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003339 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003340 err_code |= ERR_ALERT | ERR_ABORT;
3341 goto out;
3342 }
3343
3344 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003345
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003346 curagent->conf.file = strdup(file);
3347 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003348
3349 curagent->timeout.hello = TICK_ETERNITY;
3350 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003351 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003352
Christopher Fauleta1cda022016-12-21 08:58:06 +01003353 curagent->var_pfx = NULL;
3354 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003355 curagent->var_t_process = NULL;
3356 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003357 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003358 curagent->cps_max = 0;
3359 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003360 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003361 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003362
3363 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003364 LIST_INIT(&curagent->events[i]);
3365 LIST_INIT(&curagent->groups);
3366 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003367 }
3368 else if (!strcmp(args[0], "use-backend")) {
3369 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003370 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3371 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003372 err_code |= ERR_ALERT | ERR_FATAL;
3373 goto out;
3374 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003375 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003376 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003377 free(curagent->b.name);
3378 curagent->b.name = strdup(args[1]);
3379 }
3380 else if (!strcmp(args[0], "messages")) {
3381 int cur_arg = 1;
3382 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003383 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384
Christopher Faulet11610f32017-09-21 10:23:10 +02003385 list_for_each_entry(ph, &curmphs, list) {
3386 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003387 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3388 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 err_code |= ERR_ALERT | ERR_FATAL;
3390 goto out;
3391 }
3392 }
3393
Christopher Faulet11610f32017-09-21 10:23:10 +02003394 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003395 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003396 err_code |= ERR_ALERT | ERR_ABORT;
3397 goto out;
3398 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003399 ph->id = strdup(args[cur_arg]);
3400 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003401 cur_arg++;
3402 }
3403 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003404 else if (!strcmp(args[0], "groups")) {
3405 int cur_arg = 1;
3406 while (*args[cur_arg]) {
3407 struct spoe_placeholder *ph = NULL;
3408
3409 list_for_each_entry(ph, &curgphs, list) {
3410 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003411 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3412 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003413 err_code |= ERR_ALERT | ERR_FATAL;
3414 goto out;
3415 }
3416 }
3417
3418 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003419 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003420 err_code |= ERR_ALERT | ERR_ABORT;
3421 goto out;
3422 }
3423 ph->id = strdup(args[cur_arg]);
3424 LIST_ADDQ(&curgphs, &ph->list);
3425 cur_arg++;
3426 }
3427 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003428 else if (!strcmp(args[0], "timeout")) {
3429 unsigned int *tv = NULL;
3430 const char *res;
3431 unsigned timeout;
3432
3433 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003434 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3435 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003436 err_code |= ERR_ALERT | ERR_FATAL;
3437 goto out;
3438 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003439 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3440 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003441 if (!strcmp(args[1], "hello"))
3442 tv = &curagent->timeout.hello;
3443 else if (!strcmp(args[1], "idle"))
3444 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003445 else if (!strcmp(args[1], "processing"))
3446 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003447 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3449 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450 err_code |= ERR_ALERT | ERR_FATAL;
3451 goto out;
3452 }
3453 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003454 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3455 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003456 err_code |= ERR_ALERT | ERR_FATAL;
3457 goto out;
3458 }
3459 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003460 if (res == PARSE_TIME_OVER) {
3461 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3462 file, linenum, args[2], args[0], args[1]);
3463 err_code |= ERR_ALERT | ERR_FATAL;
3464 goto out;
3465 }
3466 else if (res == PARSE_TIME_UNDER) {
3467 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3468 file, linenum, args[2], args[0], args[1]);
3469 err_code |= ERR_ALERT | ERR_FATAL;
3470 goto out;
3471 }
3472 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003473 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3474 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003475 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003476 goto out;
3477 }
3478 *tv = MS_TO_TICKS(timeout);
3479 }
3480 else if (!strcmp(args[0], "option")) {
3481 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003482 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3483 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003484 err_code |= ERR_ALERT | ERR_FATAL;
3485 goto out;
3486 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003487
Christopher Faulet305c6072017-02-23 16:17:53 +01003488 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003489 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003490 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003491 if (kwm == 1)
3492 curagent->flags &= ~SPOE_FL_PIPELINING;
3493 else
3494 curagent->flags |= SPOE_FL_PIPELINING;
3495 goto out;
3496 }
3497 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003498 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003499 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003500 if (kwm == 1)
3501 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003502 else
3503 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003504 goto out;
3505 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003506 else if (!strcmp(args[1], "send-frag-payload")) {
3507 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3508 goto out;
3509 if (kwm == 1)
3510 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3511 else
3512 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3513 goto out;
3514 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003515 else if (!strcmp(args[1], "dontlog-normal")) {
3516 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3517 goto out;
3518 if (kwm == 1)
3519 curpxopts2 &= ~PR_O2_NOLOGNORM;
3520 else
3521 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003522 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003523 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003524
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003525 /* Following options does not support negation */
3526 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003527 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3528 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003529 err_code |= ERR_ALERT | ERR_FATAL;
3530 goto out;
3531 }
3532
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003533 if (!strcmp(args[1], "var-prefix")) {
3534 char *tmp;
3535
3536 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003537 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3538 file, linenum, args[0],
3539 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003540 err_code |= ERR_ALERT | ERR_FATAL;
3541 goto out;
3542 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003543 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3544 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003545 tmp = args[2];
3546 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003547 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003548 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003549 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003550 err_code |= ERR_ALERT | ERR_FATAL;
3551 goto out;
3552 }
3553 tmp++;
3554 }
3555 curagent->var_pfx = strdup(args[2]);
3556 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003557 else if (!strcmp(args[1], "force-set-var")) {
3558 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3559 goto out;
3560 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3561 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003562 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003563 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003564 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003565 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3566 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003567 else if (!strcmp(args[1], "set-on-error")) {
3568 char *tmp;
3569
3570 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003571 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3572 file, linenum, args[0],
3573 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003574 err_code |= ERR_ALERT | ERR_FATAL;
3575 goto out;
3576 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003577 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3578 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003579 tmp = args[2];
3580 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003581 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003582 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003583 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003584 err_code |= ERR_ALERT | ERR_FATAL;
3585 goto out;
3586 }
3587 tmp++;
3588 }
3589 curagent->var_on_error = strdup(args[2]);
3590 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003591 else if (!strcmp(args[1], "set-process-time")) {
3592 char *tmp;
3593
3594 if (!*args[2]) {
3595 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3596 file, linenum, args[0],
3597 args[1]);
3598 err_code |= ERR_ALERT | ERR_FATAL;
3599 goto out;
3600 }
3601 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3602 goto out;
3603 tmp = args[2];
3604 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003605 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003606 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003607 file, linenum, args[0], args[1]);
3608 err_code |= ERR_ALERT | ERR_FATAL;
3609 goto out;
3610 }
3611 tmp++;
3612 }
3613 curagent->var_t_process = strdup(args[2]);
3614 }
3615 else if (!strcmp(args[1], "set-total-time")) {
3616 char *tmp;
3617
3618 if (!*args[2]) {
3619 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3620 file, linenum, args[0],
3621 args[1]);
3622 err_code |= ERR_ALERT | ERR_FATAL;
3623 goto out;
3624 }
3625 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3626 goto out;
3627 tmp = args[2];
3628 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003629 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003630 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003631 file, linenum, args[0], args[1]);
3632 err_code |= ERR_ALERT | ERR_FATAL;
3633 goto out;
3634 }
3635 tmp++;
3636 }
3637 curagent->var_t_total = strdup(args[2]);
3638 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003639 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003640 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3641 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003642 err_code |= ERR_ALERT | ERR_FATAL;
3643 goto out;
3644 }
Christopher Faulet48026722016-11-16 15:01:12 +01003645 }
3646 else if (!strcmp(args[0], "maxconnrate")) {
3647 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003648 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3649 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003650 err_code |= ERR_ALERT | ERR_FATAL;
3651 goto out;
3652 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003653 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003654 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003655 curagent->cps_max = atol(args[1]);
3656 }
3657 else if (!strcmp(args[0], "maxerrrate")) {
3658 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003659 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3660 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003661 err_code |= ERR_ALERT | ERR_FATAL;
3662 goto out;
3663 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003664 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003665 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003666 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003667 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003668 else if (!strcmp(args[0], "max-frame-size")) {
3669 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003670 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3671 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003672 err_code |= ERR_ALERT | ERR_FATAL;
3673 goto out;
3674 }
3675 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3676 goto out;
3677 curagent->max_frame_size = atol(args[1]);
3678 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3679 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003680 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3681 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003682 err_code |= ERR_ALERT | ERR_FATAL;
3683 goto out;
3684 }
3685 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003686 else if (!strcmp(args[0], "max-waiting-frames")) {
3687 if (!*args[1]) {
3688 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3689 file, linenum, args[0]);
3690 err_code |= ERR_ALERT | ERR_FATAL;
3691 goto out;
3692 }
3693 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3694 goto out;
3695 curagent->max_fpa = atol(args[1]);
3696 if (curagent->max_fpa < 1) {
3697 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3698 file, linenum, args[0]);
3699 err_code |= ERR_ALERT | ERR_FATAL;
3700 goto out;
3701 }
3702 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003703 else if (!strcmp(args[0], "register-var-names")) {
3704 int cur_arg;
3705
3706 if (!*args[1]) {
3707 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3708 file, linenum, args[0]);
3709 err_code |= ERR_ALERT | ERR_FATAL;
3710 goto out;
3711 }
3712 cur_arg = 1;
3713 while (*args[cur_arg]) {
3714 struct spoe_var_placeholder *vph;
3715
3716 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3717 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3718 err_code |= ERR_ALERT | ERR_ABORT;
3719 goto out;
3720 }
3721 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003722 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003723 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3724 err_code |= ERR_ALERT | ERR_ABORT;
3725 goto out;
3726 }
3727 LIST_ADDQ(&curvars, &vph->list);
3728 cur_arg++;
3729 }
3730 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003731 else if (!strcmp(args[0], "log")) {
3732 char *errmsg = NULL;
3733
3734 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3735 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3736 err_code |= ERR_ALERT | ERR_FATAL;
3737 goto out;
3738 }
3739 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003740 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003741 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3742 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003743 err_code |= ERR_ALERT | ERR_FATAL;
3744 goto out;
3745 }
3746 out:
3747 return err_code;
3748}
Christopher Faulet11610f32017-09-21 10:23:10 +02003749static int
3750cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3751{
3752 struct spoe_group *grp;
3753 const char *err;
3754 int err_code = 0;
3755
3756 if ((cfg_scope == NULL && curengine != NULL) ||
3757 (cfg_scope != NULL && curengine == NULL) ||
3758 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3759 goto out;
3760
3761 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3762 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003763 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3764 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003765 err_code |= ERR_ALERT | ERR_ABORT;
3766 goto out;
3767 }
3768 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3769 err_code |= ERR_ABORT;
3770 goto out;
3771 }
3772
3773 err = invalid_char(args[1]);
3774 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003775 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3776 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003777 err_code |= ERR_ALERT | ERR_ABORT;
3778 goto out;
3779 }
3780
3781 list_for_each_entry(grp, &curgrps, list) {
3782 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003783 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3784 " name as another one declared at %s:%d.\n",
3785 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003786 err_code |= ERR_ALERT | ERR_FATAL;
3787 goto out;
3788 }
3789 }
3790
3791 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003792 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003793 err_code |= ERR_ALERT | ERR_ABORT;
3794 goto out;
3795 }
3796
3797 curgrp->id = strdup(args[1]);
3798 curgrp->conf.file = strdup(file);
3799 curgrp->conf.line = linenum;
3800 LIST_INIT(&curgrp->phs);
3801 LIST_INIT(&curgrp->messages);
3802 LIST_ADDQ(&curgrps, &curgrp->list);
3803 }
3804 else if (!strcmp(args[0], "messages")) {
3805 int cur_arg = 1;
3806 while (*args[cur_arg]) {
3807 struct spoe_placeholder *ph = NULL;
3808
3809 list_for_each_entry(ph, &curgrp->phs, list) {
3810 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003811 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3812 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003813 err_code |= ERR_ALERT | ERR_FATAL;
3814 goto out;
3815 }
3816 }
3817
3818 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003819 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003820 err_code |= ERR_ALERT | ERR_ABORT;
3821 goto out;
3822 }
3823 ph->id = strdup(args[cur_arg]);
3824 LIST_ADDQ(&curgrp->phs, &ph->list);
3825 cur_arg++;
3826 }
3827 }
3828 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003829 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3830 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003831 err_code |= ERR_ALERT | ERR_FATAL;
3832 goto out;
3833 }
3834 out:
3835 return err_code;
3836}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003837
3838static int
3839cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3840{
3841 struct spoe_message *msg;
3842 struct spoe_arg *arg;
3843 const char *err;
3844 char *errmsg = NULL;
3845 int err_code = 0;
3846
3847 if ((cfg_scope == NULL && curengine != NULL) ||
3848 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003849 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003850 goto out;
3851
3852 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3853 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003854 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3855 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003856 err_code |= ERR_ALERT | ERR_ABORT;
3857 goto out;
3858 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003859 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3860 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003861 goto out;
3862 }
3863
3864 err = invalid_char(args[1]);
3865 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003866 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3867 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003868 err_code |= ERR_ALERT | ERR_ABORT;
3869 goto out;
3870 }
3871
3872 list_for_each_entry(msg, &curmsgs, list) {
3873 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003874 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3875 " name as another one declared at %s:%d.\n",
3876 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003877 err_code |= ERR_ALERT | ERR_FATAL;
3878 goto out;
3879 }
3880 }
3881
3882 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003883 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003884 err_code |= ERR_ALERT | ERR_ABORT;
3885 goto out;
3886 }
3887
3888 curmsg->id = strdup(args[1]);
3889 curmsg->id_len = strlen(curmsg->id);
3890 curmsg->event = SPOE_EV_NONE;
3891 curmsg->conf.file = strdup(file);
3892 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003893 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003894 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003895 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003896 LIST_INIT(&curmsg->by_evt);
3897 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003898 LIST_ADDQ(&curmsgs, &curmsg->list);
3899 }
3900 else if (!strcmp(args[0], "args")) {
3901 int cur_arg = 1;
3902
3903 curproxy->conf.args.ctx = ARGC_SPOE;
3904 curproxy->conf.args.file = file;
3905 curproxy->conf.args.line = linenum;
3906 while (*args[cur_arg]) {
3907 char *delim = strchr(args[cur_arg], '=');
3908 int idx = 0;
3909
3910 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003911 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003912 err_code |= ERR_ALERT | ERR_ABORT;
3913 goto out;
3914 }
3915
3916 if (!delim) {
3917 arg->name = NULL;
3918 arg->name_len = 0;
3919 delim = args[cur_arg];
3920 }
3921 else {
3922 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3923 arg->name_len = delim - args[cur_arg];
3924 delim++;
3925 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003926 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3927 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003928 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003929 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003930 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003931 err_code |= ERR_ALERT | ERR_FATAL;
3932 free(arg->name);
3933 free(arg);
3934 goto out;
3935 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003936 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003937 LIST_ADDQ(&curmsg->args, &arg->list);
3938 cur_arg++;
3939 }
3940 curproxy->conf.args.file = NULL;
3941 curproxy->conf.args.line = 0;
3942 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003943 else if (!strcmp(args[0], "acl")) {
3944 err = invalid_char(args[1]);
3945 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003946 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3947 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003948 err_code |= ERR_ALERT | ERR_FATAL;
3949 goto out;
3950 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003951 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003952 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003953 "logical disjunction within a condition.\n",
3954 file, linenum, args[1]);
3955 err_code |= ERR_ALERT | ERR_FATAL;
3956 goto out;
3957 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003958 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003959 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3960 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003961 err_code |= ERR_ALERT | ERR_FATAL;
3962 goto out;
3963 }
3964 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 else if (!strcmp(args[0], "event")) {
3966 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003967 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003968 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003969 goto out;
3970 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003971 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3972 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003973
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003974 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3975 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3976 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3977 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3978
3979 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3980 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3981 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3982 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3983 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3984 curmsg->event = SPOE_EV_ON_TCP_RSP;
3985
3986 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3987 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3988 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3989 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3990 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3991 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3992 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08003993 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003994 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003995 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003996 goto out;
3997 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003998
3999 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4000 struct acl_cond *cond;
4001
4002 cond = build_acl_cond(file, linenum, &curmsg->acls,
4003 curproxy, (const char **)args+2,
4004 &errmsg);
4005 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004006 ha_alert("parsing [%s:%d] : error detected while "
4007 "parsing an 'event %s' condition : %s.\n",
4008 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004009 err_code |= ERR_ALERT | ERR_FATAL;
4010 goto out;
4011 }
4012 curmsg->cond = cond;
4013 }
4014 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004015 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4016 "or 'unless' followed by a condition but found '%s'.\n",
4017 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004018 err_code |= ERR_ALERT | ERR_FATAL;
4019 goto out;
4020 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004021 }
4022 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004023 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4024 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004025 err_code |= ERR_ALERT | ERR_FATAL;
4026 goto out;
4027 }
4028 out:
4029 free(errmsg);
4030 return err_code;
4031}
4032
4033/* Return -1 on error, else 0 */
4034static int
4035parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4036 struct flt_conf *fconf, char **err, void *private)
4037{
4038 struct list backup_sections;
4039 struct spoe_config *conf;
4040 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004041 struct spoe_group *grp, *grpback;
4042 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004043 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004044 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004045 char *file = NULL, *engine = NULL;
4046 int ret, pos = *cur_arg + 1;
4047
Christopher Faulet84c844e2018-03-23 14:37:14 +01004048 LIST_INIT(&curmsgs);
4049 LIST_INIT(&curgrps);
4050 LIST_INIT(&curmphs);
4051 LIST_INIT(&curgphs);
4052 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004053 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004054 curpxopts = 0;
4055 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004056
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 conf = calloc(1, sizeof(*conf));
4058 if (conf == NULL) {
4059 memprintf(err, "%s: out of memory", args[*cur_arg]);
4060 goto error;
4061 }
4062 conf->proxy = px;
4063
4064 while (*args[pos]) {
4065 if (!strcmp(args[pos], "config")) {
4066 if (!*args[pos+1]) {
4067 memprintf(err, "'%s' : '%s' option without value",
4068 args[*cur_arg], args[pos]);
4069 goto error;
4070 }
4071 file = args[pos+1];
4072 pos += 2;
4073 }
4074 else if (!strcmp(args[pos], "engine")) {
4075 if (!*args[pos+1]) {
4076 memprintf(err, "'%s' : '%s' option without value",
4077 args[*cur_arg], args[pos]);
4078 goto error;
4079 }
4080 engine = args[pos+1];
4081 pos += 2;
4082 }
4083 else {
4084 memprintf(err, "unknown keyword '%s'", args[pos]);
4085 goto error;
4086 }
4087 }
4088 if (file == NULL) {
4089 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4090 goto error;
4091 }
4092
4093 /* backup sections and register SPOE sections */
4094 LIST_INIT(&backup_sections);
4095 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004096 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4097 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004098 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004099
4100 /* Parse SPOE filter configuration file */
4101 curengine = engine;
4102 curproxy = px;
4103 curagent = NULL;
4104 curmsg = NULL;
4105 ret = readcfgfile(file);
4106 curproxy = NULL;
4107
4108 /* unregister SPOE sections and restore previous sections */
4109 cfg_unregister_sections();
4110 cfg_restore_sections(&backup_sections);
4111
4112 if (ret == -1) {
4113 memprintf(err, "Could not open configuration file %s : %s",
4114 file, strerror(errno));
4115 goto error;
4116 }
4117 if (ret & (ERR_ABORT|ERR_FATAL)) {
4118 memprintf(err, "Error(s) found in configuration file %s", file);
4119 goto error;
4120 }
4121
4122 /* Check SPOE agent */
4123 if (curagent == NULL) {
4124 memprintf(err, "No SPOE agent found in file %s", file);
4125 goto error;
4126 }
4127 if (curagent->b.name == NULL) {
4128 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4129 curagent->id, curagent->conf.file, curagent->conf.line);
4130 goto error;
4131 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004132 if (curagent->timeout.hello == TICK_ETERNITY ||
4133 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004134 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004135 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4136 " | While not properly invalid, you will certainly encounter various problems\n"
4137 " | with such a configuration. To fix this, please ensure that all following\n"
4138 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4139 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004140 }
4141 if (curagent->var_pfx == NULL) {
4142 char *tmp = curagent->id;
4143
4144 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004145 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004146 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4147 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4148 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4149 goto error;
4150 }
4151 tmp++;
4152 }
4153 curagent->var_pfx = strdup(curagent->id);
4154 }
4155
Christopher Fauletb7426d12018-03-21 14:12:17 +01004156 if (curagent->var_on_error) {
4157 struct arg arg;
4158
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004159 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004160 curagent->var_pfx, curagent->var_on_error);
4161
4162 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004163 arg.data.str.area = trash.area;
4164 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004165 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004166 if (!vars_check_arg(&arg, err)) {
4167 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4168 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4169 goto error;
4170 }
4171 }
4172
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004173 if (curagent->var_t_process) {
4174 struct arg arg;
4175
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004176 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004177 curagent->var_pfx, curagent->var_t_process);
4178
4179 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004180 arg.data.str.area = trash.area;
4181 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004182 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004183 if (!vars_check_arg(&arg, err)) {
4184 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4185 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4186 goto error;
4187 }
4188 }
4189
4190 if (curagent->var_t_total) {
4191 struct arg arg;
4192
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004193 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004194 curagent->var_pfx, curagent->var_t_total);
4195
4196 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004197 arg.data.str.area = trash.area;
4198 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004199 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004200 if (!vars_check_arg(&arg, err)) {
4201 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4202 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4203 goto error;
4204 }
4205 }
4206
Christopher Faulet11610f32017-09-21 10:23:10 +02004207 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004208 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4209 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004210 goto finish;
4211 }
4212
Christopher Faulet11610f32017-09-21 10:23:10 +02004213 /* Replace placeholders by the corresponding messages for the SPOE
4214 * agent */
4215 list_for_each_entry(ph, &curmphs, list) {
4216 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004217 struct spoe_arg *arg;
4218 unsigned int where;
4219
Christopher Faulet11610f32017-09-21 10:23:10 +02004220 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004221 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4222 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4223 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4224 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4225 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4226 }
4227 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4228 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4229 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004230 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4231 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004232 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004233 }
4234 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004235 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4236 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004237 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004238 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004239
4240 where = 0;
4241 switch (msg->event) {
4242 case SPOE_EV_ON_CLIENT_SESS:
4243 where |= SMP_VAL_FE_CON_ACC;
4244 break;
4245
4246 case SPOE_EV_ON_TCP_REQ_FE:
4247 where |= SMP_VAL_FE_REQ_CNT;
4248 break;
4249
4250 case SPOE_EV_ON_HTTP_REQ_FE:
4251 where |= SMP_VAL_FE_HRQ_HDR;
4252 break;
4253
4254 case SPOE_EV_ON_TCP_REQ_BE:
4255 if (px->cap & PR_CAP_FE)
4256 where |= SMP_VAL_FE_REQ_CNT;
4257 if (px->cap & PR_CAP_BE)
4258 where |= SMP_VAL_BE_REQ_CNT;
4259 break;
4260
4261 case SPOE_EV_ON_HTTP_REQ_BE:
4262 if (px->cap & PR_CAP_FE)
4263 where |= SMP_VAL_FE_HRQ_HDR;
4264 if (px->cap & PR_CAP_BE)
4265 where |= SMP_VAL_BE_HRQ_HDR;
4266 break;
4267
4268 case SPOE_EV_ON_SERVER_SESS:
4269 where |= SMP_VAL_BE_SRV_CON;
4270 break;
4271
4272 case SPOE_EV_ON_TCP_RSP:
4273 if (px->cap & PR_CAP_FE)
4274 where |= SMP_VAL_FE_RES_CNT;
4275 if (px->cap & PR_CAP_BE)
4276 where |= SMP_VAL_BE_RES_CNT;
4277 break;
4278
4279 case SPOE_EV_ON_HTTP_RSP:
4280 if (px->cap & PR_CAP_FE)
4281 where |= SMP_VAL_FE_HRS_HDR;
4282 if (px->cap & PR_CAP_BE)
4283 where |= SMP_VAL_BE_HRS_HDR;
4284 break;
4285
4286 default:
4287 break;
4288 }
4289
4290 list_for_each_entry(arg, &msg->args, list) {
4291 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004292 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004293 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004294 "none of which is available here ('%s')",
4295 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004296 sample_ckp_names(arg->expr->fetch->use),
4297 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004298 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004299 }
4300 }
4301
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004302 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004303 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4304 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004305 }
4306 }
4307 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004308 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004309 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004310 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004311 continue;
4312 }
4313
Christopher Faulet11610f32017-09-21 10:23:10 +02004314 /* Replace placeholders by the corresponding groups for the SPOE
4315 * agent */
4316 list_for_each_entry(ph, &curgphs, list) {
4317 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4318 if (!strcmp(grp->id, ph->id)) {
4319 grp->agent = curagent;
4320 LIST_DEL(&grp->list);
4321 LIST_ADDQ(&curagent->groups, &grp->list);
4322 goto next_aph;
4323 }
4324 }
4325 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4326 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4327 goto error;
4328 next_aph:
4329 continue;
4330 }
4331
4332 /* Replace placeholders by the corresponding message for each SPOE
4333 * group of the SPOE agent */
4334 list_for_each_entry(grp, &curagent->groups, list) {
4335 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4336 list_for_each_entry(msg, &curmsgs, list) {
4337 if (!strcmp(msg->id, ph->id)) {
4338 if (msg->group != NULL) {
4339 memprintf(err, "SPOE message '%s' already belongs to "
4340 "the SPOE group '%s' declare at %s:%d",
4341 msg->id, msg->group->id,
4342 msg->group->conf.file,
4343 msg->group->conf.line);
4344 goto error;
4345 }
4346
4347 /* Scope for arguments are not checked for now. We will check
4348 * them only if a rule use the corresponding SPOE group. */
4349 msg->agent = curagent;
4350 msg->group = grp;
4351 LIST_DEL(&ph->list);
4352 LIST_ADDQ(&grp->messages, &msg->by_grp);
4353 goto next_mph_grp;
4354 }
4355 }
4356 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4357 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4358 goto error;
4359 next_mph_grp:
4360 continue;
4361 }
4362 }
4363
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004364 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004365 /* move curmsgs to the agent message list */
4366 curmsgs.n->p = &curagent->messages;
4367 curmsgs.p->n = &curagent->messages;
4368 curagent->messages = curmsgs;
4369 LIST_INIT(&curmsgs);
4370
Christopher Faulet7ee86672017-09-19 11:08:28 +02004371 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004372 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004373
4374 /* Start agent's proxy initialization here. It will be finished during
4375 * the filter init. */
4376 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4377 init_new_proxy(&conf->agent_fe);
4378 conf->agent_fe.id = conf->agent->id;
4379 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004380 conf->agent_fe.options |= curpxopts;
4381 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004382
4383 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4384 LIST_DEL(&logsrv->list);
4385 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4386 }
4387
Christopher Faulet11610f32017-09-21 10:23:10 +02004388 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4389 LIST_DEL(&ph->list);
4390 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004391 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004392 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4393 LIST_DEL(&ph->list);
4394 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004395 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004396 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4397 struct arg arg;
4398
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004399 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004400 curagent->var_pfx, vph->name);
4401
4402 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004403 arg.data.str.area = trash.area;
4404 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004405 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004406 if (!vars_check_arg(&arg, err)) {
4407 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4408 curagent->id, curagent->var_pfx, vph->name, *err);
4409 goto error;
4410 }
4411
4412 LIST_DEL(&vph->list);
4413 free(vph->name);
4414 free(vph);
4415 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004416 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4417 LIST_DEL(&grp->list);
4418 spoe_release_group(grp);
4419 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004420 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004421 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004422 fconf->ops = &spoe_ops;
4423 fconf->conf = conf;
4424 return 0;
4425
4426 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004427 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004428 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4429 LIST_DEL(&ph->list);
4430 spoe_release_placeholder(ph);
4431 }
4432 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4433 LIST_DEL(&ph->list);
4434 spoe_release_placeholder(ph);
4435 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004436 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4437 LIST_DEL(&vph->list);
4438 free(vph->name);
4439 free(vph);
4440 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004441 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4442 LIST_DEL(&grp->list);
4443 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004444 }
4445 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4446 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004447 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004448 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004449 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4450 LIST_DEL(&logsrv->list);
4451 free(logsrv);
4452 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004453 free(conf);
4454 return -1;
4455}
4456
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004457/* Send message of a SPOE group. This is the action_ptr callback of a rule
4458 * associated to a "send-spoe-group" action.
4459 *
Christopher Faulet13403762019-12-13 09:01:57 +01004460 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4461 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004462static enum act_return
4463spoe_send_group(struct act_rule *rule, struct proxy *px,
4464 struct session *sess, struct stream *s, int flags)
4465{
4466 struct filter *filter;
4467 struct spoe_agent *agent = NULL;
4468 struct spoe_group *group = NULL;
4469 struct spoe_context *ctx = NULL;
4470 int ret, dir;
4471
4472 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4473 if (filter->config == rule->arg.act.p[0]) {
4474 agent = rule->arg.act.p[2];
4475 group = rule->arg.act.p[3];
4476 ctx = filter->ctx;
4477 break;
4478 }
4479 }
4480 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004481 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004482 if (ctx->state == SPOE_CTX_ST_NONE)
4483 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004484
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004485 switch (rule->from) {
4486 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4487 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4488 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4489 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4490 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4491 default:
4492 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4493 " - internal error while execute spoe-send-group\n",
4494 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4495 __FUNCTION__, s);
4496 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4497 agent->id);
4498 return ACT_RET_CONT;
4499 }
4500
4501 ret = spoe_process_group(s, ctx, group, dir);
4502 if (ret == 1)
4503 return ACT_RET_CONT;
4504 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004505 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004506 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4507 " - failed to process group '%s': interrupted by caller\n",
4508 (int)now.tv_sec, (int)now.tv_usec,
4509 agent->id, __FUNCTION__, s, group->id);
4510 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004511 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004512 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004513 return ACT_RET_CONT;
4514 }
4515 return ACT_RET_YIELD;
4516 }
4517 else
Christopher Faulet13403762019-12-13 09:01:57 +01004518 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004519}
4520
4521/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4522 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4523 * action should be:
4524 *
4525 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4526 *
4527 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4528 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4529 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4530 * group.
4531 *
4532 * The function returns 1 in success case, otherwise, it returns 0 and err is
4533 * filled.
4534 */
4535static int
4536check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4537{
4538 struct flt_conf *fconf;
4539 struct spoe_config *conf;
4540 struct spoe_agent *agent = NULL;
4541 struct spoe_group *group;
4542 struct spoe_message *msg;
4543 char *engine_id = rule->arg.act.p[0];
4544 char *group_id = rule->arg.act.p[1];
4545 unsigned int where = 0;
4546
4547 switch (rule->from) {
4548 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4549 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4550 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4551 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4552 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4553 default:
4554 memprintf(err,
4555 "internal error, unexpected rule->from=%d, please report this bug!",
4556 rule->from);
4557 goto error;
4558 }
4559
4560 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4561 * <px> */
4562 list_for_each_entry(fconf, &px->filter_configs, list) {
4563 conf = fconf->conf;
4564
4565 /* This is not an SPOE filter */
4566 if (fconf->id != spoe_filter_id)
4567 continue;
4568
4569 /* This is the good engine */
4570 if (!strcmp(conf->id, engine_id)) {
4571 agent = conf->agent;
4572 break;
4573 }
4574 }
4575 if (agent == NULL) {
4576 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4577 engine_id, group_id);
4578 goto error;
4579 }
4580
4581 /* Try to find the right group */
4582 list_for_each_entry(group, &agent->groups, list) {
4583 /* This is the good group */
4584 if (!strcmp(group->id, group_id))
4585 break;
4586 }
4587 if (&group->list == &agent->groups) {
4588 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4589 group_id, engine_id);
4590 goto error;
4591 }
4592
4593 /* Ok, we found the group, we need to check messages and their
4594 * arguments */
4595 list_for_each_entry(msg, &group->messages, by_grp) {
4596 struct spoe_arg *arg;
4597
4598 list_for_each_entry(arg, &msg->args, list) {
4599 if (!(arg->expr->fetch->val & where)) {
4600 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4601 "some args extract information from '%s',"
4602 "none of which is available here ('%s')",
4603 msg->id, group->id, msg->conf.file, msg->conf.line,
4604 sample_ckp_names(arg->expr->fetch->use),
4605 sample_ckp_names(where));
4606 goto error;
4607 }
4608 }
4609 }
4610
4611 free(engine_id);
4612 free(group_id);
4613 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4614 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4615 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4616 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4617 return 1;
4618
4619 error:
4620 free(engine_id);
4621 free(group_id);
4622 return 0;
4623}
4624
4625/* Parse 'send-spoe-group' action following the format:
4626 *
4627 * ... send-spoe-group <engine-id> <group-id>
4628 *
4629 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4630 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4631 * ids are saved and used later, when the rule will be checked.
4632 */
4633static enum act_parse_ret
4634parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4635 struct act_rule *rule, char **err)
4636{
4637 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4638 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4639 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4640 return ACT_RET_PRS_ERR;
4641 }
4642 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4643 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4644
4645 (*orig_arg) += 2;
4646
4647 rule->action = ACT_CUSTOM;
4648 rule->action_ptr = spoe_send_group;
4649 rule->check_ptr = check_send_spoe_group;
4650 return ACT_RET_PRS_OK;
4651}
4652
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004653
4654/* Declare the filter parser for "spoe" keyword */
4655static struct flt_kw_list flt_kws = { "SPOE", { }, {
4656 { "spoe", parse_spoe_flt, NULL },
4657 { NULL, NULL, NULL },
4658 }
4659};
4660
Willy Tarreau0108d902018-11-25 19:14:37 +01004661INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4662
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004663/* Delcate the action parser for "spoe-action" keyword */
4664static struct action_kw_list tcp_req_action_kws = { { }, {
4665 { "send-spoe-group", parse_send_spoe_group },
4666 { /* END */ },
4667 }
4668};
Willy Tarreau0108d902018-11-25 19:14:37 +01004669
4670INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4671
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004672static struct action_kw_list tcp_res_action_kws = { { }, {
4673 { "send-spoe-group", parse_send_spoe_group },
4674 { /* END */ },
4675 }
4676};
Willy Tarreau0108d902018-11-25 19:14:37 +01004677
4678INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4679
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004680static struct action_kw_list http_req_action_kws = { { }, {
4681 { "send-spoe-group", parse_send_spoe_group },
4682 { /* END */ },
4683 }
4684};
Willy Tarreau0108d902018-11-25 19:14:37 +01004685
4686INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4687
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004688static struct action_kw_list http_res_action_kws = { { }, {
4689 { "send-spoe-group", parse_send_spoe_group },
4690 { /* END */ },
4691 }
4692};
4693
Willy Tarreau0108d902018-11-25 19:14:37 +01004694INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);