blob: 7c019c412505b15c7ad1a2728f34eb5d43fbea05 [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
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
Emeric Bruna1dd2432017-06-21 15:42:52 +020021#include <common/hathreads.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020022
23#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020024#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010025#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020026
Christopher Faulet57583e42017-09-04 15:41:09 +020027#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020028#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020029#include <proto/arg.h>
30#include <proto/backend.h>
31#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010032#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033#include <proto/frontend.h>
34#include <proto/log.h>
35#include <proto/proto_http.h>
36#include <proto/proxy.h>
37#include <proto/sample.h>
38#include <proto/session.h>
39#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010040#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/stream.h>
42#include <proto/stream_interface.h>
43#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020044#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#include <proto/vars.h>
46
47#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
48#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#else
51#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010052#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020053#endif
54
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010055/* Reserved 4 bytes to the frame size. So a frame and its size can be written
56 * together in a buffer */
57#define MAX_FRAME_SIZE global.tune.bufsize - 4
58
59/* The minimum size for a frame */
60#define MIN_FRAME_SIZE 256
61
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Reserved for the metadata and the frame type.
63 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010064#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020065
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010066/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010067#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
68
Christopher Faulet3b386a32017-02-23 10:17:15 +010069/* SPOE filter id. Used to identify SPOE filters */
70const char *spoe_filter_id = "SPOE filter";
71
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020072/* Set if the handle on SIGUSR1 is registered */
73static int sighandler_registered = 0;
74
75/* proxy used during the parsing */
76struct proxy *curproxy = NULL;
77
78/* The name of the SPOE engine, used during the parsing */
79char *curengine = NULL;
80
81/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020082/* SPOE agent/group/message used during the parsing */
83struct spoe_agent *curagent = NULL;
84struct spoe_group *curgrp = NULL;
85struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086
87/* list of SPOE messages and placeholders used during the parsing */
88struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020089struct list curgrps;
90struct list curmphs;
91struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010092struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Faulet7250b8f2018-03-26 17:19:01 +020094/* list of log servers used during the parsing */
95struct list curlogsrvs;
96
Christopher Faulet0e0f0852018-03-26 17:20:36 +020097/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
98int curpxopts;
99int curpxopts2;
100
Christopher Faulet42bfa462017-01-04 14:14:19 +0100101/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100102static struct pool_head *pool_head_spoe_ctx = NULL;
103static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105struct flt_ops spoe_ops;
106
Christopher Faulet8ef75252017-02-20 22:56:03 +0100107static int spoe_queue_context(struct spoe_context *ctx);
108static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
109static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110
111/********************************************************************
112 * helper functions/globals
113 ********************************************************************/
114static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200115spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 free(ph->id);
120 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121}
122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100124spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125{
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 struct spoe_arg *arg, *argback;
127 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128
129 if (!msg)
130 return;
131 free(msg->id);
132 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 release_sample_expr(arg->expr);
135 free(arg->name);
136 LIST_DEL(&arg->list);
137 free(arg);
138 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
140 LIST_DEL(&acl->list);
141 prune_acl(acl);
142 free(acl);
143 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100173 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100174 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100175 free(agent->var_t_process);
176 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200177 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
178 LIST_DEL(&msg->list);
179 spoe_release_message(msg);
180 }
181 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
182 LIST_DEL(&grp->list);
183 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200184 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200185 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100186 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
250 * allocated string. So it is the caller's reponsibility to release it. If the
251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
255 static int init = 0;
256
257 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
258 const char uuid_chr[] = "0123456789ABCDEF-";
259 char *uuid;
260 int i;
261
262 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
263 return NULL;
264
265 if (!init) {
266 srand(now_ms);
267 init = 1;
268 }
269
270 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
271 int r = rand () % 16;
272
273 switch (uuid_fmt[i]) {
274 case 'x' : uuid[i] = uuid_chr[r]; break;
275 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
276 default : uuid[i] = uuid_fmt[i]; break;
277 }
278 }
279 return uuid;
280}
281
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100282
283static inline void
284spoe_update_stat_time(struct timeval *tv, long *t)
285{
286 if (*t == -1)
287 *t = tv_ms_elapsed(tv, &now);
288 else
289 *t += tv_ms_elapsed(tv, &now);
290 tv_zero(tv);
291}
292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293/********************************************************************
294 * Functions that encode/decode SPOE frames
295 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296/* Helper to get static string length, excluding the terminating null byte */
297#define SLEN(str) (sizeof(str)-1)
298
299/* Predefined key used in HELLO/DISCONNECT frames */
300#define SUPPORTED_VERSIONS_KEY "supported-versions"
301#define VERSION_KEY "version"
302#define MAX_FRAME_SIZE_KEY "max-frame-size"
303#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100304#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100305#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306#define STATUS_CODE_KEY "status-code"
307#define MSG_KEY "message"
308
309struct spoe_version {
310 char *str;
311 int min;
312 int max;
313};
314
315/* All supported versions */
316static struct spoe_version supported_versions[] = {
317 {"1.0", 1000, 1000},
318 {NULL, 0, 0}
319};
320
321/* Comma-separated list of supported versions */
322#define SUPPORTED_VERSIONS_VAL "1.0"
323
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324/* Convert a string to a SPOE version value. The string must follow the format
325 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
326 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200327static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100328spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 const char *p, *end;
331 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p = str;
334 end = str+len;
335 maj = min = 0;
336 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 /* skip leading spaces */
339 while (p < end && isspace(*p))
340 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342 /* parse Major number, until the '.' */
343 while (*p != '.') {
344 if (p >= end || *p < '0' || *p > '9')
345 goto out;
346 maj *= 10;
347 maj += (*p - '0');
348 p++;
349 }
350
351 /* check Major version */
352 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200353 goto out;
354
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++; /* skip the '.' */
356 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
357 goto out;
358
359 /* Parse Minor number */
360 while (p < end) {
361 if (*p < '0' || *p > '9')
362 break;
363 min *= 10;
364 min += (*p - '0');
365 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367
368 /* check Minor number */
369 if (min > 999)
370 goto out;
371
372 /* skip trailing spaces */
373 while (p < end && isspace(*p))
374 p++;
375 if (p != end)
376 goto out;
377
378 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379 out:
380 return vsn;
381}
382
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
384 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
385 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388{
Christopher Faulet305c6072017-02-23 16:17:53 +0100389 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100390 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 char *p, *end;
392 unsigned int flags = SPOE_FRM_FL_FIN;
393 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200394
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 p = frame;
396 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 /* Set Frame type */
399 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100401 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 memcpy(p, (char *)&flags, 4);
403 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200404
405 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200407
408 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
409 * and "capabilities" */
410
411 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 sz = SLEN(SUPPORTED_VERSIONS_KEY);
413 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
414 goto too_big;
415
416 *p++ = SPOE_DATA_T_STR;
417 sz = SLEN(SUPPORTED_VERSIONS_VAL);
418 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
419 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200420
421 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100422 sz = SLEN(MAX_FRAME_SIZE_KEY);
423 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
424 goto too_big;
425
426 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200427 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200429
430 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 sz = SLEN(CAPABILITIES_KEY);
432 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
433 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200434
Christopher Faulet8ef75252017-02-20 22:56:03 +0100435 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100436 chk = get_trash_chunk();
437 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
438 memcpy(chk->str, "pipelining", 10);
439 chk->len += 10;
440 }
441 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
442 if (chk->len) chk->str[chk->len++] = ',';
443 memcpy(chk->str+chk->len, "async", 5);
444 chk->len += 5;
445 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100446 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
447 if (chk->len) chk->str[chk->len++] = ',';
448 memcpy(chk->str+chk->len, "fragmentation", 13);
449 chk->len += 5;
450 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100451 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452 goto too_big;
453
454 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100455 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456 sz = SLEN(ENGINE_ID_KEY);
457 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
458 goto too_big;
459
460 *p++ = SPOE_DATA_T_STR;
461 sz = strlen(agent->engine_id);
462 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
463 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100464 }
465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 return (p - frame);
467
468 too_big:
469 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
470 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471}
472
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
474 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
475 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100477spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200478{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100479 const char *reason;
480 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100481 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200483
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 p = frame;
485 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 /* Set Frame type */
488 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100490 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 memcpy(p, (char *)&flags, 4);
492 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
494 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 *p++ = 0; *p++ = 0;
496
497 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
498 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200499
500 /* There are 2 mandatory items: "status-code" and "message" */
501
502 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100503 sz = SLEN(STATUS_CODE_KEY);
504 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
505 goto too_big;
506
507 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200508 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200510
511 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512 sz = SLEN(MSG_KEY);
513 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
514 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 /*Get the message corresponding to the status code */
517 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
518
519 *p++ = SPOE_DATA_T_STR;
520 sz = strlen(reason);
521 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
522 goto too_big;
523
524 return (p - frame);
525
526 too_big:
527 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
528 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200529}
530
Christopher Faulet8ef75252017-02-20 22:56:03 +0100531/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
532 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
533 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200534static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100535spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100536 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200537{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100538 char *p, *end;
539 unsigned int stream_id, frame_id;
540 unsigned int flags = SPOE_FRM_FL_FIN;
541 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200542
Christopher Faulet8ef75252017-02-20 22:56:03 +0100543 p = frame;
544 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200545
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 stream_id = ctx->stream_id;
547 frame_id = ctx->frame_id;
548
549 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
550 /* The fragmentation is not supported by the applet */
551 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
552 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
553 return -1;
554 }
555 flags = ctx->frag_ctx.flags;
556 }
557
558 /* Set Frame type */
559 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
560
561 /* Set flags */
562 memcpy(p, (char *)&flags, 4);
563 p += 4;
564
565 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200566 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100567 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200568 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100569 goto too_big;
570
571 /* Copy encoded messages, if possible */
572 sz = ctx->buffer->i;
573 if (p + sz >= end)
574 goto too_big;
575 memcpy(p, ctx->buffer->p, sz);
576 p += sz;
577
578 return (p - frame);
579
580 too_big:
581 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
582 return 0;
583}
584
585/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
586 * returns the number of encoded bytes in the frame on success, 0 if an encoding
587 * error occurred and -1 if a fatal error occurred. */
588static int
589spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
590 char *frame, size_t size)
591{
592 char *p, *end;
593 unsigned int stream_id, frame_id;
594 unsigned int flags;
595 size_t sz;
596
597 p = frame;
598 end = frame+size;
599
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 /* <ctx> is null when the stream has aborted the processing of a
601 * fragmented frame. In this case, we must notify the corresponding
602 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100603 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100604 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100605 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
606 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
607 }
608 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100610 stream_id = ctx->stream_id;
611 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100612 }
613
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100615 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100617 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 memcpy(p, (char *)&flags, 4);
619 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200620
621 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200622 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200624 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 if (ctx == NULL)
628 goto end;
629
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100631 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 if (p + sz >= end)
633 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100634 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100636
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100637 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 return (p - frame);
639
640 too_big:
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
642 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643}
644
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645/* Decode and process the HELLO frame sent by an agent. It returns the number of
646 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
647 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650{
Christopher Faulet305c6072017-02-23 16:17:53 +0100651 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
652 char *p, *end;
653 int vsn, max_frame_size;
654 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655
656 p = frame;
657 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658
659 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
661 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100663 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 if (size < 7 /* TYPE + METADATA */) {
666 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
667 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 }
669
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100670 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 memcpy((char *)&flags, p, 4);
672 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200673
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100674 /* Fragmentation is not supported for HELLO frame */
675 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100677 return -1;
678 }
679
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 if (*p != 0 || *(p+1) != 0) {
682 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
683 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200684 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100685 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686
687 /* There are 3 mandatory items: "version", "max-frame-size" and
688 * "capabilities" */
689
690 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100691 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 while (p < end) {
693 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200694 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696
697 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 ret = spoe_decode_buffer(&p, end, &str, &sz);
699 if (ret == -1 || !sz) {
700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
701 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 /* Check "version" K/V item */
705 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 int i, type = *p++;
707
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
714 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
715 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 }
717
Christopher Faulet8ef75252017-02-20 22:56:03 +0100718 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 return -1;
722 }
723 for (i = 0; supported_versions[i].str != NULL; ++i) {
724 if (vsn >= supported_versions[i].min &&
725 vsn <= supported_versions[i].max)
726 break;
727 }
728 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 return -1;
731 }
732 }
733 /* Check "max-frame-size" K/V item */
734 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200736
737 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
739 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
740 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
741 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
743 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200745 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 if (sz < MIN_FRAME_SIZE ||
750 sz > SPOE_APPCTX(appctx)->max_frame_size) {
751 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200752 return -1;
753 }
754 max_frame_size = sz;
755 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100756 /* Check "capabilities" K/V item */
757 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100758 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759
760 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
763 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
766 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
767 return 0;
768 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771 char *delim;
772
773 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
777 str += 10; sz -= 10;
778 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779 flags |= SPOE_APPCTX_FL_PIPELINING;
780 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 else if (sz >= 5 && !strncmp(str, "async", 5)) {
782 str += 5; sz -= 5;
783 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 flags |= SPOE_APPCTX_FL_ASYNC;
785 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
787 str += 13; sz -= 13;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100789 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
790 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100791
Christopher Faulet8ef75252017-02-20 22:56:03 +0100792 /* Get the next comma or break */
793 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100794 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 delim++;
796 sz -= (delim - str);
797 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100798 }
799 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800 else {
801 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 if (spoe_skip_data(&p, end) == -1) {
803 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
804 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806 }
807 }
808
809 /* Final checks */
810 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812 return -1;
813 }
814 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100815 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816 return -1;
817 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100818 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
819 flags &= ~SPOE_APPCTX_FL_PIPELINING;
820 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
821 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822
Christopher Faulet42bfa462017-01-04 14:14:19 +0100823 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
824 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
825 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826
827 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828}
829
830/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
831 * bytes on success, 0 if the frame can be ignored and -1 if an error
832 * occurred. */
833static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100837 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838
839 p = frame;
840 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841
842 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
844 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 if (size < 7 /* TYPE + METADATA */) {
849 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
850 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851 }
852
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100853 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 memcpy((char *)&flags, p, 4);
855 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100857 /* Fragmentation is not supported for DISCONNECT frame */
858 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100860 return -1;
861 }
862
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 if (*p != 0 || *(p+1) != 0) {
865 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
866 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200867 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869
870 /* There are 2 mandatory items: "status-code" and "message" */
871
872 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 while (p < end) {
874 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200875 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877
878 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 ret = spoe_decode_buffer(&p, end, &str, &sz);
880 if (ret == -1 || !sz) {
881 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
882 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883 }
884
885 /* Check "status-code" K/V item */
886 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888
889 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
891 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
892 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
893 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
895 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200897 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
899 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
903
904 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 else if (!memcmp(str, MSG_KEY, sz)) {
906 int type = *p++;
907
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
910 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
911 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200912 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 ret = spoe_decode_buffer(&p, end, &str, &sz);
914 if (ret == -1 || sz > 255) {
915 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
916 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
919 SPOE_APPCTX(appctx)->reason = str;
920 SPOE_APPCTX(appctx)->rlen = sz;
921#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922 }
923 else {
924 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 if (spoe_skip_data(&p, end) == -1) {
926 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
927 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929 }
930 }
931
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933}
934
935
Christopher Fauleta1cda022016-12-21 08:58:06 +0100936/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 * success, 0 if the frame can be ignored and -1 if an error occurred. */
938static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100940 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100942 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100944 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100946 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947
948 p = frame;
949 end = frame + size;
950 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951
952 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
954 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 if (size < 7 /* TYPE + METADATA */) {
959 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
960 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200961 }
962
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 memcpy((char *)&flags, p, 4);
965 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100967 /* Fragmentation is not supported for now */
968 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100970 return -1;
971 }
972
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200974 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100976 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200978 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100982
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100984 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200985 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100986 if ((*ctx)->stream_id == (unsigned int)stream_id &&
987 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100988 goto found;
989 }
990 }
991 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100992 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
993 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100994 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 goto found;
996 }
997 }
998
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100999 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1000 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1001 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1002
1003 /* ABRT bit is set for an unfinished fragmented frame */
1004 if (flags & SPOE_FRM_FL_ABRT) {
1005 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001006 (*ctx)->state = SPOE_CTX_ST_ERROR;
1007 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1008 /* Ignore the payload */
1009 goto end;
1010 }
1011 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1012 /* For now, we ignore the ack */
1013 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1014 return 0;
1015 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001016
Christopher Fauleta1cda022016-12-21 08:58:06 +01001017 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001018 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1019 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001020 " - stream-id=%u - frame-id=%u\n",
1021 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1022 __FUNCTION__, appctx,
1023 (unsigned int)stream_id, (unsigned int)frame_id);
1024
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001025 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001026 return 0;
1027
1028 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1030 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001032 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001034
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001035 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 len = (end - p);
1037 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1038 SPOE_APPCTX(appctx)->buffer->i = len;
1039 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001040
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 /* Transfer the buffer ownership to the SPOE context */
1042 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1043 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 (*ctx)->state = SPOE_CTX_ST_DONE;
1046
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001047 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 " - ACK frame received"
1050 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1053 (*ctx)->frame_id, flags);
1054 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001055}
1056
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057/* This function is used in cfgparse.c and declared in proto/checks.h. It
1058 * prepare the request to send to agents during a healthcheck. It returns 0 on
1059 * success and -1 if an error occurred. */
1060int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 struct appctx appctx;
1064 struct spoe_appctx spoe_appctx;
1065 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1066 size_t sz;
1067 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069 memset(&appctx, 0, sizeof(appctx));
1070 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072
1073 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001074 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1077 end = frame + MAX_FRAME_SIZE;
1078
1079 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1080 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 /* Add "healthcheck" K/V item */
1085 sz = SLEN(HEALTHCHECK_KEY);
1086 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1087 return -1;
1088 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 *len = frame - buf;
1091 sz = htonl(*len - 4);
1092 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097 return 0;
1098}
1099
1100/* This function is used in checks.c and declared in proto/checks.h. It decode
1101 * the response received from an agent during a healthcheck. It returns 0 on
1102 * success and -1 if an error occurred. */
1103int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 struct appctx appctx;
1107 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 memset(&appctx, 0, sizeof(appctx));
1110 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001113 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1116 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117 goto error;
1118 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001119 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1120 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121
1122 return 0;
1123
1124 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1126 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1127 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return -1;
1129}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001130
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1132 * the frame can be ignored, 1 to retry later, and the frame legnth on
1133 * success. */
1134static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136{
1137 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138 int ret;
1139 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1142 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 netint = htonl(framesz);
1144 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001145 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001147 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 }
1151 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return -1; /* error */
1153 }
1154 return framesz;
1155}
1156
1157/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1158 * when the frame can be ignored, 1 to retry later and the frame length on
1159 * success. */
1160static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162{
1163 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 int ret;
1165 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166
Willy Tarreau06d80a92017-10-19 14:32:15 +02001167 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 if (ret > 0) {
1169 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001170 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 return -1;
1173 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 }
1176 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001179 }
1180 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001181 return -1; /* error */
1182 }
1183 return framesz;
1184}
1185
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001186/********************************************************************
1187 * Functions that manage the SPOE applet
1188 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001189static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001190spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001191{
1192 si_applet_want_get(appctx->owner);
1193 si_applet_want_put(appctx->owner);
1194 appctx_wakeup(appctx);
1195 return 1;
1196}
1197
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198/* Callback function that catches applet timeouts. If a timeout occurred, we set
1199 * <appctx->st1> flag and the SPOE applet is woken up. */
1200static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001201spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202{
1203 struct appctx *appctx = task->context;
1204
1205 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1206 if (tick_is_expired(task->expire, now_ms)) {
1207 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211 return task;
1212}
1213
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214/* Callback function that releases a SPOE applet. This happens when the
1215 * connection with the agent is closed. */
1216static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219 struct stream_interface *si = appctx->owner;
1220 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1221 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001222 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223
1224 if (spoe_appctx == NULL)
1225 return;
1226
1227 appctx->ctx.spoe.ptr = NULL;
1228 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229
1230 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1231 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1232 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233
Christopher Faulet8ef75252017-02-20 22:56:03 +01001234 /* Remove applet from the list of running applets */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001235 SPOE_DEBUG_STMT(agent->rt[tid].applets_act--);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001236 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001237 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001238 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1239 LIST_DEL(&spoe_appctx->list);
1240 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001241 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001242 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001246 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1247 eb32_delete(&spoe_appctx->node);
1248 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001249 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001250 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001251
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001253 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1254 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001255
1256 si_shutw(si);
1257 si_shutr(si);
1258 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001259 }
1260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Destroy the task attached to this applet */
1262 if (spoe_appctx->task) {
1263 task_delete(spoe_appctx->task);
1264 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001265 }
1266
Christopher Faulet8ef75252017-02-20 22:56:03 +01001267 /* Notify all waiting streams */
1268 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001269 LIST_DEL(&ctx->list);
1270 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001271 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001272 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001273 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001276 }
1277
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 /* If the applet was processing a fragmented frame, notify the
1279 * corresponding stream. */
1280 if (spoe_appctx->frag_ctx.ctx) {
1281 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001282 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001283 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001284 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001285 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1286 }
1287
Christopher Faulet8ef75252017-02-20 22:56:03 +01001288 /* Release allocated memory */
1289 spoe_release_buffer(&spoe_appctx->buffer,
1290 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001291 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001292
Christopher Faulet24289f22017-09-25 14:48:02 +02001293 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001294 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001295
Christopher Faulet8ef75252017-02-20 22:56:03 +01001296 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001297 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001298 LIST_DEL(&ctx->list);
1299 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001300 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001301 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001302 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001303 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001304 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001305 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001306 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 LIST_DEL(&ctx->list);
1308 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001309 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001310 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001311 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001313 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1314 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001315
1316 end:
1317 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001318 agent->rt[tid].frame_size = agent->max_frame_size;
1319 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1320 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001321}
1322
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001323static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001324spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001325{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001326 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001327 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001328 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001329 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 if (si->state <= SI_ST_CON) {
1332 si_applet_want_put(si);
1333 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1334 goto stop;
1335 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001336 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001339 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001340
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1343 " - Connection timed out\n",
1344 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1345 __FUNCTION__, appctx);
1346 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001348 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001349
Christopher Faulet42bfa462017-01-04 14:14:19 +01001350 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001351 SPOE_APPCTX(appctx)->task->expire =
1352 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1355 * length. */
1356 buf = trash.str; frame = buf+4;
1357 ret = spoe_prepare_hahello_frame(appctx, frame,
1358 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001359 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001360 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001361
1362 switch (ret) {
1363 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 case 0: /* ignore => an error, cannot be ignored */
1365 goto exit;
1366
1367 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 goto stop;
1369
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 default:
1371 /* HELLO frame successfully sent, now wait for the
1372 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1374 goto next;
1375 }
1376
1377 next:
1378 return 0;
1379 stop:
1380 return 1;
1381 exit:
1382 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1383 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001384}
1385
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001386static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001387spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001389 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001390 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 char *frame;
1392 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394
Christopher Fauletb067b062017-01-04 16:39:11 +01001395 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001397 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001398 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1402 " - Connection timed out\n",
1403 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1404 __FUNCTION__, appctx);
1405 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 goto exit;
1407 }
1408
Christopher Faulet8ef75252017-02-20 22:56:03 +01001409 frame = trash.str; trash.len = 0;
1410 ret = spoe_recv_frame(appctx, frame,
1411 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 if (ret > 1) {
1413 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1414 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1415 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001416 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 trash.len = ret + 4;
1418 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001419 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001420
Christopher Fauleta1cda022016-12-21 08:58:06 +01001421 switch (ret) {
1422 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001424 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1425 goto next;
1426
1427 case 1: /* retry later */
1428 goto stop;
1429
1430 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001431 /* HELLO handshake is finished, set the idle timeout and
1432 * add the applet in the list of running applets. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001433 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001434 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001435 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001436 SPOE_APPCTX(appctx)->node.key = 0;
1437 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001438
1439 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001440 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001441 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001442 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001443
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001445 /* Do not forget to remove processed frame from the output buffer */
1446 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001447 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001448
1449 SPOE_APPCTX(appctx)->task->expire =
1450 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001451 return 0;
1452 stop:
1453 return 1;
1454 exit:
1455 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1456 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457}
1458
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001459
Christopher Fauleta1cda022016-12-21 08:58:06 +01001460static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001461spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001462{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1464 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001465 char *frame, *buf;
1466 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001467
Christopher Faulet8ef75252017-02-20 22:56:03 +01001468 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1469 * length. */
1470 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471
1472 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1473 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1474 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1475 SPOE_APPCTX(appctx)->max_frame_size);
1476 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001477 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001478 *skip = 1;
1479 ret = 1;
1480 goto end;
1481 }
1482 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001483 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001484 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1485 SPOE_APPCTX(appctx)->max_frame_size);
1486
1487 }
1488
Christopher Faulet8ef75252017-02-20 22:56:03 +01001489 if (ret > 1)
1490 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001491
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 switch (ret) {
1493 case -1: /* error */
1494 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1495 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001496
Christopher Faulet8ef75252017-02-20 22:56:03 +01001497 case 0: /* ignore */
1498 if (ctx == NULL)
1499 goto abort_frag_frame;
1500
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001501 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001502 LIST_DEL(&ctx->list);
1503 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001504 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001505 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001506 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001507 ctx->state = SPOE_CTX_ST_ERROR;
1508 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1509 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001510 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 break;
1512
1513 case 1: /* retry */
1514 *skip = 1;
1515 break;
1516
1517 default:
1518 if (ctx == NULL)
1519 goto abort_frag_frame;
1520
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001521 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001522 LIST_DEL(&ctx->list);
1523 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001524 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001525 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001526 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001527 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1528 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1529 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001530 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001532 }
1533 goto end;
1534
1535 frag_frame_sent:
1536 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001537 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001538 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1539 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1540 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1542 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1543 goto end;
1544
1545 no_frag_frame_sent:
1546 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1547 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001548 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 }
1550 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1551 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1552 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1553 }
1554 else {
1555 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001556 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001557 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1558 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001559 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001560 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001561 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1562 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1563 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001564 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001565
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1567 goto end;
1568
1569 abort_frag_frame:
1570 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1571 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1572 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1573 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1574 goto end;
1575
1576 end:
1577 return ret;
1578}
1579
1580static int
1581spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1582{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001583 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001584 struct spoe_context *ctx = NULL;
1585 char *frame;
1586 int ret;
1587
1588 frame = trash.str; trash.len = 0;
1589 ret = spoe_recv_frame(appctx, frame,
1590 SPOE_APPCTX(appctx)->max_frame_size);
1591 if (ret > 1) {
1592 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1593 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001594 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001595 goto end;
1596 }
1597 trash.len = ret + 4;
1598 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1599 }
1600 switch (ret) {
1601 case -1: /* error */
1602 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1603 break;
1604
1605 case 0: /* ignore */
1606 break;
1607
1608 case 1: /* retry */
1609 *skip = 1;
1610 break;
1611
1612 default:
1613 LIST_DEL(&ctx->list);
1614 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001615 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001616 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1617 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001618 if (ctx->spoe_appctx) {
1619 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001620 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001621 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001622 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1623 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1624 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1625 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1626 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1627 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1628 }
1629 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1630 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001631 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1632 break;
1633 }
1634
1635 /* Do not forget to remove processed frame from the output buffer */
1636 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001637 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001638 end:
1639 return ret;
1640}
1641
1642static int
1643spoe_handle_processing_appctx(struct appctx *appctx)
1644{
1645 struct stream_interface *si = appctx->owner;
1646 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001647 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001648
1649 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1650 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1651 goto exit;
1652 }
1653
1654 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1655 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1656 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1657 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1658 goto next;
1659 }
1660
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001661 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001662 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001663 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001664 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1665 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001666 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001667
Christopher Faulet8f82b202018-01-24 16:23:03 +01001668 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1669 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 /* receiving_frame loop */
1672 while (!skip_receiving) {
1673 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1674 switch (ret) {
1675 case -1: /* error */
1676 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001677
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 case 0: /* ignore */
1679 active_r = 1;
1680 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001681
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 case 1: /* retry */
1683 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001684
Christopher Faulet8f82b202018-01-24 16:23:03 +01001685 default:
1686 active_r = 1;
1687 break;
1688 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001689 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 /* send_frame loop */
1692 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1693 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1694 switch (ret) {
1695 case -1: /* error */
1696 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697
Christopher Faulet8f82b202018-01-24 16:23:03 +01001698 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001699 if (SPOE_APPCTX(appctx)->node.key)
1700 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001701 active_s++;
1702 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001703
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 case 1: /* retry */
1705 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001708 if (SPOE_APPCTX(appctx)->node.key)
1709 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 active_s++;
1711 break;
1712 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1717 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1718 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001719
Christopher Faulet8f82b202018-01-24 16:23:03 +01001720 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001721 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001722 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001724 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001725 }
1726 return 1;
1727
Christopher Faulet8f82b202018-01-24 16:23:03 +01001728 next:
1729 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1730 return 0;
1731
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732 exit:
1733 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1734 return 0;
1735}
1736
1737static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001738spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739{
1740 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001741 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001742 char *frame, *buf;
1743 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001744
Christopher Fauleta1cda022016-12-21 08:58:06 +01001745 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1746 goto exit;
1747
1748 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1749 goto exit;
1750
Christopher Faulet8ef75252017-02-20 22:56:03 +01001751 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1752 * length. */
1753 buf = trash.str; frame = buf+4;
1754 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1755 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001757 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758
1759 switch (ret) {
1760 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001761 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762 goto exit;
1763
1764 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 goto stop;
1766
1767 default:
1768 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1769 " - disconnected by HAProxy (%d): %s\n",
1770 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 __FUNCTION__, appctx,
1772 SPOE_APPCTX(appctx)->status_code,
1773 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774
1775 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1776 goto next;
1777 }
1778
1779 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001780 SPOE_APPCTX(appctx)->task->expire =
1781 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782 return 0;
1783 stop:
1784 return 1;
1785 exit:
1786 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1787 return 0;
1788}
1789
1790static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001791spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792{
1793 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794 char *frame;
1795 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796
Christopher Fauletb067b062017-01-04 16:39:11 +01001797 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001800 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801
Christopher Fauletb067b062017-01-04 16:39:11 +01001802 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001803 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001805 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806
Christopher Faulet8ef75252017-02-20 22:56:03 +01001807 frame = trash.str; trash.len = 0;
1808 ret = spoe_recv_frame(appctx, frame,
1809 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 trash.len = ret + 4;
1812 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 }
1814
1815 switch (ret) {
1816 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1818 " - error on frame (%s)\n",
1819 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001820 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 goto exit;
1824
1825 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 goto next;
1827
1828 case 1: /* retry */
1829 goto stop;
1830
1831 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001832 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001835 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1837 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 goto exit;
1839 }
1840
1841 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001842 /* Do not forget to remove processed frame from the output buffer */
1843 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001844 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 return 0;
1847 stop:
1848 return 1;
1849 exit:
1850 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1851 return 0;
1852}
1853
1854/* I/O Handler processing messages exchanged with the agent */
1855static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001857{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001858 struct stream_interface *si = appctx->owner;
1859 struct spoe_agent *agent;
1860
1861 if (SPOE_APPCTX(appctx) == NULL)
1862 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1865 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001866
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 switchstate:
1868 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1869 " - appctx-state=%s\n",
1870 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1871 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1872
1873 switch (appctx->st0) {
1874 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 goto out;
1877 goto switchstate;
1878
1879 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 goto out;
1882 goto switchstate;
1883
1884 case SPOE_APPCTX_ST_IDLE:
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001885 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001886 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001887 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001889 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001890 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001891 SPOE_APPCTX(appctx)->task->expire =
1892 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001894 goto switchstate;
1895 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001896 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1897 /* fall through */
1898
1899 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001900 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1901 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001902 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001903 goto out;
1904 goto switchstate;
1905
1906 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001907 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001908 goto out;
1909 goto switchstate;
1910
1911 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001912 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001913 goto out;
1914 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001915
1916 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001917 appctx->st0 = SPOE_APPCTX_ST_END;
1918 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1919
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001920 si_shutw(si);
1921 si_shutr(si);
1922 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001923 /* fall through */
1924
1925 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001926 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001927 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001928 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001929 if (stopping)
1930 spoe_wakeup_appctx(appctx);
1931
Christopher Faulet42bfa462017-01-04 14:14:19 +01001932 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1933 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934 si_oc(si)->flags |= CF_READ_DONTWAIT;
1935 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936}
1937
1938struct applet spoe_applet = {
1939 .obj_type = OBJ_TYPE_APPLET,
1940 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001941 .fct = spoe_handle_appctx,
1942 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943};
1944
1945/* Create a SPOE applet. On success, the created applet is returned, else
1946 * NULL. */
1947static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001948spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001949{
1950 struct appctx *appctx;
1951 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001952 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001953
Emeric Brun1138fd02017-06-19 12:38:55 +02001954 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955 goto out_error;
1956
Willy Tarreaubafbe012017-11-24 17:34:44 +01001957 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001958 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001960 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001961
Christopher Faulet42bfa462017-01-04 14:14:19 +01001962 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001963 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001964 goto out_free_spoe_appctx;
1965
1966 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001967 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001968 SPOE_APPCTX(appctx)->task->context = appctx;
1969 SPOE_APPCTX(appctx)->agent = conf->agent;
1970 SPOE_APPCTX(appctx)->version = 0;
1971 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1972 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001973 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001974 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001975 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001976
1977 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1978 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001979 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001980
1981 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1982 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001983
Willy Tarreau5820a362016-12-22 15:59:02 +01001984 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985 if (!sess)
1986 goto out_free_spoe;
1987
Willy Tarreau87787ac2017-08-28 16:22:54 +02001988 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001989 goto out_free_sess;
1990
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991 stream_set_backend(strm, conf->agent->b.be);
1992
1993 /* applet is waiting for data */
1994 si_applet_cant_get(&strm->si[0]);
1995 appctx_wakeup(appctx);
1996
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 strm->do_log = NULL;
1998 strm->res.flags |= CF_READ_DONTWAIT;
1999
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002000 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002001 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002002 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002003 SPOE_DEBUG_STMT(conf->agent->rt[tid].applets_act++);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002004 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002005
Emeric Brunc60def82017-09-27 14:59:38 +02002006 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002007 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002008 return appctx;
2009
2010 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011 out_free_sess:
2012 session_free(sess);
2013 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002014 task_free(SPOE_APPCTX(appctx)->task);
2015 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002016 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 out_free_appctx:
2018 appctx_free(appctx);
2019 out_error:
2020 return NULL;
2021}
2022
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002024spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002025{
2026 struct spoe_config *conf = FLT_CONF(ctx->filter);
2027 struct spoe_agent *agent = conf->agent;
2028 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002029 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030
Christopher Fauleta1cda022016-12-21 08:58:06 +01002031 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002032 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002033 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035
2036 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002037 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2039 ctx->strm);
2040
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 /* Do not try to create a new applet if there is no server up for the
2042 * agent's backend. */
2043 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2045 " - cannot create SPOE appctx: no server up\n",
2046 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2047 __FUNCTION__, ctx->strm);
2048 goto end;
2049 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002050
Christopher Fauleta1cda022016-12-21 08:58:06 +01002051 /* Do not try to create a new applet if we have reached the maximum of
2052 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002053 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002054 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2056 " - cannot create SPOE appctx: max CPS reached\n",
2057 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2058 __FUNCTION__, ctx->strm);
2059 goto end;
2060 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002061 }
2062
Christopher Faulet8ef75252017-02-20 22:56:03 +01002063 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 if (appctx == NULL) {
2065 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2066 " - failed to create SPOE appctx\n",
2067 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2068 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002069 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002070 "SPOE: [%s] failed to create SPOE applet\n",
2071 agent->id);
2072
Christopher Fauleta1cda022016-12-21 08:58:06 +01002073 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074 }
2075
Christopher Fauleta1cda022016-12-21 08:58:06 +01002076 /* Increase the per-process number of cumulated connections */
2077 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002078 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 end:
2081 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002082 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002083 ctx->status_code = SPOE_CTX_ERR_RES;
2084 return -1;
2085 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002087 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002088 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002089 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002090 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2091 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092
2093 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002094 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002095 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002096 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002097 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002098 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002099
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002100 /* Finally try to wakeup an IDLE applet. */
2101 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2102 struct eb32_node *node;
2103
2104 node = eb32_first(&agent->rt[tid].idle_applets);
2105 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2106 if (node && spoe_appctx) {
2107 eb32_delete(&spoe_appctx->node);
2108 spoe_appctx->node.key++;
2109 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2110 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002111 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002112 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002113 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002114}
2115
2116/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002117 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002118 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002119/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2120 * fragmented_content. If the next message can be processed, it returns 0. If
2121 * the message is too big, it returns -1.*/
2122static int
2123spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2124 struct spoe_message *msg, int dir,
2125 char **buf, char *end)
2126{
2127 struct sample *smp;
2128 struct spoe_arg *arg;
2129 int ret;
2130
2131 if (msg->cond) {
2132 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2133 ret = acl_pass(ret);
2134 if (msg->cond->pol == ACL_COND_UNLESS)
2135 ret = !ret;
2136
2137 /* the rule does not match */
2138 if (!ret)
2139 goto next;
2140 }
2141
2142 /* Resume encoding of a SPOE argument */
2143 if (ctx->frag_ctx.curarg != NULL) {
2144 arg = ctx->frag_ctx.curarg;
2145 goto encode_argument;
2146 }
2147
2148 if (ctx->frag_ctx.curoff != UINT_MAX)
2149 goto encode_msg_payload;
2150
2151 /* Check if there is enough space for the message name and the
2152 * number of arguments. It implies <msg->id_len> is encoded on 2
2153 * bytes, at most (< 2288). */
2154 if (*buf + 2 + msg->id_len + 1 > end)
2155 goto too_big;
2156
2157 /* Encode the message name */
2158 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2159 goto too_big;
2160
2161 /* Set the number of arguments for this message */
2162 **buf = msg->nargs;
2163 (*buf)++;
2164
2165 ctx->frag_ctx.curoff = 0;
2166 encode_msg_payload:
2167
2168 /* Loop on arguments */
2169 list_for_each_entry(arg, &msg->args, list) {
2170 ctx->frag_ctx.curarg = arg;
2171 ctx->frag_ctx.curoff = UINT_MAX;
2172
2173 encode_argument:
2174 if (ctx->frag_ctx.curoff != UINT_MAX)
2175 goto encode_arg_value;
2176
2177 /* Encode the arguement name as a string. It can by NULL */
2178 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2179 goto too_big;
2180
2181 ctx->frag_ctx.curoff = 0;
2182 encode_arg_value:
2183
2184 /* Fetch the arguement value */
2185 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2186 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2187 if (ret == -1 || ctx->frag_ctx.curoff)
2188 goto too_big;
2189 }
2190
2191 next:
2192 return 0;
2193
2194 too_big:
2195 return -1;
2196}
2197
Christopher Fauletc718b822017-09-21 16:50:56 +02002198/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2199 * handle fragmented content. On success it returns 1. If an error occurred, -1
2200 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2201 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002202static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002203spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002204 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002205{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002206 struct spoe_config *conf = FLT_CONF(ctx->filter);
2207 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002208 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002209 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002210
Christopher Faulet8ef75252017-02-20 22:56:03 +01002211 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002212 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002213
Christopher Fauletc718b822017-09-21 16:50:56 +02002214 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2215 /* Resume encoding of a SPOE message */
2216 if (ctx->frag_ctx.curmsg != NULL) {
2217 msg = ctx->frag_ctx.curmsg;
2218 goto encode_evt_message;
2219 }
2220
2221 list_for_each_entry(msg, messages, by_evt) {
2222 ctx->frag_ctx.curmsg = msg;
2223 ctx->frag_ctx.curarg = NULL;
2224 ctx->frag_ctx.curoff = UINT_MAX;
2225
2226 encode_evt_message:
2227 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2228 goto too_big;
2229 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002230 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002231 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2232 /* Resume encoding of a SPOE message */
2233 if (ctx->frag_ctx.curmsg != NULL) {
2234 msg = ctx->frag_ctx.curmsg;
2235 goto encode_grp_message;
2236 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002237
Christopher Fauletc718b822017-09-21 16:50:56 +02002238 list_for_each_entry(msg, messages, by_grp) {
2239 ctx->frag_ctx.curmsg = msg;
2240 ctx->frag_ctx.curarg = NULL;
2241 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002242
Christopher Fauletc718b822017-09-21 16:50:56 +02002243 encode_grp_message:
2244 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2245 goto too_big;
2246 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002247 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002248 else
2249 goto skip;
2250
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002251
Christopher Faulet57583e42017-09-04 15:41:09 +02002252 /* nothing has been encoded for an unfragmented payload */
2253 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2254 goto skip;
2255
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002257 " - encode %s messages - spoe_appctx=%p"
2258 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259 (int)now.tv_sec, (int)now.tv_usec,
2260 agent->id, __FUNCTION__, s,
2261 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002262 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002263 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002264
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266 ctx->frag_ctx.curmsg = NULL;
2267 ctx->frag_ctx.curarg = NULL;
2268 ctx->frag_ctx.curoff = 0;
2269 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002270
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002271 return 1;
2272
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002273 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002274 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2275 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2276 return -1;
2277 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002278
2279 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002280 " - encode fragmented messages - spoe_appctx=%p"
2281 " - curmsg=%p - curarg=%p - curoff=%u"
2282 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002283 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002284 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002285 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002286 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002287
Christopher Faulet8ef75252017-02-20 22:56:03 +01002288 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2290 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2291 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002292
2293 skip:
2294 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2295 " - skip the frame because nothing has been encoded\n",
2296 (int)now.tv_sec, (int)now.tv_usec,
2297 agent->id, __FUNCTION__, s);
2298 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002299}
2300
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002301
2302/***************************************************************************
2303 * Functions that handle SPOE actions
2304 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002305/* Helper function to set a variable */
2306static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002307spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002308 struct sample *smp)
2309{
2310 struct spoe_config *conf = FLT_CONF(ctx->filter);
2311 struct spoe_agent *agent = conf->agent;
2312 char varname[64];
2313
2314 memset(varname, 0, sizeof(varname));
2315 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2316 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002317 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2318 vars_set_by_name(varname, len, smp);
2319 else
2320 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002321}
2322
2323/* Helper function to unset a variable */
2324static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002325spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326 struct sample *smp)
2327{
2328 struct spoe_config *conf = FLT_CONF(ctx->filter);
2329 struct spoe_agent *agent = conf->agent;
2330 char varname[64];
2331
2332 memset(varname, 0, sizeof(varname));
2333 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2334 scope, agent->var_pfx, len, name);
2335 vars_unset_by_name_ifexist(varname, len, smp);
2336}
2337
2338
Christopher Faulet8ef75252017-02-20 22:56:03 +01002339static inline int
2340spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2341 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002342{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002343 char *str, *scope, *p = *buf;
2344 struct sample smp;
2345 uint64_t sz;
2346 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 if (p + 2 >= end)
2349 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350
Christopher Faulet8ef75252017-02-20 22:56:03 +01002351 /* SET-VAR requires 3 arguments */
2352 if (*p++ != 3)
2353 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002354
Christopher Faulet8ef75252017-02-20 22:56:03 +01002355 switch (*p++) {
2356 case SPOE_SCOPE_PROC: scope = "proc"; break;
2357 case SPOE_SCOPE_SESS: scope = "sess"; break;
2358 case SPOE_SCOPE_TXN : scope = "txn"; break;
2359 case SPOE_SCOPE_REQ : scope = "req"; break;
2360 case SPOE_SCOPE_RES : scope = "res"; break;
2361 default: goto skip;
2362 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002363
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2365 goto skip;
2366 memset(&smp, 0, sizeof(smp));
2367 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368
Christopher Faulet8ef75252017-02-20 22:56:03 +01002369 if (spoe_decode_data(&p, end, &smp) == -1)
2370 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2373 " - set-var '%s.%s.%.*s'\n",
2374 (int)now.tv_sec, (int)now.tv_usec,
2375 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2376 __FUNCTION__, s, scope,
2377 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2378 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002379
Christopher Faulet8ef75252017-02-20 22:56:03 +01002380 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002381
Christopher Faulet8ef75252017-02-20 22:56:03 +01002382 ret = (p - *buf);
2383 *buf = p;
2384 return ret;
2385 skip:
2386 return 0;
2387}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002388
Christopher Faulet8ef75252017-02-20 22:56:03 +01002389static inline int
2390spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2391 char **buf, char *end, int dir)
2392{
2393 char *str, *scope, *p = *buf;
2394 struct sample smp;
2395 uint64_t sz;
2396 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002397
Christopher Faulet8ef75252017-02-20 22:56:03 +01002398 if (p + 2 >= end)
2399 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401 /* UNSET-VAR requires 2 arguments */
2402 if (*p++ != 2)
2403 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002404
Christopher Faulet8ef75252017-02-20 22:56:03 +01002405 switch (*p++) {
2406 case SPOE_SCOPE_PROC: scope = "proc"; break;
2407 case SPOE_SCOPE_SESS: scope = "sess"; break;
2408 case SPOE_SCOPE_TXN : scope = "txn"; break;
2409 case SPOE_SCOPE_REQ : scope = "req"; break;
2410 case SPOE_SCOPE_RES : scope = "res"; break;
2411 default: goto skip;
2412 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002413
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2415 goto skip;
2416 memset(&smp, 0, sizeof(smp));
2417 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2420 " - unset-var '%s.%s.%.*s'\n",
2421 (int)now.tv_sec, (int)now.tv_usec,
2422 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2423 __FUNCTION__, s, scope,
2424 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2425 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002426
Christopher Faulet8ef75252017-02-20 22:56:03 +01002427 spoe_unset_var(ctx, scope, str, sz, &smp);
2428
2429 ret = (p - *buf);
2430 *buf = p;
2431 return ret;
2432 skip:
2433 return 0;
2434}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002435
Christopher Faulet8ef75252017-02-20 22:56:03 +01002436/* Process SPOE actions for a specific event. It returns 1 on success. If an
2437 * error occurred, 0 is returned. */
2438static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002439spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002440{
2441 char *p, *end;
2442 int ret;
2443
2444 p = ctx->buffer->p;
2445 end = p + ctx->buffer->i;
2446
2447 while (p < end) {
2448 enum spoe_action_type type;
2449
2450 type = *p++;
2451 switch (type) {
2452 case SPOE_ACT_T_SET_VAR:
2453 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2454 if (!ret)
2455 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002456 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002457
Christopher Faulet8ef75252017-02-20 22:56:03 +01002458 case SPOE_ACT_T_UNSET_VAR:
2459 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2460 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002461 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002462 break;
2463
2464 default:
2465 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002466 }
2467 }
2468
2469 return 1;
2470 skip:
2471 return 0;
2472}
2473
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002474/***************************************************************************
2475 * Functions that process SPOE events
2476 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002477static void
2478spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2479 struct spoe_context *ctx, int dir)
2480{
2481 if (!tv_iszero(&ctx->stats.tv_start)) {
2482 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2483 ctx->stats.t_total += ctx->stats.t_process;
2484 tv_zero(&ctx->stats.tv_request);
2485 tv_zero(&ctx->stats.tv_queue);
2486 tv_zero(&ctx->stats.tv_wait);
2487 tv_zero(&ctx->stats.tv_response);
2488 }
2489
2490 if (agent->var_t_process) {
2491 struct sample smp;
2492
2493 memset(&smp, 0, sizeof(smp));
2494 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2495 smp.data.u.sint = ctx->stats.t_process;
2496 smp.data.type = SMP_T_SINT;
2497
2498 spoe_set_var(ctx, "txn", agent->var_t_process,
2499 strlen(agent->var_t_process), &smp);
2500 }
2501
2502 if (agent->var_t_total) {
2503 struct sample smp;
2504
2505 memset(&smp, 0, sizeof(smp));
2506 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2507 smp.data.u.sint = ctx->stats.t_total;
2508 smp.data.type = SMP_T_SINT;
2509
2510 spoe_set_var(ctx, "txn", agent->var_t_total,
2511 strlen(agent->var_t_total), &smp);
2512 }
2513}
2514
2515static void
2516spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2517 struct spoe_context *ctx, int dir)
2518{
2519 if (agent->eps_max > 0)
2520 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2521
2522 if (agent->var_on_error) {
2523 struct sample smp;
2524
2525 memset(&smp, 0, sizeof(smp));
2526 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2527 smp.data.u.sint = ctx->status_code;
2528 smp.data.type = SMP_T_BOOL;
2529
2530 spoe_set_var(ctx, "txn", agent->var_on_error,
2531 strlen(agent->var_on_error), &smp);
2532 }
2533
2534 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2535 ? SPOE_CTX_ST_READY
2536 : SPOE_CTX_ST_NONE);
2537}
2538
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002539static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002540spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002541{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002542 /* If a process is already started for this SPOE context, retry
2543 * later. */
2544 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002545 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002546
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002547 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002548 ctx->stats.tv_start = now;
2549 ctx->stats.tv_request = now;
2550 ctx->stats.t_request = -1;
2551 ctx->stats.t_queue = -1;
2552 ctx->stats.t_waiting = -1;
2553 ctx->stats.t_response = -1;
2554 ctx->stats.t_process = -1;
2555
2556 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002557
Christopher Fauleta1cda022016-12-21 08:58:06 +01002558 /* Set the right flag to prevent request and response processing
2559 * in same time. */
2560 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2561 ? SPOE_CTX_FL_REQ_PROCESS
2562 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564}
2565
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002566static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002567spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002568{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002569 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002570
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002571 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2572 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002573 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002574 if (sa) {
2575 if (sa->frag_ctx.ctx == ctx) {
2576 sa->frag_ctx.ctx = NULL;
2577 spoe_wakeup_appctx(sa->owner);
2578 }
2579 else
2580 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002581 }
2582
Christopher Fauleta1cda022016-12-21 08:58:06 +01002583 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002584 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002585 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002586
2587 /* Reset processing timer */
2588 ctx->process_exp = TICK_ETERNITY;
2589
Christopher Faulet8ef75252017-02-20 22:56:03 +01002590 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002591
Christopher Fauletfce747b2018-01-24 15:59:32 +01002592 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002593 ctx->frag_ctx.curmsg = NULL;
2594 ctx->frag_ctx.curarg = NULL;
2595 ctx->frag_ctx.curoff = 0;
2596 ctx->frag_ctx.flags = 0;
2597
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002599 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
2600 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
2601 else
2602 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2603
Christopher Fauleta1cda022016-12-21 08:58:06 +01002604 LIST_DEL(&ctx->list);
2605 LIST_INIT(&ctx->list);
2606 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002607}
2608
Christopher Faulet58d03682017-09-21 16:57:24 +02002609/* Process a list of SPOE messages. First, this functions will process messages
2610 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2611 * to process corresponding actions. During all the processing, it returns 0
2612 * and it returns 1 when the processing is finished. If an error occurred, -1
2613 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002614static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002615spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2616 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002617{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002618 struct spoe_config *conf = FLT_CONF(ctx->filter);
2619 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002620 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002621
2622 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002623 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002624
2625 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2626 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002627 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002628 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002629 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002630 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002631 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002632 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002633
2634 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002636 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002637 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002638 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002639 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002640 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641 goto skip;
2642 }
2643 }
2644
Christopher Fauletf7a30922016-11-10 15:04:51 +01002645 if (!tick_isset(ctx->process_exp)) {
2646 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2647 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2648 ctx->process_exp);
2649 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002650 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002651 if (!ret)
2652 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002654 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002655 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002656 }
2657
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002658 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002659 if (!tv_iszero(&ctx->stats.tv_request))
2660 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002661 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002662 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002663 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002664 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002665 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002666 if (!ret)
2667 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002668 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002669 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002670 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2671 }
2672
2673 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002674 if (ctx->spoe_appctx)
2675 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002676 ret = 0;
2677 goto out;
2678 }
2679
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002680 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2681 ret = 0;
2682 goto out;
2683 }
2684
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002685 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002686 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002687 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002689 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002690 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002691 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002692 }
2693
2694 out:
2695 return ret;
2696
Christopher Fauleta1cda022016-12-21 08:58:06 +01002697 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002698 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002700 spoe_stop_processing(agent, ctx);
2701 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002702
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002704 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002705 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002706 if (ctx->status_code) {
2707 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2708 spoe_handle_processing_error(s, agent, ctx, dir);
2709 ret = 1;
2710 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002711 return ret;
2712}
2713
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002714/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2715 * See spoe_process_message for details. */
2716static int
2717spoe_process_group(struct stream *s, struct spoe_context *ctx,
2718 struct spoe_group *group, int dir)
2719{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002720 struct spoe_config *conf = FLT_CONF(ctx->filter);
2721 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002722 int ret;
2723
2724 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2725 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002726 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002727 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2728 group->id);
2729
2730 if (LIST_ISEMPTY(&group->messages))
2731 return 1;
2732
2733 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002734 if (ret && ctx->stats.t_process != -1) {
2735 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002736 " - <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 +01002737 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002738 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002739 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002740 ctx->stats.t_response, ctx->stats.t_process,
2741 agent->counters.idles, agent->counters.applets,
2742 agent->counters.nb_sending, agent->counters.nb_waiting,
2743 agent->counters.nb_errors, agent->counters.nb_processed,
2744 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2745 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2746 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2747 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2748 agent->id, group->id, s->uniq_id, ctx->status_code,
2749 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2750 ctx->stats.t_response, ctx->stats.t_process,
2751 agent->counters.idles, agent->counters.applets,
2752 agent->counters.nb_sending, agent->counters.nb_waiting,
2753 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002754 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002755 return ret;
2756}
2757
Christopher Faulet58d03682017-09-21 16:57:24 +02002758/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2759 * See spoe_process_message for details. */
2760static int
2761spoe_process_event(struct stream *s, struct spoe_context *ctx,
2762 enum spoe_event ev)
2763{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002764 struct spoe_config *conf = FLT_CONF(ctx->filter);
2765 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002766 int dir, ret;
2767
2768 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002769 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002770 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002771 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2772 spoe_event_str[ev]);
2773
2774 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2775
2776 if (LIST_ISEMPTY(&(ctx->events[ev])))
2777 return 1;
2778
2779 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002780 if (ret && ctx->stats.t_process != -1) {
2781 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002782 " - <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 +01002783 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2784 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2785 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002786 ctx->stats.t_response, ctx->stats.t_process,
2787 agent->counters.idles, agent->counters.applets,
2788 agent->counters.nb_sending, agent->counters.nb_waiting,
2789 agent->counters.nb_errors, agent->counters.nb_processed,
2790 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2791 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2792 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2793 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2794 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2795 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2796 ctx->stats.t_response, ctx->stats.t_process,
2797 agent->counters.idles, agent->counters.applets,
2798 agent->counters.nb_sending, agent->counters.nb_waiting,
2799 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002800 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002801 return ret;
2802}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002803
2804/***************************************************************************
2805 * Functions that create/destroy SPOE contexts
2806 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002807static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002808spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002809{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002810 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002811 return 1;
2812
Christopher Faulet4596fb72017-01-11 14:05:19 +01002813 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002814 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002815 LIST_DEL(&buffer_wait->list);
2816 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002817 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002818 }
2819
Christopher Faulet4596fb72017-01-11 14:05:19 +01002820 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002821 return 1;
2822
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002823 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002824 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002825 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002826 return 0;
2827}
2828
2829static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002830spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002831{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002832 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002833 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002834 LIST_DEL(&buffer_wait->list);
2835 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002836 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002837 }
2838
2839 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002840 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002841 b_free(buf);
2842 offer_buffers(buffer_wait->target,
2843 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844 }
2845}
2846
Christopher Faulet4596fb72017-01-11 14:05:19 +01002847static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002848spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002849{
2850 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2851 return 1;
2852}
2853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002854static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002855spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002856{
2857 struct spoe_config *conf = FLT_CONF(filter);
2858 struct spoe_context *ctx;
2859
Willy Tarreaubafbe012017-11-24 17:34:44 +01002860 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861 if (ctx == NULL) {
2862 return NULL;
2863 }
2864 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002865 ctx->filter = filter;
2866 ctx->state = SPOE_CTX_ST_NONE;
2867 ctx->status_code = SPOE_CTX_ERR_NONE;
2868 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002869 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002870 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002871 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002872 LIST_INIT(&ctx->buffer_wait.list);
2873 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002874 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002875 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002876
Christopher Fauletf7a30922016-11-10 15:04:51 +01002877 ctx->stream_id = 0;
2878 ctx->frame_id = 1;
2879 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002880
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002881 tv_zero(&ctx->stats.tv_start);
2882 tv_zero(&ctx->stats.tv_request);
2883 tv_zero(&ctx->stats.tv_queue);
2884 tv_zero(&ctx->stats.tv_wait);
2885 tv_zero(&ctx->stats.tv_response);
2886 ctx->stats.t_request = -1;
2887 ctx->stats.t_queue = -1;
2888 ctx->stats.t_waiting = -1;
2889 ctx->stats.t_response = -1;
2890 ctx->stats.t_process = -1;
2891 ctx->stats.t_total = 0;
2892
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002893 ctx->strm = s;
2894 ctx->state = SPOE_CTX_ST_READY;
2895 filter->ctx = ctx;
2896
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897 return ctx;
2898}
2899
2900static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002901spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002903 struct spoe_config *conf = FLT_CONF(filter);
2904 struct spoe_context *ctx = filter->ctx;
2905
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002906 if (!ctx)
2907 return;
2908
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002909 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002910 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002911 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002912}
2913
2914static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002915spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002916{
2917 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002918 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002919
2920 tv_zero(&ctx->stats.tv_start);
2921 tv_zero(&ctx->stats.tv_request);
2922 tv_zero(&ctx->stats.tv_queue);
2923 tv_zero(&ctx->stats.tv_wait);
2924 tv_zero(&ctx->stats.tv_response);
2925 ctx->stats.t_request = -1;
2926 ctx->stats.t_queue = -1;
2927 ctx->stats.t_waiting = -1;
2928 ctx->stats.t_response = -1;
2929 ctx->stats.t_process = -1;
2930 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931}
2932
2933
2934/***************************************************************************
2935 * Hooks that manage the filter lifecycle (init/check/deinit)
2936 **************************************************************************/
2937/* Signal handler: Do a soft stop, wakeup SPOE applet */
2938static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002939spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940{
2941 struct proxy *p;
2942
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002943 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944 while (p) {
2945 struct flt_conf *fconf;
2946
2947 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002948 struct spoe_config *conf;
2949 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002950 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002951 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952
Christopher Faulet3b386a32017-02-23 10:17:15 +01002953 if (fconf->id != spoe_filter_id)
2954 continue;
2955
2956 conf = fconf->conf;
2957 agent = conf->agent;
2958
Christopher Faulet24289f22017-09-25 14:48:02 +02002959 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002960 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002961 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2962 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002963 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964 }
2965 }
2966 p = p->next;
2967 }
2968}
2969
2970
2971/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2972static int
2973spoe_init(struct proxy *px, struct flt_conf *fconf)
2974{
2975 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002977 /* conf->agent_fe was already initialized during the config
2978 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002979 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980 conf->agent_fe.cap = PR_CAP_FE;
2981 conf->agent_fe.mode = PR_MODE_TCP;
2982 conf->agent_fe.maxconn = 0;
2983 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2984 conf->agent_fe.conn_retries = CONN_RETRIES;
2985 conf->agent_fe.accept = frontend_accept;
2986 conf->agent_fe.srv = NULL;
2987 conf->agent_fe.timeout.client = TICK_ETERNITY;
2988 conf->agent_fe.default_target = &spoe_applet.obj_type;
2989 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2990
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002992 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993 sighandler_registered = 1;
2994 }
2995
2996 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997}
2998
2999/* Free ressources allocated by the SPOE filter. */
3000static void
3001spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3002{
3003 struct spoe_config *conf = fconf->conf;
3004
3005 if (conf) {
3006 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007
Christopher Faulet8ef75252017-02-20 22:56:03 +01003008 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003009 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 free(conf);
3011 }
3012 fconf->conf = NULL;
3013}
3014
3015/* Check configuration of a SPOE filter for a specified proxy.
3016 * Return 1 on error, else 0. */
3017static int
3018spoe_check(struct proxy *px, struct flt_conf *fconf)
3019{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003020 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021 struct spoe_config *conf = fconf->conf;
3022 struct proxy *target;
3023
Christopher Faulet7ee86672017-09-19 11:08:28 +02003024 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3025 * are uniq */
3026 list_for_each_entry(f, &px->filter_configs, list) {
3027 struct spoe_config *c = f->conf;
3028
3029 /* This is not an SPOE filter */
3030 if (f->id != spoe_filter_id)
3031 continue;
3032 /* This is the current SPOE filter */
3033 if (f == fconf)
3034 continue;
3035
3036 /* Check engine Id. It should be uniq */
3037 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003038 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3039 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003040 return 1;
3041 }
3042 }
3043
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044 target = proxy_be_by_name(conf->agent->b.name);
3045 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003046 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3047 " declared at %s:%d.\n",
3048 px->id, conf->agent->b.name, conf->agent->id,
3049 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003050 return 1;
3051 }
3052 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003053 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3054 " at %s:%d does not support HTTP mode.\n",
3055 px->id, target->id, conf->agent->id,
3056 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057 return 1;
3058 }
3059
3060 free(conf->agent->b.name);
3061 conf->agent->b.name = NULL;
3062 conf->agent->b.be = target;
3063 return 0;
3064}
3065
3066/**************************************************************************
3067 * Hooks attached to a stream
3068 *************************************************************************/
3069/* Called when a filter instance is created and attach to a stream. It creates
3070 * the context that will be used to process this stream. */
3071static int
3072spoe_start(struct stream *s, struct filter *filter)
3073{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003074 struct spoe_config *conf = FLT_CONF(filter);
3075 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 struct spoe_context *ctx;
3077
3078 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003079 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003080 __FUNCTION__, s);
3081
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003082 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003083 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3084 " - failed to create SPOE context\n",
3085 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003086 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003087 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003088 "SPOE: [%s] failed to create SPOE context\n",
3089 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003090 return 0;
3091 }
3092
Christopher Faulet11610f32017-09-21 10:23:10 +02003093 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3095
Christopher Faulet11610f32017-09-21 10:23:10 +02003096 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003097 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3098
Christopher Faulet11610f32017-09-21 10:23:10 +02003099 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003100 filter->pre_analyzers |= AN_RES_INSPECT;
3101
Christopher Faulet11610f32017-09-21 10:23:10 +02003102 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3104
Christopher Faulet11610f32017-09-21 10:23:10 +02003105 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003106 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3107
Christopher Faulet11610f32017-09-21 10:23:10 +02003108 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003109 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3110
3111 return 1;
3112}
3113
3114/* Called when a filter instance is detached from a stream. It release the
3115 * attached SPOE context. */
3116static void
3117spoe_stop(struct stream *s, struct filter *filter)
3118{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003119 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3120 (int)now.tv_sec, (int)now.tv_usec,
3121 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3122 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003123 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124}
3125
Christopher Fauletf7a30922016-11-10 15:04:51 +01003126
3127/*
3128 * Called when the stream is woken up because of expired timer.
3129 */
3130static void
3131spoe_check_timeouts(struct stream *s, struct filter *filter)
3132{
3133 struct spoe_context *ctx = filter->ctx;
3134
Christopher Fauletac580602018-03-20 16:09:20 +01003135 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003136 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003137}
3138
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139/* Called when we are ready to filter data on a channel */
3140static int
3141spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3142{
3143 struct spoe_context *ctx = filter->ctx;
3144 int ret = 1;
3145
3146 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3147 " - ctx-flags=0x%08x\n",
3148 (int)now.tv_sec, (int)now.tv_usec,
3149 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3150 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3151
Christopher Fauletb067b062017-01-04 16:39:11 +01003152 if (ctx->state == SPOE_CTX_ST_NONE)
3153 goto out;
3154
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155 if (!(chn->flags & CF_ISRESP)) {
3156 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3157 chn->analysers |= AN_REQ_INSPECT_FE;
3158 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3159 chn->analysers |= AN_REQ_INSPECT_BE;
3160
3161 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3162 goto out;
3163
3164 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003165 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003166 if (!ret)
3167 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3169 }
3170 else {
3171 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3172 chn->analysers |= AN_RES_INSPECT;
3173
3174 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3175 goto out;
3176
Christopher Faulet8ef75252017-02-20 22:56:03 +01003177 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003178 if (!ret) {
3179 channel_dont_read(chn);
3180 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003181 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003182 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003183 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184 }
3185
3186 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 return ret;
3188}
3189
3190/* Called before a processing happens on a given channel */
3191static int
3192spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3193 struct channel *chn, unsigned an_bit)
3194{
3195 struct spoe_context *ctx = filter->ctx;
3196 int ret = 1;
3197
3198 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3199 " - ctx-flags=0x%08x - ana=0x%08x\n",
3200 (int)now.tv_sec, (int)now.tv_usec,
3201 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3202 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3203 ctx->flags, an_bit);
3204
Christopher Fauletb067b062017-01-04 16:39:11 +01003205 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003206 goto out;
3207
3208 switch (an_bit) {
3209 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003210 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 break;
3212 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003213 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 break;
3215 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003216 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003217 break;
3218 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003219 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003220 break;
3221 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003222 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003223 break;
3224 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003225 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003226 break;
3227 }
3228
3229 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003230 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003231 channel_dont_read(chn);
3232 channel_dont_close(chn);
3233 }
3234 return ret;
3235}
3236
3237/* Called when the filtering on the channel ends. */
3238static int
3239spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3240{
3241 struct spoe_context *ctx = filter->ctx;
3242
3243 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3244 " - ctx-flags=0x%08x\n",
3245 (int)now.tv_sec, (int)now.tv_usec,
3246 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3247 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3248
3249 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003250 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003251 }
3252
3253 return 1;
3254}
3255
3256/********************************************************************
3257 * Functions that manage the filter initialization
3258 ********************************************************************/
3259struct flt_ops spoe_ops = {
3260 /* Manage SPOE filter, called for each filter declaration */
3261 .init = spoe_init,
3262 .deinit = spoe_deinit,
3263 .check = spoe_check,
3264
3265 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003266 .attach = spoe_start,
3267 .detach = spoe_stop,
3268 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003269
3270 /* Handle channels activity */
3271 .channel_start_analyze = spoe_start_analyze,
3272 .channel_pre_analyze = spoe_chn_pre_analyze,
3273 .channel_end_analyze = spoe_end_analyze,
3274};
3275
3276
3277static int
3278cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3279{
3280 const char *err;
3281 int i, err_code = 0;
3282
3283 if ((cfg_scope == NULL && curengine != NULL) ||
3284 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003285 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003286 goto out;
3287
3288 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3289 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003290 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3291 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 err_code |= ERR_ALERT | ERR_ABORT;
3293 goto out;
3294 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003295 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3296 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 goto out;
3298 }
3299
3300 err = invalid_char(args[1]);
3301 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003302 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3303 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 err_code |= ERR_ALERT | ERR_ABORT;
3305 goto out;
3306 }
3307
3308 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003309 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3310 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 err_code |= ERR_ALERT | ERR_ABORT;
3312 goto out;
3313 }
3314 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003315 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003316 err_code |= ERR_ALERT | ERR_ABORT;
3317 goto out;
3318 }
3319
3320 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003321
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 curagent->conf.file = strdup(file);
3323 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003324
3325 curagent->timeout.hello = TICK_ETERNITY;
3326 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003327 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003328
3329 curagent->engine_id = NULL;
3330 curagent->var_pfx = NULL;
3331 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003332 curagent->var_t_process = NULL;
3333 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003334 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3335 if (global.nbthread == 1)
3336 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003337 curagent->cps_max = 0;
3338 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003339 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003340 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003341
3342 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003343 LIST_INIT(&curagent->events[i]);
3344 LIST_INIT(&curagent->groups);
3345 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003346
Christopher Faulet24289f22017-09-25 14:48:02 +02003347 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003348 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003349 err_code |= ERR_ALERT | ERR_ABORT;
3350 goto out;
3351 }
3352 for (i = 0; i < global.nbthread; ++i) {
3353 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003354 SPOE_DEBUG_STMT(curagent->rt[i].applets_act = 0);
3355 SPOE_DEBUG_STMT(curagent->rt[i].applets_idle = 0);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003356 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003357 LIST_INIT(&curagent->rt[i].applets);
3358 LIST_INIT(&curagent->rt[i].sending_queue);
3359 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003360 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003362 }
3363 else if (!strcmp(args[0], "use-backend")) {
3364 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003365 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3366 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003367 err_code |= ERR_ALERT | ERR_FATAL;
3368 goto out;
3369 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003370 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003372 free(curagent->b.name);
3373 curagent->b.name = strdup(args[1]);
3374 }
3375 else if (!strcmp(args[0], "messages")) {
3376 int cur_arg = 1;
3377 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003378 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379
Christopher Faulet11610f32017-09-21 10:23:10 +02003380 list_for_each_entry(ph, &curmphs, list) {
3381 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003382 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3383 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384 err_code |= ERR_ALERT | ERR_FATAL;
3385 goto out;
3386 }
3387 }
3388
Christopher Faulet11610f32017-09-21 10:23:10 +02003389 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003390 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391 err_code |= ERR_ALERT | ERR_ABORT;
3392 goto out;
3393 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003394 ph->id = strdup(args[cur_arg]);
3395 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003396 cur_arg++;
3397 }
3398 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003399 else if (!strcmp(args[0], "groups")) {
3400 int cur_arg = 1;
3401 while (*args[cur_arg]) {
3402 struct spoe_placeholder *ph = NULL;
3403
3404 list_for_each_entry(ph, &curgphs, list) {
3405 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003406 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3407 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003408 err_code |= ERR_ALERT | ERR_FATAL;
3409 goto out;
3410 }
3411 }
3412
3413 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003414 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003415 err_code |= ERR_ALERT | ERR_ABORT;
3416 goto out;
3417 }
3418 ph->id = strdup(args[cur_arg]);
3419 LIST_ADDQ(&curgphs, &ph->list);
3420 cur_arg++;
3421 }
3422 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003423 else if (!strcmp(args[0], "timeout")) {
3424 unsigned int *tv = NULL;
3425 const char *res;
3426 unsigned timeout;
3427
3428 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003429 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3430 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003431 err_code |= ERR_ALERT | ERR_FATAL;
3432 goto out;
3433 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003434 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3435 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003436 if (!strcmp(args[1], "hello"))
3437 tv = &curagent->timeout.hello;
3438 else if (!strcmp(args[1], "idle"))
3439 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003440 else if (!strcmp(args[1], "processing"))
3441 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003442 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003443 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3444 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 err_code |= ERR_ALERT | ERR_FATAL;
3446 goto out;
3447 }
3448 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003449 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3450 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 err_code |= ERR_ALERT | ERR_FATAL;
3452 goto out;
3453 }
3454 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3455 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003456 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3457 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003458 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 goto out;
3460 }
3461 *tv = MS_TO_TICKS(timeout);
3462 }
3463 else if (!strcmp(args[0], "option")) {
3464 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003465 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3466 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003467 err_code |= ERR_ALERT | ERR_FATAL;
3468 goto out;
3469 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003470
Christopher Faulet305c6072017-02-23 16:17:53 +01003471 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003472 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003473 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003474 if (kwm == 1)
3475 curagent->flags &= ~SPOE_FL_PIPELINING;
3476 else
3477 curagent->flags |= SPOE_FL_PIPELINING;
3478 goto out;
3479 }
3480 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003481 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003482 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003483 if (kwm == 1)
3484 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003485 else {
3486 if (global.nbthread == 1)
3487 curagent->flags |= SPOE_FL_ASYNC;
3488 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003489 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3490 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003491 err_code |= ERR_WARN;
3492 }
3493 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003494 goto out;
3495 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003496 else if (!strcmp(args[1], "send-frag-payload")) {
3497 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3498 goto out;
3499 if (kwm == 1)
3500 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3501 else
3502 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3503 goto out;
3504 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003505 else if (!strcmp(args[1], "dontlog-normal")) {
3506 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3507 goto out;
3508 if (kwm == 1)
3509 curpxopts2 &= ~PR_O2_NOLOGNORM;
3510 else
3511 curpxopts2 |= PR_O2_NOLOGNORM;
3512 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003513
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003514 /* Following options does not support negation */
3515 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3517 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003518 err_code |= ERR_ALERT | ERR_FATAL;
3519 goto out;
3520 }
3521
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003522 if (!strcmp(args[1], "var-prefix")) {
3523 char *tmp;
3524
3525 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003526 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3527 file, linenum, args[0],
3528 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003529 err_code |= ERR_ALERT | ERR_FATAL;
3530 goto out;
3531 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003532 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3533 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003534 tmp = args[2];
3535 while (*tmp) {
3536 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003537 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3538 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003539 err_code |= ERR_ALERT | ERR_FATAL;
3540 goto out;
3541 }
3542 tmp++;
3543 }
3544 curagent->var_pfx = strdup(args[2]);
3545 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003546 else if (!strcmp(args[1], "force-set-var")) {
3547 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3548 goto out;
3549 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3550 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003551 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003552 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003553 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003554 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3555 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003556 else if (!strcmp(args[1], "set-on-error")) {
3557 char *tmp;
3558
3559 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003560 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3561 file, linenum, args[0],
3562 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003563 err_code |= ERR_ALERT | ERR_FATAL;
3564 goto out;
3565 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003566 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3567 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003568 tmp = args[2];
3569 while (*tmp) {
3570 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003571 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3572 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003573 err_code |= ERR_ALERT | ERR_FATAL;
3574 goto out;
3575 }
3576 tmp++;
3577 }
3578 curagent->var_on_error = strdup(args[2]);
3579 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003580 else if (!strcmp(args[1], "set-process-time")) {
3581 char *tmp;
3582
3583 if (!*args[2]) {
3584 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3585 file, linenum, args[0],
3586 args[1]);
3587 err_code |= ERR_ALERT | ERR_FATAL;
3588 goto out;
3589 }
3590 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3591 goto out;
3592 tmp = args[2];
3593 while (*tmp) {
3594 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3595 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3596 file, linenum, args[0], args[1]);
3597 err_code |= ERR_ALERT | ERR_FATAL;
3598 goto out;
3599 }
3600 tmp++;
3601 }
3602 curagent->var_t_process = strdup(args[2]);
3603 }
3604 else if (!strcmp(args[1], "set-total-time")) {
3605 char *tmp;
3606
3607 if (!*args[2]) {
3608 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3609 file, linenum, args[0],
3610 args[1]);
3611 err_code |= ERR_ALERT | ERR_FATAL;
3612 goto out;
3613 }
3614 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3615 goto out;
3616 tmp = args[2];
3617 while (*tmp) {
3618 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3619 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3620 file, linenum, args[0], args[1]);
3621 err_code |= ERR_ALERT | ERR_FATAL;
3622 goto out;
3623 }
3624 tmp++;
3625 }
3626 curagent->var_t_total = strdup(args[2]);
3627 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003628 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003629 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3630 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003631 err_code |= ERR_ALERT | ERR_FATAL;
3632 goto out;
3633 }
Christopher Faulet48026722016-11-16 15:01:12 +01003634 }
3635 else if (!strcmp(args[0], "maxconnrate")) {
3636 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003637 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3638 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003642 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003643 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003644 curagent->cps_max = atol(args[1]);
3645 }
3646 else if (!strcmp(args[0], "maxerrrate")) {
3647 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003648 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3649 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003650 err_code |= ERR_ALERT | ERR_FATAL;
3651 goto out;
3652 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003653 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003654 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003655 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003656 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003657 else if (!strcmp(args[0], "max-frame-size")) {
3658 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003659 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3660 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003661 err_code |= ERR_ALERT | ERR_FATAL;
3662 goto out;
3663 }
3664 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3665 goto out;
3666 curagent->max_frame_size = atol(args[1]);
3667 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3668 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003669 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3670 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003671 err_code |= ERR_ALERT | ERR_FATAL;
3672 goto out;
3673 }
3674 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003675 else if (!strcmp(args[0], "max-waiting-frames")) {
3676 if (!*args[1]) {
3677 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3678 file, linenum, args[0]);
3679 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_fpa = atol(args[1]);
3685 if (curagent->max_fpa < 1) {
3686 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3687 file, linenum, args[0]);
3688 err_code |= ERR_ALERT | ERR_FATAL;
3689 goto out;
3690 }
3691 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003692 else if (!strcmp(args[0], "register-var-names")) {
3693 int cur_arg;
3694
3695 if (!*args[1]) {
3696 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3697 file, linenum, args[0]);
3698 err_code |= ERR_ALERT | ERR_FATAL;
3699 goto out;
3700 }
3701 cur_arg = 1;
3702 while (*args[cur_arg]) {
3703 struct spoe_var_placeholder *vph;
3704
3705 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3706 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3707 err_code |= ERR_ALERT | ERR_ABORT;
3708 goto out;
3709 }
3710 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3711 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3712 err_code |= ERR_ALERT | ERR_ABORT;
3713 goto out;
3714 }
3715 LIST_ADDQ(&curvars, &vph->list);
3716 cur_arg++;
3717 }
3718 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003719 else if (!strcmp(args[0], "log")) {
3720 char *errmsg = NULL;
3721
3722 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3723 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3724 err_code |= ERR_ALERT | ERR_FATAL;
3725 goto out;
3726 }
3727 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003728 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003729 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3730 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003731 err_code |= ERR_ALERT | ERR_FATAL;
3732 goto out;
3733 }
3734 out:
3735 return err_code;
3736}
Christopher Faulet11610f32017-09-21 10:23:10 +02003737static int
3738cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3739{
3740 struct spoe_group *grp;
3741 const char *err;
3742 int err_code = 0;
3743
3744 if ((cfg_scope == NULL && curengine != NULL) ||
3745 (cfg_scope != NULL && curengine == NULL) ||
3746 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3747 goto out;
3748
3749 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3750 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003751 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3752 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003753 err_code |= ERR_ALERT | ERR_ABORT;
3754 goto out;
3755 }
3756 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3757 err_code |= ERR_ABORT;
3758 goto out;
3759 }
3760
3761 err = invalid_char(args[1]);
3762 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003763 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3764 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003765 err_code |= ERR_ALERT | ERR_ABORT;
3766 goto out;
3767 }
3768
3769 list_for_each_entry(grp, &curgrps, list) {
3770 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003771 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3772 " name as another one declared at %s:%d.\n",
3773 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003774 err_code |= ERR_ALERT | ERR_FATAL;
3775 goto out;
3776 }
3777 }
3778
3779 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003780 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003781 err_code |= ERR_ALERT | ERR_ABORT;
3782 goto out;
3783 }
3784
3785 curgrp->id = strdup(args[1]);
3786 curgrp->conf.file = strdup(file);
3787 curgrp->conf.line = linenum;
3788 LIST_INIT(&curgrp->phs);
3789 LIST_INIT(&curgrp->messages);
3790 LIST_ADDQ(&curgrps, &curgrp->list);
3791 }
3792 else if (!strcmp(args[0], "messages")) {
3793 int cur_arg = 1;
3794 while (*args[cur_arg]) {
3795 struct spoe_placeholder *ph = NULL;
3796
3797 list_for_each_entry(ph, &curgrp->phs, list) {
3798 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003799 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3800 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003801 err_code |= ERR_ALERT | ERR_FATAL;
3802 goto out;
3803 }
3804 }
3805
3806 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003807 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003808 err_code |= ERR_ALERT | ERR_ABORT;
3809 goto out;
3810 }
3811 ph->id = strdup(args[cur_arg]);
3812 LIST_ADDQ(&curgrp->phs, &ph->list);
3813 cur_arg++;
3814 }
3815 }
3816 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003817 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3818 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003819 err_code |= ERR_ALERT | ERR_FATAL;
3820 goto out;
3821 }
3822 out:
3823 return err_code;
3824}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003825
3826static int
3827cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3828{
3829 struct spoe_message *msg;
3830 struct spoe_arg *arg;
3831 const char *err;
3832 char *errmsg = NULL;
3833 int err_code = 0;
3834
3835 if ((cfg_scope == NULL && curengine != NULL) ||
3836 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003837 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003838 goto out;
3839
3840 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3841 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003842 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3843 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003844 err_code |= ERR_ALERT | ERR_ABORT;
3845 goto out;
3846 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003847 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3848 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003849 goto out;
3850 }
3851
3852 err = invalid_char(args[1]);
3853 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003854 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3855 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003856 err_code |= ERR_ALERT | ERR_ABORT;
3857 goto out;
3858 }
3859
3860 list_for_each_entry(msg, &curmsgs, list) {
3861 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3863 " name as another one declared at %s:%d.\n",
3864 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003865 err_code |= ERR_ALERT | ERR_FATAL;
3866 goto out;
3867 }
3868 }
3869
3870 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003871 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003872 err_code |= ERR_ALERT | ERR_ABORT;
3873 goto out;
3874 }
3875
3876 curmsg->id = strdup(args[1]);
3877 curmsg->id_len = strlen(curmsg->id);
3878 curmsg->event = SPOE_EV_NONE;
3879 curmsg->conf.file = strdup(file);
3880 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003881 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003882 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003883 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003884 LIST_INIT(&curmsg->by_evt);
3885 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003886 LIST_ADDQ(&curmsgs, &curmsg->list);
3887 }
3888 else if (!strcmp(args[0], "args")) {
3889 int cur_arg = 1;
3890
3891 curproxy->conf.args.ctx = ARGC_SPOE;
3892 curproxy->conf.args.file = file;
3893 curproxy->conf.args.line = linenum;
3894 while (*args[cur_arg]) {
3895 char *delim = strchr(args[cur_arg], '=');
3896 int idx = 0;
3897
3898 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003899 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003900 err_code |= ERR_ALERT | ERR_ABORT;
3901 goto out;
3902 }
3903
3904 if (!delim) {
3905 arg->name = NULL;
3906 arg->name_len = 0;
3907 delim = args[cur_arg];
3908 }
3909 else {
3910 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3911 arg->name_len = delim - args[cur_arg];
3912 delim++;
3913 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003914 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3915 &idx, file, linenum, &errmsg,
3916 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003917 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003918 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919 err_code |= ERR_ALERT | ERR_FATAL;
3920 free(arg->name);
3921 free(arg);
3922 goto out;
3923 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003924 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003925 LIST_ADDQ(&curmsg->args, &arg->list);
3926 cur_arg++;
3927 }
3928 curproxy->conf.args.file = NULL;
3929 curproxy->conf.args.line = 0;
3930 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003931 else if (!strcmp(args[0], "acl")) {
3932 err = invalid_char(args[1]);
3933 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003934 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3935 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003936 err_code |= ERR_ALERT | ERR_FATAL;
3937 goto out;
3938 }
3939 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003940 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3941 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003942 err_code |= ERR_ALERT | ERR_FATAL;
3943 goto out;
3944 }
3945 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 else if (!strcmp(args[0], "event")) {
3947 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003948 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003949 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 goto out;
3951 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003952 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3953 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003954
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003955 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3956 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3957 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3958 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3959
3960 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3961 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3962 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3963 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3964 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3965 curmsg->event = SPOE_EV_ON_TCP_RSP;
3966
3967 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3968 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3969 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3970 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3971 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3972 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3973 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003974 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3975 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003976 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003977 goto out;
3978 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003979
3980 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3981 struct acl_cond *cond;
3982
3983 cond = build_acl_cond(file, linenum, &curmsg->acls,
3984 curproxy, (const char **)args+2,
3985 &errmsg);
3986 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003987 ha_alert("parsing [%s:%d] : error detected while "
3988 "parsing an 'event %s' condition : %s.\n",
3989 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003990 err_code |= ERR_ALERT | ERR_FATAL;
3991 goto out;
3992 }
3993 curmsg->cond = cond;
3994 }
3995 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003996 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3997 "or 'unless' followed by a condition but found '%s'.\n",
3998 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003999 err_code |= ERR_ALERT | ERR_FATAL;
4000 goto out;
4001 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004002 }
4003 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004004 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4005 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004006 err_code |= ERR_ALERT | ERR_FATAL;
4007 goto out;
4008 }
4009 out:
4010 free(errmsg);
4011 return err_code;
4012}
4013
4014/* Return -1 on error, else 0 */
4015static int
4016parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4017 struct flt_conf *fconf, char **err, void *private)
4018{
4019 struct list backup_sections;
4020 struct spoe_config *conf;
4021 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004022 struct spoe_group *grp, *grpback;
4023 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004024 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004025 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004026 char *file = NULL, *engine = NULL;
4027 int ret, pos = *cur_arg + 1;
4028
Christopher Faulet84c844e2018-03-23 14:37:14 +01004029 LIST_INIT(&curmsgs);
4030 LIST_INIT(&curgrps);
4031 LIST_INIT(&curmphs);
4032 LIST_INIT(&curgphs);
4033 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004034 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004035 curpxopts = 0;
4036 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004037
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004038 conf = calloc(1, sizeof(*conf));
4039 if (conf == NULL) {
4040 memprintf(err, "%s: out of memory", args[*cur_arg]);
4041 goto error;
4042 }
4043 conf->proxy = px;
4044
4045 while (*args[pos]) {
4046 if (!strcmp(args[pos], "config")) {
4047 if (!*args[pos+1]) {
4048 memprintf(err, "'%s' : '%s' option without value",
4049 args[*cur_arg], args[pos]);
4050 goto error;
4051 }
4052 file = args[pos+1];
4053 pos += 2;
4054 }
4055 else if (!strcmp(args[pos], "engine")) {
4056 if (!*args[pos+1]) {
4057 memprintf(err, "'%s' : '%s' option without value",
4058 args[*cur_arg], args[pos]);
4059 goto error;
4060 }
4061 engine = args[pos+1];
4062 pos += 2;
4063 }
4064 else {
4065 memprintf(err, "unknown keyword '%s'", args[pos]);
4066 goto error;
4067 }
4068 }
4069 if (file == NULL) {
4070 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4071 goto error;
4072 }
4073
4074 /* backup sections and register SPOE sections */
4075 LIST_INIT(&backup_sections);
4076 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004077 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4078 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004079 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004080
4081 /* Parse SPOE filter configuration file */
4082 curengine = engine;
4083 curproxy = px;
4084 curagent = NULL;
4085 curmsg = NULL;
4086 ret = readcfgfile(file);
4087 curproxy = NULL;
4088
4089 /* unregister SPOE sections and restore previous sections */
4090 cfg_unregister_sections();
4091 cfg_restore_sections(&backup_sections);
4092
4093 if (ret == -1) {
4094 memprintf(err, "Could not open configuration file %s : %s",
4095 file, strerror(errno));
4096 goto error;
4097 }
4098 if (ret & (ERR_ABORT|ERR_FATAL)) {
4099 memprintf(err, "Error(s) found in configuration file %s", file);
4100 goto error;
4101 }
4102
4103 /* Check SPOE agent */
4104 if (curagent == NULL) {
4105 memprintf(err, "No SPOE agent found in file %s", file);
4106 goto error;
4107 }
4108 if (curagent->b.name == NULL) {
4109 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4110 curagent->id, curagent->conf.file, curagent->conf.line);
4111 goto error;
4112 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004113 if (curagent->timeout.hello == TICK_ETERNITY ||
4114 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004115 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004116 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4117 " | While not properly invalid, you will certainly encounter various problems\n"
4118 " | with such a configuration. To fix this, please ensure that all following\n"
4119 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4120 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004121 }
4122 if (curagent->var_pfx == NULL) {
4123 char *tmp = curagent->id;
4124
4125 while (*tmp) {
4126 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4127 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4128 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4129 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4130 goto error;
4131 }
4132 tmp++;
4133 }
4134 curagent->var_pfx = strdup(curagent->id);
4135 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004136 if (curagent->engine_id == NULL)
4137 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004138
Christopher Fauletb7426d12018-03-21 14:12:17 +01004139 if (curagent->var_on_error) {
4140 struct arg arg;
4141
4142 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4143 curagent->var_pfx, curagent->var_on_error);
4144
4145 arg.type = ARGT_STR;
4146 arg.data.str.str = trash.str;
4147 arg.data.str.len = trash.len;
4148 if (!vars_check_arg(&arg, err)) {
4149 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4150 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4151 goto error;
4152 }
4153 }
4154
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004155 if (curagent->var_t_process) {
4156 struct arg arg;
4157
4158 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4159 curagent->var_pfx, curagent->var_t_process);
4160
4161 arg.type = ARGT_STR;
4162 arg.data.str.str = trash.str;
4163 arg.data.str.len = trash.len;
4164 if (!vars_check_arg(&arg, err)) {
4165 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4166 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4167 goto error;
4168 }
4169 }
4170
4171 if (curagent->var_t_total) {
4172 struct arg arg;
4173
4174 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4175 curagent->var_pfx, curagent->var_t_total);
4176
4177 arg.type = ARGT_STR;
4178 arg.data.str.str = trash.str;
4179 arg.data.str.len = trash.len;
4180 if (!vars_check_arg(&arg, err)) {
4181 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4182 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4183 goto error;
4184 }
4185 }
4186
Christopher Faulet11610f32017-09-21 10:23:10 +02004187 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004188 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4189 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004190 goto finish;
4191 }
4192
Christopher Faulet11610f32017-09-21 10:23:10 +02004193 /* Replace placeholders by the corresponding messages for the SPOE
4194 * agent */
4195 list_for_each_entry(ph, &curmphs, list) {
4196 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004197 struct spoe_arg *arg;
4198 unsigned int where;
4199
Christopher Faulet11610f32017-09-21 10:23:10 +02004200 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004201 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4202 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4203 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4204 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4205 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4206 }
4207 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4208 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4209 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004210 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4211 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004212 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004213 }
4214 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004215 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4216 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004217 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004218 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004219
4220 where = 0;
4221 switch (msg->event) {
4222 case SPOE_EV_ON_CLIENT_SESS:
4223 where |= SMP_VAL_FE_CON_ACC;
4224 break;
4225
4226 case SPOE_EV_ON_TCP_REQ_FE:
4227 where |= SMP_VAL_FE_REQ_CNT;
4228 break;
4229
4230 case SPOE_EV_ON_HTTP_REQ_FE:
4231 where |= SMP_VAL_FE_HRQ_HDR;
4232 break;
4233
4234 case SPOE_EV_ON_TCP_REQ_BE:
4235 if (px->cap & PR_CAP_FE)
4236 where |= SMP_VAL_FE_REQ_CNT;
4237 if (px->cap & PR_CAP_BE)
4238 where |= SMP_VAL_BE_REQ_CNT;
4239 break;
4240
4241 case SPOE_EV_ON_HTTP_REQ_BE:
4242 if (px->cap & PR_CAP_FE)
4243 where |= SMP_VAL_FE_HRQ_HDR;
4244 if (px->cap & PR_CAP_BE)
4245 where |= SMP_VAL_BE_HRQ_HDR;
4246 break;
4247
4248 case SPOE_EV_ON_SERVER_SESS:
4249 where |= SMP_VAL_BE_SRV_CON;
4250 break;
4251
4252 case SPOE_EV_ON_TCP_RSP:
4253 if (px->cap & PR_CAP_FE)
4254 where |= SMP_VAL_FE_RES_CNT;
4255 if (px->cap & PR_CAP_BE)
4256 where |= SMP_VAL_BE_RES_CNT;
4257 break;
4258
4259 case SPOE_EV_ON_HTTP_RSP:
4260 if (px->cap & PR_CAP_FE)
4261 where |= SMP_VAL_FE_HRS_HDR;
4262 if (px->cap & PR_CAP_BE)
4263 where |= SMP_VAL_BE_HRS_HDR;
4264 break;
4265
4266 default:
4267 break;
4268 }
4269
4270 list_for_each_entry(arg, &msg->args, list) {
4271 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004272 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004273 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004274 "none of which is available here ('%s')",
4275 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004276 sample_ckp_names(arg->expr->fetch->use),
4277 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004278 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004279 }
4280 }
4281
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004282 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004283 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4284 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004285 }
4286 }
4287 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004288 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004289 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004290 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004291 continue;
4292 }
4293
Christopher Faulet11610f32017-09-21 10:23:10 +02004294 /* Replace placeholders by the corresponding groups for the SPOE
4295 * agent */
4296 list_for_each_entry(ph, &curgphs, list) {
4297 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4298 if (!strcmp(grp->id, ph->id)) {
4299 grp->agent = curagent;
4300 LIST_DEL(&grp->list);
4301 LIST_ADDQ(&curagent->groups, &grp->list);
4302 goto next_aph;
4303 }
4304 }
4305 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4306 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4307 goto error;
4308 next_aph:
4309 continue;
4310 }
4311
4312 /* Replace placeholders by the corresponding message for each SPOE
4313 * group of the SPOE agent */
4314 list_for_each_entry(grp, &curagent->groups, list) {
4315 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4316 list_for_each_entry(msg, &curmsgs, list) {
4317 if (!strcmp(msg->id, ph->id)) {
4318 if (msg->group != NULL) {
4319 memprintf(err, "SPOE message '%s' already belongs to "
4320 "the SPOE group '%s' declare at %s:%d",
4321 msg->id, msg->group->id,
4322 msg->group->conf.file,
4323 msg->group->conf.line);
4324 goto error;
4325 }
4326
4327 /* Scope for arguments are not checked for now. We will check
4328 * them only if a rule use the corresponding SPOE group. */
4329 msg->agent = curagent;
4330 msg->group = grp;
4331 LIST_DEL(&ph->list);
4332 LIST_ADDQ(&grp->messages, &msg->by_grp);
4333 goto next_mph_grp;
4334 }
4335 }
4336 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4337 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4338 goto error;
4339 next_mph_grp:
4340 continue;
4341 }
4342 }
4343
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004344 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004345 /* move curmsgs to the agent message list */
4346 curmsgs.n->p = &curagent->messages;
4347 curmsgs.p->n = &curagent->messages;
4348 curagent->messages = curmsgs;
4349 LIST_INIT(&curmsgs);
4350
Christopher Faulet7ee86672017-09-19 11:08:28 +02004351 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004352 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004353
4354 /* Start agent's proxy initialization here. It will be finished during
4355 * the filter init. */
4356 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4357 init_new_proxy(&conf->agent_fe);
4358 conf->agent_fe.id = conf->agent->id;
4359 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004360 conf->agent_fe.options |= curpxopts;
4361 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004362
4363 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4364 LIST_DEL(&logsrv->list);
4365 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4366 }
4367
Christopher Faulet11610f32017-09-21 10:23:10 +02004368 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4369 LIST_DEL(&ph->list);
4370 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004371 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004372 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4373 LIST_DEL(&ph->list);
4374 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004375 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004376 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4377 struct arg arg;
4378
4379 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4380 curagent->var_pfx, vph->name);
4381
4382 arg.type = ARGT_STR;
4383 arg.data.str.str = trash.str;
4384 arg.data.str.len = trash.len;
4385 if (!vars_check_arg(&arg, err)) {
4386 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4387 curagent->id, curagent->var_pfx, vph->name, *err);
4388 goto error;
4389 }
4390
4391 LIST_DEL(&vph->list);
4392 free(vph->name);
4393 free(vph);
4394 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004395 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4396 LIST_DEL(&grp->list);
4397 spoe_release_group(grp);
4398 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004399 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004400 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004401 fconf->ops = &spoe_ops;
4402 fconf->conf = conf;
4403 return 0;
4404
4405 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004406 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004407 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4408 LIST_DEL(&ph->list);
4409 spoe_release_placeholder(ph);
4410 }
4411 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4412 LIST_DEL(&ph->list);
4413 spoe_release_placeholder(ph);
4414 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004415 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4416 LIST_DEL(&vph->list);
4417 free(vph->name);
4418 free(vph);
4419 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004420 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4421 LIST_DEL(&grp->list);
4422 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004423 }
4424 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4425 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004426 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004427 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004428 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4429 LIST_DEL(&logsrv->list);
4430 free(logsrv);
4431 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004432 free(conf);
4433 return -1;
4434}
4435
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004436/* Send message of a SPOE group. This is the action_ptr callback of a rule
4437 * associated to a "send-spoe-group" action.
4438 *
4439 * It returns ACT_RET_CONT is processing is finished without error, it returns
4440 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4441 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004442static enum act_return
4443spoe_send_group(struct act_rule *rule, struct proxy *px,
4444 struct session *sess, struct stream *s, int flags)
4445{
4446 struct filter *filter;
4447 struct spoe_agent *agent = NULL;
4448 struct spoe_group *group = NULL;
4449 struct spoe_context *ctx = NULL;
4450 int ret, dir;
4451
4452 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4453 if (filter->config == rule->arg.act.p[0]) {
4454 agent = rule->arg.act.p[2];
4455 group = rule->arg.act.p[3];
4456 ctx = filter->ctx;
4457 break;
4458 }
4459 }
4460 if (agent == NULL || group == NULL || ctx == NULL)
4461 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004462 if (ctx->state == SPOE_CTX_ST_NONE)
4463 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004464
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004465 switch (rule->from) {
4466 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4467 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4468 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4469 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4470 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4471 default:
4472 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4473 " - internal error while execute spoe-send-group\n",
4474 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4475 __FUNCTION__, s);
4476 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4477 agent->id);
4478 return ACT_RET_CONT;
4479 }
4480
4481 ret = spoe_process_group(s, ctx, group, dir);
4482 if (ret == 1)
4483 return ACT_RET_CONT;
4484 else if (ret == 0) {
4485 if (flags & ACT_FLAG_FINAL) {
4486 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4487 " - failed to process group '%s': interrupted by caller\n",
4488 (int)now.tv_sec, (int)now.tv_usec,
4489 agent->id, __FUNCTION__, s, group->id);
4490 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004491 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004492 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004493 return ACT_RET_CONT;
4494 }
4495 return ACT_RET_YIELD;
4496 }
4497 else
4498 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004499}
4500
4501/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4502 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4503 * action should be:
4504 *
4505 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4506 *
4507 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4508 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4509 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4510 * group.
4511 *
4512 * The function returns 1 in success case, otherwise, it returns 0 and err is
4513 * filled.
4514 */
4515static int
4516check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4517{
4518 struct flt_conf *fconf;
4519 struct spoe_config *conf;
4520 struct spoe_agent *agent = NULL;
4521 struct spoe_group *group;
4522 struct spoe_message *msg;
4523 char *engine_id = rule->arg.act.p[0];
4524 char *group_id = rule->arg.act.p[1];
4525 unsigned int where = 0;
4526
4527 switch (rule->from) {
4528 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4529 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4530 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4531 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4532 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4533 default:
4534 memprintf(err,
4535 "internal error, unexpected rule->from=%d, please report this bug!",
4536 rule->from);
4537 goto error;
4538 }
4539
4540 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4541 * <px> */
4542 list_for_each_entry(fconf, &px->filter_configs, list) {
4543 conf = fconf->conf;
4544
4545 /* This is not an SPOE filter */
4546 if (fconf->id != spoe_filter_id)
4547 continue;
4548
4549 /* This is the good engine */
4550 if (!strcmp(conf->id, engine_id)) {
4551 agent = conf->agent;
4552 break;
4553 }
4554 }
4555 if (agent == NULL) {
4556 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4557 engine_id, group_id);
4558 goto error;
4559 }
4560
4561 /* Try to find the right group */
4562 list_for_each_entry(group, &agent->groups, list) {
4563 /* This is the good group */
4564 if (!strcmp(group->id, group_id))
4565 break;
4566 }
4567 if (&group->list == &agent->groups) {
4568 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4569 group_id, engine_id);
4570 goto error;
4571 }
4572
4573 /* Ok, we found the group, we need to check messages and their
4574 * arguments */
4575 list_for_each_entry(msg, &group->messages, by_grp) {
4576 struct spoe_arg *arg;
4577
4578 list_for_each_entry(arg, &msg->args, list) {
4579 if (!(arg->expr->fetch->val & where)) {
4580 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4581 "some args extract information from '%s',"
4582 "none of which is available here ('%s')",
4583 msg->id, group->id, msg->conf.file, msg->conf.line,
4584 sample_ckp_names(arg->expr->fetch->use),
4585 sample_ckp_names(where));
4586 goto error;
4587 }
4588 }
4589 }
4590
4591 free(engine_id);
4592 free(group_id);
4593 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4594 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4595 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4596 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4597 return 1;
4598
4599 error:
4600 free(engine_id);
4601 free(group_id);
4602 return 0;
4603}
4604
4605/* Parse 'send-spoe-group' action following the format:
4606 *
4607 * ... send-spoe-group <engine-id> <group-id>
4608 *
4609 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4610 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4611 * ids are saved and used later, when the rule will be checked.
4612 */
4613static enum act_parse_ret
4614parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4615 struct act_rule *rule, char **err)
4616{
4617 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4618 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4619 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4620 return ACT_RET_PRS_ERR;
4621 }
4622 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4623 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4624
4625 (*orig_arg) += 2;
4626
4627 rule->action = ACT_CUSTOM;
4628 rule->action_ptr = spoe_send_group;
4629 rule->check_ptr = check_send_spoe_group;
4630 return ACT_RET_PRS_OK;
4631}
4632
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004633
4634/* Declare the filter parser for "spoe" keyword */
4635static struct flt_kw_list flt_kws = { "SPOE", { }, {
4636 { "spoe", parse_spoe_flt, NULL },
4637 { NULL, NULL, NULL },
4638 }
4639};
4640
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004641/* Delcate the action parser for "spoe-action" keyword */
4642static struct action_kw_list tcp_req_action_kws = { { }, {
4643 { "send-spoe-group", parse_send_spoe_group },
4644 { /* END */ },
4645 }
4646};
4647static struct action_kw_list tcp_res_action_kws = { { }, {
4648 { "send-spoe-group", parse_send_spoe_group },
4649 { /* END */ },
4650 }
4651};
4652static struct action_kw_list http_req_action_kws = { { }, {
4653 { "send-spoe-group", parse_send_spoe_group },
4654 { /* END */ },
4655 }
4656};
4657static struct action_kw_list http_res_action_kws = { { }, {
4658 { "send-spoe-group", parse_send_spoe_group },
4659 { /* END */ },
4660 }
4661};
4662
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004663__attribute__((constructor))
4664static void __spoe_init(void)
4665{
4666 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004667 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4668 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4669 http_req_keywords_register(&http_req_action_kws);
4670 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004671
Willy Tarreaubafbe012017-11-24 17:34:44 +01004672 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4673 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004674}
4675
4676__attribute__((destructor))
4677static void
4678__spoe_deinit(void)
4679{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004680 pool_destroy(pool_head_spoe_ctx);
4681 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004682}