blob: 856050d07d28515af91d25adcf646cfb43a4a38e [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 */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200402 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 memcpy(p, (char *)&flags, 4);
404 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
406 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
410 * and "capabilities" */
411
412 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(SUPPORTED_VERSIONS_KEY);
414 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
415 goto too_big;
416
417 *p++ = SPOE_DATA_T_STR;
418 sz = SLEN(SUPPORTED_VERSIONS_VAL);
419 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
420 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200421
422 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100423 sz = SLEN(MAX_FRAME_SIZE_KEY);
424 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
425 goto too_big;
426
427 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200428 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
431 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 sz = SLEN(CAPABILITIES_KEY);
433 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
434 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100437 chk = get_trash_chunk();
438 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
439 memcpy(chk->str, "pipelining", 10);
440 chk->len += 10;
441 }
442 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
443 if (chk->len) chk->str[chk->len++] = ',';
444 memcpy(chk->str+chk->len, "async", 5);
445 chk->len += 5;
446 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100447 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
448 if (chk->len) chk->str[chk->len++] = ',';
449 memcpy(chk->str+chk->len, "fragmentation", 13);
450 chk->len += 5;
451 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100452 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453 goto too_big;
454
455 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100456 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 sz = SLEN(ENGINE_ID_KEY);
458 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
459 goto too_big;
460
461 *p++ = SPOE_DATA_T_STR;
462 sz = strlen(agent->engine_id);
463 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
464 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100465 }
466
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 return (p - frame);
468
469 too_big:
470 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
471 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200472}
473
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
475 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
476 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 const char *reason;
481 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100482 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 p = frame;
486 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200487
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 /* Set Frame type */
489 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100491 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200492 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 memcpy(p, (char *)&flags, 4);
494 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
496 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 *p++ = 0; *p++ = 0;
498
499 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
500 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501
502 /* There are 2 mandatory items: "status-code" and "message" */
503
504 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 sz = SLEN(STATUS_CODE_KEY);
506 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
507 goto too_big;
508
509 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200510 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512
513 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514 sz = SLEN(MSG_KEY);
515 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
516 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 /*Get the message corresponding to the status code */
519 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
520
521 *p++ = SPOE_DATA_T_STR;
522 sz = strlen(reason);
523 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
524 goto too_big;
525
526 return (p - frame);
527
528 too_big:
529 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
530 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200531}
532
Christopher Faulet8ef75252017-02-20 22:56:03 +0100533/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
534 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
535 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100537spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100538 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200539{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100540 char *p, *end;
541 unsigned int stream_id, frame_id;
542 unsigned int flags = SPOE_FRM_FL_FIN;
543 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200544
Christopher Faulet8ef75252017-02-20 22:56:03 +0100545 p = frame;
546 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200547
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 stream_id = ctx->stream_id;
549 frame_id = ctx->frame_id;
550
551 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
552 /* The fragmentation is not supported by the applet */
553 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
554 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
555 return -1;
556 }
557 flags = ctx->frag_ctx.flags;
558 }
559
560 /* Set Frame type */
561 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
562
563 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200564 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100565 memcpy(p, (char *)&flags, 4);
566 p += 4;
567
568 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200569 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100570 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200571 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100572 goto too_big;
573
574 /* Copy encoded messages, if possible */
575 sz = ctx->buffer->i;
576 if (p + sz >= end)
577 goto too_big;
578 memcpy(p, ctx->buffer->p, sz);
579 p += sz;
580
581 return (p - frame);
582
583 too_big:
584 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
585 return 0;
586}
587
588/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
589 * returns the number of encoded bytes in the frame on success, 0 if an encoding
590 * error occurred and -1 if a fatal error occurred. */
591static int
592spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
593 char *frame, size_t size)
594{
595 char *p, *end;
596 unsigned int stream_id, frame_id;
597 unsigned int flags;
598 size_t sz;
599
600 p = frame;
601 end = frame+size;
602
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 /* <ctx> is null when the stream has aborted the processing of a
604 * fragmented frame. In this case, we must notify the corresponding
605 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100606 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100607 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
609 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
610 }
611 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 stream_id = ctx->stream_id;
614 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100615 }
616
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100618 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100620 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200621 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 memcpy(p, (char *)&flags, 4);
623 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200624
625 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200626 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200628 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100631 if (ctx == NULL)
632 goto end;
633
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100635 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 if (p + sz >= end)
637 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100638 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100640
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100641 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 return (p - frame);
643
644 too_big:
645 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
646 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647}
648
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649/* Decode and process the HELLO frame sent by an agent. It returns the number of
650 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
651 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200652static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654{
Christopher Faulet305c6072017-02-23 16:17:53 +0100655 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
656 char *p, *end;
657 int vsn, max_frame_size;
658 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659
660 p = frame;
661 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662
663 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 if (size < 7 /* TYPE + METADATA */) {
670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
671 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672 }
673
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100674 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200676 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100679 /* Fragmentation is not supported for HELLO frame */
680 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100682 return -1;
683 }
684
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 if (*p != 0 || *(p+1) != 0) {
687 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
688 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691
692 /* There are 3 mandatory items: "version", "max-frame-size" and
693 * "capabilities" */
694
695 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100696 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 while (p < end) {
698 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200699 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701
702 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 ret = spoe_decode_buffer(&p, end, &str, &sz);
704 if (ret == -1 || !sz) {
705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
706 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 /* Check "version" K/V item */
710 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 int i, type = *p++;
712
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
716 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100718 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
719 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
720 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 }
722
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 return -1;
727 }
728 for (i = 0; supported_versions[i].str != NULL; ++i) {
729 if (vsn >= supported_versions[i].min &&
730 vsn <= supported_versions[i].max)
731 break;
732 }
733 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 }
738 /* Check "max-frame-size" K/V item */
739 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200741
742 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
744 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
745 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
746 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
748 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200749 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200750 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
752 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200753 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 if (sz < MIN_FRAME_SIZE ||
755 sz > SPOE_APPCTX(appctx)->max_frame_size) {
756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200757 return -1;
758 }
759 max_frame_size = sz;
760 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761 /* Check "capabilities" K/V item */
762 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764
765 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
767 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
768 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
771 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
772 return 0;
773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 char *delim;
777
778 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
782 str += 10; sz -= 10;
783 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 flags |= SPOE_APPCTX_FL_PIPELINING;
785 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 else if (sz >= 5 && !strncmp(str, "async", 5)) {
787 str += 5; sz -= 5;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 flags |= SPOE_APPCTX_FL_ASYNC;
790 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
792 str += 13; sz -= 13;
793 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100794 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
795 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100796
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 /* Get the next comma or break */
798 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100799 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 delim++;
801 sz -= (delim - str);
802 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100803 }
804 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 else {
806 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 if (spoe_skip_data(&p, end) == -1) {
808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
809 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200811 }
812 }
813
814 /* Final checks */
815 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200817 return -1;
818 }
819 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100820 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200821 return -1;
822 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100823 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
824 flags &= ~SPOE_APPCTX_FL_PIPELINING;
825 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
826 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827
Christopher Faulet42bfa462017-01-04 14:14:19 +0100828 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
829 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
830 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831
832 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833}
834
835/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
836 * bytes on success, 0 if the frame can be ignored and -1 if an error
837 * occurred. */
838static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100842 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843
844 p = frame;
845 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200846
847 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
849 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200850 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 if (size < 7 /* TYPE + METADATA */) {
854 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
855 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 }
857
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100858 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200860 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100863 /* Fragmentation is not supported for DISCONNECT frame */
864 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100866 return -1;
867 }
868
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 if (*p != 0 || *(p+1) != 0) {
871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
872 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875
876 /* There are 2 mandatory items: "status-code" and "message" */
877
878 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 while (p < end) {
880 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200881 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883
884 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 ret = spoe_decode_buffer(&p, end, &str, &sz);
886 if (ret == -1 || !sz) {
887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
890
891 /* Check "status-code" K/V item */
892 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100893 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200894
895 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
897 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
898 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
899 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200903 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
909
910 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911 else if (!memcmp(str, MSG_KEY, sz)) {
912 int type = *p++;
913
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100915 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
916 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
917 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100919 ret = spoe_decode_buffer(&p, end, &str, &sz);
920 if (ret == -1 || sz > 255) {
921 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
922 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
925 SPOE_APPCTX(appctx)->reason = str;
926 SPOE_APPCTX(appctx)->rlen = sz;
927#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 }
929 else {
930 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 if (spoe_skip_data(&p, end) == -1) {
932 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
933 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200934 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935 }
936 }
937
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939}
940
941
Christopher Fauleta1cda022016-12-21 08:58:06 +0100942/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 * success, 0 if the frame can be ignored and -1 if an error occurred. */
944static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100946 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100950 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100952 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953
954 p = frame;
955 end = frame + size;
956 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
958 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200961 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 if (size < 7 /* TYPE + METADATA */) {
965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
966 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200967 }
968
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100969 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200971 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100974 /* Fragmentation is not supported for now */
975 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100976 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100977 return -1;
978 }
979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200981 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100982 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100983 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200985 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200987 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100989
Christopher Faulet8ef75252017-02-20 22:56:03 +0100990 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100991 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200992 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100993 if ((*ctx)->stream_id == (unsigned int)stream_id &&
994 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 goto found;
996 }
997 }
998 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100999 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1000 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001001 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001002 goto found;
1003 }
1004 }
1005
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001006 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1007 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1008 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1009
1010 /* ABRT bit is set for an unfinished fragmented frame */
1011 if (flags & SPOE_FRM_FL_ABRT) {
1012 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001013 (*ctx)->state = SPOE_CTX_ST_ERROR;
1014 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1015 /* Ignore the payload */
1016 goto end;
1017 }
1018 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1019 /* For now, we ignore the ack */
1020 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1021 return 0;
1022 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001023
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001025 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1026 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001027 " - stream-id=%u - frame-id=%u\n",
1028 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1029 __FUNCTION__, appctx,
1030 (unsigned int)stream_id, (unsigned int)frame_id);
1031
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001032 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001033 return 0;
1034
1035 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1037 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001038 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001039 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001042 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 len = (end - p);
1044 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1045 SPOE_APPCTX(appctx)->buffer->i = len;
1046 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001047
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 /* Transfer the buffer ownership to the SPOE context */
1049 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1050 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001051
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 (*ctx)->state = SPOE_CTX_ST_DONE;
1053
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001054 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001055 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 " - ACK frame received"
1057 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1060 (*ctx)->frame_id, flags);
1061 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001062}
1063
Christopher Fauletba7bc162016-11-07 21:07:38 +01001064/* This function is used in cfgparse.c and declared in proto/checks.h. It
1065 * prepare the request to send to agents during a healthcheck. It returns 0 on
1066 * success and -1 if an error occurred. */
1067int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001068spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 struct appctx appctx;
1071 struct spoe_appctx spoe_appctx;
1072 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1073 size_t sz;
1074 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet42bfa462017-01-04 14:14:19 +01001076 memset(&appctx, 0, sizeof(appctx));
1077 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001079
1080 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001081 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1084 end = frame + MAX_FRAME_SIZE;
1085
1086 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1087 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001089 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 /* Add "healthcheck" K/V item */
1092 sz = SLEN(HEALTHCHECK_KEY);
1093 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1094 return -1;
1095 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 *len = frame - buf;
1098 sz = htonl(*len - 4);
1099 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104 return 0;
1105}
1106
1107/* This function is used in checks.c and declared in proto/checks.h. It decode
1108 * the response received from an agent during a healthcheck. It returns 0 on
1109 * success and -1 if an error occurred. */
1110int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001113 struct appctx appctx;
1114 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115
Christopher Faulet42bfa462017-01-04 14:14:19 +01001116 memset(&appctx, 0, sizeof(appctx));
1117 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118
Christopher Faulet42bfa462017-01-04 14:14:19 +01001119 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001120 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001121
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1123 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124 goto error;
1125 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1127 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128
1129 return 0;
1130
1131 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001132 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1133 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1134 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001135 return -1;
1136}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001137
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1139 * the frame can be ignored, 1 to retry later, and the frame legnth on
1140 * success. */
1141static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143{
1144 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001145 int ret;
1146 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1149 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 netint = htonl(framesz);
1151 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001152 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001154 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001155 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 }
1158 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 return -1; /* error */
1160 }
1161 return framesz;
1162}
1163
1164/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1165 * when the frame can be ignored, 1 to retry later and the frame length on
1166 * success. */
1167static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169{
1170 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 int ret;
1172 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 if (ret > 0) {
1176 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001177 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return -1;
1180 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001181 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 }
1183 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001184 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186 }
1187 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001188 return -1; /* error */
1189 }
1190 return framesz;
1191}
1192
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001193/********************************************************************
1194 * Functions that manage the SPOE applet
1195 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001196static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198{
1199 si_applet_want_get(appctx->owner);
1200 si_applet_want_put(appctx->owner);
1201 appctx_wakeup(appctx);
1202 return 1;
1203}
1204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205/* Callback function that catches applet timeouts. If a timeout occurred, we set
1206 * <appctx->st1> flag and the SPOE applet is woken up. */
1207static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001208spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001210 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211
1212 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1213 if (tick_is_expired(task->expire, now_ms)) {
1214 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218 return task;
1219}
1220
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221/* Callback function that releases a SPOE applet. This happens when the
1222 * connection with the agent is closed. */
1223static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 struct stream_interface *si = appctx->owner;
1227 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1228 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230
1231 if (spoe_appctx == NULL)
1232 return;
1233
1234 appctx->ctx.spoe.ptr = NULL;
1235 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236
1237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1238 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1239 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001242 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001243 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1245 LIST_DEL(&spoe_appctx->list);
1246 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001248 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001252 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1253 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001254 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001255 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001256
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001257 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1259 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001260
1261 si_shutw(si);
1262 si_shutr(si);
1263 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264 }
1265
Christopher Faulet8ef75252017-02-20 22:56:03 +01001266 /* Destroy the task attached to this applet */
1267 if (spoe_appctx->task) {
1268 task_delete(spoe_appctx->task);
1269 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 }
1271
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 /* Notify all waiting streams */
1273 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 LIST_DEL(&ctx->list);
1275 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001276 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001277 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001278 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001279 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001280 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001281 }
1282
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 /* If the applet was processing a fragmented frame, notify the
1284 * corresponding stream. */
1285 if (spoe_appctx->frag_ctx.ctx) {
1286 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001287 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001288 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001290 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1291 }
1292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* Release allocated memory */
1294 spoe_release_buffer(&spoe_appctx->buffer,
1295 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001296 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001297
Christopher Faulet24289f22017-09-25 14:48:02 +02001298 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001299 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001302 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001303 LIST_DEL(&ctx->list);
1304 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001305 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001306 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001308 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001310 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001311 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 LIST_DEL(&ctx->list);
1313 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001314 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001315 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001317 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001318 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1319 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001320
1321 end:
1322 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001323 agent->rt[tid].frame_size = agent->max_frame_size;
1324 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1325 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001326}
1327
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001328static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001329spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001332 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001333 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001334 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001335
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 if (si->state <= SI_ST_CON) {
1337 si_applet_want_put(si);
1338 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1339 goto stop;
1340 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001341 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001344 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001345
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001347 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1348 " - Connection timed out\n",
1349 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1350 __FUNCTION__, appctx);
1351 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001353 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001354
Christopher Faulet42bfa462017-01-04 14:14:19 +01001355 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001356 SPOE_APPCTX(appctx)->task->expire =
1357 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1360 * length. */
1361 buf = trash.str; frame = buf+4;
1362 ret = spoe_prepare_hahello_frame(appctx, frame,
1363 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366
1367 switch (ret) {
1368 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 case 0: /* ignore => an error, cannot be ignored */
1370 goto exit;
1371
1372 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 goto stop;
1374
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 default:
1376 /* HELLO frame successfully sent, now wait for the
1377 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1379 goto next;
1380 }
1381
1382 next:
1383 return 0;
1384 stop:
1385 return 1;
1386 exit:
1387 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1388 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389}
1390
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001391static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001392spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001395 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396 char *frame;
1397 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399
Christopher Fauletb067b062017-01-04 16:39:11 +01001400 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001403 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001404
Christopher Fauleta1cda022016-12-21 08:58:06 +01001405 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1407 " - Connection timed out\n",
1408 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1409 __FUNCTION__, appctx);
1410 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 goto exit;
1412 }
1413
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 frame = trash.str; trash.len = 0;
1415 ret = spoe_recv_frame(appctx, frame,
1416 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001417 if (ret > 1) {
1418 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1419 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1420 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001421 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 trash.len = ret + 4;
1423 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001424 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001425
Christopher Fauleta1cda022016-12-21 08:58:06 +01001426 switch (ret) {
1427 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001428 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1430 goto next;
1431
1432 case 1: /* retry later */
1433 goto stop;
1434
1435 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001436 /* HELLO handshake is finished, set the idle timeout and
1437 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001438 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001440 SPOE_APPCTX(appctx)->node.key = 0;
1441 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001442
1443 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001444 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001445 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001447
Christopher Fauleta1cda022016-12-21 08:58:06 +01001448 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001449 /* Do not forget to remove processed frame from the output buffer */
1450 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001451 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452
1453 SPOE_APPCTX(appctx)->task->expire =
1454 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455 return 0;
1456 stop:
1457 return 1;
1458 exit:
1459 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1460 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001461}
1462
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463
Christopher Fauleta1cda022016-12-21 08:58:06 +01001464static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001465spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001467 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1468 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001469 char *frame, *buf;
1470 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001471
Christopher Faulet8ef75252017-02-20 22:56:03 +01001472 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1473 * length. */
1474 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475
1476 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1477 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1478 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1479 SPOE_APPCTX(appctx)->max_frame_size);
1480 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001481 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001482 *skip = 1;
1483 ret = 1;
1484 goto end;
1485 }
1486 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001487 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001488 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1489 SPOE_APPCTX(appctx)->max_frame_size);
1490
1491 }
1492
Christopher Faulet8ef75252017-02-20 22:56:03 +01001493 if (ret > 1)
1494 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001495
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 switch (ret) {
1497 case -1: /* error */
1498 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1499 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001500
Christopher Faulet8ef75252017-02-20 22:56:03 +01001501 case 0: /* ignore */
1502 if (ctx == NULL)
1503 goto abort_frag_frame;
1504
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001505 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 LIST_DEL(&ctx->list);
1507 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001508 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001509 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001510 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 ctx->state = SPOE_CTX_ST_ERROR;
1512 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1513 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001514 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 break;
1516
1517 case 1: /* retry */
1518 *skip = 1;
1519 break;
1520
1521 default:
1522 if (ctx == NULL)
1523 goto abort_frag_frame;
1524
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001525 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001526 LIST_DEL(&ctx->list);
1527 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001528 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001529 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001530 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1532 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1533 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001534 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001535 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 }
1537 goto end;
1538
1539 frag_frame_sent:
1540 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001541 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1543 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1544 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1546 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1547 goto end;
1548
1549 no_frag_frame_sent:
1550 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1551 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001552 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001553 }
1554 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1555 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1556 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1557 }
1558 else {
1559 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001560 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001561 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1562 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001563 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001564 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001565 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1566 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1567 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001568 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001569
Christopher Faulet8ef75252017-02-20 22:56:03 +01001570 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1571 goto end;
1572
1573 abort_frag_frame:
1574 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1575 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1576 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1577 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1578 goto end;
1579
1580 end:
1581 return ret;
1582}
1583
1584static int
1585spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1586{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001587 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 struct spoe_context *ctx = NULL;
1589 char *frame;
1590 int ret;
1591
1592 frame = trash.str; trash.len = 0;
1593 ret = spoe_recv_frame(appctx, frame,
1594 SPOE_APPCTX(appctx)->max_frame_size);
1595 if (ret > 1) {
1596 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1597 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001598 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001599 goto end;
1600 }
1601 trash.len = ret + 4;
1602 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1603 }
1604 switch (ret) {
1605 case -1: /* error */
1606 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1607 break;
1608
1609 case 0: /* ignore */
1610 break;
1611
1612 case 1: /* retry */
1613 *skip = 1;
1614 break;
1615
1616 default:
1617 LIST_DEL(&ctx->list);
1618 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001619 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001620 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1621 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001622 if (ctx->spoe_appctx) {
1623 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001624 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001625 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001626 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1627 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1628 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1629 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1630 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1631 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1632 }
1633 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1634 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001635 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1636 break;
1637 }
1638
1639 /* Do not forget to remove processed frame from the output buffer */
1640 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001641 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001642 end:
1643 return ret;
1644}
1645
1646static int
1647spoe_handle_processing_appctx(struct appctx *appctx)
1648{
1649 struct stream_interface *si = appctx->owner;
1650 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001651 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652
1653 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1655 goto exit;
1656 }
1657
1658 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1660 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1661 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1662 goto next;
1663 }
1664
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001665 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001666 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001667 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001668 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1669 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001670 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001671
Christopher Faulet8f82b202018-01-24 16:23:03 +01001672 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1673 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 /* receiving_frame loop */
1676 while (!skip_receiving) {
1677 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1678 switch (ret) {
1679 case -1: /* error */
1680 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001681
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 case 0: /* ignore */
1683 active_r = 1;
1684 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001685
Christopher Faulet8f82b202018-01-24 16:23:03 +01001686 case 1: /* retry */
1687 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 default:
1690 active_r = 1;
1691 break;
1692 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001694
Christopher Faulet8f82b202018-01-24 16:23:03 +01001695 /* send_frame loop */
1696 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1697 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1698 switch (ret) {
1699 case -1: /* error */
1700 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701
Christopher Faulet8f82b202018-01-24 16:23:03 +01001702 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001703 if (SPOE_APPCTX(appctx)->node.key)
1704 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 active_s++;
1706 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 case 1: /* retry */
1709 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001712 if (SPOE_APPCTX(appctx)->node.key)
1713 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001714 active_s++;
1715 break;
1716 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001718
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001720 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1721 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1722 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001723
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001725 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001727 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728 }
1729 return 1;
1730
Christopher Faulet8f82b202018-01-24 16:23:03 +01001731 next:
1732 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1733 return 0;
1734
Christopher Fauleta1cda022016-12-21 08:58:06 +01001735 exit:
1736 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1737 return 0;
1738}
1739
1740static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001741spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742{
1743 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001744 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745 char *frame, *buf;
1746 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001747
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1749 goto exit;
1750
1751 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1752 goto exit;
1753
Christopher Faulet8ef75252017-02-20 22:56:03 +01001754 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1755 * length. */
1756 buf = trash.str; frame = buf+4;
1757 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1758 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001759 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761
1762 switch (ret) {
1763 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 goto exit;
1766
1767 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001768 goto stop;
1769
1770 default:
1771 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1772 " - disconnected by HAProxy (%d): %s\n",
1773 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001774 __FUNCTION__, appctx,
1775 SPOE_APPCTX(appctx)->status_code,
1776 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001777
1778 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1779 goto next;
1780 }
1781
1782 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783 SPOE_APPCTX(appctx)->task->expire =
1784 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785 return 0;
1786 stop:
1787 return 1;
1788 exit:
1789 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1790 return 0;
1791}
1792
1793static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795{
1796 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001797 char *frame;
1798 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799
Christopher Fauletb067b062017-01-04 16:39:11 +01001800 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001803 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804
Christopher Fauletb067b062017-01-04 16:39:11 +01001805 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001808 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 frame = trash.str; trash.len = 0;
1811 ret = spoe_recv_frame(appctx, frame,
1812 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 trash.len = ret + 4;
1815 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 }
1817
1818 switch (ret) {
1819 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1821 " - error on frame (%s)\n",
1822 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001823 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 goto exit;
1827
1828 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 goto next;
1830
1831 case 1: /* retry */
1832 goto stop;
1833
1834 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001838 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001839 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1840 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 goto exit;
1842 }
1843
1844 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 /* Do not forget to remove processed frame from the output buffer */
1846 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001847 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 return 0;
1850 stop:
1851 return 1;
1852 exit:
1853 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1854 return 0;
1855}
1856
1857/* I/O Handler processing messages exchanged with the agent */
1858static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001859spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001861 struct stream_interface *si = appctx->owner;
1862 struct spoe_agent *agent;
1863
1864 if (SPOE_APPCTX(appctx) == NULL)
1865 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001866
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1868 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001869
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870 switchstate:
1871 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1872 " - appctx-state=%s\n",
1873 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1874 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1875
1876 switch (appctx->st0) {
1877 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001879 goto out;
1880 goto switchstate;
1881
1882 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 goto out;
1885 goto switchstate;
1886
1887 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001888 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001889 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001891 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001892 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 SPOE_APPCTX(appctx)->task->expire =
1894 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001896 goto switchstate;
1897 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1899 /* fall through */
1900
1901 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001902 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1903 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001905 goto out;
1906 goto switchstate;
1907
1908 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001909 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 goto out;
1911 goto switchstate;
1912
1913 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 goto out;
1916 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001917
1918 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 appctx->st0 = SPOE_APPCTX_ST_END;
1920 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1921
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922 si_shutw(si);
1923 si_shutr(si);
1924 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925 /* fall through */
1926
1927 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001928 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001930 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001931 if (stopping)
1932 spoe_wakeup_appctx(appctx);
1933
Christopher Faulet42bfa462017-01-04 14:14:19 +01001934 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1935 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 si_oc(si)->flags |= CF_READ_DONTWAIT;
1937 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938}
1939
1940struct applet spoe_applet = {
1941 .obj_type = OBJ_TYPE_APPLET,
1942 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001943 .fct = spoe_handle_appctx,
1944 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945};
1946
1947/* Create a SPOE applet. On success, the created applet is returned, else
1948 * NULL. */
1949static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001950spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951{
1952 struct appctx *appctx;
1953 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001954 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955
Emeric Brun1138fd02017-06-19 12:38:55 +02001956 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957 goto out_error;
1958
Willy Tarreaubafbe012017-11-24 17:34:44 +01001959 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001960 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001962 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001963
Christopher Faulet42bfa462017-01-04 14:14:19 +01001964 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001965 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001966 goto out_free_spoe_appctx;
1967
1968 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001969 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001970 SPOE_APPCTX(appctx)->task->context = appctx;
1971 SPOE_APPCTX(appctx)->agent = conf->agent;
1972 SPOE_APPCTX(appctx)->version = 0;
1973 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1974 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001975 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001976 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001977 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001978
1979 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1980 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001981 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001982
1983 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1984 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985
Willy Tarreau5820a362016-12-22 15:59:02 +01001986 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987 if (!sess)
1988 goto out_free_spoe;
1989
Willy Tarreau87787ac2017-08-28 16:22:54 +02001990 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991 goto out_free_sess;
1992
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993 stream_set_backend(strm, conf->agent->b.be);
1994
1995 /* applet is waiting for data */
1996 si_applet_cant_get(&strm->si[0]);
1997 appctx_wakeup(appctx);
1998
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 strm->do_log = NULL;
2000 strm->res.flags |= CF_READ_DONTWAIT;
2001
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002002 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002003 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002004 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002005 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002006
Emeric Brunc60def82017-09-27 14:59:38 +02002007 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002008 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009 return appctx;
2010
2011 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012 out_free_sess:
2013 session_free(sess);
2014 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002015 task_free(SPOE_APPCTX(appctx)->task);
2016 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002017 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 out_free_appctx:
2019 appctx_free(appctx);
2020 out_error:
2021 return NULL;
2022}
2023
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002025spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026{
2027 struct spoe_config *conf = FLT_CONF(ctx->filter);
2028 struct spoe_agent *agent = conf->agent;
2029 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002030 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002031
Christopher Fauleta1cda022016-12-21 08:58:06 +01002032 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002033 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002034 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036
2037 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002039 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2040 ctx->strm);
2041
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 /* Do not try to create a new applet if there is no server up for the
2043 * agent's backend. */
2044 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2045 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2046 " - cannot create SPOE appctx: no server up\n",
2047 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2048 __FUNCTION__, ctx->strm);
2049 goto end;
2050 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 /* Do not try to create a new applet if we have reached the maximum of
2053 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002054 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002055 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002056 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2057 " - cannot create SPOE appctx: max CPS reached\n",
2058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2059 __FUNCTION__, ctx->strm);
2060 goto end;
2061 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002062 }
2063
Christopher Faulet8ef75252017-02-20 22:56:03 +01002064 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 if (appctx == NULL) {
2066 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2067 " - failed to create SPOE appctx\n",
2068 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2069 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002070 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002071 "SPOE: [%s] failed to create SPOE applet\n",
2072 agent->id);
2073
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002075 }
2076
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 /* Increase the per-process number of cumulated connections */
2078 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002079 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 end:
2082 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002083 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002084 ctx->status_code = SPOE_CTX_ERR_RES;
2085 return -1;
2086 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002088 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002089 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002090 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002091 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2092 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093
2094 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002095 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002096 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002097 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002098 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002099 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002100
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002101 /* Finally try to wakeup an IDLE applet. */
2102 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2103 struct eb32_node *node;
2104
2105 node = eb32_first(&agent->rt[tid].idle_applets);
2106 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2107 if (node && spoe_appctx) {
2108 eb32_delete(&spoe_appctx->node);
2109 spoe_appctx->node.key++;
2110 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2111 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002112 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002113 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002114 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002115}
2116
2117/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002118 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002119 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002120/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2121 * fragmented_content. If the next message can be processed, it returns 0. If
2122 * the message is too big, it returns -1.*/
2123static int
2124spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2125 struct spoe_message *msg, int dir,
2126 char **buf, char *end)
2127{
2128 struct sample *smp;
2129 struct spoe_arg *arg;
2130 int ret;
2131
2132 if (msg->cond) {
2133 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2134 ret = acl_pass(ret);
2135 if (msg->cond->pol == ACL_COND_UNLESS)
2136 ret = !ret;
2137
2138 /* the rule does not match */
2139 if (!ret)
2140 goto next;
2141 }
2142
2143 /* Resume encoding of a SPOE argument */
2144 if (ctx->frag_ctx.curarg != NULL) {
2145 arg = ctx->frag_ctx.curarg;
2146 goto encode_argument;
2147 }
2148
2149 if (ctx->frag_ctx.curoff != UINT_MAX)
2150 goto encode_msg_payload;
2151
2152 /* Check if there is enough space for the message name and the
2153 * number of arguments. It implies <msg->id_len> is encoded on 2
2154 * bytes, at most (< 2288). */
2155 if (*buf + 2 + msg->id_len + 1 > end)
2156 goto too_big;
2157
2158 /* Encode the message name */
2159 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2160 goto too_big;
2161
2162 /* Set the number of arguments for this message */
2163 **buf = msg->nargs;
2164 (*buf)++;
2165
2166 ctx->frag_ctx.curoff = 0;
2167 encode_msg_payload:
2168
2169 /* Loop on arguments */
2170 list_for_each_entry(arg, &msg->args, list) {
2171 ctx->frag_ctx.curarg = arg;
2172 ctx->frag_ctx.curoff = UINT_MAX;
2173
2174 encode_argument:
2175 if (ctx->frag_ctx.curoff != UINT_MAX)
2176 goto encode_arg_value;
2177
2178 /* Encode the arguement name as a string. It can by NULL */
2179 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2180 goto too_big;
2181
2182 ctx->frag_ctx.curoff = 0;
2183 encode_arg_value:
2184
2185 /* Fetch the arguement value */
2186 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2187 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2188 if (ret == -1 || ctx->frag_ctx.curoff)
2189 goto too_big;
2190 }
2191
2192 next:
2193 return 0;
2194
2195 too_big:
2196 return -1;
2197}
2198
Christopher Fauletc718b822017-09-21 16:50:56 +02002199/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2200 * handle fragmented content. On success it returns 1. If an error occurred, -1
2201 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2202 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002203static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002204spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002205 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002207 struct spoe_config *conf = FLT_CONF(ctx->filter);
2208 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002209 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002210 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002211
Christopher Faulet8ef75252017-02-20 22:56:03 +01002212 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002213 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002214
Christopher Fauletc718b822017-09-21 16:50:56 +02002215 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2216 /* Resume encoding of a SPOE message */
2217 if (ctx->frag_ctx.curmsg != NULL) {
2218 msg = ctx->frag_ctx.curmsg;
2219 goto encode_evt_message;
2220 }
2221
2222 list_for_each_entry(msg, messages, by_evt) {
2223 ctx->frag_ctx.curmsg = msg;
2224 ctx->frag_ctx.curarg = NULL;
2225 ctx->frag_ctx.curoff = UINT_MAX;
2226
2227 encode_evt_message:
2228 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2229 goto too_big;
2230 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002231 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002232 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2233 /* Resume encoding of a SPOE message */
2234 if (ctx->frag_ctx.curmsg != NULL) {
2235 msg = ctx->frag_ctx.curmsg;
2236 goto encode_grp_message;
2237 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002238
Christopher Fauletc718b822017-09-21 16:50:56 +02002239 list_for_each_entry(msg, messages, by_grp) {
2240 ctx->frag_ctx.curmsg = msg;
2241 ctx->frag_ctx.curarg = NULL;
2242 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002243
Christopher Fauletc718b822017-09-21 16:50:56 +02002244 encode_grp_message:
2245 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2246 goto too_big;
2247 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002248 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002249 else
2250 goto skip;
2251
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252
Christopher Faulet57583e42017-09-04 15:41:09 +02002253 /* nothing has been encoded for an unfragmented payload */
2254 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2255 goto skip;
2256
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258 " - encode %s messages - spoe_appctx=%p"
2259 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002260 (int)now.tv_sec, (int)now.tv_usec,
2261 agent->id, __FUNCTION__, s,
2262 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002263 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002264 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002265
Christopher Faulet8ef75252017-02-20 22:56:03 +01002266 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 ctx->frag_ctx.curmsg = NULL;
2268 ctx->frag_ctx.curarg = NULL;
2269 ctx->frag_ctx.curoff = 0;
2270 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002271
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002272 return 1;
2273
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002275 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2276 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2277 return -1;
2278 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279
2280 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002281 " - encode fragmented messages - spoe_appctx=%p"
2282 " - curmsg=%p - curarg=%p - curoff=%u"
2283 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002285 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002287 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288
Christopher Faulet8ef75252017-02-20 22:56:03 +01002289 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002290 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2291 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2292 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002293
2294 skip:
2295 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2296 " - skip the frame because nothing has been encoded\n",
2297 (int)now.tv_sec, (int)now.tv_usec,
2298 agent->id, __FUNCTION__, s);
2299 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002300}
2301
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002302
2303/***************************************************************************
2304 * Functions that handle SPOE actions
2305 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002306/* Helper function to set a variable */
2307static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002308spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002309 struct sample *smp)
2310{
2311 struct spoe_config *conf = FLT_CONF(ctx->filter);
2312 struct spoe_agent *agent = conf->agent;
2313 char varname[64];
2314
2315 memset(varname, 0, sizeof(varname));
2316 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2317 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002318 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2319 vars_set_by_name(varname, len, smp);
2320 else
2321 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002322}
2323
2324/* Helper function to unset a variable */
2325static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002326spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002327 struct sample *smp)
2328{
2329 struct spoe_config *conf = FLT_CONF(ctx->filter);
2330 struct spoe_agent *agent = conf->agent;
2331 char varname[64];
2332
2333 memset(varname, 0, sizeof(varname));
2334 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2335 scope, agent->var_pfx, len, name);
2336 vars_unset_by_name_ifexist(varname, len, smp);
2337}
2338
2339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340static inline int
2341spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2342 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 char *str, *scope, *p = *buf;
2345 struct sample smp;
2346 uint64_t sz;
2347 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 if (p + 2 >= end)
2350 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002351
Christopher Faulet8ef75252017-02-20 22:56:03 +01002352 /* SET-VAR requires 3 arguments */
2353 if (*p++ != 3)
2354 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 switch (*p++) {
2357 case SPOE_SCOPE_PROC: scope = "proc"; break;
2358 case SPOE_SCOPE_SESS: scope = "sess"; break;
2359 case SPOE_SCOPE_TXN : scope = "txn"; break;
2360 case SPOE_SCOPE_REQ : scope = "req"; break;
2361 case SPOE_SCOPE_RES : scope = "res"; break;
2362 default: goto skip;
2363 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2366 goto skip;
2367 memset(&smp, 0, sizeof(smp));
2368 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369
Christopher Faulet8ef75252017-02-20 22:56:03 +01002370 if (spoe_decode_data(&p, end, &smp) == -1)
2371 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372
Christopher Faulet8ef75252017-02-20 22:56:03 +01002373 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2374 " - set-var '%s.%s.%.*s'\n",
2375 (int)now.tv_sec, (int)now.tv_usec,
2376 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2377 __FUNCTION__, s, scope,
2378 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2379 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 ret = (p - *buf);
2384 *buf = p;
2385 return ret;
2386 skip:
2387 return 0;
2388}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390static inline int
2391spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2392 char **buf, char *end, int dir)
2393{
2394 char *str, *scope, *p = *buf;
2395 struct sample smp;
2396 uint64_t sz;
2397 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002398
Christopher Faulet8ef75252017-02-20 22:56:03 +01002399 if (p + 2 >= end)
2400 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401
Christopher Faulet8ef75252017-02-20 22:56:03 +01002402 /* UNSET-VAR requires 2 arguments */
2403 if (*p++ != 2)
2404 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 switch (*p++) {
2407 case SPOE_SCOPE_PROC: scope = "proc"; break;
2408 case SPOE_SCOPE_SESS: scope = "sess"; break;
2409 case SPOE_SCOPE_TXN : scope = "txn"; break;
2410 case SPOE_SCOPE_REQ : scope = "req"; break;
2411 case SPOE_SCOPE_RES : scope = "res"; break;
2412 default: goto skip;
2413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
Christopher Faulet8ef75252017-02-20 22:56:03 +01002415 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2416 goto skip;
2417 memset(&smp, 0, sizeof(smp));
2418 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002419
Christopher Faulet8ef75252017-02-20 22:56:03 +01002420 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2421 " - unset-var '%s.%s.%.*s'\n",
2422 (int)now.tv_sec, (int)now.tv_usec,
2423 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2424 __FUNCTION__, s, scope,
2425 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2426 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002427
Christopher Faulet8ef75252017-02-20 22:56:03 +01002428 spoe_unset_var(ctx, scope, str, sz, &smp);
2429
2430 ret = (p - *buf);
2431 *buf = p;
2432 return ret;
2433 skip:
2434 return 0;
2435}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002436
Christopher Faulet8ef75252017-02-20 22:56:03 +01002437/* Process SPOE actions for a specific event. It returns 1 on success. If an
2438 * error occurred, 0 is returned. */
2439static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002440spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002441{
2442 char *p, *end;
2443 int ret;
2444
2445 p = ctx->buffer->p;
2446 end = p + ctx->buffer->i;
2447
2448 while (p < end) {
2449 enum spoe_action_type type;
2450
2451 type = *p++;
2452 switch (type) {
2453 case SPOE_ACT_T_SET_VAR:
2454 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2455 if (!ret)
2456 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002457 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002458
Christopher Faulet8ef75252017-02-20 22:56:03 +01002459 case SPOE_ACT_T_UNSET_VAR:
2460 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2461 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002462 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002463 break;
2464
2465 default:
2466 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002467 }
2468 }
2469
2470 return 1;
2471 skip:
2472 return 0;
2473}
2474
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002475/***************************************************************************
2476 * Functions that process SPOE events
2477 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002478static void
2479spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2480 struct spoe_context *ctx, int dir)
2481{
2482 if (!tv_iszero(&ctx->stats.tv_start)) {
2483 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2484 ctx->stats.t_total += ctx->stats.t_process;
2485 tv_zero(&ctx->stats.tv_request);
2486 tv_zero(&ctx->stats.tv_queue);
2487 tv_zero(&ctx->stats.tv_wait);
2488 tv_zero(&ctx->stats.tv_response);
2489 }
2490
2491 if (agent->var_t_process) {
2492 struct sample smp;
2493
2494 memset(&smp, 0, sizeof(smp));
2495 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2496 smp.data.u.sint = ctx->stats.t_process;
2497 smp.data.type = SMP_T_SINT;
2498
2499 spoe_set_var(ctx, "txn", agent->var_t_process,
2500 strlen(agent->var_t_process), &smp);
2501 }
2502
2503 if (agent->var_t_total) {
2504 struct sample smp;
2505
2506 memset(&smp, 0, sizeof(smp));
2507 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2508 smp.data.u.sint = ctx->stats.t_total;
2509 smp.data.type = SMP_T_SINT;
2510
2511 spoe_set_var(ctx, "txn", agent->var_t_total,
2512 strlen(agent->var_t_total), &smp);
2513 }
2514}
2515
2516static void
2517spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2518 struct spoe_context *ctx, int dir)
2519{
2520 if (agent->eps_max > 0)
2521 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2522
2523 if (agent->var_on_error) {
2524 struct sample smp;
2525
2526 memset(&smp, 0, sizeof(smp));
2527 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2528 smp.data.u.sint = ctx->status_code;
2529 smp.data.type = SMP_T_BOOL;
2530
2531 spoe_set_var(ctx, "txn", agent->var_on_error,
2532 strlen(agent->var_on_error), &smp);
2533 }
2534
2535 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2536 ? SPOE_CTX_ST_READY
2537 : SPOE_CTX_ST_NONE);
2538}
2539
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002540static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002541spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002542{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543 /* If a process is already started for this SPOE context, retry
2544 * later. */
2545 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002546 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002547
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002548 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002549 ctx->stats.tv_start = now;
2550 ctx->stats.tv_request = now;
2551 ctx->stats.t_request = -1;
2552 ctx->stats.t_queue = -1;
2553 ctx->stats.t_waiting = -1;
2554 ctx->stats.t_response = -1;
2555 ctx->stats.t_process = -1;
2556
2557 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002558
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559 /* Set the right flag to prevent request and response processing
2560 * in same time. */
2561 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2562 ? SPOE_CTX_FL_REQ_PROCESS
2563 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565}
2566
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002567static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002568spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002569{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002570 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002571
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002572 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2573 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002574 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002575 if (sa) {
2576 if (sa->frag_ctx.ctx == ctx) {
2577 sa->frag_ctx.ctx = NULL;
2578 spoe_wakeup_appctx(sa->owner);
2579 }
2580 else
2581 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002582 }
2583
Christopher Fauleta1cda022016-12-21 08:58:06 +01002584 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002585 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002586 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587
2588 /* Reset processing timer */
2589 ctx->process_exp = TICK_ETERNITY;
2590
Christopher Faulet8ef75252017-02-20 22:56:03 +01002591 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592
Christopher Fauletfce747b2018-01-24 15:59:32 +01002593 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594 ctx->frag_ctx.curmsg = NULL;
2595 ctx->frag_ctx.curarg = NULL;
2596 ctx->frag_ctx.curoff = 0;
2597 ctx->frag_ctx.flags = 0;
2598
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002600 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002601 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002602 else
2603 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2604
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 LIST_DEL(&ctx->list);
2606 LIST_INIT(&ctx->list);
2607 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002608}
2609
Christopher Faulet58d03682017-09-21 16:57:24 +02002610/* Process a list of SPOE messages. First, this functions will process messages
2611 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2612 * to process corresponding actions. During all the processing, it returns 0
2613 * and it returns 1 when the processing is finished. If an error occurred, -1
2614 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002615static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002616spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2617 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002618{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002619 struct spoe_config *conf = FLT_CONF(ctx->filter);
2620 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002621 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002622
2623 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002624 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002625
2626 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2627 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002628 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002629 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002630 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002631 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002632 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002633 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002634
2635 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002636 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002637 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002639 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002641 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642 goto skip;
2643 }
2644 }
2645
Christopher Fauletf7a30922016-11-10 15:04:51 +01002646 if (!tick_isset(ctx->process_exp)) {
2647 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2648 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2649 ctx->process_exp);
2650 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002651 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002652 if (!ret)
2653 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002655 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002656 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002657 }
2658
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002659 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002660 if (!tv_iszero(&ctx->stats.tv_request))
2661 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002662 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002663 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002665 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002666 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002667 if (!ret)
2668 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002669 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002670 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002671 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2672 }
2673
2674 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002675 if (ctx->spoe_appctx)
2676 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677 ret = 0;
2678 goto out;
2679 }
2680
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002681 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2682 ret = 0;
2683 goto out;
2684 }
2685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002686 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002687 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002688 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002689 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002690 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002691 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002692 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 }
2694
2695 out:
2696 return ret;
2697
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002699 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002700 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002701 spoe_stop_processing(agent, ctx);
2702 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002703
Christopher Fauleta1cda022016-12-21 08:58:06 +01002704 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002705 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002706 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002707 if (ctx->status_code) {
2708 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2709 spoe_handle_processing_error(s, agent, ctx, dir);
2710 ret = 1;
2711 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002712 return ret;
2713}
2714
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002715/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2716 * See spoe_process_message for details. */
2717static int
2718spoe_process_group(struct stream *s, struct spoe_context *ctx,
2719 struct spoe_group *group, int dir)
2720{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002721 struct spoe_config *conf = FLT_CONF(ctx->filter);
2722 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002723 int ret;
2724
2725 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2726 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002727 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002728 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2729 group->id);
2730
2731 if (LIST_ISEMPTY(&group->messages))
2732 return 1;
2733
2734 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002735 if (ret && ctx->stats.t_process != -1) {
2736 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002737 " - <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 +01002738 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002739 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002740 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002741 ctx->stats.t_response, ctx->stats.t_process,
2742 agent->counters.idles, agent->counters.applets,
2743 agent->counters.nb_sending, agent->counters.nb_waiting,
2744 agent->counters.nb_errors, agent->counters.nb_processed,
2745 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2746 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2747 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2748 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2749 agent->id, group->id, s->uniq_id, ctx->status_code,
2750 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2751 ctx->stats.t_response, ctx->stats.t_process,
2752 agent->counters.idles, agent->counters.applets,
2753 agent->counters.nb_sending, agent->counters.nb_waiting,
2754 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002755 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002756 return ret;
2757}
2758
Christopher Faulet58d03682017-09-21 16:57:24 +02002759/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2760 * See spoe_process_message for details. */
2761static int
2762spoe_process_event(struct stream *s, struct spoe_context *ctx,
2763 enum spoe_event ev)
2764{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002765 struct spoe_config *conf = FLT_CONF(ctx->filter);
2766 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002767 int dir, ret;
2768
2769 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002770 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002771 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002772 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2773 spoe_event_str[ev]);
2774
2775 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2776
2777 if (LIST_ISEMPTY(&(ctx->events[ev])))
2778 return 1;
2779
2780 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002781 if (ret && ctx->stats.t_process != -1) {
2782 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002783 " - <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 +01002784 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2785 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2786 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002787 ctx->stats.t_response, ctx->stats.t_process,
2788 agent->counters.idles, agent->counters.applets,
2789 agent->counters.nb_sending, agent->counters.nb_waiting,
2790 agent->counters.nb_errors, agent->counters.nb_processed,
2791 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2792 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2793 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2794 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2795 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2796 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2797 ctx->stats.t_response, ctx->stats.t_process,
2798 agent->counters.idles, agent->counters.applets,
2799 agent->counters.nb_sending, agent->counters.nb_waiting,
2800 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002801 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002802 return ret;
2803}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002804
2805/***************************************************************************
2806 * Functions that create/destroy SPOE contexts
2807 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002808static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002809spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002810{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002811 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002812 return 1;
2813
Christopher Faulet4596fb72017-01-11 14:05:19 +01002814 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002815 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002816 LIST_DEL(&buffer_wait->list);
2817 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002818 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819 }
2820
Christopher Faulet4596fb72017-01-11 14:05:19 +01002821 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002822 return 1;
2823
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002824 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002825 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002826 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827 return 0;
2828}
2829
2830static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002831spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002832{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002833 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002834 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002835 LIST_DEL(&buffer_wait->list);
2836 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002837 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002838 }
2839
2840 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002841 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002842 b_free(buf);
2843 offer_buffers(buffer_wait->target,
2844 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002845 }
2846}
2847
Christopher Faulet4596fb72017-01-11 14:05:19 +01002848static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002849spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002850{
2851 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2852 return 1;
2853}
2854
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002855static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002856spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002857{
2858 struct spoe_config *conf = FLT_CONF(filter);
2859 struct spoe_context *ctx;
2860
Willy Tarreaubafbe012017-11-24 17:34:44 +01002861 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002862 if (ctx == NULL) {
2863 return NULL;
2864 }
2865 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002866 ctx->filter = filter;
2867 ctx->state = SPOE_CTX_ST_NONE;
2868 ctx->status_code = SPOE_CTX_ERR_NONE;
2869 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002870 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002871 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002872 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002873 LIST_INIT(&ctx->buffer_wait.list);
2874 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002875 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002876 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002877
Christopher Fauletf7a30922016-11-10 15:04:51 +01002878 ctx->stream_id = 0;
2879 ctx->frame_id = 1;
2880 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002882 tv_zero(&ctx->stats.tv_start);
2883 tv_zero(&ctx->stats.tv_request);
2884 tv_zero(&ctx->stats.tv_queue);
2885 tv_zero(&ctx->stats.tv_wait);
2886 tv_zero(&ctx->stats.tv_response);
2887 ctx->stats.t_request = -1;
2888 ctx->stats.t_queue = -1;
2889 ctx->stats.t_waiting = -1;
2890 ctx->stats.t_response = -1;
2891 ctx->stats.t_process = -1;
2892 ctx->stats.t_total = 0;
2893
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002894 ctx->strm = s;
2895 ctx->state = SPOE_CTX_ST_READY;
2896 filter->ctx = ctx;
2897
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002898 return ctx;
2899}
2900
2901static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002902spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002903{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002904 struct spoe_config *conf = FLT_CONF(filter);
2905 struct spoe_context *ctx = filter->ctx;
2906
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002907 if (!ctx)
2908 return;
2909
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002910 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002911 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002912 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913}
2914
2915static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002916spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917{
2918 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002919 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002920
2921 tv_zero(&ctx->stats.tv_start);
2922 tv_zero(&ctx->stats.tv_request);
2923 tv_zero(&ctx->stats.tv_queue);
2924 tv_zero(&ctx->stats.tv_wait);
2925 tv_zero(&ctx->stats.tv_response);
2926 ctx->stats.t_request = -1;
2927 ctx->stats.t_queue = -1;
2928 ctx->stats.t_waiting = -1;
2929 ctx->stats.t_response = -1;
2930 ctx->stats.t_process = -1;
2931 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002932}
2933
2934
2935/***************************************************************************
2936 * Hooks that manage the filter lifecycle (init/check/deinit)
2937 **************************************************************************/
2938/* Signal handler: Do a soft stop, wakeup SPOE applet */
2939static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002941{
2942 struct proxy *p;
2943
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002944 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945 while (p) {
2946 struct flt_conf *fconf;
2947
2948 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002949 struct spoe_config *conf;
2950 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002951 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002952 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953
Christopher Faulet3b386a32017-02-23 10:17:15 +01002954 if (fconf->id != spoe_filter_id)
2955 continue;
2956
2957 conf = fconf->conf;
2958 agent = conf->agent;
2959
Christopher Faulet24289f22017-09-25 14:48:02 +02002960 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002961 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002962 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2963 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002964 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965 }
2966 }
2967 p = p->next;
2968 }
2969}
2970
2971
2972/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2973static int
2974spoe_init(struct proxy *px, struct flt_conf *fconf)
2975{
2976 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002977
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002978 /* conf->agent_fe was already initialized during the config
2979 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981 conf->agent_fe.cap = PR_CAP_FE;
2982 conf->agent_fe.mode = PR_MODE_TCP;
2983 conf->agent_fe.maxconn = 0;
2984 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2985 conf->agent_fe.conn_retries = CONN_RETRIES;
2986 conf->agent_fe.accept = frontend_accept;
2987 conf->agent_fe.srv = NULL;
2988 conf->agent_fe.timeout.client = TICK_ETERNITY;
2989 conf->agent_fe.default_target = &spoe_applet.obj_type;
2990 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2991
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002993 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002994 sighandler_registered = 1;
2995 }
2996
2997 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002998}
2999
3000/* Free ressources allocated by the SPOE filter. */
3001static void
3002spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3003{
3004 struct spoe_config *conf = fconf->conf;
3005
3006 if (conf) {
3007 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003008
Christopher Faulet8ef75252017-02-20 22:56:03 +01003009 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003010 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003011 free(conf);
3012 }
3013 fconf->conf = NULL;
3014}
3015
3016/* Check configuration of a SPOE filter for a specified proxy.
3017 * Return 1 on error, else 0. */
3018static int
3019spoe_check(struct proxy *px, struct flt_conf *fconf)
3020{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003021 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003022 struct spoe_config *conf = fconf->conf;
3023 struct proxy *target;
3024
Christopher Faulet7ee86672017-09-19 11:08:28 +02003025 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3026 * are uniq */
3027 list_for_each_entry(f, &px->filter_configs, list) {
3028 struct spoe_config *c = f->conf;
3029
3030 /* This is not an SPOE filter */
3031 if (f->id != spoe_filter_id)
3032 continue;
3033 /* This is the current SPOE filter */
3034 if (f == fconf)
3035 continue;
3036
3037 /* Check engine Id. It should be uniq */
3038 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003039 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3040 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003041 return 1;
3042 }
3043 }
3044
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 target = proxy_be_by_name(conf->agent->b.name);
3046 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003047 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3048 " declared at %s:%d.\n",
3049 px->id, conf->agent->b.name, conf->agent->id,
3050 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003051 return 1;
3052 }
3053 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003054 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3055 " at %s:%d does not support HTTP mode.\n",
3056 px->id, target->id, conf->agent->id,
3057 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 return 1;
3059 }
3060
3061 free(conf->agent->b.name);
3062 conf->agent->b.name = NULL;
3063 conf->agent->b.be = target;
3064 return 0;
3065}
3066
3067/**************************************************************************
3068 * Hooks attached to a stream
3069 *************************************************************************/
3070/* Called when a filter instance is created and attach to a stream. It creates
3071 * the context that will be used to process this stream. */
3072static int
3073spoe_start(struct stream *s, struct filter *filter)
3074{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003075 struct spoe_config *conf = FLT_CONF(filter);
3076 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003077 struct spoe_context *ctx;
3078
3079 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003080 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003081 __FUNCTION__, s);
3082
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003083 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003084 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3085 " - failed to create SPOE context\n",
3086 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003087 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003088 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003089 "SPOE: [%s] failed to create SPOE context\n",
3090 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091 return 0;
3092 }
3093
Christopher Faulet11610f32017-09-21 10:23:10 +02003094 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003095 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3096
Christopher Faulet11610f32017-09-21 10:23:10 +02003097 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003098 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3099
Christopher Faulet11610f32017-09-21 10:23:10 +02003100 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003101 filter->pre_analyzers |= AN_RES_INSPECT;
3102
Christopher Faulet11610f32017-09-21 10:23:10 +02003103 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3105
Christopher Faulet11610f32017-09-21 10:23:10 +02003106 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003107 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3108
Christopher Faulet11610f32017-09-21 10:23:10 +02003109 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003110 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3111
3112 return 1;
3113}
3114
3115/* Called when a filter instance is detached from a stream. It release the
3116 * attached SPOE context. */
3117static void
3118spoe_stop(struct stream *s, struct filter *filter)
3119{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003120 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3121 (int)now.tv_sec, (int)now.tv_usec,
3122 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3123 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003124 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003125}
3126
Christopher Fauletf7a30922016-11-10 15:04:51 +01003127
3128/*
3129 * Called when the stream is woken up because of expired timer.
3130 */
3131static void
3132spoe_check_timeouts(struct stream *s, struct filter *filter)
3133{
3134 struct spoe_context *ctx = filter->ctx;
3135
Christopher Fauletac580602018-03-20 16:09:20 +01003136 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003137 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003138}
3139
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003140/* Called when we are ready to filter data on a channel */
3141static int
3142spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3143{
3144 struct spoe_context *ctx = filter->ctx;
3145 int ret = 1;
3146
3147 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3148 " - ctx-flags=0x%08x\n",
3149 (int)now.tv_sec, (int)now.tv_usec,
3150 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3151 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3152
Christopher Fauletb067b062017-01-04 16:39:11 +01003153 if (ctx->state == SPOE_CTX_ST_NONE)
3154 goto out;
3155
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003156 if (!(chn->flags & CF_ISRESP)) {
3157 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3158 chn->analysers |= AN_REQ_INSPECT_FE;
3159 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3160 chn->analysers |= AN_REQ_INSPECT_BE;
3161
3162 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3163 goto out;
3164
3165 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003166 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003167 if (!ret)
3168 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3170 }
3171 else {
3172 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3173 chn->analysers |= AN_RES_INSPECT;
3174
3175 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3176 goto out;
3177
Christopher Faulet8ef75252017-02-20 22:56:03 +01003178 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003179 if (!ret) {
3180 channel_dont_read(chn);
3181 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003182 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003183 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003184 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 }
3186
3187 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003188 return ret;
3189}
3190
3191/* Called before a processing happens on a given channel */
3192static int
3193spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3194 struct channel *chn, unsigned an_bit)
3195{
3196 struct spoe_context *ctx = filter->ctx;
3197 int ret = 1;
3198
3199 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3200 " - ctx-flags=0x%08x - ana=0x%08x\n",
3201 (int)now.tv_sec, (int)now.tv_usec,
3202 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3203 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3204 ctx->flags, an_bit);
3205
Christopher Fauletb067b062017-01-04 16:39:11 +01003206 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003207 goto out;
3208
3209 switch (an_bit) {
3210 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003211 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003212 break;
3213 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003214 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003215 break;
3216 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003217 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003218 break;
3219 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003220 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 break;
3222 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003223 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 break;
3225 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003226 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227 break;
3228 }
3229
3230 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003231 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003232 channel_dont_read(chn);
3233 channel_dont_close(chn);
3234 }
3235 return ret;
3236}
3237
3238/* Called when the filtering on the channel ends. */
3239static int
3240spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3241{
3242 struct spoe_context *ctx = filter->ctx;
3243
3244 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3245 " - ctx-flags=0x%08x\n",
3246 (int)now.tv_sec, (int)now.tv_usec,
3247 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3248 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3249
3250 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003251 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003252 }
3253
3254 return 1;
3255}
3256
3257/********************************************************************
3258 * Functions that manage the filter initialization
3259 ********************************************************************/
3260struct flt_ops spoe_ops = {
3261 /* Manage SPOE filter, called for each filter declaration */
3262 .init = spoe_init,
3263 .deinit = spoe_deinit,
3264 .check = spoe_check,
3265
3266 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003267 .attach = spoe_start,
3268 .detach = spoe_stop,
3269 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003270
3271 /* Handle channels activity */
3272 .channel_start_analyze = spoe_start_analyze,
3273 .channel_pre_analyze = spoe_chn_pre_analyze,
3274 .channel_end_analyze = spoe_end_analyze,
3275};
3276
3277
3278static int
3279cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3280{
3281 const char *err;
3282 int i, err_code = 0;
3283
3284 if ((cfg_scope == NULL && curengine != NULL) ||
3285 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003286 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003287 goto out;
3288
3289 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3290 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003291 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3292 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003293 err_code |= ERR_ALERT | ERR_ABORT;
3294 goto out;
3295 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003296 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3297 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003298 goto out;
3299 }
3300
3301 err = invalid_char(args[1]);
3302 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003303 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3304 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003305 err_code |= ERR_ALERT | ERR_ABORT;
3306 goto out;
3307 }
3308
3309 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003310 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3311 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003312 err_code |= ERR_ALERT | ERR_ABORT;
3313 goto out;
3314 }
3315 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003316 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 err_code |= ERR_ALERT | ERR_ABORT;
3318 goto out;
3319 }
3320
3321 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003322
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003323 curagent->conf.file = strdup(file);
3324 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003325
3326 curagent->timeout.hello = TICK_ETERNITY;
3327 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003328 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003329
3330 curagent->engine_id = NULL;
3331 curagent->var_pfx = NULL;
3332 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003333 curagent->var_t_process = NULL;
3334 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003335 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3336 if (global.nbthread == 1)
3337 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003338 curagent->cps_max = 0;
3339 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003340 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003341 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342
3343 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003344 LIST_INIT(&curagent->events[i]);
3345 LIST_INIT(&curagent->groups);
3346 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003347
Christopher Faulet24289f22017-09-25 14:48:02 +02003348 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003349 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003350 err_code |= ERR_ALERT | ERR_ABORT;
3351 goto out;
3352 }
3353 for (i = 0; i < global.nbthread; ++i) {
3354 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003355 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003356 LIST_INIT(&curagent->rt[i].applets);
3357 LIST_INIT(&curagent->rt[i].sending_queue);
3358 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003359 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003360 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361 }
3362 else if (!strcmp(args[0], "use-backend")) {
3363 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003364 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3365 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366 err_code |= ERR_ALERT | ERR_FATAL;
3367 goto out;
3368 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003369 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003370 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 free(curagent->b.name);
3372 curagent->b.name = strdup(args[1]);
3373 }
3374 else if (!strcmp(args[0], "messages")) {
3375 int cur_arg = 1;
3376 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003377 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378
Christopher Faulet11610f32017-09-21 10:23:10 +02003379 list_for_each_entry(ph, &curmphs, list) {
3380 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003381 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3382 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003383 err_code |= ERR_ALERT | ERR_FATAL;
3384 goto out;
3385 }
3386 }
3387
Christopher Faulet11610f32017-09-21 10:23:10 +02003388 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003389 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003390 err_code |= ERR_ALERT | ERR_ABORT;
3391 goto out;
3392 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003393 ph->id = strdup(args[cur_arg]);
3394 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003395 cur_arg++;
3396 }
3397 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003398 else if (!strcmp(args[0], "groups")) {
3399 int cur_arg = 1;
3400 while (*args[cur_arg]) {
3401 struct spoe_placeholder *ph = NULL;
3402
3403 list_for_each_entry(ph, &curgphs, list) {
3404 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003405 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3406 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003407 err_code |= ERR_ALERT | ERR_FATAL;
3408 goto out;
3409 }
3410 }
3411
3412 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003414 err_code |= ERR_ALERT | ERR_ABORT;
3415 goto out;
3416 }
3417 ph->id = strdup(args[cur_arg]);
3418 LIST_ADDQ(&curgphs, &ph->list);
3419 cur_arg++;
3420 }
3421 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 else if (!strcmp(args[0], "timeout")) {
3423 unsigned int *tv = NULL;
3424 const char *res;
3425 unsigned timeout;
3426
3427 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003428 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3429 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003430 err_code |= ERR_ALERT | ERR_FATAL;
3431 goto out;
3432 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003433 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3434 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003435 if (!strcmp(args[1], "hello"))
3436 tv = &curagent->timeout.hello;
3437 else if (!strcmp(args[1], "idle"))
3438 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003439 else if (!strcmp(args[1], "processing"))
3440 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003441 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003442 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3443 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444 err_code |= ERR_ALERT | ERR_FATAL;
3445 goto out;
3446 }
3447 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003448 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3449 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450 err_code |= ERR_ALERT | ERR_FATAL;
3451 goto out;
3452 }
3453 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3454 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003455 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3456 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003457 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003458 goto out;
3459 }
3460 *tv = MS_TO_TICKS(timeout);
3461 }
3462 else if (!strcmp(args[0], "option")) {
3463 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003464 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3465 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003466 err_code |= ERR_ALERT | ERR_FATAL;
3467 goto out;
3468 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003469
Christopher Faulet305c6072017-02-23 16:17:53 +01003470 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003471 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003472 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003473 if (kwm == 1)
3474 curagent->flags &= ~SPOE_FL_PIPELINING;
3475 else
3476 curagent->flags |= SPOE_FL_PIPELINING;
3477 goto out;
3478 }
3479 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003480 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003481 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003482 if (kwm == 1)
3483 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003484 else {
3485 if (global.nbthread == 1)
3486 curagent->flags |= SPOE_FL_ASYNC;
3487 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003488 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3489 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003490 err_code |= ERR_WARN;
3491 }
3492 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003493 goto out;
3494 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003495 else if (!strcmp(args[1], "send-frag-payload")) {
3496 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3497 goto out;
3498 if (kwm == 1)
3499 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3500 else
3501 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3502 goto out;
3503 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003504 else if (!strcmp(args[1], "dontlog-normal")) {
3505 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3506 goto out;
3507 if (kwm == 1)
3508 curpxopts2 &= ~PR_O2_NOLOGNORM;
3509 else
3510 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003511 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003512 }
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 != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003537 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003538 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 != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003571 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003572 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 != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003595 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003596 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 != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003619 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003620 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}