blob: 8e35018aaf0010617e5971ea060aa0a503dd19a8 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Willy Tarreaudcc048a2020-06-04 19:11:43 +020015#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020020#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020021#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020023#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020024#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020025#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020027#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020029#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/signal.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020031#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020033#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020034#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020035#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020037#include <haproxy/time.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020039
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020040
41#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
42#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010043#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020044#else
45#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010046#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020047#endif
48
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010049/* Reserved 4 bytes to the frame size. So a frame and its size can be written
50 * together in a buffer */
51#define MAX_FRAME_SIZE global.tune.bufsize - 4
52
53/* The minimum size for a frame */
54#define MIN_FRAME_SIZE 256
55
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010056/* Reserved for the metadata and the frame type.
57 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010058#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020059
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010061#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
62
Christopher Faulet3b386a32017-02-23 10:17:15 +010063/* SPOE filter id. Used to identify SPOE filters */
64const char *spoe_filter_id = "SPOE filter";
65
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020066/* Set if the handle on SIGUSR1 is registered */
67static int sighandler_registered = 0;
68
69/* proxy used during the parsing */
70struct proxy *curproxy = NULL;
71
72/* The name of the SPOE engine, used during the parsing */
73char *curengine = NULL;
74
75/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020076/* SPOE agent/group/message used during the parsing */
77struct spoe_agent *curagent = NULL;
78struct spoe_group *curgrp = NULL;
79struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020080
81/* list of SPOE messages and placeholders used during the parsing */
82struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020083struct list curgrps;
84struct list curmphs;
85struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010086struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020087
Christopher Faulet7250b8f2018-03-26 17:19:01 +020088/* list of log servers used during the parsing */
89struct list curlogsrvs;
90
Christopher Faulet0e0f0852018-03-26 17:20:36 +020091/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
92int curpxopts;
93int curpxopts2;
94
Christopher Faulet42bfa462017-01-04 14:14:19 +010095/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +010096DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
97DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020098
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020099struct flt_ops spoe_ops;
100
Christopher Faulet8ef75252017-02-20 22:56:03 +0100101static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
103static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104
105/********************************************************************
106 * helper functions/globals
107 ********************************************************************/
108static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200109spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110{
Christopher Faulet11610f32017-09-21 10:23:10 +0200111 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200113 free(ph->id);
114 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200115}
116
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200117static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100118spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119{
Christopher Faulet57583e42017-09-04 15:41:09 +0200120 struct spoe_arg *arg, *argback;
121 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200122
123 if (!msg)
124 return;
125 free(msg->id);
126 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200127 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128 release_sample_expr(arg->expr);
129 free(arg->name);
130 LIST_DEL(&arg->list);
131 free(arg);
132 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
134 LIST_DEL(&acl->list);
135 prune_acl(acl);
136 free(acl);
137 }
138 if (msg->cond) {
139 prune_acl_cond(msg->cond);
140 free(msg->cond);
141 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200142 free(msg);
143}
144
145static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200146spoe_release_group(struct spoe_group *grp)
147{
148 if (!grp)
149 return;
150 free(grp->id);
151 free(grp->conf.file);
152 free(grp);
153}
154
155static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100156spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200157{
Christopher Faulet11610f32017-09-21 10:23:10 +0200158 struct spoe_message *msg, *msgback;
159 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200160 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200161
162 if (!agent)
163 return;
164 free(agent->id);
165 free(agent->conf.file);
166 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100167 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100168 free(agent->var_t_process);
169 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200170 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
171 LIST_DEL(&msg->list);
172 spoe_release_message(msg);
173 }
174 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
175 LIST_DEL(&grp->list);
176 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200177 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100178 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200179 for (i = 0; i < global.nbthread; ++i) {
180 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100181 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200182 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100183 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200184 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200185 free(agent);
186}
187
188static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100189 [SPOE_FRM_ERR_NONE] = "normal",
190 [SPOE_FRM_ERR_IO] = "I/O error",
191 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
192 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
193 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
194 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
195 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
196 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
197 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
198 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
199 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
200 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100201 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100202 [SPOE_FRM_ERR_RES] = "resource allocation error",
203 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200204};
205
206static const char *spoe_event_str[SPOE_EV_EVENTS] = {
207 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
208 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
209 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
210 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
211 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
212
213 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
214 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
215 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
216};
217
218
219#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
220
221static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100222 [SPOE_CTX_ST_NONE] = "NONE",
223 [SPOE_CTX_ST_READY] = "READY",
224 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
225 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
226 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
227 [SPOE_CTX_ST_DONE] = "DONE",
228 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200229};
230
231static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100232 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
233 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
234 [SPOE_APPCTX_ST_IDLE] = "IDLE",
235 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
236 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
237 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
238 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
239 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
240 [SPOE_APPCTX_ST_EXIT] = "EXIT",
241 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200242};
243
244#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100245
Christopher Faulet8ef75252017-02-20 22:56:03 +0100246/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500247 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100248 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100249static char *
250generate_pseudo_uuid()
251{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100252 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800253 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254}
255
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100256
257static inline void
258spoe_update_stat_time(struct timeval *tv, long *t)
259{
260 if (*t == -1)
261 *t = tv_ms_elapsed(tv, &now);
262 else
263 *t += tv_ms_elapsed(tv, &now);
264 tv_zero(tv);
265}
266
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200267/********************************************************************
268 * Functions that encode/decode SPOE frames
269 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270/* Helper to get static string length, excluding the terminating null byte */
271#define SLEN(str) (sizeof(str)-1)
272
273/* Predefined key used in HELLO/DISCONNECT frames */
274#define SUPPORTED_VERSIONS_KEY "supported-versions"
275#define VERSION_KEY "version"
276#define MAX_FRAME_SIZE_KEY "max-frame-size"
277#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100278#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100279#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200280#define STATUS_CODE_KEY "status-code"
281#define MSG_KEY "message"
282
283struct spoe_version {
284 char *str;
285 int min;
286 int max;
287};
288
289/* All supported versions */
290static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200291 /* 1.0 is now unsupported because of a bug about frame's flags*/
292 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293 {NULL, 0, 0}
294};
295
296/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200297#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200298
Christopher Faulet8ef75252017-02-20 22:56:03 +0100299/* Convert a string to a SPOE version value. The string must follow the format
300 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
301 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200302static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100303spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305 const char *p, *end;
306 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 p = str;
309 end = str+len;
310 maj = min = 0;
311 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200312
Christopher Faulet8ef75252017-02-20 22:56:03 +0100313 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100314 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100315 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200316
Christopher Faulet8ef75252017-02-20 22:56:03 +0100317 /* parse Major number, until the '.' */
318 while (*p != '.') {
319 if (p >= end || *p < '0' || *p > '9')
320 goto out;
321 maj *= 10;
322 maj += (*p - '0');
323 p++;
324 }
325
326 /* check Major version */
327 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328 goto out;
329
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 p++; /* skip the '.' */
331 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
332 goto out;
333
334 /* Parse Minor number */
335 while (p < end) {
336 if (*p < '0' || *p > '9')
337 break;
338 min *= 10;
339 min += (*p - '0');
340 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342
343 /* check Minor number */
344 if (min > 999)
345 goto out;
346
347 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100348 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100349 p++;
350 if (p != end)
351 goto out;
352
353 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200354 out:
355 return vsn;
356}
357
Christopher Faulet8ef75252017-02-20 22:56:03 +0100358/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800359 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100360 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200361static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200363{
Willy Tarreau83061a82018-07-13 11:56:34 +0200364 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100365 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 char *p, *end;
367 unsigned int flags = SPOE_FRM_FL_FIN;
368 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370 p = frame;
371 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 /* Set Frame type */
374 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100376 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200377 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378 memcpy(p, (char *)&flags, 4);
379 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200380
381 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100382 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200383
384 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
385 * and "capabilities" */
386
387 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 sz = SLEN(SUPPORTED_VERSIONS_KEY);
389 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
390 goto too_big;
391
392 *p++ = SPOE_DATA_T_STR;
393 sz = SLEN(SUPPORTED_VERSIONS_VAL);
394 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
395 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200396
397 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 sz = SLEN(MAX_FRAME_SIZE_KEY);
399 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
400 goto too_big;
401
402 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200403 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
406 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 sz = SLEN(CAPABILITIES_KEY);
408 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
409 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200410
Christopher Faulet8ef75252017-02-20 22:56:03 +0100411 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100412 chk = get_trash_chunk();
413 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200414 memcpy(chk->area, "pipelining", 10);
415 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100416 }
417 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200418 if (chk->data) chk->area[chk->data++] = ',';
419 memcpy(chk->area+chk->data, "async", 5);
420 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100421 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100422 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200423 if (chk->data) chk->area[chk->data++] = ',';
424 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100425 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100426 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200427 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 goto too_big;
429
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500430 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200431 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 sz = SLEN(ENGINE_ID_KEY);
433 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
434 goto too_big;
435
436 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200437 sz = strlen(agent->rt[tid].engine_id);
438 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100439 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100440 }
441
Christopher Faulet8ef75252017-02-20 22:56:03 +0100442 return (p - frame);
443
444 too_big:
445 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
446 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200447}
448
Christopher Faulet8ef75252017-02-20 22:56:03 +0100449/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
450 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
451 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200452static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200454{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455 const char *reason;
456 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100457 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200459
Christopher Faulet8ef75252017-02-20 22:56:03 +0100460 p = frame;
461 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 /* Set Frame type */
464 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100466 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200467 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468 memcpy(p, (char *)&flags, 4);
469 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200470
471 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 *p++ = 0; *p++ = 0;
473
474 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
475 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476
477 /* There are 2 mandatory items: "status-code" and "message" */
478
479 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 sz = SLEN(STATUS_CODE_KEY);
481 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
482 goto too_big;
483
484 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200485 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200487
488 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 sz = SLEN(MSG_KEY);
490 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
491 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 /*Get the message corresponding to the status code */
494 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
495
496 *p++ = SPOE_DATA_T_STR;
497 sz = strlen(reason);
498 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
499 goto too_big;
500
501 return (p - frame);
502
503 too_big:
504 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
505 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506}
507
Christopher Faulet8ef75252017-02-20 22:56:03 +0100508/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
509 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
510 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200511static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100513 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100515 char *p, *end;
516 unsigned int stream_id, frame_id;
517 unsigned int flags = SPOE_FRM_FL_FIN;
518 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200519
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520 p = frame;
521 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100523 stream_id = ctx->stream_id;
524 frame_id = ctx->frame_id;
525
526 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
527 /* The fragmentation is not supported by the applet */
528 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
529 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
530 return -1;
531 }
532 flags = ctx->frag_ctx.flags;
533 }
534
535 /* Set Frame type */
536 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
537
538 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200539 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100540 memcpy(p, (char *)&flags, 4);
541 p += 4;
542
543 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200544 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100545 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200546 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100547 goto too_big;
548
549 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200550 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 if (p + sz >= end)
552 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200553 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100554 p += sz;
555
556 return (p - frame);
557
558 too_big:
559 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
560 return 0;
561}
562
563/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
564 * returns the number of encoded bytes in the frame on success, 0 if an encoding
565 * error occurred and -1 if a fatal error occurred. */
566static int
567spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
568 char *frame, size_t size)
569{
570 char *p, *end;
571 unsigned int stream_id, frame_id;
572 unsigned int flags;
573 size_t sz;
574
575 p = frame;
576 end = frame+size;
577
Christopher Faulet8ef75252017-02-20 22:56:03 +0100578 /* <ctx> is null when the stream has aborted the processing of a
579 * fragmented frame. In this case, we must notify the corresponding
580 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100581 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100582 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100583 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
584 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
585 }
586 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100587 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100588 stream_id = ctx->stream_id;
589 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100590 }
591
Christopher Faulet8ef75252017-02-20 22:56:03 +0100592 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100595 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200596 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597 memcpy(p, (char *)&flags, 4);
598 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200599
600 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200601 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100602 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200603 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100604 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200605
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100606 if (ctx == NULL)
607 goto end;
608
Christopher Faulet8ef75252017-02-20 22:56:03 +0100609 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200610 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100611 if (p + sz >= end)
612 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200613 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100615
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100616 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 return (p - frame);
618
619 too_big:
620 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
621 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200622}
623
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624/* Decode and process the HELLO frame sent by an agent. It returns the number of
625 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
626 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200627static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200629{
Christopher Faulet305c6072017-02-23 16:17:53 +0100630 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
631 char *p, *end;
632 int vsn, max_frame_size;
633 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634
635 p = frame;
636 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200637
638 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
640 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200641 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 if (size < 7 /* TYPE + METADATA */) {
645 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
646 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647 }
648
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100649 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200651 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100652 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100654 /* Fragmentation is not supported for HELLO frame */
655 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100657 return -1;
658 }
659
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200660 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661 if (*p != 0 || *(p+1) != 0) {
662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
663 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666
667 /* There are 3 mandatory items: "version", "max-frame-size" and
668 * "capabilities" */
669
670 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100671 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 while (p < end) {
673 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200674 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200676
677 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 ret = spoe_decode_buffer(&p, end, &str, &sz);
679 if (ret == -1 || !sz) {
680 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
681 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200684 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200685 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 int i, type = *p++;
687
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
690 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
691 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
694 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
695 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696 }
697
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 return -1;
702 }
703 for (i = 0; supported_versions[i].str != NULL; ++i) {
704 if (vsn >= supported_versions[i].min &&
705 vsn <= supported_versions[i].max)
706 break;
707 }
708 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710 return -1;
711 }
712 }
713 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200714 else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716
717 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
719 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
720 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
721 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100722 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
723 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200725 if (decode_varint(&p, end, &sz) == -1) {
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 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 if (sz < MIN_FRAME_SIZE ||
730 sz > SPOE_APPCTX(appctx)->max_frame_size) {
731 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732 return -1;
733 }
734 max_frame_size = sz;
735 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100736 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200737 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100738 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739
740 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100741 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
743 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100744 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
748 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749
Christopher Faulet8ef75252017-02-20 22:56:03 +0100750 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100751 char *delim;
752
753 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100754 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
757 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100758 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759 flags |= SPOE_APPCTX_FL_PIPELINING;
760 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 else if (sz >= 5 && !strncmp(str, "async", 5)) {
762 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100763 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 flags |= SPOE_APPCTX_FL_ASYNC;
765 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
767 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100768 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100769 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
770 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 /* Get the next comma or break */
773 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 delim++;
776 sz -= (delim - str);
777 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 }
779 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200780 else {
781 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100782 if (spoe_skip_data(&p, end) == -1) {
783 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
784 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200785 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 }
787 }
788
789 /* Final checks */
790 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792 return -1;
793 }
794 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 return -1;
797 }
Christopher Faulet11389012019-02-07 16:13:26 +0100798 if (!agent)
799 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
800 else {
801 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
802 flags &= ~SPOE_APPCTX_FL_PIPELINING;
803 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
804 flags &= ~SPOE_APPCTX_FL_ASYNC;
805 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806
Christopher Faulet42bfa462017-01-04 14:14:19 +0100807 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
808 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
809 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100810
811 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812}
813
814/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
815 * bytes on success, 0 if the frame can be ignored and -1 if an error
816 * occurred. */
817static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100820 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100821 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822
823 p = frame;
824 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825
826 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100827 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
828 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 if (size < 7 /* TYPE + METADATA */) {
833 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
834 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835 }
836
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100837 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200839 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100842 /* Fragmentation is not supported for DISCONNECT frame */
843 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100845 return -1;
846 }
847
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200848 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100849 if (*p != 0 || *(p+1) != 0) {
850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
851 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854
855 /* There are 2 mandatory items: "status-code" and "message" */
856
857 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100858 while (p < end) {
859 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200860 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862
863 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 ret = spoe_decode_buffer(&p, end, &str, &sz);
865 if (ret == -1 || !sz) {
866 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
867 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868 }
869
870 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200871 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873
874 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
876 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
877 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
878 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
880 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200882 if (decode_varint(&p, end, &sz) == -1) {
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 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
888
889 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200890 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 int type = *p++;
892
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
895 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
896 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 ret = spoe_decode_buffer(&p, end, &str, &sz);
899 if (ret == -1 || sz > 255) {
900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
904 SPOE_APPCTX(appctx)->reason = str;
905 SPOE_APPCTX(appctx)->rlen = sz;
906#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907 }
908 else {
909 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100910 if (spoe_skip_data(&p, end) == -1) {
911 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
912 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 }
915 }
916
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918}
919
920
Christopher Fauleta1cda022016-12-21 08:58:06 +0100921/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922 * success, 0 if the frame can be ignored and -1 if an error occurred. */
923static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100925 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100927 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100929 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932
933 p = frame;
934 end = frame + size;
935 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200936
937 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
939 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200940 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 if (size < 7 /* TYPE + METADATA */) {
944 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
945 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 }
947
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200950 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100953 /* Fragmentation is not supported for now */
954 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 return -1;
957 }
958
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200959 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200960 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100962 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100963 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200964 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100968
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100970 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200971 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100972 if ((*ctx)->stream_id == (unsigned int)stream_id &&
973 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100974 goto found;
975 }
976 }
977 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
979 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100980 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100981 goto found;
982 }
983 }
984
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100985 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
986 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
987 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
988
989 /* ABRT bit is set for an unfinished fragmented frame */
990 if (flags & SPOE_FRM_FL_ABRT) {
991 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100992 (*ctx)->state = SPOE_CTX_ST_ERROR;
993 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
994 /* Ignore the payload */
995 goto end;
996 }
997 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
998 /* For now, we ignore the ack */
999 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1000 return 0;
1001 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001002
Christopher Fauleta1cda022016-12-21 08:58:06 +01001003 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001004 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1005 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001006 " - stream-id=%u - frame-id=%u\n",
1007 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1008 __FUNCTION__, appctx,
1009 (unsigned int)stream_id, (unsigned int)frame_id);
1010
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001011 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001012 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1013 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001014 return 0;
1015
1016 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001017 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1018 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001019 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001020 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001021 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001022
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001023 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001024 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001025 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1026 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001028
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 /* Transfer the buffer ownership to the SPOE context */
1030 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001031 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001032
Christopher Faulet8ef75252017-02-20 22:56:03 +01001033 (*ctx)->state = SPOE_CTX_ST_DONE;
1034
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001035 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 " - ACK frame received"
1038 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001039 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001040 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1041 (*ctx)->frame_id, flags);
1042 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001043}
1044
Christopher Fauletba7bc162016-11-07 21:07:38 +01001045/* This function is used in cfgparse.c and declared in proto/checks.h. It
1046 * prepare the request to send to agents during a healthcheck. It returns 0 on
1047 * success and -1 if an error occurred. */
1048int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001050{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001051 struct appctx appctx;
1052 struct spoe_appctx spoe_appctx;
1053 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1054 size_t sz;
1055 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001056
Christopher Faulet42bfa462017-01-04 14:14:19 +01001057 memset(&appctx, 0, sizeof(appctx));
1058 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001060
1061 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001062 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001063
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1065 end = frame + MAX_FRAME_SIZE;
1066
1067 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1068 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet8ef75252017-02-20 22:56:03 +01001072 /* Add "healthcheck" K/V item */
1073 sz = SLEN(HEALTHCHECK_KEY);
1074 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1075 return -1;
1076 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001077
Christopher Faulet8ef75252017-02-20 22:56:03 +01001078 *len = frame - buf;
1079 sz = htonl(*len - 4);
1080 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085 return 0;
1086}
1087
1088/* This function is used in checks.c and declared in proto/checks.h. It decode
1089 * the response received from an agent during a healthcheck. It returns 0 on
1090 * success and -1 if an error occurred. */
1091int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001094 struct appctx appctx;
1095 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet42bfa462017-01-04 14:14:19 +01001097 memset(&appctx, 0, sizeof(appctx));
1098 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099
Christopher Faulet42bfa462017-01-04 14:14:19 +01001100 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001101 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001102
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1104 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105 goto error;
1106 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1108 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109
1110 return 0;
1111
1112 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1114 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1115 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001116 return -1;
1117}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001118
Christopher Fauleta1cda022016-12-21 08:58:06 +01001119/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001120 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001121 * success. */
1122static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001123spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001124{
1125 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 int ret;
1127 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128
Christopher Faulet8ef75252017-02-20 22:56:03 +01001129 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1130 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131 netint = htonl(framesz);
1132 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001133 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001135 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001136 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001137 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138 }
1139 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140 return -1; /* error */
1141 }
1142 return framesz;
1143}
1144
1145/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1146 * when the frame can be ignored, 1 to retry later and the frame length on
1147 * success. */
1148static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001149spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150{
1151 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001152 int ret;
1153 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154
Willy Tarreau06d80a92017-10-19 14:32:15 +02001155 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 if (ret > 0) {
1157 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001158 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001160 return -1;
1161 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001162 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163 }
1164 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001167 }
1168 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169 return -1; /* error */
1170 }
1171 return framesz;
1172}
1173
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001174/********************************************************************
1175 * Functions that manage the SPOE applet
1176 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001177static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001179{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001180 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001181 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001182 appctx_wakeup(appctx);
1183 return 1;
1184}
1185
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001186/* Callback function that catches applet timeouts. If a timeout occurred, we set
1187 * <appctx->st1> flag and the SPOE applet is woken up. */
1188static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001189spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001190{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001191 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001192
1193 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1194 if (tick_is_expired(task->expire, now_ms)) {
1195 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001196 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001197 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001198 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199 return task;
1200}
1201
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202/* Callback function that releases a SPOE applet. This happens when the
1203 * connection with the agent is closed. */
1204static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001205spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001206{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001207 struct stream_interface *si = appctx->owner;
1208 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1209 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001210 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001211
1212 if (spoe_appctx == NULL)
1213 return;
1214
1215 appctx->ctx.spoe.ptr = NULL;
1216 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001217
1218 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1219 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1220 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221
Christopher Faulet8ef75252017-02-20 22:56:03 +01001222 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001223 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001224 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001225 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1226 LIST_DEL(&spoe_appctx->list);
1227 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001228 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001229 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001230
Christopher Faulet8ef75252017-02-20 22:56:03 +01001231 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001232 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001233 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1234 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001235 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001236 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001237
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001238 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1240 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001241
1242 si_shutw(si);
1243 si_shutr(si);
1244 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245 }
1246
Christopher Faulet8ef75252017-02-20 22:56:03 +01001247 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001248 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001249
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 /* Notify all waiting streams */
1251 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001252 LIST_DEL(&ctx->list);
1253 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001254 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001255 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletabe894d2020-11-10 18:45:34 +01001256 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001257 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001259 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001260 }
1261
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 /* If the applet was processing a fragmented frame, notify the
1263 * corresponding stream. */
1264 if (spoe_appctx->frag_ctx.ctx) {
1265 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001266 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001267 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001269 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1270 }
1271
Christopher Fauletabe894d2020-11-10 18:45:34 +01001272 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1273 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1274 if (ctx->spoe_appctx == spoe_appctx)
1275 ctx->spoe_appctx = NULL;
1276 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001277 goto end;
Christopher Fauletabe894d2020-11-10 18:45:34 +01001278 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001279
Christopher Faulet8ef75252017-02-20 22:56:03 +01001280 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001281 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001282 LIST_DEL(&ctx->list);
1283 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001284 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001285 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletabe894d2020-11-10 18:45:34 +01001286 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001287 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001288 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001289 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001290 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001291 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292 LIST_DEL(&ctx->list);
1293 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001294 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001295 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletabe894d2020-11-10 18:45:34 +01001296 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1300 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001301
1302 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001303 /* Release allocated memory */
1304 spoe_release_buffer(&spoe_appctx->buffer,
1305 &spoe_appctx->buffer_wait);
1306 pool_free(pool_head_spoe_appctx, spoe_appctx);
1307
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001308 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001309 agent->rt[tid].frame_size = agent->max_frame_size;
1310 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1311 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312}
1313
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001314static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001315spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001318 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001321
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001322 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1323 /* closed */
1324 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1325 goto exit;
1326 }
1327
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001328 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001329 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001330 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1332 goto stop;
1333 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001334
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001336 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1337 " - Connection timed out\n",
1338 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1339 __FUNCTION__, appctx);
1340 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001342 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343
Christopher Faulet42bfa462017-01-04 14:14:19 +01001344 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345 SPOE_APPCTX(appctx)->task->expire =
1346 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1349 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001350 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001351 ret = spoe_prepare_hahello_frame(appctx, frame,
1352 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355
1356 switch (ret) {
1357 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 case 0: /* ignore => an error, cannot be ignored */
1359 goto exit;
1360
1361 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001362 goto stop;
1363
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 default:
1365 /* HELLO frame successfully sent, now wait for the
1366 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001367 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1368 goto next;
1369 }
1370
1371 next:
1372 return 0;
1373 stop:
1374 return 1;
1375 exit:
1376 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1377 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378}
1379
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001380static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001382{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001384 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 char *frame;
1386 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001387
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauletb067b062017-01-04 16:39:11 +01001389 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001390 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001392 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1396 " - Connection timed out\n",
1397 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1398 __FUNCTION__, appctx);
1399 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 goto exit;
1401 }
1402
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001403 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 ret = spoe_recv_frame(appctx, frame,
1405 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (ret > 1) {
1407 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1408 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1409 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001411 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001412 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 switch (ret) {
1416 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1419 goto next;
1420
1421 case 1: /* retry later */
1422 goto stop;
1423
1424 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 /* HELLO handshake is finished, set the idle timeout and
1426 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001427 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001429 SPOE_APPCTX(appctx)->node.key = 0;
1430 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001431
1432 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001433 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001439 if (trash.data)
1440 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001441
1442 SPOE_APPCTX(appctx)->task->expire =
1443 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 return 0;
1445 stop:
1446 return 1;
1447 exit:
1448 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1449 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001450}
1451
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001452
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001454spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001455{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001456 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1457 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 char *frame, *buf;
1459 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001460
Christopher Faulet8ef75252017-02-20 22:56:03 +01001461 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1462 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001463 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001464
1465 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1466 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1467 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1468 SPOE_APPCTX(appctx)->max_frame_size);
1469 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001470 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471 *skip = 1;
1472 ret = 1;
1473 goto end;
1474 }
1475 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001476 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1478 SPOE_APPCTX(appctx)->max_frame_size);
1479
1480 }
1481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 if (ret > 1)
1483 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001484
Christopher Faulet8ef75252017-02-20 22:56:03 +01001485 switch (ret) {
1486 case -1: /* error */
1487 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1488 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001489
Christopher Faulet8ef75252017-02-20 22:56:03 +01001490 case 0: /* ignore */
1491 if (ctx == NULL)
1492 goto abort_frag_frame;
1493
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001494 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001495 LIST_DEL(&ctx->list);
1496 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001497 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001498 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001499 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 ctx->state = SPOE_CTX_ST_ERROR;
1501 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1502 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001503 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001504 break;
1505
1506 case 1: /* retry */
1507 *skip = 1;
1508 break;
1509
1510 default:
1511 if (ctx == NULL)
1512 goto abort_frag_frame;
1513
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001514 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 LIST_DEL(&ctx->list);
1516 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001517 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001518 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001519 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1521 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1522 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001523 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001524 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 }
1526 goto end;
1527
1528 frag_frame_sent:
1529 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001530 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1532 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1533 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1535 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1536 goto end;
1537
1538 no_frag_frame_sent:
1539 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1540 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001541 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 }
1543 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1544 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1545 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1546 }
1547 else {
1548 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001549 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1551 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001552 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001553 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001554 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1555 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1556 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001557 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558
Christopher Faulet8ef75252017-02-20 22:56:03 +01001559 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1560 goto end;
1561
1562 abort_frag_frame:
1563 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1564 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1565 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1566 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1567 goto end;
1568
1569 end:
1570 return ret;
1571}
1572
1573static int
1574spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1575{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001576 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001577 struct spoe_context *ctx = NULL;
1578 char *frame;
1579 int ret;
1580
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001581 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001582 ret = spoe_recv_frame(appctx, frame,
1583 SPOE_APPCTX(appctx)->max_frame_size);
1584 if (ret > 1) {
1585 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1586 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001587 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 goto end;
1589 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001590 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1592 }
1593 switch (ret) {
1594 case -1: /* error */
1595 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1596 break;
1597
1598 case 0: /* ignore */
1599 break;
1600
1601 case 1: /* retry */
1602 *skip = 1;
1603 break;
1604
1605 default:
1606 LIST_DEL(&ctx->list);
1607 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001608 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001609 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1610 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001611 if (ctx->spoe_appctx) {
1612 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001613 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001614 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001615 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1616 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1617 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1618 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1619 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1620 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1621 }
1622 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1623 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1625 break;
1626 }
1627
1628 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001629 if (trash.data)
1630 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001631 end:
1632 return ret;
1633}
1634
1635static int
1636spoe_handle_processing_appctx(struct appctx *appctx)
1637{
1638 struct stream_interface *si = appctx->owner;
1639 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001640 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001641
1642 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1643 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1644 goto exit;
1645 }
1646
1647 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1649 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1650 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1651 goto next;
1652 }
1653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001654 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001655 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001656 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001657 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1658 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001659 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001660
Christopher Faulet8f82b202018-01-24 16:23:03 +01001661 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1662 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001663
Christopher Faulet8f82b202018-01-24 16:23:03 +01001664 /* receiving_frame loop */
1665 while (!skip_receiving) {
1666 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1667 switch (ret) {
1668 case -1: /* error */
1669 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 case 0: /* ignore */
1672 active_r = 1;
1673 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 case 1: /* retry */
1676 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001677
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 default:
1679 active_r = 1;
1680 break;
1681 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001682 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 /* send_frame loop */
1685 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1686 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1687 switch (ret) {
1688 case -1: /* error */
1689 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001692 if (SPOE_APPCTX(appctx)->node.key)
1693 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 active_s++;
1695 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 case 1: /* retry */
1698 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001699
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001701 if (SPOE_APPCTX(appctx)->node.key)
1702 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001703 active_s++;
1704 break;
1705 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1710 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1711 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001712
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001714 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001716 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 }
1718 return 1;
1719
Christopher Faulet8f82b202018-01-24 16:23:03 +01001720 next:
1721 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1722 return 0;
1723
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724 exit:
1725 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1726 return 0;
1727}
1728
1729static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001730spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731{
1732 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001733 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001734 char *frame, *buf;
1735 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001736
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1738 goto exit;
1739
1740 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1741 goto exit;
1742
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1744 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001745 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001746 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1747 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750
1751 switch (ret) {
1752 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001753 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001754 goto exit;
1755
1756 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757 goto stop;
1758
1759 default:
1760 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1761 " - disconnected by HAProxy (%d): %s\n",
1762 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 __FUNCTION__, appctx,
1764 SPOE_APPCTX(appctx)->status_code,
1765 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766
1767 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1768 goto next;
1769 }
1770
1771 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 SPOE_APPCTX(appctx)->task->expire =
1773 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 return 0;
1775 stop:
1776 return 1;
1777 exit:
1778 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1779 return 0;
1780}
1781
1782static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784{
1785 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 char *frame;
1787 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788
Christopher Fauletb067b062017-01-04 16:39:11 +01001789 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001792 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793
Christopher Fauletb067b062017-01-04 16:39:11 +01001794 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001797 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001799 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001800 ret = spoe_recv_frame(appctx, frame,
1801 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001803 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 }
1806
1807 switch (ret) {
1808 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1810 " - error on frame (%s)\n",
1811 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001812 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 goto exit;
1816
1817 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 goto next;
1819
1820 case 1: /* retry */
1821 goto stop;
1822
1823 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001827 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1829 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 goto exit;
1831 }
1832
1833 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001835 if (trash.data)
1836 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 return 0;
1839 stop:
1840 return 1;
1841 exit:
1842 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1843 return 0;
1844}
1845
1846/* I/O Handler processing messages exchanged with the agent */
1847static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 struct stream_interface *si = appctx->owner;
1851 struct spoe_agent *agent;
1852
1853 if (SPOE_APPCTX(appctx) == NULL)
1854 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1857 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001858
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 switchstate:
1860 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1861 " - appctx-state=%s\n",
1862 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1863 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1864
1865 switch (appctx->st0) {
1866 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto out;
1869 goto switchstate;
1870
1871 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 goto out;
1874 goto switchstate;
1875
1876 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001877 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001878 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001879 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001880 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001881 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882 SPOE_APPCTX(appctx)->task->expire =
1883 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001885 goto switchstate;
1886 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001887 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1888 /* fall through */
1889
1890 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001891 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1892 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 goto out;
1895 goto switchstate;
1896
1897 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001898 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001899 goto out;
1900 goto switchstate;
1901
1902 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 goto out;
1905 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001906
1907 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 appctx->st0 = SPOE_APPCTX_ST_END;
1909 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1910
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001911 si_shutw(si);
1912 si_shutr(si);
1913 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914 /* fall through */
1915
1916 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001917 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001918 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001919 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001920 if (stopping)
1921 spoe_wakeup_appctx(appctx);
1922
Christopher Faulet42bfa462017-01-04 14:14:19 +01001923 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1924 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925}
1926
1927struct applet spoe_applet = {
1928 .obj_type = OBJ_TYPE_APPLET,
1929 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 .fct = spoe_handle_appctx,
1931 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932};
1933
1934/* Create a SPOE applet. On success, the created applet is returned, else
1935 * NULL. */
1936static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001937spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938{
1939 struct appctx *appctx;
1940 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942
Emeric Brun1138fd02017-06-19 12:38:55 +02001943 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 goto out_error;
1945
Willy Tarreaubafbe012017-11-24 17:34:44 +01001946 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001947 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001949 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001950
Christopher Faulet42bfa462017-01-04 14:14:19 +01001951 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001952 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001953 goto out_free_spoe_appctx;
1954
1955 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001956 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001957 SPOE_APPCTX(appctx)->task->context = appctx;
1958 SPOE_APPCTX(appctx)->agent = conf->agent;
1959 SPOE_APPCTX(appctx)->version = 0;
1960 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1961 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001963 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001964 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001965
Willy Tarreau21046592020-02-26 10:39:36 +01001966 MT_LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001967 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001968 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969
1970 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1971 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001972
Willy Tarreau5820a362016-12-22 15:59:02 +01001973 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001974 if (!sess)
1975 goto out_free_spoe;
1976
Willy Tarreau87787ac2017-08-28 16:22:54 +02001977 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001978 goto out_free_sess;
1979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001980 stream_set_backend(strm, conf->agent->b.be);
1981
1982 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001983 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984 appctx_wakeup(appctx);
1985
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001986 strm->do_log = NULL;
1987 strm->res.flags |= CF_READ_DONTWAIT;
1988
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001989 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001990 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001991 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001992 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001993
Emeric Brunc60def82017-09-27 14:59:38 +02001994 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001995 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 return appctx;
1997
1998 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 out_free_sess:
2000 session_free(sess);
2001 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002002 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002003 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002004 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 out_free_appctx:
2006 appctx_free(appctx);
2007 out_error:
2008 return NULL;
2009}
2010
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002012spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013{
2014 struct spoe_config *conf = FLT_CONF(ctx->filter);
2015 struct spoe_agent *agent = conf->agent;
2016 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002017 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018
Christopher Fauleta1cda022016-12-21 08:58:06 +01002019 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002020 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002021 (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002022 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023
2024 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2027 ctx->strm);
2028
Christopher Fauleta1cda022016-12-21 08:58:06 +01002029 /* Do not try to create a new applet if there is no server up for the
2030 * agent's backend. */
2031 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2032 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2033 " - cannot create SPOE appctx: no server up\n",
2034 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2035 __FUNCTION__, ctx->strm);
2036 goto end;
2037 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 /* Do not try to create a new applet if we have reached the maximum of
2040 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002041 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002042 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002043 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2044 " - cannot create SPOE appctx: max CPS reached\n",
2045 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2046 __FUNCTION__, ctx->strm);
2047 goto end;
2048 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 }
2050
Christopher Faulet8ef75252017-02-20 22:56:03 +01002051 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 if (appctx == NULL) {
2053 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2054 " - failed to create SPOE appctx\n",
2055 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2056 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002057 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002058 "SPOE: [%s] failed to create SPOE applet\n",
2059 agent->id);
2060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002062 }
2063
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 /* Increase the per-process number of cumulated connections */
2065 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002066 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002067
Christopher Fauleta1cda022016-12-21 08:58:06 +01002068 end:
2069 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002070 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002071 ctx->status_code = SPOE_CTX_ERR_RES;
2072 return -1;
2073 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002075 /* Add the SPOE context in the sending queue if the stream has no applet
2076 * already assigned and wakeup all idle applets. Otherwise, don't queue
2077 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002078 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002079 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2080 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002081 if (ctx->spoe_appctx)
2082 return 1;
2083 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084
2085 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002086 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002087 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002088 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002089 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002090 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002091
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002092 /* Finally try to wakeup an IDLE applet. */
2093 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2094 struct eb32_node *node;
2095
2096 node = eb32_first(&agent->rt[tid].idle_applets);
2097 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2098 if (node && spoe_appctx) {
2099 eb32_delete(&spoe_appctx->node);
2100 spoe_appctx->node.key++;
2101 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2102 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002103 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002104 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002105 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106}
2107
2108/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002109 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002110 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002111/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2112 * fragmented_content. If the next message can be processed, it returns 0. If
2113 * the message is too big, it returns -1.*/
2114static int
2115spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2116 struct spoe_message *msg, int dir,
2117 char **buf, char *end)
2118{
2119 struct sample *smp;
2120 struct spoe_arg *arg;
2121 int ret;
2122
2123 if (msg->cond) {
2124 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2125 ret = acl_pass(ret);
2126 if (msg->cond->pol == ACL_COND_UNLESS)
2127 ret = !ret;
2128
2129 /* the rule does not match */
2130 if (!ret)
2131 goto next;
2132 }
2133
2134 /* Resume encoding of a SPOE argument */
2135 if (ctx->frag_ctx.curarg != NULL) {
2136 arg = ctx->frag_ctx.curarg;
2137 goto encode_argument;
2138 }
2139
2140 if (ctx->frag_ctx.curoff != UINT_MAX)
2141 goto encode_msg_payload;
2142
2143 /* Check if there is enough space for the message name and the
2144 * number of arguments. It implies <msg->id_len> is encoded on 2
2145 * bytes, at most (< 2288). */
2146 if (*buf + 2 + msg->id_len + 1 > end)
2147 goto too_big;
2148
2149 /* Encode the message name */
2150 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2151 goto too_big;
2152
2153 /* Set the number of arguments for this message */
2154 **buf = msg->nargs;
2155 (*buf)++;
2156
2157 ctx->frag_ctx.curoff = 0;
2158 encode_msg_payload:
2159
2160 /* Loop on arguments */
2161 list_for_each_entry(arg, &msg->args, list) {
2162 ctx->frag_ctx.curarg = arg;
2163 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002164 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002165
2166 encode_argument:
2167 if (ctx->frag_ctx.curoff != UINT_MAX)
2168 goto encode_arg_value;
2169
Joseph Herlantf7f60312018-11-15 13:46:49 -08002170 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002171 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2172 goto too_big;
2173
2174 ctx->frag_ctx.curoff = 0;
2175 encode_arg_value:
2176
Joseph Herlantf7f60312018-11-15 13:46:49 -08002177 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002178 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002179 if (smp) {
2180 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2181 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2182 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002183 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002184 if (ret == -1 || ctx->frag_ctx.curoff)
2185 goto too_big;
2186 }
2187
2188 next:
2189 return 0;
2190
2191 too_big:
2192 return -1;
2193}
2194
Christopher Fauletc718b822017-09-21 16:50:56 +02002195/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2196 * handle fragmented content. On success it returns 1. If an error occurred, -1
2197 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2198 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002199static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002200spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002201 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002202{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002203 struct spoe_config *conf = FLT_CONF(ctx->filter);
2204 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002205 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002206 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002207
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002208 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002209 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002210
Christopher Fauletc718b822017-09-21 16:50:56 +02002211 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2212 /* Resume encoding of a SPOE message */
2213 if (ctx->frag_ctx.curmsg != NULL) {
2214 msg = ctx->frag_ctx.curmsg;
2215 goto encode_evt_message;
2216 }
2217
2218 list_for_each_entry(msg, messages, by_evt) {
2219 ctx->frag_ctx.curmsg = msg;
2220 ctx->frag_ctx.curarg = NULL;
2221 ctx->frag_ctx.curoff = UINT_MAX;
2222
2223 encode_evt_message:
2224 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2225 goto too_big;
2226 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002227 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002228 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2229 /* Resume encoding of a SPOE message */
2230 if (ctx->frag_ctx.curmsg != NULL) {
2231 msg = ctx->frag_ctx.curmsg;
2232 goto encode_grp_message;
2233 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234
Christopher Fauletc718b822017-09-21 16:50:56 +02002235 list_for_each_entry(msg, messages, by_grp) {
2236 ctx->frag_ctx.curmsg = msg;
2237 ctx->frag_ctx.curarg = NULL;
2238 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002239
Christopher Fauletc718b822017-09-21 16:50:56 +02002240 encode_grp_message:
2241 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2242 goto too_big;
2243 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002244 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002245 else
2246 goto skip;
2247
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002248
Christopher Faulet57583e42017-09-04 15:41:09 +02002249 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002250 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002251 goto skip;
2252
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002253 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002254 " - encode %s messages - spoe_appctx=%p"
2255 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256 (int)now.tv_sec, (int)now.tv_usec,
2257 agent->id, __FUNCTION__, s,
2258 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002259 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002260 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002262 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263 ctx->frag_ctx.curmsg = NULL;
2264 ctx->frag_ctx.curarg = NULL;
2265 ctx->frag_ctx.curoff = 0;
2266 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002268 return 1;
2269
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002271 /* Return an error if fragmentation is unsupported or if nothing has
2272 * been encoded because its too big and not splittable. */
2273 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002274 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2275 return -1;
2276 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002277
2278 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002279 " - encode fragmented messages - spoe_appctx=%p"
2280 " - curmsg=%p - curarg=%p - curoff=%u"
2281 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002283 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002285 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002287 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2289 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2290 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002291
2292 skip:
2293 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2294 " - skip the frame because nothing has been encoded\n",
2295 (int)now.tv_sec, (int)now.tv_usec,
2296 agent->id, __FUNCTION__, s);
2297 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298}
2299
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002300
2301/***************************************************************************
2302 * Functions that handle SPOE actions
2303 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002304/* Helper function to set a variable */
2305static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002306spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002307 struct sample *smp)
2308{
2309 struct spoe_config *conf = FLT_CONF(ctx->filter);
2310 struct spoe_agent *agent = conf->agent;
2311 char varname[64];
2312
2313 memset(varname, 0, sizeof(varname));
2314 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2315 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002316 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2317 vars_set_by_name(varname, len, smp);
2318 else
2319 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320}
2321
2322/* Helper function to unset a variable */
2323static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002325 struct sample *smp)
2326{
2327 struct spoe_config *conf = FLT_CONF(ctx->filter);
2328 struct spoe_agent *agent = conf->agent;
2329 char varname[64];
2330
2331 memset(varname, 0, sizeof(varname));
2332 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2333 scope, agent->var_pfx, len, name);
2334 vars_unset_by_name_ifexist(varname, len, smp);
2335}
2336
2337
Christopher Faulet8ef75252017-02-20 22:56:03 +01002338static inline int
2339spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2340 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002341{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342 char *str, *scope, *p = *buf;
2343 struct sample smp;
2344 uint64_t sz;
2345 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347 if (p + 2 >= end)
2348 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002349
Christopher Faulet8ef75252017-02-20 22:56:03 +01002350 /* SET-VAR requires 3 arguments */
2351 if (*p++ != 3)
2352 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 switch (*p++) {
2355 case SPOE_SCOPE_PROC: scope = "proc"; break;
2356 case SPOE_SCOPE_SESS: scope = "sess"; break;
2357 case SPOE_SCOPE_TXN : scope = "txn"; break;
2358 case SPOE_SCOPE_REQ : scope = "req"; break;
2359 case SPOE_SCOPE_RES : scope = "res"; break;
2360 default: goto skip;
2361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002362
Christopher Faulet8ef75252017-02-20 22:56:03 +01002363 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2364 goto skip;
2365 memset(&smp, 0, sizeof(smp));
2366 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002367
Christopher Faulet8ef75252017-02-20 22:56:03 +01002368 if (spoe_decode_data(&p, end, &smp) == -1)
2369 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002370
Christopher Faulet8ef75252017-02-20 22:56:03 +01002371 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2372 " - set-var '%s.%s.%.*s'\n",
2373 (int)now.tv_sec, (int)now.tv_usec,
2374 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2375 __FUNCTION__, s, scope,
2376 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2377 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378
Christopher Faulet2469eba2020-10-15 16:08:30 +02002379 if (smp.data.type == SMP_T_ANY)
2380 spoe_unset_var(ctx, scope, str, sz, &smp);
2381 else
2382 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002383
Christopher Faulet8ef75252017-02-20 22:56:03 +01002384 ret = (p - *buf);
2385 *buf = p;
2386 return ret;
2387 skip:
2388 return 0;
2389}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002390
Christopher Faulet8ef75252017-02-20 22:56:03 +01002391static inline int
2392spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2393 char **buf, char *end, int dir)
2394{
2395 char *str, *scope, *p = *buf;
2396 struct sample smp;
2397 uint64_t sz;
2398 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 if (p + 2 >= end)
2401 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 /* UNSET-VAR requires 2 arguments */
2404 if (*p++ != 2)
2405 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002406
Christopher Faulet8ef75252017-02-20 22:56:03 +01002407 switch (*p++) {
2408 case SPOE_SCOPE_PROC: scope = "proc"; break;
2409 case SPOE_SCOPE_SESS: scope = "sess"; break;
2410 case SPOE_SCOPE_TXN : scope = "txn"; break;
2411 case SPOE_SCOPE_REQ : scope = "req"; break;
2412 case SPOE_SCOPE_RES : scope = "res"; break;
2413 default: goto skip;
2414 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002415
Christopher Faulet8ef75252017-02-20 22:56:03 +01002416 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2417 goto skip;
2418 memset(&smp, 0, sizeof(smp));
2419 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002420
Christopher Faulet8ef75252017-02-20 22:56:03 +01002421 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2422 " - unset-var '%s.%s.%.*s'\n",
2423 (int)now.tv_sec, (int)now.tv_usec,
2424 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2425 __FUNCTION__, s, scope,
2426 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2427 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet8ef75252017-02-20 22:56:03 +01002429 spoe_unset_var(ctx, scope, str, sz, &smp);
2430
2431 ret = (p - *buf);
2432 *buf = p;
2433 return ret;
2434 skip:
2435 return 0;
2436}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438/* Process SPOE actions for a specific event. It returns 1 on success. If an
2439 * error occurred, 0 is returned. */
2440static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002441spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002442{
2443 char *p, *end;
2444 int ret;
2445
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 p = b_head(&ctx->buffer);
2447 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002448
2449 while (p < end) {
2450 enum spoe_action_type type;
2451
2452 type = *p++;
2453 switch (type) {
2454 case SPOE_ACT_T_SET_VAR:
2455 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2456 if (!ret)
2457 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002458 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460 case SPOE_ACT_T_UNSET_VAR:
2461 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2462 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002463 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464 break;
2465
2466 default:
2467 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002468 }
2469 }
2470
2471 return 1;
2472 skip:
2473 return 0;
2474}
2475
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002476/***************************************************************************
2477 * Functions that process SPOE events
2478 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002479static void
2480spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2481 struct spoe_context *ctx, int dir)
2482{
2483 if (!tv_iszero(&ctx->stats.tv_start)) {
2484 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2485 ctx->stats.t_total += ctx->stats.t_process;
2486 tv_zero(&ctx->stats.tv_request);
2487 tv_zero(&ctx->stats.tv_queue);
2488 tv_zero(&ctx->stats.tv_wait);
2489 tv_zero(&ctx->stats.tv_response);
2490 }
2491
2492 if (agent->var_t_process) {
2493 struct sample smp;
2494
2495 memset(&smp, 0, sizeof(smp));
2496 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2497 smp.data.u.sint = ctx->stats.t_process;
2498 smp.data.type = SMP_T_SINT;
2499
2500 spoe_set_var(ctx, "txn", agent->var_t_process,
2501 strlen(agent->var_t_process), &smp);
2502 }
2503
2504 if (agent->var_t_total) {
2505 struct sample smp;
2506
2507 memset(&smp, 0, sizeof(smp));
2508 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2509 smp.data.u.sint = ctx->stats.t_total;
2510 smp.data.type = SMP_T_SINT;
2511
2512 spoe_set_var(ctx, "txn", agent->var_t_total,
2513 strlen(agent->var_t_total), &smp);
2514 }
2515}
2516
2517static void
2518spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2519 struct spoe_context *ctx, int dir)
2520{
2521 if (agent->eps_max > 0)
2522 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2523
2524 if (agent->var_on_error) {
2525 struct sample smp;
2526
2527 memset(&smp, 0, sizeof(smp));
2528 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2529 smp.data.u.sint = ctx->status_code;
2530 smp.data.type = SMP_T_BOOL;
2531
2532 spoe_set_var(ctx, "txn", agent->var_on_error,
2533 strlen(agent->var_on_error), &smp);
2534 }
2535
2536 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2537 ? SPOE_CTX_ST_READY
2538 : SPOE_CTX_ST_NONE);
2539}
2540
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002541static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002542spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002544 /* If a process is already started for this SPOE context, retry
2545 * later. */
2546 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002547 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002549 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002550 ctx->stats.tv_start = now;
2551 ctx->stats.tv_request = now;
2552 ctx->stats.t_request = -1;
2553 ctx->stats.t_queue = -1;
2554 ctx->stats.t_waiting = -1;
2555 ctx->stats.t_response = -1;
2556 ctx->stats.t_process = -1;
2557
2558 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002559
Christopher Fauleta1cda022016-12-21 08:58:06 +01002560 /* Set the right flag to prevent request and response processing
2561 * in same time. */
2562 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2563 ? SPOE_CTX_FL_REQ_PROCESS
2564 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566}
2567
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002568static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002569spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002571 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002572
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002573 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2574 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002575 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002576 if (sa) {
2577 if (sa->frag_ctx.ctx == ctx) {
2578 sa->frag_ctx.ctx = NULL;
2579 spoe_wakeup_appctx(sa->owner);
2580 }
2581 else
2582 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002583 }
2584
Christopher Fauleta1cda022016-12-21 08:58:06 +01002585 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002586 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002587 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588
2589 /* Reset processing timer */
2590 ctx->process_exp = TICK_ETERNITY;
2591
Christopher Faulet8ef75252017-02-20 22:56:03 +01002592 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593
Christopher Fauletfce747b2018-01-24 15:59:32 +01002594 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002595 ctx->frag_ctx.curmsg = NULL;
2596 ctx->frag_ctx.curarg = NULL;
2597 ctx->frag_ctx.curoff = 0;
2598 ctx->frag_ctx.flags = 0;
2599
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002601 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002602 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002603 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002604 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002605
Christopher Fauleta1cda022016-12-21 08:58:06 +01002606 LIST_DEL(&ctx->list);
2607 LIST_INIT(&ctx->list);
2608 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002609}
2610
Christopher Faulet58d03682017-09-21 16:57:24 +02002611/* Process a list of SPOE messages. First, this functions will process messages
2612 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2613 * to process corresponding actions. During all the processing, it returns 0
2614 * and it returns 1 when the processing is finished. If an error occurred, -1
2615 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002617spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2618 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002619{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002620 struct spoe_config *conf = FLT_CONF(ctx->filter);
2621 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002622 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002623
2624 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002625 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002626
2627 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2628 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002629 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002630 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002631 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002632 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002633 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002634 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002635
2636 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002637 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002638 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002639 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002640 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002642 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643 goto skip;
2644 }
2645 }
2646
Christopher Fauletf7a30922016-11-10 15:04:51 +01002647 if (!tick_isset(ctx->process_exp)) {
2648 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2649 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2650 ctx->process_exp);
2651 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002652 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002653 if (!ret)
2654 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002655
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002656 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002657 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002658 }
2659
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002660 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002661 if (tv_iszero(&ctx->stats.tv_request))
2662 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002663 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002664 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002665 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002666 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002667 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002668 if (!ret)
2669 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002670 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002671 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002672 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2673 }
2674
2675 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002676 if (ctx->spoe_appctx)
2677 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002678 ret = 0;
2679 goto out;
2680 }
2681
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002682 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2683 ret = 0;
2684 goto out;
2685 }
2686
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002687 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002688 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002689 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002690 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002692 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002693 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002694 }
2695
2696 out:
2697 return ret;
2698
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002700 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002701 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002702 spoe_stop_processing(agent, ctx);
2703 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002704
Christopher Fauleta1cda022016-12-21 08:58:06 +01002705 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002706 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002707 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002708 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002709 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002710 spoe_handle_processing_error(s, agent, ctx, dir);
2711 ret = 1;
2712 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002713 return ret;
2714}
2715
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002716/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2717 * See spoe_process_message for details. */
2718static int
2719spoe_process_group(struct stream *s, struct spoe_context *ctx,
2720 struct spoe_group *group, int dir)
2721{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002722 struct spoe_config *conf = FLT_CONF(ctx->filter);
2723 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002724 int ret;
2725
2726 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2727 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002728 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002729 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2730 group->id);
2731
2732 if (LIST_ISEMPTY(&group->messages))
2733 return 1;
2734
2735 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002736 if (ret && ctx->stats.t_process != -1) {
2737 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002738 " - <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 +01002739 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002740 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002741 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002742 ctx->stats.t_response, ctx->stats.t_process,
2743 agent->counters.idles, agent->counters.applets,
2744 agent->counters.nb_sending, agent->counters.nb_waiting,
2745 agent->counters.nb_errors, agent->counters.nb_processed,
2746 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2747 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2748 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2749 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2750 agent->id, group->id, s->uniq_id, ctx->status_code,
2751 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2752 ctx->stats.t_response, ctx->stats.t_process,
2753 agent->counters.idles, agent->counters.applets,
2754 agent->counters.nb_sending, agent->counters.nb_waiting,
2755 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002756 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002757 return ret;
2758}
2759
Christopher Faulet58d03682017-09-21 16:57:24 +02002760/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2761 * See spoe_process_message for details. */
2762static int
2763spoe_process_event(struct stream *s, struct spoe_context *ctx,
2764 enum spoe_event ev)
2765{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002766 struct spoe_config *conf = FLT_CONF(ctx->filter);
2767 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002768 int dir, ret;
2769
2770 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002771 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002772 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002773 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2774 spoe_event_str[ev]);
2775
2776 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2777
2778 if (LIST_ISEMPTY(&(ctx->events[ev])))
2779 return 1;
2780
2781 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002782 if (ret && ctx->stats.t_process != -1) {
2783 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002784 " - <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 +01002785 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2786 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2787 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002788 ctx->stats.t_response, ctx->stats.t_process,
2789 agent->counters.idles, agent->counters.applets,
2790 agent->counters.nb_sending, agent->counters.nb_waiting,
2791 agent->counters.nb_errors, agent->counters.nb_processed,
2792 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2793 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2794 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2795 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2796 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2797 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2798 ctx->stats.t_response, ctx->stats.t_process,
2799 agent->counters.idles, agent->counters.applets,
2800 agent->counters.nb_sending, agent->counters.nb_waiting,
2801 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002802 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002803 return ret;
2804}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002805
2806/***************************************************************************
2807 * Functions that create/destroy SPOE contexts
2808 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002809static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002810spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002811{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002812 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002813 return 1;
2814
Willy Tarreau21046592020-02-26 10:39:36 +01002815 if (MT_LIST_ADDED(&buffer_wait->list))
2816 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002817
Christopher Faulet4596fb72017-01-11 14:05:19 +01002818 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819 return 1;
2820
Willy Tarreau86891272020-07-10 08:22:26 +02002821 MT_LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002822 return 0;
2823}
2824
2825static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827{
Willy Tarreau21046592020-02-26 10:39:36 +01002828 if (MT_LIST_ADDED(&buffer_wait->list))
2829 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002830
2831 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002833 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002834 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002835 }
2836}
2837
Christopher Faulet4596fb72017-01-11 14:05:19 +01002838static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002839spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002840{
2841 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2842 return 1;
2843}
2844
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002845static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002846spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002847{
2848 struct spoe_config *conf = FLT_CONF(filter);
2849 struct spoe_context *ctx;
2850
Willy Tarreaubafbe012017-11-24 17:34:44 +01002851 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002852 if (ctx == NULL) {
2853 return NULL;
2854 }
2855 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002856 ctx->filter = filter;
2857 ctx->state = SPOE_CTX_ST_NONE;
2858 ctx->status_code = SPOE_CTX_ERR_NONE;
2859 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002860 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002861 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002862 ctx->buffer = BUF_NULL;
Willy Tarreau21046592020-02-26 10:39:36 +01002863 MT_LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002864 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002865 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002866 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002867
Christopher Fauletf7a30922016-11-10 15:04:51 +01002868 ctx->stream_id = 0;
2869 ctx->frame_id = 1;
2870 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002871
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002872 tv_zero(&ctx->stats.tv_start);
2873 tv_zero(&ctx->stats.tv_request);
2874 tv_zero(&ctx->stats.tv_queue);
2875 tv_zero(&ctx->stats.tv_wait);
2876 tv_zero(&ctx->stats.tv_response);
2877 ctx->stats.t_request = -1;
2878 ctx->stats.t_queue = -1;
2879 ctx->stats.t_waiting = -1;
2880 ctx->stats.t_response = -1;
2881 ctx->stats.t_process = -1;
2882 ctx->stats.t_total = 0;
2883
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002884 ctx->strm = s;
2885 ctx->state = SPOE_CTX_ST_READY;
2886 filter->ctx = ctx;
2887
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002888 return ctx;
2889}
2890
2891static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002892spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002893{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002894 struct spoe_config *conf = FLT_CONF(filter);
2895 struct spoe_context *ctx = filter->ctx;
2896
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897 if (!ctx)
2898 return;
2899
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002900 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002901 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002902 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002903}
2904
2905static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002906spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002907{
2908 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002909 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002910
2911 tv_zero(&ctx->stats.tv_start);
2912 tv_zero(&ctx->stats.tv_request);
2913 tv_zero(&ctx->stats.tv_queue);
2914 tv_zero(&ctx->stats.tv_wait);
2915 tv_zero(&ctx->stats.tv_response);
2916 ctx->stats.t_request = -1;
2917 ctx->stats.t_queue = -1;
2918 ctx->stats.t_waiting = -1;
2919 ctx->stats.t_response = -1;
2920 ctx->stats.t_process = -1;
2921 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002922}
2923
2924
2925/***************************************************************************
2926 * Hooks that manage the filter lifecycle (init/check/deinit)
2927 **************************************************************************/
2928/* Signal handler: Do a soft stop, wakeup SPOE applet */
2929static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002930spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931{
2932 struct proxy *p;
2933
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002934 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935 while (p) {
2936 struct flt_conf *fconf;
2937
2938 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002939 struct spoe_config *conf;
2940 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002941 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002942 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002943
Christopher Faulet3b386a32017-02-23 10:17:15 +01002944 if (fconf->id != spoe_filter_id)
2945 continue;
2946
2947 conf = fconf->conf;
2948 agent = conf->agent;
2949
Christopher Faulet24289f22017-09-25 14:48:02 +02002950 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002951 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002952 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2953 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002954 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002955 }
2956 }
2957 p = p->next;
2958 }
2959}
2960
2961
2962/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2963static int
2964spoe_init(struct proxy *px, struct flt_conf *fconf)
2965{
2966 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002968 /* conf->agent_fe was already initialized during the config
2969 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002970 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002971 conf->agent_fe.cap = PR_CAP_FE;
2972 conf->agent_fe.mode = PR_MODE_TCP;
2973 conf->agent_fe.maxconn = 0;
2974 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2975 conf->agent_fe.conn_retries = CONN_RETRIES;
2976 conf->agent_fe.accept = frontend_accept;
2977 conf->agent_fe.srv = NULL;
2978 conf->agent_fe.timeout.client = TICK_ETERNITY;
2979 conf->agent_fe.default_target = &spoe_applet.obj_type;
2980 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2981
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002983 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002984 sighandler_registered = 1;
2985 }
2986
Christopher Faulet00292352019-01-09 15:05:10 +01002987 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002988 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989}
2990
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002991/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992static void
2993spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2994{
2995 struct spoe_config *conf = fconf->conf;
2996
2997 if (conf) {
2998 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002999
Christopher Faulet8ef75252017-02-20 22:56:03 +01003000 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003001 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002 free(conf);
3003 }
3004 fconf->conf = NULL;
3005}
3006
3007/* Check configuration of a SPOE filter for a specified proxy.
3008 * Return 1 on error, else 0. */
3009static int
3010spoe_check(struct proxy *px, struct flt_conf *fconf)
3011{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003012 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013 struct spoe_config *conf = fconf->conf;
3014 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003015 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003016
Christopher Faulet7ee86672017-09-19 11:08:28 +02003017 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3018 * are uniq */
3019 list_for_each_entry(f, &px->filter_configs, list) {
3020 struct spoe_config *c = f->conf;
3021
3022 /* This is not an SPOE filter */
3023 if (f->id != spoe_filter_id)
3024 continue;
3025 /* This is the current SPOE filter */
3026 if (f == fconf)
3027 continue;
3028
3029 /* Check engine Id. It should be uniq */
3030 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003031 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3032 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003033 return 1;
3034 }
3035 }
3036
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003037 target = proxy_be_by_name(conf->agent->b.name);
3038 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003039 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3040 " declared at %s:%d.\n",
3041 px->id, conf->agent->b.name, conf->agent->id,
3042 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003043 return 1;
3044 }
3045 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003046 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3047 " at %s:%d does not support HTTP mode.\n",
3048 px->id, target->id, conf->agent->id,
3049 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003050 return 1;
3051 }
3052
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003053 if (px->bind_proc & ~target->bind_proc) {
3054 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3055 " at %s:%d does not cover all of its processes.\n",
3056 px->id, target->id, conf->agent->id,
3057 conf->agent->conf.file, conf->agent->conf.line);
3058 return 1;
3059 }
3060
Christopher Fauletfe261552019-03-18 13:57:42 +01003061 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003062 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3063 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3064 return 1;
3065 }
3066 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003067 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003068 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3069 conf->agent->rt[i].processing = 0;
3070 LIST_INIT(&conf->agent->rt[i].applets);
3071 LIST_INIT(&conf->agent->rt[i].sending_queue);
3072 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3073 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003074 }
3075
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 free(conf->agent->b.name);
3077 conf->agent->b.name = NULL;
3078 conf->agent->b.be = target;
3079 return 0;
3080}
3081
Kevin Zhud87b1a52019-09-17 15:05:45 +02003082/* Initializes the SPOE filter for a proxy for a specific thread.
3083 * Returns a negative value if an error occurs. */
3084static int
3085spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3086{
3087 struct spoe_config *conf = fconf->conf;
3088 struct spoe_agent *agent = conf->agent;
3089
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003090 agent->rt[tid].engine_id = generate_pseudo_uuid();
3091 if (agent->rt[tid].engine_id == NULL)
3092 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003093 return 0;
3094}
3095
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003096/**************************************************************************
3097 * Hooks attached to a stream
3098 *************************************************************************/
3099/* Called when a filter instance is created and attach to a stream. It creates
3100 * the context that will be used to process this stream. */
3101static int
3102spoe_start(struct stream *s, struct filter *filter)
3103{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003104 struct spoe_config *conf = FLT_CONF(filter);
3105 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003106 struct spoe_context *ctx;
3107
3108 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003109 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003110 __FUNCTION__, s);
3111
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003112 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003113 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3114 " - failed to create SPOE context\n",
3115 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003116 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003117 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003118 "SPOE: [%s] failed to create SPOE context\n",
3119 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003120 return 0;
3121 }
3122
Christopher Faulet11610f32017-09-21 10:23:10 +02003123 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3125
Christopher Faulet11610f32017-09-21 10:23:10 +02003126 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3128
Christopher Faulet11610f32017-09-21 10:23:10 +02003129 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003130 filter->pre_analyzers |= AN_RES_INSPECT;
3131
Christopher Faulet11610f32017-09-21 10:23:10 +02003132 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003133 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3134
Christopher Faulet11610f32017-09-21 10:23:10 +02003135 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003136 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3137
Christopher Faulet11610f32017-09-21 10:23:10 +02003138 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3140
3141 return 1;
3142}
3143
3144/* Called when a filter instance is detached from a stream. It release the
3145 * attached SPOE context. */
3146static void
3147spoe_stop(struct stream *s, struct filter *filter)
3148{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003149 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3150 (int)now.tv_sec, (int)now.tv_usec,
3151 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3152 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003153 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003154}
3155
Christopher Fauletf7a30922016-11-10 15:04:51 +01003156
3157/*
3158 * Called when the stream is woken up because of expired timer.
3159 */
3160static void
3161spoe_check_timeouts(struct stream *s, struct filter *filter)
3162{
3163 struct spoe_context *ctx = filter->ctx;
3164
Christopher Fauletac580602018-03-20 16:09:20 +01003165 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003166 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003167}
3168
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169/* Called when we are ready to filter data on a channel */
3170static int
3171spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3172{
3173 struct spoe_context *ctx = filter->ctx;
3174 int ret = 1;
3175
3176 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3177 " - ctx-flags=0x%08x\n",
3178 (int)now.tv_sec, (int)now.tv_usec,
3179 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3180 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3181
Christopher Fauletb067b062017-01-04 16:39:11 +01003182 if (ctx->state == SPOE_CTX_ST_NONE)
3183 goto out;
3184
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 if (!(chn->flags & CF_ISRESP)) {
3186 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3187 chn->analysers |= AN_REQ_INSPECT_FE;
3188 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3189 chn->analysers |= AN_REQ_INSPECT_BE;
3190
3191 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3192 goto out;
3193
3194 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003195 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003196 if (!ret)
3197 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003198 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3199 }
3200 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003201 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003202 chn->analysers |= AN_RES_INSPECT;
3203
3204 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3205 goto out;
3206
Christopher Faulet8ef75252017-02-20 22:56:03 +01003207 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003208 if (!ret) {
3209 channel_dont_read(chn);
3210 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003211 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003212 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003213 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 }
3215
3216 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003217 return ret;
3218}
3219
3220/* Called before a processing happens on a given channel */
3221static int
3222spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3223 struct channel *chn, unsigned an_bit)
3224{
3225 struct spoe_context *ctx = filter->ctx;
3226 int ret = 1;
3227
3228 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3229 " - ctx-flags=0x%08x - ana=0x%08x\n",
3230 (int)now.tv_sec, (int)now.tv_usec,
3231 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3232 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3233 ctx->flags, an_bit);
3234
Christopher Fauletb067b062017-01-04 16:39:11 +01003235 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003236 goto out;
3237
3238 switch (an_bit) {
3239 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003240 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003241 break;
3242 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003243 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003244 break;
3245 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003246 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247 break;
3248 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003249 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 break;
3251 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003252 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003253 break;
3254 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003255 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 break;
3257 }
3258
3259 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003260 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003261 channel_dont_read(chn);
3262 channel_dont_close(chn);
3263 }
3264 return ret;
3265}
3266
3267/* Called when the filtering on the channel ends. */
3268static int
3269spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3270{
3271 struct spoe_context *ctx = filter->ctx;
3272
3273 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3274 " - ctx-flags=0x%08x\n",
3275 (int)now.tv_sec, (int)now.tv_usec,
3276 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3277 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3278
3279 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003280 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003281 }
3282
3283 return 1;
3284}
3285
3286/********************************************************************
3287 * Functions that manage the filter initialization
3288 ********************************************************************/
3289struct flt_ops spoe_ops = {
3290 /* Manage SPOE filter, called for each filter declaration */
3291 .init = spoe_init,
3292 .deinit = spoe_deinit,
3293 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003294 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295
3296 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003297 .attach = spoe_start,
3298 .detach = spoe_stop,
3299 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003300
3301 /* Handle channels activity */
3302 .channel_start_analyze = spoe_start_analyze,
3303 .channel_pre_analyze = spoe_chn_pre_analyze,
3304 .channel_end_analyze = spoe_end_analyze,
3305};
3306
3307
3308static int
3309cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3310{
3311 const char *err;
3312 int i, err_code = 0;
3313
3314 if ((cfg_scope == NULL && curengine != NULL) ||
3315 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003316 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 goto out;
3318
3319 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3320 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003321 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3322 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003323 err_code |= ERR_ALERT | ERR_ABORT;
3324 goto out;
3325 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003326 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3327 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003328 goto out;
3329 }
3330
3331 err = invalid_char(args[1]);
3332 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003333 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3334 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003335 err_code |= ERR_ALERT | ERR_ABORT;
3336 goto out;
3337 }
3338
3339 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003340 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3341 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342 err_code |= ERR_ALERT | ERR_ABORT;
3343 goto out;
3344 }
3345 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003346 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003347 err_code |= ERR_ALERT | ERR_ABORT;
3348 goto out;
3349 }
3350
3351 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003352
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353 curagent->conf.file = strdup(file);
3354 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003355
3356 curagent->timeout.hello = TICK_ETERNITY;
3357 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003358 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003359
Christopher Fauleta1cda022016-12-21 08:58:06 +01003360 curagent->var_pfx = NULL;
3361 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003362 curagent->var_t_process = NULL;
3363 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003364 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003365 curagent->cps_max = 0;
3366 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003367 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003368 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003369
3370 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003371 LIST_INIT(&curagent->events[i]);
3372 LIST_INIT(&curagent->groups);
3373 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003374 }
3375 else if (!strcmp(args[0], "use-backend")) {
3376 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003377 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3378 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379 err_code |= ERR_ALERT | ERR_FATAL;
3380 goto out;
3381 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003382 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003383 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384 free(curagent->b.name);
3385 curagent->b.name = strdup(args[1]);
3386 }
3387 else if (!strcmp(args[0], "messages")) {
3388 int cur_arg = 1;
3389 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003390 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391
Christopher Faulet11610f32017-09-21 10:23:10 +02003392 list_for_each_entry(ph, &curmphs, list) {
3393 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003394 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3395 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003396 err_code |= ERR_ALERT | ERR_FATAL;
3397 goto out;
3398 }
3399 }
3400
Christopher Faulet11610f32017-09-21 10:23:10 +02003401 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003402 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003403 err_code |= ERR_ALERT | ERR_ABORT;
3404 goto out;
3405 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003406 ph->id = strdup(args[cur_arg]);
3407 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003408 cur_arg++;
3409 }
3410 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003411 else if (!strcmp(args[0], "groups")) {
3412 int cur_arg = 1;
3413 while (*args[cur_arg]) {
3414 struct spoe_placeholder *ph = NULL;
3415
3416 list_for_each_entry(ph, &curgphs, list) {
3417 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003418 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3419 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003420 err_code |= ERR_ALERT | ERR_FATAL;
3421 goto out;
3422 }
3423 }
3424
3425 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003426 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003427 err_code |= ERR_ALERT | ERR_ABORT;
3428 goto out;
3429 }
3430 ph->id = strdup(args[cur_arg]);
3431 LIST_ADDQ(&curgphs, &ph->list);
3432 cur_arg++;
3433 }
3434 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003435 else if (!strcmp(args[0], "timeout")) {
3436 unsigned int *tv = NULL;
3437 const char *res;
3438 unsigned timeout;
3439
3440 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3442 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 err_code |= ERR_ALERT | ERR_FATAL;
3444 goto out;
3445 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003446 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3447 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003448 if (!strcmp(args[1], "hello"))
3449 tv = &curagent->timeout.hello;
3450 else if (!strcmp(args[1], "idle"))
3451 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003452 else if (!strcmp(args[1], "processing"))
3453 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003455 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3456 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003457 err_code |= ERR_ALERT | ERR_FATAL;
3458 goto out;
3459 }
3460 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003461 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3462 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003463 err_code |= ERR_ALERT | ERR_FATAL;
3464 goto out;
3465 }
3466 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003467 if (res == PARSE_TIME_OVER) {
3468 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3469 file, linenum, args[2], args[0], args[1]);
3470 err_code |= ERR_ALERT | ERR_FATAL;
3471 goto out;
3472 }
3473 else if (res == PARSE_TIME_UNDER) {
3474 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3475 file, linenum, args[2], args[0], args[1]);
3476 err_code |= ERR_ALERT | ERR_FATAL;
3477 goto out;
3478 }
3479 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003480 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3481 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003482 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003483 goto out;
3484 }
3485 *tv = MS_TO_TICKS(timeout);
3486 }
3487 else if (!strcmp(args[0], "option")) {
3488 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003489 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3490 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003491 err_code |= ERR_ALERT | ERR_FATAL;
3492 goto out;
3493 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003494
Christopher Faulet305c6072017-02-23 16:17:53 +01003495 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003496 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003497 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003498 if (kwm == 1)
3499 curagent->flags &= ~SPOE_FL_PIPELINING;
3500 else
3501 curagent->flags |= SPOE_FL_PIPELINING;
3502 goto out;
3503 }
3504 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003505 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003506 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003507 if (kwm == 1)
3508 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003509 else
3510 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003511 goto out;
3512 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003513 else if (!strcmp(args[1], "send-frag-payload")) {
3514 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3515 goto out;
3516 if (kwm == 1)
3517 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3518 else
3519 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3520 goto out;
3521 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003522 else if (!strcmp(args[1], "dontlog-normal")) {
3523 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3524 goto out;
3525 if (kwm == 1)
3526 curpxopts2 &= ~PR_O2_NOLOGNORM;
3527 else
3528 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003529 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003530 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003531
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003532 /* Following options does not support negation */
3533 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003534 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3535 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003536 err_code |= ERR_ALERT | ERR_FATAL;
3537 goto out;
3538 }
3539
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003540 if (!strcmp(args[1], "var-prefix")) {
3541 char *tmp;
3542
3543 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003544 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3545 file, linenum, args[0],
3546 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003547 err_code |= ERR_ALERT | ERR_FATAL;
3548 goto out;
3549 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003550 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3551 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003552 tmp = args[2];
3553 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003554 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003555 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003556 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003557 err_code |= ERR_ALERT | ERR_FATAL;
3558 goto out;
3559 }
3560 tmp++;
3561 }
3562 curagent->var_pfx = strdup(args[2]);
3563 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003564 else if (!strcmp(args[1], "force-set-var")) {
3565 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3566 goto out;
3567 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3568 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003569 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003570 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003571 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003572 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3573 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003574 else if (!strcmp(args[1], "set-on-error")) {
3575 char *tmp;
3576
3577 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003578 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3579 file, linenum, args[0],
3580 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003581 err_code |= ERR_ALERT | ERR_FATAL;
3582 goto out;
3583 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003584 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3585 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003586 tmp = args[2];
3587 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003588 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003589 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003590 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003591 err_code |= ERR_ALERT | ERR_FATAL;
3592 goto out;
3593 }
3594 tmp++;
3595 }
3596 curagent->var_on_error = strdup(args[2]);
3597 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003598 else if (!strcmp(args[1], "set-process-time")) {
3599 char *tmp;
3600
3601 if (!*args[2]) {
3602 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3603 file, linenum, args[0],
3604 args[1]);
3605 err_code |= ERR_ALERT | ERR_FATAL;
3606 goto out;
3607 }
3608 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3609 goto out;
3610 tmp = args[2];
3611 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003612 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003613 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003614 file, linenum, args[0], args[1]);
3615 err_code |= ERR_ALERT | ERR_FATAL;
3616 goto out;
3617 }
3618 tmp++;
3619 }
3620 curagent->var_t_process = strdup(args[2]);
3621 }
3622 else if (!strcmp(args[1], "set-total-time")) {
3623 char *tmp;
3624
3625 if (!*args[2]) {
3626 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3627 file, linenum, args[0],
3628 args[1]);
3629 err_code |= ERR_ALERT | ERR_FATAL;
3630 goto out;
3631 }
3632 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3633 goto out;
3634 tmp = args[2];
3635 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003636 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003637 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003638 file, linenum, args[0], args[1]);
3639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
3642 tmp++;
3643 }
3644 curagent->var_t_total = strdup(args[2]);
3645 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003646 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003647 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3648 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003649 err_code |= ERR_ALERT | ERR_FATAL;
3650 goto out;
3651 }
Christopher Faulet48026722016-11-16 15:01:12 +01003652 }
3653 else if (!strcmp(args[0], "maxconnrate")) {
3654 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003655 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3656 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003657 err_code |= ERR_ALERT | ERR_FATAL;
3658 goto out;
3659 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003660 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003661 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003662 curagent->cps_max = atol(args[1]);
3663 }
3664 else if (!strcmp(args[0], "maxerrrate")) {
3665 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003666 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3667 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003668 err_code |= ERR_ALERT | ERR_FATAL;
3669 goto out;
3670 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003671 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003672 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003673 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003674 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003675 else if (!strcmp(args[0], "max-frame-size")) {
3676 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003677 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3678 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003679 err_code |= ERR_ALERT | ERR_FATAL;
3680 goto out;
3681 }
3682 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3683 goto out;
3684 curagent->max_frame_size = atol(args[1]);
3685 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3686 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003687 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3688 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003689 err_code |= ERR_ALERT | ERR_FATAL;
3690 goto out;
3691 }
3692 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003693 else if (!strcmp(args[0], "max-waiting-frames")) {
3694 if (!*args[1]) {
3695 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3696 file, linenum, args[0]);
3697 err_code |= ERR_ALERT | ERR_FATAL;
3698 goto out;
3699 }
3700 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3701 goto out;
3702 curagent->max_fpa = atol(args[1]);
3703 if (curagent->max_fpa < 1) {
3704 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3705 file, linenum, args[0]);
3706 err_code |= ERR_ALERT | ERR_FATAL;
3707 goto out;
3708 }
3709 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003710 else if (!strcmp(args[0], "register-var-names")) {
3711 int cur_arg;
3712
3713 if (!*args[1]) {
3714 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3715 file, linenum, args[0]);
3716 err_code |= ERR_ALERT | ERR_FATAL;
3717 goto out;
3718 }
3719 cur_arg = 1;
3720 while (*args[cur_arg]) {
3721 struct spoe_var_placeholder *vph;
3722
3723 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3724 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3725 err_code |= ERR_ALERT | ERR_ABORT;
3726 goto out;
3727 }
3728 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003729 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003730 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3731 err_code |= ERR_ALERT | ERR_ABORT;
3732 goto out;
3733 }
3734 LIST_ADDQ(&curvars, &vph->list);
3735 cur_arg++;
3736 }
3737 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003738 else if (!strcmp(args[0], "log")) {
3739 char *errmsg = NULL;
3740
3741 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3742 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3743 err_code |= ERR_ALERT | ERR_FATAL;
3744 goto out;
3745 }
3746 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003747 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003748 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3749 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003750 err_code |= ERR_ALERT | ERR_FATAL;
3751 goto out;
3752 }
3753 out:
3754 return err_code;
3755}
Christopher Faulet11610f32017-09-21 10:23:10 +02003756static int
3757cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3758{
3759 struct spoe_group *grp;
3760 const char *err;
3761 int err_code = 0;
3762
3763 if ((cfg_scope == NULL && curengine != NULL) ||
3764 (cfg_scope != NULL && curengine == NULL) ||
3765 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3766 goto out;
3767
3768 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3769 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003770 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3771 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003772 err_code |= ERR_ALERT | ERR_ABORT;
3773 goto out;
3774 }
3775 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3776 err_code |= ERR_ABORT;
3777 goto out;
3778 }
3779
3780 err = invalid_char(args[1]);
3781 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003782 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3783 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003784 err_code |= ERR_ALERT | ERR_ABORT;
3785 goto out;
3786 }
3787
3788 list_for_each_entry(grp, &curgrps, list) {
3789 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003790 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3791 " name as another one declared at %s:%d.\n",
3792 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003793 err_code |= ERR_ALERT | ERR_FATAL;
3794 goto out;
3795 }
3796 }
3797
3798 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003799 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003800 err_code |= ERR_ALERT | ERR_ABORT;
3801 goto out;
3802 }
3803
3804 curgrp->id = strdup(args[1]);
3805 curgrp->conf.file = strdup(file);
3806 curgrp->conf.line = linenum;
3807 LIST_INIT(&curgrp->phs);
3808 LIST_INIT(&curgrp->messages);
3809 LIST_ADDQ(&curgrps, &curgrp->list);
3810 }
3811 else if (!strcmp(args[0], "messages")) {
3812 int cur_arg = 1;
3813 while (*args[cur_arg]) {
3814 struct spoe_placeholder *ph = NULL;
3815
3816 list_for_each_entry(ph, &curgrp->phs, list) {
3817 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003818 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3819 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003820 err_code |= ERR_ALERT | ERR_FATAL;
3821 goto out;
3822 }
3823 }
3824
3825 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003826 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 err_code |= ERR_ALERT | ERR_ABORT;
3828 goto out;
3829 }
3830 ph->id = strdup(args[cur_arg]);
3831 LIST_ADDQ(&curgrp->phs, &ph->list);
3832 cur_arg++;
3833 }
3834 }
3835 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003836 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3837 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003838 err_code |= ERR_ALERT | ERR_FATAL;
3839 goto out;
3840 }
3841 out:
3842 return err_code;
3843}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003844
3845static int
3846cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3847{
3848 struct spoe_message *msg;
3849 struct spoe_arg *arg;
3850 const char *err;
3851 char *errmsg = NULL;
3852 int err_code = 0;
3853
3854 if ((cfg_scope == NULL && curengine != NULL) ||
3855 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003856 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003857 goto out;
3858
3859 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3860 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3862 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003863 err_code |= ERR_ALERT | ERR_ABORT;
3864 goto out;
3865 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003866 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3867 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003868 goto out;
3869 }
3870
3871 err = invalid_char(args[1]);
3872 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003873 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3874 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003875 err_code |= ERR_ALERT | ERR_ABORT;
3876 goto out;
3877 }
3878
3879 list_for_each_entry(msg, &curmsgs, list) {
3880 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003881 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3882 " name as another one declared at %s:%d.\n",
3883 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003884 err_code |= ERR_ALERT | ERR_FATAL;
3885 goto out;
3886 }
3887 }
3888
3889 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003890 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003891 err_code |= ERR_ALERT | ERR_ABORT;
3892 goto out;
3893 }
3894
3895 curmsg->id = strdup(args[1]);
3896 curmsg->id_len = strlen(curmsg->id);
3897 curmsg->event = SPOE_EV_NONE;
3898 curmsg->conf.file = strdup(file);
3899 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003900 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003901 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003902 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003903 LIST_INIT(&curmsg->by_evt);
3904 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003905 LIST_ADDQ(&curmsgs, &curmsg->list);
3906 }
3907 else if (!strcmp(args[0], "args")) {
3908 int cur_arg = 1;
3909
3910 curproxy->conf.args.ctx = ARGC_SPOE;
3911 curproxy->conf.args.file = file;
3912 curproxy->conf.args.line = linenum;
3913 while (*args[cur_arg]) {
3914 char *delim = strchr(args[cur_arg], '=');
3915 int idx = 0;
3916
3917 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003918 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919 err_code |= ERR_ALERT | ERR_ABORT;
3920 goto out;
3921 }
3922
3923 if (!delim) {
3924 arg->name = NULL;
3925 arg->name_len = 0;
3926 delim = args[cur_arg];
3927 }
3928 else {
3929 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3930 arg->name_len = delim - args[cur_arg];
3931 delim++;
3932 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003933 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3934 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003935 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003936 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003937 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003938 err_code |= ERR_ALERT | ERR_FATAL;
3939 free(arg->name);
3940 free(arg);
3941 goto out;
3942 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003943 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003944 LIST_ADDQ(&curmsg->args, &arg->list);
3945 cur_arg++;
3946 }
3947 curproxy->conf.args.file = NULL;
3948 curproxy->conf.args.line = 0;
3949 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003950 else if (!strcmp(args[0], "acl")) {
3951 err = invalid_char(args[1]);
3952 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003953 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3954 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003955 err_code |= ERR_ALERT | ERR_FATAL;
3956 goto out;
3957 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003958 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003959 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003960 "logical disjunction within a condition.\n",
3961 file, linenum, args[1]);
3962 err_code |= ERR_ALERT | ERR_FATAL;
3963 goto out;
3964 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003965 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003966 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3967 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003968 err_code |= ERR_ALERT | ERR_FATAL;
3969 goto out;
3970 }
3971 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003972 else if (!strcmp(args[0], "event")) {
3973 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003974 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003975 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003976 goto out;
3977 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003978 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3979 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003980
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3982 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3983 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3984 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3985
3986 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3987 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3988 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3989 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3990 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3991 curmsg->event = SPOE_EV_ON_TCP_RSP;
3992
3993 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3994 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3995 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3996 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3997 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3998 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3999 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004000 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004001 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004002 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004003 goto out;
4004 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004005
4006 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4007 struct acl_cond *cond;
4008
4009 cond = build_acl_cond(file, linenum, &curmsg->acls,
4010 curproxy, (const char **)args+2,
4011 &errmsg);
4012 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004013 ha_alert("parsing [%s:%d] : error detected while "
4014 "parsing an 'event %s' condition : %s.\n",
4015 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004016 err_code |= ERR_ALERT | ERR_FATAL;
4017 goto out;
4018 }
4019 curmsg->cond = cond;
4020 }
4021 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004022 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4023 "or 'unless' followed by a condition but found '%s'.\n",
4024 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004025 err_code |= ERR_ALERT | ERR_FATAL;
4026 goto out;
4027 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004028 }
4029 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004030 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4031 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004032 err_code |= ERR_ALERT | ERR_FATAL;
4033 goto out;
4034 }
4035 out:
4036 free(errmsg);
4037 return err_code;
4038}
4039
4040/* Return -1 on error, else 0 */
4041static int
4042parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4043 struct flt_conf *fconf, char **err, void *private)
4044{
4045 struct list backup_sections;
4046 struct spoe_config *conf;
4047 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004048 struct spoe_group *grp, *grpback;
4049 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004050 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004051 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004052 char *file = NULL, *engine = NULL;
4053 int ret, pos = *cur_arg + 1;
4054
Christopher Faulet84c844e2018-03-23 14:37:14 +01004055 LIST_INIT(&curmsgs);
4056 LIST_INIT(&curgrps);
4057 LIST_INIT(&curmphs);
4058 LIST_INIT(&curgphs);
4059 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004060 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004061 curpxopts = 0;
4062 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004063
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004064 conf = calloc(1, sizeof(*conf));
4065 if (conf == NULL) {
4066 memprintf(err, "%s: out of memory", args[*cur_arg]);
4067 goto error;
4068 }
4069 conf->proxy = px;
4070
4071 while (*args[pos]) {
4072 if (!strcmp(args[pos], "config")) {
4073 if (!*args[pos+1]) {
4074 memprintf(err, "'%s' : '%s' option without value",
4075 args[*cur_arg], args[pos]);
4076 goto error;
4077 }
4078 file = args[pos+1];
4079 pos += 2;
4080 }
4081 else if (!strcmp(args[pos], "engine")) {
4082 if (!*args[pos+1]) {
4083 memprintf(err, "'%s' : '%s' option without value",
4084 args[*cur_arg], args[pos]);
4085 goto error;
4086 }
4087 engine = args[pos+1];
4088 pos += 2;
4089 }
4090 else {
4091 memprintf(err, "unknown keyword '%s'", args[pos]);
4092 goto error;
4093 }
4094 }
4095 if (file == NULL) {
4096 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4097 goto error;
4098 }
4099
4100 /* backup sections and register SPOE sections */
4101 LIST_INIT(&backup_sections);
4102 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004103 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4104 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004105 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004106
4107 /* Parse SPOE filter configuration file */
4108 curengine = engine;
4109 curproxy = px;
4110 curagent = NULL;
4111 curmsg = NULL;
4112 ret = readcfgfile(file);
4113 curproxy = NULL;
4114
4115 /* unregister SPOE sections and restore previous sections */
4116 cfg_unregister_sections();
4117 cfg_restore_sections(&backup_sections);
4118
4119 if (ret == -1) {
4120 memprintf(err, "Could not open configuration file %s : %s",
4121 file, strerror(errno));
4122 goto error;
4123 }
4124 if (ret & (ERR_ABORT|ERR_FATAL)) {
4125 memprintf(err, "Error(s) found in configuration file %s", file);
4126 goto error;
4127 }
4128
4129 /* Check SPOE agent */
4130 if (curagent == NULL) {
4131 memprintf(err, "No SPOE agent found in file %s", file);
4132 goto error;
4133 }
4134 if (curagent->b.name == NULL) {
4135 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4136 curagent->id, curagent->conf.file, curagent->conf.line);
4137 goto error;
4138 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004139 if (curagent->timeout.hello == TICK_ETERNITY ||
4140 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004141 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004142 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4143 " | While not properly invalid, you will certainly encounter various problems\n"
4144 " | with such a configuration. To fix this, please ensure that all following\n"
4145 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4146 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004147 }
4148 if (curagent->var_pfx == NULL) {
4149 char *tmp = curagent->id;
4150
4151 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004152 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004153 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4154 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4155 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4156 goto error;
4157 }
4158 tmp++;
4159 }
4160 curagent->var_pfx = strdup(curagent->id);
4161 }
4162
Christopher Fauletb7426d12018-03-21 14:12:17 +01004163 if (curagent->var_on_error) {
4164 struct arg arg;
4165
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004166 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004167 curagent->var_pfx, curagent->var_on_error);
4168
4169 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004170 arg.data.str.area = trash.area;
4171 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004172 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004173 if (!vars_check_arg(&arg, err)) {
4174 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4175 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4176 goto error;
4177 }
4178 }
4179
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004180 if (curagent->var_t_process) {
4181 struct arg arg;
4182
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004183 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004184 curagent->var_pfx, curagent->var_t_process);
4185
4186 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004187 arg.data.str.area = trash.area;
4188 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004189 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004190 if (!vars_check_arg(&arg, err)) {
4191 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4192 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4193 goto error;
4194 }
4195 }
4196
4197 if (curagent->var_t_total) {
4198 struct arg arg;
4199
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004200 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004201 curagent->var_pfx, curagent->var_t_total);
4202
4203 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004204 arg.data.str.area = trash.area;
4205 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004206 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004207 if (!vars_check_arg(&arg, err)) {
4208 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4209 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4210 goto error;
4211 }
4212 }
4213
Christopher Faulet11610f32017-09-21 10:23:10 +02004214 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004215 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4216 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004217 goto finish;
4218 }
4219
Christopher Faulet11610f32017-09-21 10:23:10 +02004220 /* Replace placeholders by the corresponding messages for the SPOE
4221 * agent */
4222 list_for_each_entry(ph, &curmphs, list) {
4223 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004224 struct spoe_arg *arg;
4225 unsigned int where;
4226
Christopher Faulet11610f32017-09-21 10:23:10 +02004227 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004228 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4229 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4230 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4231 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4232 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4233 }
4234 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4235 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4236 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004237 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4238 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004239 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004240 }
4241 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004242 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4243 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004244 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004245 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004246
4247 where = 0;
4248 switch (msg->event) {
4249 case SPOE_EV_ON_CLIENT_SESS:
4250 where |= SMP_VAL_FE_CON_ACC;
4251 break;
4252
4253 case SPOE_EV_ON_TCP_REQ_FE:
4254 where |= SMP_VAL_FE_REQ_CNT;
4255 break;
4256
4257 case SPOE_EV_ON_HTTP_REQ_FE:
4258 where |= SMP_VAL_FE_HRQ_HDR;
4259 break;
4260
4261 case SPOE_EV_ON_TCP_REQ_BE:
4262 if (px->cap & PR_CAP_FE)
4263 where |= SMP_VAL_FE_REQ_CNT;
4264 if (px->cap & PR_CAP_BE)
4265 where |= SMP_VAL_BE_REQ_CNT;
4266 break;
4267
4268 case SPOE_EV_ON_HTTP_REQ_BE:
4269 if (px->cap & PR_CAP_FE)
4270 where |= SMP_VAL_FE_HRQ_HDR;
4271 if (px->cap & PR_CAP_BE)
4272 where |= SMP_VAL_BE_HRQ_HDR;
4273 break;
4274
4275 case SPOE_EV_ON_SERVER_SESS:
4276 where |= SMP_VAL_BE_SRV_CON;
4277 break;
4278
4279 case SPOE_EV_ON_TCP_RSP:
4280 if (px->cap & PR_CAP_FE)
4281 where |= SMP_VAL_FE_RES_CNT;
4282 if (px->cap & PR_CAP_BE)
4283 where |= SMP_VAL_BE_RES_CNT;
4284 break;
4285
4286 case SPOE_EV_ON_HTTP_RSP:
4287 if (px->cap & PR_CAP_FE)
4288 where |= SMP_VAL_FE_HRS_HDR;
4289 if (px->cap & PR_CAP_BE)
4290 where |= SMP_VAL_BE_HRS_HDR;
4291 break;
4292
4293 default:
4294 break;
4295 }
4296
4297 list_for_each_entry(arg, &msg->args, list) {
4298 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004299 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004300 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004301 "none of which is available here ('%s')",
4302 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004303 sample_ckp_names(arg->expr->fetch->use),
4304 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004305 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004306 }
4307 }
4308
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004309 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004310 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4311 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004312 }
4313 }
4314 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004315 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004316 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004317 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004318 continue;
4319 }
4320
Christopher Faulet11610f32017-09-21 10:23:10 +02004321 /* Replace placeholders by the corresponding groups for the SPOE
4322 * agent */
4323 list_for_each_entry(ph, &curgphs, list) {
4324 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4325 if (!strcmp(grp->id, ph->id)) {
4326 grp->agent = curagent;
4327 LIST_DEL(&grp->list);
4328 LIST_ADDQ(&curagent->groups, &grp->list);
4329 goto next_aph;
4330 }
4331 }
4332 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4333 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4334 goto error;
4335 next_aph:
4336 continue;
4337 }
4338
4339 /* Replace placeholders by the corresponding message for each SPOE
4340 * group of the SPOE agent */
4341 list_for_each_entry(grp, &curagent->groups, list) {
4342 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4343 list_for_each_entry(msg, &curmsgs, list) {
4344 if (!strcmp(msg->id, ph->id)) {
4345 if (msg->group != NULL) {
4346 memprintf(err, "SPOE message '%s' already belongs to "
4347 "the SPOE group '%s' declare at %s:%d",
4348 msg->id, msg->group->id,
4349 msg->group->conf.file,
4350 msg->group->conf.line);
4351 goto error;
4352 }
4353
4354 /* Scope for arguments are not checked for now. We will check
4355 * them only if a rule use the corresponding SPOE group. */
4356 msg->agent = curagent;
4357 msg->group = grp;
4358 LIST_DEL(&ph->list);
4359 LIST_ADDQ(&grp->messages, &msg->by_grp);
4360 goto next_mph_grp;
4361 }
4362 }
4363 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4364 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4365 goto error;
4366 next_mph_grp:
4367 continue;
4368 }
4369 }
4370
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004371 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004372 /* move curmsgs to the agent message list */
4373 curmsgs.n->p = &curagent->messages;
4374 curmsgs.p->n = &curagent->messages;
4375 curagent->messages = curmsgs;
4376 LIST_INIT(&curmsgs);
4377
Christopher Faulet7ee86672017-09-19 11:08:28 +02004378 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004379 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004380
4381 /* Start agent's proxy initialization here. It will be finished during
4382 * the filter init. */
4383 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4384 init_new_proxy(&conf->agent_fe);
4385 conf->agent_fe.id = conf->agent->id;
4386 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004387 conf->agent_fe.options |= curpxopts;
4388 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004389
4390 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4391 LIST_DEL(&logsrv->list);
4392 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4393 }
4394
Christopher Faulet11610f32017-09-21 10:23:10 +02004395 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4396 LIST_DEL(&ph->list);
4397 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004398 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004399 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4400 LIST_DEL(&ph->list);
4401 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004402 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004403 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4404 struct arg arg;
4405
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004406 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004407 curagent->var_pfx, vph->name);
4408
4409 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004410 arg.data.str.area = trash.area;
4411 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004412 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004413 if (!vars_check_arg(&arg, err)) {
4414 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4415 curagent->id, curagent->var_pfx, vph->name, *err);
4416 goto error;
4417 }
4418
4419 LIST_DEL(&vph->list);
4420 free(vph->name);
4421 free(vph);
4422 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004423 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4424 LIST_DEL(&grp->list);
4425 spoe_release_group(grp);
4426 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004427 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004428 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004429 fconf->ops = &spoe_ops;
4430 fconf->conf = conf;
4431 return 0;
4432
4433 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004434 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004435 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4436 LIST_DEL(&ph->list);
4437 spoe_release_placeholder(ph);
4438 }
4439 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4440 LIST_DEL(&ph->list);
4441 spoe_release_placeholder(ph);
4442 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004443 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4444 LIST_DEL(&vph->list);
4445 free(vph->name);
4446 free(vph);
4447 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004448 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4449 LIST_DEL(&grp->list);
4450 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004451 }
4452 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4453 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004454 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004455 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004456 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4457 LIST_DEL(&logsrv->list);
4458 free(logsrv);
4459 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004460 free(conf);
4461 return -1;
4462}
4463
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004464/* Send message of a SPOE group. This is the action_ptr callback of a rule
4465 * associated to a "send-spoe-group" action.
4466 *
Christopher Faulet13403762019-12-13 09:01:57 +01004467 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4468 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004469static enum act_return
4470spoe_send_group(struct act_rule *rule, struct proxy *px,
4471 struct session *sess, struct stream *s, int flags)
4472{
4473 struct filter *filter;
4474 struct spoe_agent *agent = NULL;
4475 struct spoe_group *group = NULL;
4476 struct spoe_context *ctx = NULL;
4477 int ret, dir;
4478
4479 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4480 if (filter->config == rule->arg.act.p[0]) {
4481 agent = rule->arg.act.p[2];
4482 group = rule->arg.act.p[3];
4483 ctx = filter->ctx;
4484 break;
4485 }
4486 }
4487 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004488 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004489 if (ctx->state == SPOE_CTX_ST_NONE)
4490 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004491
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004492 switch (rule->from) {
4493 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4494 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4495 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4496 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4497 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4498 default:
4499 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4500 " - internal error while execute spoe-send-group\n",
4501 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4502 __FUNCTION__, s);
4503 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4504 agent->id);
4505 return ACT_RET_CONT;
4506 }
4507
4508 ret = spoe_process_group(s, ctx, group, dir);
4509 if (ret == 1)
4510 return ACT_RET_CONT;
4511 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004512 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004513 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4514 " - failed to process group '%s': interrupted by caller\n",
4515 (int)now.tv_sec, (int)now.tv_usec,
4516 agent->id, __FUNCTION__, s, group->id);
4517 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004518 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004519 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004520 return ACT_RET_CONT;
4521 }
4522 return ACT_RET_YIELD;
4523 }
4524 else
Christopher Faulet13403762019-12-13 09:01:57 +01004525 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004526}
4527
4528/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4529 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4530 * action should be:
4531 *
4532 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4533 *
4534 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4535 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4536 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4537 * group.
4538 *
4539 * The function returns 1 in success case, otherwise, it returns 0 and err is
4540 * filled.
4541 */
4542static int
4543check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4544{
4545 struct flt_conf *fconf;
4546 struct spoe_config *conf;
4547 struct spoe_agent *agent = NULL;
4548 struct spoe_group *group;
4549 struct spoe_message *msg;
4550 char *engine_id = rule->arg.act.p[0];
4551 char *group_id = rule->arg.act.p[1];
4552 unsigned int where = 0;
4553
4554 switch (rule->from) {
4555 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4556 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4557 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4558 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4559 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4560 default:
4561 memprintf(err,
4562 "internal error, unexpected rule->from=%d, please report this bug!",
4563 rule->from);
4564 goto error;
4565 }
4566
4567 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4568 * <px> */
4569 list_for_each_entry(fconf, &px->filter_configs, list) {
4570 conf = fconf->conf;
4571
4572 /* This is not an SPOE filter */
4573 if (fconf->id != spoe_filter_id)
4574 continue;
4575
4576 /* This is the good engine */
4577 if (!strcmp(conf->id, engine_id)) {
4578 agent = conf->agent;
4579 break;
4580 }
4581 }
4582 if (agent == NULL) {
4583 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4584 engine_id, group_id);
4585 goto error;
4586 }
4587
4588 /* Try to find the right group */
4589 list_for_each_entry(group, &agent->groups, list) {
4590 /* This is the good group */
4591 if (!strcmp(group->id, group_id))
4592 break;
4593 }
4594 if (&group->list == &agent->groups) {
4595 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4596 group_id, engine_id);
4597 goto error;
4598 }
4599
4600 /* Ok, we found the group, we need to check messages and their
4601 * arguments */
4602 list_for_each_entry(msg, &group->messages, by_grp) {
4603 struct spoe_arg *arg;
4604
4605 list_for_each_entry(arg, &msg->args, list) {
4606 if (!(arg->expr->fetch->val & where)) {
4607 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4608 "some args extract information from '%s',"
4609 "none of which is available here ('%s')",
4610 msg->id, group->id, msg->conf.file, msg->conf.line,
4611 sample_ckp_names(arg->expr->fetch->use),
4612 sample_ckp_names(where));
4613 goto error;
4614 }
4615 }
4616 }
4617
4618 free(engine_id);
4619 free(group_id);
4620 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4621 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4622 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4623 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4624 return 1;
4625
4626 error:
4627 free(engine_id);
4628 free(group_id);
4629 return 0;
4630}
4631
4632/* Parse 'send-spoe-group' action following the format:
4633 *
4634 * ... send-spoe-group <engine-id> <group-id>
4635 *
4636 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4637 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4638 * ids are saved and used later, when the rule will be checked.
4639 */
4640static enum act_parse_ret
4641parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4642 struct act_rule *rule, char **err)
4643{
4644 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4645 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4646 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4647 return ACT_RET_PRS_ERR;
4648 }
4649 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4650 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4651
4652 (*orig_arg) += 2;
4653
4654 rule->action = ACT_CUSTOM;
4655 rule->action_ptr = spoe_send_group;
4656 rule->check_ptr = check_send_spoe_group;
4657 return ACT_RET_PRS_OK;
4658}
4659
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004660
4661/* Declare the filter parser for "spoe" keyword */
4662static struct flt_kw_list flt_kws = { "SPOE", { }, {
4663 { "spoe", parse_spoe_flt, NULL },
4664 { NULL, NULL, NULL },
4665 }
4666};
4667
Willy Tarreau0108d902018-11-25 19:14:37 +01004668INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4669
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004670/* Delcate the action parser for "spoe-action" keyword */
4671static struct action_kw_list tcp_req_action_kws = { { }, {
4672 { "send-spoe-group", parse_send_spoe_group },
4673 { /* END */ },
4674 }
4675};
Willy Tarreau0108d902018-11-25 19:14:37 +01004676
4677INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4678
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004679static struct action_kw_list tcp_res_action_kws = { { }, {
4680 { "send-spoe-group", parse_send_spoe_group },
4681 { /* END */ },
4682 }
4683};
Willy Tarreau0108d902018-11-25 19:14:37 +01004684
4685INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4686
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004687static struct action_kw_list http_req_action_kws = { { }, {
4688 { "send-spoe-group", parse_send_spoe_group },
4689 { /* END */ },
4690 }
4691};
Willy Tarreau0108d902018-11-25 19:14:37 +01004692
4693INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4694
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004695static struct action_kw_list http_res_action_kws = { { }, {
4696 { "send-spoe-group", parse_send_spoe_group },
4697 { /* END */ },
4698 }
4699};
4700
Willy Tarreau0108d902018-11-25 19:14:37 +01004701INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);