blob: 3b425cc3cf44bb6347f9a34a206c9b1c11e715fe [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
Emeric Bruna1dd2432017-06-21 15:42:52 +020021#include <common/hathreads.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020022
23#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020024#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010025#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020026
Christopher Faulet57583e42017-09-04 15:41:09 +020027#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020028#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020029#include <proto/arg.h>
30#include <proto/backend.h>
31#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010032#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033#include <proto/frontend.h>
34#include <proto/log.h>
35#include <proto/proto_http.h>
36#include <proto/proxy.h>
37#include <proto/sample.h>
38#include <proto/session.h>
39#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010040#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/stream.h>
42#include <proto/stream_interface.h>
43#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020044#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#include <proto/vars.h>
46
47#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
48#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#else
51#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010052#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020053#endif
54
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010055/* Reserved 4 bytes to the frame size. So a frame and its size can be written
56 * together in a buffer */
57#define MAX_FRAME_SIZE global.tune.bufsize - 4
58
59/* The minimum size for a frame */
60#define MIN_FRAME_SIZE 256
61
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Reserved for the metadata and the frame type.
63 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010064#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020065
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010066/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010067#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
68
Christopher Faulet3b386a32017-02-23 10:17:15 +010069/* SPOE filter id. Used to identify SPOE filters */
70const char *spoe_filter_id = "SPOE filter";
71
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020072/* Set if the handle on SIGUSR1 is registered */
73static int sighandler_registered = 0;
74
75/* proxy used during the parsing */
76struct proxy *curproxy = NULL;
77
78/* The name of the SPOE engine, used during the parsing */
79char *curengine = NULL;
80
81/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020082/* SPOE agent/group/message used during the parsing */
83struct spoe_agent *curagent = NULL;
84struct spoe_group *curgrp = NULL;
85struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086
87/* list of SPOE messages and placeholders used during the parsing */
88struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020089struct list curgrps;
90struct list curmphs;
91struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010092struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Faulet7250b8f2018-03-26 17:19:01 +020094/* list of log servers used during the parsing */
95struct list curlogsrvs;
96
Christopher Faulet0e0f0852018-03-26 17:20:36 +020097/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
98int curpxopts;
99int curpxopts2;
100
Christopher Faulet42bfa462017-01-04 14:14:19 +0100101/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100102static struct pool_head *pool_head_spoe_ctx = NULL;
103static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105struct flt_ops spoe_ops;
106
Christopher Faulet8ef75252017-02-20 22:56:03 +0100107static int spoe_queue_context(struct spoe_context *ctx);
108static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
109static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110
111/********************************************************************
112 * helper functions/globals
113 ********************************************************************/
114static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200115spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 free(ph->id);
120 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121}
122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100124spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125{
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 struct spoe_arg *arg, *argback;
127 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128
129 if (!msg)
130 return;
131 free(msg->id);
132 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 release_sample_expr(arg->expr);
135 free(arg->name);
136 LIST_DEL(&arg->list);
137 free(arg);
138 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
140 LIST_DEL(&acl->list);
141 prune_acl(acl);
142 free(acl);
143 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100173 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100174 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100175 free(agent->var_t_process);
176 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200177 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
178 LIST_DEL(&msg->list);
179 spoe_release_message(msg);
180 }
181 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
182 LIST_DEL(&grp->list);
183 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200184 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200185 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100186 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
250 * allocated string. So it is the caller's reponsibility to release it. If the
251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
255 static int init = 0;
256
257 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
258 const char uuid_chr[] = "0123456789ABCDEF-";
259 char *uuid;
260 int i;
261
262 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
263 return NULL;
264
265 if (!init) {
266 srand(now_ms);
267 init = 1;
268 }
269
270 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
271 int r = rand () % 16;
272
273 switch (uuid_fmt[i]) {
274 case 'x' : uuid[i] = uuid_chr[r]; break;
275 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
276 default : uuid[i] = uuid_fmt[i]; break;
277 }
278 }
279 return uuid;
280}
281
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100282
283static inline void
284spoe_update_stat_time(struct timeval *tv, long *t)
285{
286 if (*t == -1)
287 *t = tv_ms_elapsed(tv, &now);
288 else
289 *t += tv_ms_elapsed(tv, &now);
290 tv_zero(tv);
291}
292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293/********************************************************************
294 * Functions that encode/decode SPOE frames
295 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296/* Helper to get static string length, excluding the terminating null byte */
297#define SLEN(str) (sizeof(str)-1)
298
299/* Predefined key used in HELLO/DISCONNECT frames */
300#define SUPPORTED_VERSIONS_KEY "supported-versions"
301#define VERSION_KEY "version"
302#define MAX_FRAME_SIZE_KEY "max-frame-size"
303#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100304#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100305#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306#define STATUS_CODE_KEY "status-code"
307#define MSG_KEY "message"
308
309struct spoe_version {
310 char *str;
311 int min;
312 int max;
313};
314
315/* All supported versions */
316static struct spoe_version supported_versions[] = {
317 {"1.0", 1000, 1000},
318 {NULL, 0, 0}
319};
320
321/* Comma-separated list of supported versions */
322#define SUPPORTED_VERSIONS_VAL "1.0"
323
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324/* Convert a string to a SPOE version value. The string must follow the format
325 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
326 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200327static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100328spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 const char *p, *end;
331 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p = str;
334 end = str+len;
335 maj = min = 0;
336 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 /* skip leading spaces */
339 while (p < end && isspace(*p))
340 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342 /* parse Major number, until the '.' */
343 while (*p != '.') {
344 if (p >= end || *p < '0' || *p > '9')
345 goto out;
346 maj *= 10;
347 maj += (*p - '0');
348 p++;
349 }
350
351 /* check Major version */
352 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200353 goto out;
354
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++; /* skip the '.' */
356 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
357 goto out;
358
359 /* Parse Minor number */
360 while (p < end) {
361 if (*p < '0' || *p > '9')
362 break;
363 min *= 10;
364 min += (*p - '0');
365 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367
368 /* check Minor number */
369 if (min > 999)
370 goto out;
371
372 /* skip trailing spaces */
373 while (p < end && isspace(*p))
374 p++;
375 if (p != end)
376 goto out;
377
378 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379 out:
380 return vsn;
381}
382
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
384 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
385 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388{
Christopher Faulet305c6072017-02-23 16:17:53 +0100389 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100390 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 char *p, *end;
392 unsigned int flags = SPOE_FRM_FL_FIN;
393 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200394
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 p = frame;
396 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 /* Set Frame type */
399 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100401 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 memcpy(p, (char *)&flags, 4);
403 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200404
405 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200407
408 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
409 * and "capabilities" */
410
411 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 sz = SLEN(SUPPORTED_VERSIONS_KEY);
413 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
414 goto too_big;
415
416 *p++ = SPOE_DATA_T_STR;
417 sz = SLEN(SUPPORTED_VERSIONS_VAL);
418 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
419 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200420
421 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100422 sz = SLEN(MAX_FRAME_SIZE_KEY);
423 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
424 goto too_big;
425
426 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200427 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200429
430 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 sz = SLEN(CAPABILITIES_KEY);
432 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
433 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200434
Christopher Faulet8ef75252017-02-20 22:56:03 +0100435 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100436 chk = get_trash_chunk();
437 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
438 memcpy(chk->str, "pipelining", 10);
439 chk->len += 10;
440 }
441 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
442 if (chk->len) chk->str[chk->len++] = ',';
443 memcpy(chk->str+chk->len, "async", 5);
444 chk->len += 5;
445 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100446 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
447 if (chk->len) chk->str[chk->len++] = ',';
448 memcpy(chk->str+chk->len, "fragmentation", 13);
449 chk->len += 5;
450 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100451 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452 goto too_big;
453
454 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100455 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456 sz = SLEN(ENGINE_ID_KEY);
457 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
458 goto too_big;
459
460 *p++ = SPOE_DATA_T_STR;
461 sz = strlen(agent->engine_id);
462 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
463 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100464 }
465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 return (p - frame);
467
468 too_big:
469 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
470 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471}
472
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
474 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
475 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100477spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200478{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100479 const char *reason;
480 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100481 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200483
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 p = frame;
485 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 /* Set Frame type */
488 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100490 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 memcpy(p, (char *)&flags, 4);
492 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
494 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 *p++ = 0; *p++ = 0;
496
497 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
498 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200499
500 /* There are 2 mandatory items: "status-code" and "message" */
501
502 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100503 sz = SLEN(STATUS_CODE_KEY);
504 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
505 goto too_big;
506
507 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200508 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200510
511 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512 sz = SLEN(MSG_KEY);
513 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
514 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 /*Get the message corresponding to the status code */
517 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
518
519 *p++ = SPOE_DATA_T_STR;
520 sz = strlen(reason);
521 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
522 goto too_big;
523
524 return (p - frame);
525
526 too_big:
527 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
528 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200529}
530
Christopher Faulet8ef75252017-02-20 22:56:03 +0100531/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
532 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
533 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200534static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100535spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100536 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200537{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100538 char *p, *end;
539 unsigned int stream_id, frame_id;
540 unsigned int flags = SPOE_FRM_FL_FIN;
541 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200542
Christopher Faulet8ef75252017-02-20 22:56:03 +0100543 p = frame;
544 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200545
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 stream_id = ctx->stream_id;
547 frame_id = ctx->frame_id;
548
549 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
550 /* The fragmentation is not supported by the applet */
551 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
552 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
553 return -1;
554 }
555 flags = ctx->frag_ctx.flags;
556 }
557
558 /* Set Frame type */
559 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
560
561 /* Set flags */
562 memcpy(p, (char *)&flags, 4);
563 p += 4;
564
565 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200566 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100567 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200568 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100569 goto too_big;
570
571 /* Copy encoded messages, if possible */
572 sz = ctx->buffer->i;
573 if (p + sz >= end)
574 goto too_big;
575 memcpy(p, ctx->buffer->p, sz);
576 p += sz;
577
578 return (p - frame);
579
580 too_big:
581 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
582 return 0;
583}
584
585/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
586 * returns the number of encoded bytes in the frame on success, 0 if an encoding
587 * error occurred and -1 if a fatal error occurred. */
588static int
589spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
590 char *frame, size_t size)
591{
592 char *p, *end;
593 unsigned int stream_id, frame_id;
594 unsigned int flags;
595 size_t sz;
596
597 p = frame;
598 end = frame+size;
599
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 /* <ctx> is null when the stream has aborted the processing of a
601 * fragmented frame. In this case, we must notify the corresponding
602 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100603 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100604 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100605 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
606 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
607 }
608 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100610 stream_id = ctx->stream_id;
611 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100612 }
613
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100615 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100617 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 memcpy(p, (char *)&flags, 4);
619 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200620
621 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200622 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200624 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 if (ctx == NULL)
628 goto end;
629
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100631 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 if (p + sz >= end)
633 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100634 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100636
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100637 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 return (p - frame);
639
640 too_big:
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
642 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643}
644
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645/* Decode and process the HELLO frame sent by an agent. It returns the number of
646 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
647 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650{
Christopher Faulet305c6072017-02-23 16:17:53 +0100651 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
652 char *p, *end;
653 int vsn, max_frame_size;
654 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655
656 p = frame;
657 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658
659 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
661 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100663 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 if (size < 7 /* TYPE + METADATA */) {
666 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
667 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 }
669
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100670 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 memcpy((char *)&flags, p, 4);
672 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200673
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100674 /* Fragmentation is not supported for HELLO frame */
675 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100677 return -1;
678 }
679
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 if (*p != 0 || *(p+1) != 0) {
682 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
683 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200684 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100685 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686
687 /* There are 3 mandatory items: "version", "max-frame-size" and
688 * "capabilities" */
689
690 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100691 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 while (p < end) {
693 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200694 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696
697 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 ret = spoe_decode_buffer(&p, end, &str, &sz);
699 if (ret == -1 || !sz) {
700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
701 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 /* Check "version" K/V item */
705 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 int i, type = *p++;
707
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
714 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
715 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 }
717
Christopher Faulet8ef75252017-02-20 22:56:03 +0100718 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 return -1;
722 }
723 for (i = 0; supported_versions[i].str != NULL; ++i) {
724 if (vsn >= supported_versions[i].min &&
725 vsn <= supported_versions[i].max)
726 break;
727 }
728 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 return -1;
731 }
732 }
733 /* Check "max-frame-size" K/V item */
734 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200736
737 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
739 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
740 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
741 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
743 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200745 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 if (sz < MIN_FRAME_SIZE ||
750 sz > SPOE_APPCTX(appctx)->max_frame_size) {
751 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200752 return -1;
753 }
754 max_frame_size = sz;
755 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100756 /* Check "capabilities" K/V item */
757 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100758 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759
760 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
763 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
766 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
767 return 0;
768 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771 char *delim;
772
773 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
777 str += 10; sz -= 10;
778 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779 flags |= SPOE_APPCTX_FL_PIPELINING;
780 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 else if (sz >= 5 && !strncmp(str, "async", 5)) {
782 str += 5; sz -= 5;
783 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 flags |= SPOE_APPCTX_FL_ASYNC;
785 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
787 str += 13; sz -= 13;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100789 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
790 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100791
Christopher Faulet8ef75252017-02-20 22:56:03 +0100792 /* Get the next comma or break */
793 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100794 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 delim++;
796 sz -= (delim - str);
797 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100798 }
799 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800 else {
801 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 if (spoe_skip_data(&p, end) == -1) {
803 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
804 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806 }
807 }
808
809 /* Final checks */
810 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812 return -1;
813 }
814 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100815 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816 return -1;
817 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100818 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
819 flags &= ~SPOE_APPCTX_FL_PIPELINING;
820 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
821 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822
Christopher Faulet42bfa462017-01-04 14:14:19 +0100823 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
824 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
825 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826
827 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828}
829
830/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
831 * bytes on success, 0 if the frame can be ignored and -1 if an error
832 * occurred. */
833static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100837 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838
839 p = frame;
840 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841
842 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
844 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 if (size < 7 /* TYPE + METADATA */) {
849 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
850 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851 }
852
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100853 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 memcpy((char *)&flags, p, 4);
855 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100857 /* Fragmentation is not supported for DISCONNECT frame */
858 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100860 return -1;
861 }
862
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 if (*p != 0 || *(p+1) != 0) {
865 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
866 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200867 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869
870 /* There are 2 mandatory items: "status-code" and "message" */
871
872 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 while (p < end) {
874 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200875 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877
878 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 ret = spoe_decode_buffer(&p, end, &str, &sz);
880 if (ret == -1 || !sz) {
881 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
882 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883 }
884
885 /* Check "status-code" K/V item */
886 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888
889 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
891 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
892 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
893 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
895 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200897 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
899 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
903
904 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 else if (!memcmp(str, MSG_KEY, sz)) {
906 int type = *p++;
907
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
910 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
911 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200912 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 ret = spoe_decode_buffer(&p, end, &str, &sz);
914 if (ret == -1 || sz > 255) {
915 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
916 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
919 SPOE_APPCTX(appctx)->reason = str;
920 SPOE_APPCTX(appctx)->rlen = sz;
921#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922 }
923 else {
924 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 if (spoe_skip_data(&p, end) == -1) {
926 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
927 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929 }
930 }
931
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933}
934
935
Christopher Fauleta1cda022016-12-21 08:58:06 +0100936/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 * success, 0 if the frame can be ignored and -1 if an error occurred. */
938static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100940 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100942 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100944 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100946 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947
948 p = frame;
949 end = frame + size;
950 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951
952 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
954 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 if (size < 7 /* TYPE + METADATA */) {
959 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
960 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200961 }
962
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 memcpy((char *)&flags, p, 4);
965 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100967 /* Fragmentation is not supported for now */
968 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100970 return -1;
971 }
972
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200974 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100976 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200978 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100982
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100984 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200985 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100986 if ((*ctx)->stream_id == (unsigned int)stream_id &&
987 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100988 goto found;
989 }
990 }
991 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100992 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
993 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100994 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 goto found;
996 }
997 }
998
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100999 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1000 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1001 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1002
1003 /* ABRT bit is set for an unfinished fragmented frame */
1004 if (flags & SPOE_FRM_FL_ABRT) {
1005 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001006 (*ctx)->state = SPOE_CTX_ST_ERROR;
1007 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1008 /* Ignore the payload */
1009 goto end;
1010 }
1011 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1012 /* For now, we ignore the ack */
1013 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1014 return 0;
1015 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001016
Christopher Fauleta1cda022016-12-21 08:58:06 +01001017 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001018 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1019 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001020 " - stream-id=%u - frame-id=%u\n",
1021 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1022 __FUNCTION__, appctx,
1023 (unsigned int)stream_id, (unsigned int)frame_id);
1024
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001025 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001026 return 0;
1027
1028 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1030 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001032 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001034
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001035 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 len = (end - p);
1037 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1038 SPOE_APPCTX(appctx)->buffer->i = len;
1039 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001040
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 /* Transfer the buffer ownership to the SPOE context */
1042 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1043 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 (*ctx)->state = SPOE_CTX_ST_DONE;
1046
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001047 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 " - ACK frame received"
1050 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1053 (*ctx)->frame_id, flags);
1054 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001055}
1056
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057/* This function is used in cfgparse.c and declared in proto/checks.h. It
1058 * prepare the request to send to agents during a healthcheck. It returns 0 on
1059 * success and -1 if an error occurred. */
1060int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 struct appctx appctx;
1064 struct spoe_appctx spoe_appctx;
1065 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1066 size_t sz;
1067 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069 memset(&appctx, 0, sizeof(appctx));
1070 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072
1073 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001074 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1077 end = frame + MAX_FRAME_SIZE;
1078
1079 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1080 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 /* Add "healthcheck" K/V item */
1085 sz = SLEN(HEALTHCHECK_KEY);
1086 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1087 return -1;
1088 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 *len = frame - buf;
1091 sz = htonl(*len - 4);
1092 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097 return 0;
1098}
1099
1100/* This function is used in checks.c and declared in proto/checks.h. It decode
1101 * the response received from an agent during a healthcheck. It returns 0 on
1102 * success and -1 if an error occurred. */
1103int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 struct appctx appctx;
1107 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 memset(&appctx, 0, sizeof(appctx));
1110 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001113 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1116 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117 goto error;
1118 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001119 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1120 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121
1122 return 0;
1123
1124 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1126 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1127 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return -1;
1129}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001130
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1132 * the frame can be ignored, 1 to retry later, and the frame legnth on
1133 * success. */
1134static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136{
1137 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138 int ret;
1139 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1142 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 netint = htonl(framesz);
1144 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001145 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001147 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 }
1151 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return -1; /* error */
1153 }
1154 return framesz;
1155}
1156
1157/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1158 * when the frame can be ignored, 1 to retry later and the frame length on
1159 * success. */
1160static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162{
1163 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 int ret;
1165 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166
Willy Tarreau06d80a92017-10-19 14:32:15 +02001167 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 if (ret > 0) {
1169 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001170 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 return -1;
1173 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 }
1176 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001179 }
1180 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001181 return -1; /* error */
1182 }
1183 return framesz;
1184}
1185
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001186/********************************************************************
1187 * Functions that manage the SPOE applet
1188 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001189static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001190spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001191{
1192 si_applet_want_get(appctx->owner);
1193 si_applet_want_put(appctx->owner);
1194 appctx_wakeup(appctx);
1195 return 1;
1196}
1197
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198/* Callback function that catches applet timeouts. If a timeout occurred, we set
1199 * <appctx->st1> flag and the SPOE applet is woken up. */
1200static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001201spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202{
1203 struct appctx *appctx = task->context;
1204
1205 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1206 if (tick_is_expired(task->expire, now_ms)) {
1207 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211 return task;
1212}
1213
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214/* Callback function that releases a SPOE applet. This happens when the
1215 * connection with the agent is closed. */
1216static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219 struct stream_interface *si = appctx->owner;
1220 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1221 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001222 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223
1224 if (spoe_appctx == NULL)
1225 return;
1226
1227 appctx->ctx.spoe.ptr = NULL;
1228 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229
1230 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1231 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1232 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233
Christopher Faulet8ef75252017-02-20 22:56:03 +01001234 /* Remove applet from the list of running applets */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001235 SPOE_DEBUG_STMT(agent->rt[tid].applets_act--);
1236 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1238 LIST_DEL(&spoe_appctx->list);
1239 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001241 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242
Christopher Faulet8ef75252017-02-20 22:56:03 +01001243 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001244 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001245 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1246 eb32_delete(&spoe_appctx->node);
1247 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1248 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001249
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001250 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1252 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001253
1254 si_shutw(si);
1255 si_shutr(si);
1256 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001257 }
1258
Christopher Faulet8ef75252017-02-20 22:56:03 +01001259 /* Destroy the task attached to this applet */
1260 if (spoe_appctx->task) {
1261 task_delete(spoe_appctx->task);
1262 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001263 }
1264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 /* Notify all waiting streams */
1266 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001267 LIST_DEL(&ctx->list);
1268 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001269 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001271 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001272 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001273 }
1274
Christopher Faulet8ef75252017-02-20 22:56:03 +01001275 /* If the applet was processing a fragmented frame, notify the
1276 * corresponding stream. */
1277 if (spoe_appctx->frag_ctx.ctx) {
1278 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001279 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001280 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001282 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1283 }
1284
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 /* Release allocated memory */
1286 spoe_release_buffer(&spoe_appctx->buffer,
1287 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001288 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001289
Christopher Faulet24289f22017-09-25 14:48:02 +02001290 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001291 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001294 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 LIST_DEL(&ctx->list);
1296 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001297 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001298 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001299 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001301 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001302 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001303 LIST_DEL(&ctx->list);
1304 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001305 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001306 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001307 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001308 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1309 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001310
1311 end:
1312 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001313 agent->rt[tid].frame_size = agent->max_frame_size;
1314 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1315 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316}
1317
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001318static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001320{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001322 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001324 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001325
Christopher Fauleta1cda022016-12-21 08:58:06 +01001326 if (si->state <= SI_ST_CON) {
1327 si_applet_want_put(si);
1328 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1329 goto stop;
1330 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001331 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001332 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001334 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001335
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1338 " - Connection timed out\n",
1339 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1340 __FUNCTION__, appctx);
1341 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001344
Christopher Faulet42bfa462017-01-04 14:14:19 +01001345 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001346 SPOE_APPCTX(appctx)->task->expire =
1347 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001348
Christopher Faulet8ef75252017-02-20 22:56:03 +01001349 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1350 * length. */
1351 buf = trash.str; frame = buf+4;
1352 ret = spoe_prepare_hahello_frame(appctx, frame,
1353 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001356
1357 switch (ret) {
1358 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001359 case 0: /* ignore => an error, cannot be ignored */
1360 goto exit;
1361
1362 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 goto stop;
1364
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 default:
1366 /* HELLO frame successfully sent, now wait for the
1367 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1369 goto next;
1370 }
1371
1372 next:
1373 return 0;
1374 stop:
1375 return 1;
1376 exit:
1377 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1378 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001379}
1380
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001381static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001382spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001383{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001385 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 char *frame;
1387 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389
Christopher Fauletb067b062017-01-04 16:39:11 +01001390 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001393 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394
Christopher Fauleta1cda022016-12-21 08:58:06 +01001395 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1397 " - Connection timed out\n",
1398 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1399 __FUNCTION__, appctx);
1400 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001401 goto exit;
1402 }
1403
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 frame = trash.str; trash.len = 0;
1405 ret = spoe_recv_frame(appctx, frame,
1406 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 if (ret > 1) {
1408 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1409 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1410 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001412 trash.len = ret + 4;
1413 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 switch (ret) {
1417 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001418 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1420 goto next;
1421
1422 case 1: /* retry later */
1423 goto stop;
1424
1425 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 /* HELLO handshake is finished, set the idle timeout and
1427 * add the applet in the list of running applets. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001428 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001430 SPOE_APPCTX(appctx)->node.key = 0;
1431 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001432
1433 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001434 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001435 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001437
Christopher Fauleta1cda022016-12-21 08:58:06 +01001438 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 /* Do not forget to remove processed frame from the output buffer */
1440 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001441 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001442
1443 SPOE_APPCTX(appctx)->task->expire =
1444 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001445 return 0;
1446 stop:
1447 return 1;
1448 exit:
1449 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1450 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001451}
1452
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001453
Christopher Fauleta1cda022016-12-21 08:58:06 +01001454static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001455spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001456{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001457 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1458 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001459 char *frame, *buf;
1460 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001461
Christopher Faulet8ef75252017-02-20 22:56:03 +01001462 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1463 * length. */
1464 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001465
1466 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1467 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1468 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1469 SPOE_APPCTX(appctx)->max_frame_size);
1470 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001471 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001472 *skip = 1;
1473 ret = 1;
1474 goto end;
1475 }
1476 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001477 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001478 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1479 SPOE_APPCTX(appctx)->max_frame_size);
1480
1481 }
1482
Christopher Faulet8ef75252017-02-20 22:56:03 +01001483 if (ret > 1)
1484 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001485
Christopher Faulet8ef75252017-02-20 22:56:03 +01001486 switch (ret) {
1487 case -1: /* error */
1488 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1489 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001490
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 case 0: /* ignore */
1492 if (ctx == NULL)
1493 goto abort_frag_frame;
1494
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001495 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 LIST_DEL(&ctx->list);
1497 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001498 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001499 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 ctx->state = SPOE_CTX_ST_ERROR;
1501 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1502 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001503 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001504 break;
1505
1506 case 1: /* retry */
1507 *skip = 1;
1508 break;
1509
1510 default:
1511 if (ctx == NULL)
1512 goto abort_frag_frame;
1513
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001514 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 LIST_DEL(&ctx->list);
1516 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001517 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001518 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001519 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1520 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1521 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001522 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001524 }
1525 goto end;
1526
1527 frag_frame_sent:
1528 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001529 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001530 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1531 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1532 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1534 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1535 goto end;
1536
1537 no_frag_frame_sent:
1538 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1539 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001540 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 }
1542 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1543 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1544 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1545 }
1546 else {
1547 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001548 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1550 }
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001551 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001552 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1553 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1554 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001555 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001556
Christopher Faulet8ef75252017-02-20 22:56:03 +01001557 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1558 goto end;
1559
1560 abort_frag_frame:
1561 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1562 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1563 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1564 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1565 goto end;
1566
1567 end:
1568 return ret;
1569}
1570
1571static int
1572spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1573{
1574 struct spoe_context *ctx = NULL;
1575 char *frame;
1576 int ret;
1577
1578 frame = trash.str; trash.len = 0;
1579 ret = spoe_recv_frame(appctx, frame,
1580 SPOE_APPCTX(appctx)->max_frame_size);
1581 if (ret > 1) {
1582 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1583 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001584 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001585 goto end;
1586 }
1587 trash.len = ret + 4;
1588 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1589 }
1590 switch (ret) {
1591 case -1: /* error */
1592 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1593 break;
1594
1595 case 0: /* ignore */
1596 break;
1597
1598 case 1: /* retry */
1599 *skip = 1;
1600 break;
1601
1602 default:
1603 LIST_DEL(&ctx->list);
1604 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001605 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1606 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001607 if (ctx->spoe_appctx) {
1608 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001609 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001610 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001611 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1612 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1613 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1614 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1615 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1616 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1617 }
1618 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1619 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001620 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1621 break;
1622 }
1623
1624 /* Do not forget to remove processed frame from the output buffer */
1625 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001626 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001627 end:
1628 return ret;
1629}
1630
1631static int
1632spoe_handle_processing_appctx(struct appctx *appctx)
1633{
1634 struct stream_interface *si = appctx->owner;
1635 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001636 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001637
1638 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1639 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1640 goto exit;
1641 }
1642
1643 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1644 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1645 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1646 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1647 goto next;
1648 }
1649
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001650 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001651 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001652 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001653 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1654 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001655 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001656
Christopher Faulet8f82b202018-01-24 16:23:03 +01001657 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1658 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001659
Christopher Faulet8f82b202018-01-24 16:23:03 +01001660 /* receiving_frame loop */
1661 while (!skip_receiving) {
1662 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1663 switch (ret) {
1664 case -1: /* error */
1665 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001666
Christopher Faulet8f82b202018-01-24 16:23:03 +01001667 case 0: /* ignore */
1668 active_r = 1;
1669 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 case 1: /* retry */
1672 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001673
Christopher Faulet8f82b202018-01-24 16:23:03 +01001674 default:
1675 active_r = 1;
1676 break;
1677 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001678 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679
Christopher Faulet8f82b202018-01-24 16:23:03 +01001680 /* send_frame loop */
1681 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1682 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1683 switch (ret) {
1684 case -1: /* error */
1685 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686
Christopher Faulet8f82b202018-01-24 16:23:03 +01001687 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001688 if (SPOE_APPCTX(appctx)->node.key)
1689 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001690 active_s++;
1691 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692
Christopher Faulet8f82b202018-01-24 16:23:03 +01001693 case 1: /* retry */
1694 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001697 if (SPOE_APPCTX(appctx)->node.key)
1698 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001699 active_s++;
1700 break;
1701 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001702 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001703
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1706 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1707 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001710 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001712 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 }
1714 return 1;
1715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 next:
1717 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1718 return 0;
1719
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720 exit:
1721 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1722 return 0;
1723}
1724
1725static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001726spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001727{
1728 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001729 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001730 char *frame, *buf;
1731 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001732
Christopher Fauleta1cda022016-12-21 08:58:06 +01001733 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1734 goto exit;
1735
1736 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1737 goto exit;
1738
Christopher Faulet8ef75252017-02-20 22:56:03 +01001739 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1740 * length. */
1741 buf = trash.str; frame = buf+4;
1742 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1743 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746
1747 switch (ret) {
1748 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750 goto exit;
1751
1752 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753 goto stop;
1754
1755 default:
1756 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1757 " - disconnected by HAProxy (%d): %s\n",
1758 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 __FUNCTION__, appctx,
1760 SPOE_APPCTX(appctx)->status_code,
1761 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762
1763 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1764 goto next;
1765 }
1766
1767 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 SPOE_APPCTX(appctx)->task->expire =
1769 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 return 0;
1771 stop:
1772 return 1;
1773 exit:
1774 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1775 return 0;
1776}
1777
1778static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780{
1781 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 char *frame;
1783 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784
Christopher Fauletb067b062017-01-04 16:39:11 +01001785 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001788 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789
Christopher Fauletb067b062017-01-04 16:39:11 +01001790 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001793 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001794
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 frame = trash.str; trash.len = 0;
1796 ret = spoe_recv_frame(appctx, frame,
1797 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 trash.len = ret + 4;
1800 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801 }
1802
1803 switch (ret) {
1804 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1806 " - error on frame (%s)\n",
1807 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001808 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 goto exit;
1812
1813 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 goto next;
1815
1816 case 1: /* retry */
1817 goto stop;
1818
1819 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001822 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001823 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1825 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 goto exit;
1827 }
1828
1829 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001830 /* Do not forget to remove processed frame from the output buffer */
1831 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001832 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 return 0;
1835 stop:
1836 return 1;
1837 exit:
1838 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1839 return 0;
1840}
1841
1842/* I/O Handler processing messages exchanged with the agent */
1843static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001846 struct stream_interface *si = appctx->owner;
1847 struct spoe_agent *agent;
1848
1849 if (SPOE_APPCTX(appctx) == NULL)
1850 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1853 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001854
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 switchstate:
1856 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1857 " - appctx-state=%s\n",
1858 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1859 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1860
1861 switch (appctx->st0) {
1862 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864 goto out;
1865 goto switchstate;
1866
1867 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001868 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869 goto out;
1870 goto switchstate;
1871
1872 case SPOE_APPCTX_ST_IDLE:
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001873 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1874 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001875 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001876 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001877 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 SPOE_APPCTX(appctx)->task->expire =
1879 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001881 goto switchstate;
1882 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1884 /* fall through */
1885
1886 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001887 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1888 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 goto out;
1891 goto switchstate;
1892
1893 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto out;
1896 goto switchstate;
1897
1898 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 goto out;
1901 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001902
1903 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 appctx->st0 = SPOE_APPCTX_ST_END;
1905 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1906
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907 si_shutw(si);
1908 si_shutr(si);
1909 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001910 /* fall through */
1911
1912 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001913 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001916 if (stopping)
1917 spoe_wakeup_appctx(appctx);
1918
Christopher Faulet42bfa462017-01-04 14:14:19 +01001919 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1920 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001921 si_oc(si)->flags |= CF_READ_DONTWAIT;
1922 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001923}
1924
1925struct applet spoe_applet = {
1926 .obj_type = OBJ_TYPE_APPLET,
1927 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001928 .fct = spoe_handle_appctx,
1929 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930};
1931
1932/* Create a SPOE applet. On success, the created applet is returned, else
1933 * NULL. */
1934static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001935spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936{
1937 struct appctx *appctx;
1938 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940
Emeric Brun1138fd02017-06-19 12:38:55 +02001941 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942 goto out_error;
1943
Willy Tarreaubafbe012017-11-24 17:34:44 +01001944 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001945 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001946 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001947 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001948
Christopher Faulet42bfa462017-01-04 14:14:19 +01001949 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001950 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001951 goto out_free_spoe_appctx;
1952
1953 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001954 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001955 SPOE_APPCTX(appctx)->task->context = appctx;
1956 SPOE_APPCTX(appctx)->agent = conf->agent;
1957 SPOE_APPCTX(appctx)->version = 0;
1958 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1959 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001961 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001962 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001963
1964 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1965 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001966 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001967
1968 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1969 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001970
Willy Tarreau5820a362016-12-22 15:59:02 +01001971 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001972 if (!sess)
1973 goto out_free_spoe;
1974
Willy Tarreau87787ac2017-08-28 16:22:54 +02001975 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001976 goto out_free_sess;
1977
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001978 stream_set_backend(strm, conf->agent->b.be);
1979
1980 /* applet is waiting for data */
1981 si_applet_cant_get(&strm->si[0]);
1982 appctx_wakeup(appctx);
1983
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984 strm->do_log = NULL;
1985 strm->res.flags |= CF_READ_DONTWAIT;
1986
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001987 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001988 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001989 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001990 SPOE_DEBUG_STMT(conf->agent->rt[tid].applets_act++);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001991
Emeric Brunc60def82017-09-27 14:59:38 +02001992 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001993 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 return appctx;
1995
1996 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 out_free_sess:
1998 session_free(sess);
1999 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002000 task_free(SPOE_APPCTX(appctx)->task);
2001 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002002 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003 out_free_appctx:
2004 appctx_free(appctx);
2005 out_error:
2006 return NULL;
2007}
2008
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002010spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011{
2012 struct spoe_config *conf = FLT_CONF(ctx->filter);
2013 struct spoe_agent *agent = conf->agent;
2014 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002015 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002016
Christopher Fauleta1cda022016-12-21 08:58:06 +01002017 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002018 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002019 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002020 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021
2022 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002023 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2025 ctx->strm);
2026
Christopher Fauleta1cda022016-12-21 08:58:06 +01002027 /* Do not try to create a new applet if there is no server up for the
2028 * agent's backend. */
2029 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2030 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2031 " - cannot create SPOE appctx: no server up\n",
2032 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2033 __FUNCTION__, ctx->strm);
2034 goto end;
2035 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036
Christopher Fauleta1cda022016-12-21 08:58:06 +01002037 /* Do not try to create a new applet if we have reached the maximum of
2038 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002039 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002040 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2042 " - cannot create SPOE appctx: max CPS reached\n",
2043 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2044 __FUNCTION__, ctx->strm);
2045 goto end;
2046 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002047 }
2048
Christopher Faulet8ef75252017-02-20 22:56:03 +01002049 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002050 if (appctx == NULL) {
2051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2052 " - failed to create SPOE appctx\n",
2053 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2054 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002055 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002056 "SPOE: [%s] failed to create SPOE applet\n",
2057 agent->id);
2058
Christopher Fauleta1cda022016-12-21 08:58:06 +01002059 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060 }
2061
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 /* Increase the per-process number of cumulated connections */
2063 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002064 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065
Christopher Fauleta1cda022016-12-21 08:58:06 +01002066 end:
2067 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002068 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002069 ctx->status_code = SPOE_CTX_ERR_RES;
2070 return -1;
2071 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002073 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002074 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002075 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2076 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077
2078 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002079 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002080 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002082 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002083 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002084
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002085 /* Finally try to wakeup an IDLE applet. */
2086 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2087 struct eb32_node *node;
2088
2089 node = eb32_first(&agent->rt[tid].idle_applets);
2090 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2091 if (node && spoe_appctx) {
2092 eb32_delete(&spoe_appctx->node);
2093 spoe_appctx->node.key++;
2094 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2095 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002096 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002097 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002098 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099}
2100
2101/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002102 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002103 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002104/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2105 * fragmented_content. If the next message can be processed, it returns 0. If
2106 * the message is too big, it returns -1.*/
2107static int
2108spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2109 struct spoe_message *msg, int dir,
2110 char **buf, char *end)
2111{
2112 struct sample *smp;
2113 struct spoe_arg *arg;
2114 int ret;
2115
2116 if (msg->cond) {
2117 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2118 ret = acl_pass(ret);
2119 if (msg->cond->pol == ACL_COND_UNLESS)
2120 ret = !ret;
2121
2122 /* the rule does not match */
2123 if (!ret)
2124 goto next;
2125 }
2126
2127 /* Resume encoding of a SPOE argument */
2128 if (ctx->frag_ctx.curarg != NULL) {
2129 arg = ctx->frag_ctx.curarg;
2130 goto encode_argument;
2131 }
2132
2133 if (ctx->frag_ctx.curoff != UINT_MAX)
2134 goto encode_msg_payload;
2135
2136 /* Check if there is enough space for the message name and the
2137 * number of arguments. It implies <msg->id_len> is encoded on 2
2138 * bytes, at most (< 2288). */
2139 if (*buf + 2 + msg->id_len + 1 > end)
2140 goto too_big;
2141
2142 /* Encode the message name */
2143 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2144 goto too_big;
2145
2146 /* Set the number of arguments for this message */
2147 **buf = msg->nargs;
2148 (*buf)++;
2149
2150 ctx->frag_ctx.curoff = 0;
2151 encode_msg_payload:
2152
2153 /* Loop on arguments */
2154 list_for_each_entry(arg, &msg->args, list) {
2155 ctx->frag_ctx.curarg = arg;
2156 ctx->frag_ctx.curoff = UINT_MAX;
2157
2158 encode_argument:
2159 if (ctx->frag_ctx.curoff != UINT_MAX)
2160 goto encode_arg_value;
2161
2162 /* Encode the arguement name as a string. It can by NULL */
2163 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2164 goto too_big;
2165
2166 ctx->frag_ctx.curoff = 0;
2167 encode_arg_value:
2168
2169 /* Fetch the arguement value */
2170 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2171 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2172 if (ret == -1 || ctx->frag_ctx.curoff)
2173 goto too_big;
2174 }
2175
2176 next:
2177 return 0;
2178
2179 too_big:
2180 return -1;
2181}
2182
Christopher Fauletc718b822017-09-21 16:50:56 +02002183/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2184 * handle fragmented content. On success it returns 1. If an error occurred, -1
2185 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2186 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002187static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002188spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002189 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002190{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002191 struct spoe_config *conf = FLT_CONF(ctx->filter);
2192 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002193 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002194 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002195
Christopher Faulet8ef75252017-02-20 22:56:03 +01002196 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002197 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002198
Christopher Fauletc718b822017-09-21 16:50:56 +02002199 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2200 /* Resume encoding of a SPOE message */
2201 if (ctx->frag_ctx.curmsg != NULL) {
2202 msg = ctx->frag_ctx.curmsg;
2203 goto encode_evt_message;
2204 }
2205
2206 list_for_each_entry(msg, messages, by_evt) {
2207 ctx->frag_ctx.curmsg = msg;
2208 ctx->frag_ctx.curarg = NULL;
2209 ctx->frag_ctx.curoff = UINT_MAX;
2210
2211 encode_evt_message:
2212 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2213 goto too_big;
2214 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002215 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002216 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2217 /* Resume encoding of a SPOE message */
2218 if (ctx->frag_ctx.curmsg != NULL) {
2219 msg = ctx->frag_ctx.curmsg;
2220 goto encode_grp_message;
2221 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002222
Christopher Fauletc718b822017-09-21 16:50:56 +02002223 list_for_each_entry(msg, messages, by_grp) {
2224 ctx->frag_ctx.curmsg = msg;
2225 ctx->frag_ctx.curarg = NULL;
2226 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002227
Christopher Fauletc718b822017-09-21 16:50:56 +02002228 encode_grp_message:
2229 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2230 goto too_big;
2231 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002233 else
2234 goto skip;
2235
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002236
Christopher Faulet57583e42017-09-04 15:41:09 +02002237 /* nothing has been encoded for an unfragmented payload */
2238 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2239 goto skip;
2240
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002241 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002242 " - encode %s messages - spoe_appctx=%p"
2243 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244 (int)now.tv_sec, (int)now.tv_usec,
2245 agent->id, __FUNCTION__, s,
2246 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002247 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002248 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249
Christopher Faulet8ef75252017-02-20 22:56:03 +01002250 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002251 ctx->frag_ctx.curmsg = NULL;
2252 ctx->frag_ctx.curarg = NULL;
2253 ctx->frag_ctx.curoff = 0;
2254 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002255
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002256 return 1;
2257
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002258 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002259 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2260 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2261 return -1;
2262 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263
2264 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 " - encode fragmented messages - spoe_appctx=%p"
2266 " - curmsg=%p - curarg=%p - curoff=%u"
2267 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002268 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002269 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002271 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002272
Christopher Faulet8ef75252017-02-20 22:56:03 +01002273 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2275 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2276 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002277
2278 skip:
2279 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2280 " - skip the frame because nothing has been encoded\n",
2281 (int)now.tv_sec, (int)now.tv_usec,
2282 agent->id, __FUNCTION__, s);
2283 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002284}
2285
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286
2287/***************************************************************************
2288 * Functions that handle SPOE actions
2289 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002290/* Helper function to set a variable */
2291static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002292spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002293 struct sample *smp)
2294{
2295 struct spoe_config *conf = FLT_CONF(ctx->filter);
2296 struct spoe_agent *agent = conf->agent;
2297 char varname[64];
2298
2299 memset(varname, 0, sizeof(varname));
2300 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2301 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002302 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2303 vars_set_by_name(varname, len, smp);
2304 else
2305 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002306}
2307
2308/* Helper function to unset a variable */
2309static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002310spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002311 struct sample *smp)
2312{
2313 struct spoe_config *conf = FLT_CONF(ctx->filter);
2314 struct spoe_agent *agent = conf->agent;
2315 char varname[64];
2316
2317 memset(varname, 0, sizeof(varname));
2318 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2319 scope, agent->var_pfx, len, name);
2320 vars_unset_by_name_ifexist(varname, len, smp);
2321}
2322
2323
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324static inline int
2325spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2326 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002327{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002328 char *str, *scope, *p = *buf;
2329 struct sample smp;
2330 uint64_t sz;
2331 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002332
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333 if (p + 2 >= end)
2334 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002335
Christopher Faulet8ef75252017-02-20 22:56:03 +01002336 /* SET-VAR requires 3 arguments */
2337 if (*p++ != 3)
2338 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340 switch (*p++) {
2341 case SPOE_SCOPE_PROC: scope = "proc"; break;
2342 case SPOE_SCOPE_SESS: scope = "sess"; break;
2343 case SPOE_SCOPE_TXN : scope = "txn"; break;
2344 case SPOE_SCOPE_REQ : scope = "req"; break;
2345 case SPOE_SCOPE_RES : scope = "res"; break;
2346 default: goto skip;
2347 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2350 goto skip;
2351 memset(&smp, 0, sizeof(smp));
2352 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 if (spoe_decode_data(&p, end, &smp) == -1)
2355 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002356
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2358 " - set-var '%s.%s.%.*s'\n",
2359 (int)now.tv_sec, (int)now.tv_usec,
2360 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2361 __FUNCTION__, s, scope,
2362 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2363 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002366
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367 ret = (p - *buf);
2368 *buf = p;
2369 return ret;
2370 skip:
2371 return 0;
2372}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374static inline int
2375spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2376 char **buf, char *end, int dir)
2377{
2378 char *str, *scope, *p = *buf;
2379 struct sample smp;
2380 uint64_t sz;
2381 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 if (p + 2 >= end)
2384 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002385
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386 /* UNSET-VAR requires 2 arguments */
2387 if (*p++ != 2)
2388 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 switch (*p++) {
2391 case SPOE_SCOPE_PROC: scope = "proc"; break;
2392 case SPOE_SCOPE_SESS: scope = "sess"; break;
2393 case SPOE_SCOPE_TXN : scope = "txn"; break;
2394 case SPOE_SCOPE_REQ : scope = "req"; break;
2395 case SPOE_SCOPE_RES : scope = "res"; break;
2396 default: goto skip;
2397 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002398
Christopher Faulet8ef75252017-02-20 22:56:03 +01002399 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2400 goto skip;
2401 memset(&smp, 0, sizeof(smp));
2402 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2405 " - unset-var '%s.%s.%.*s'\n",
2406 (int)now.tv_sec, (int)now.tv_usec,
2407 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2408 __FUNCTION__, s, scope,
2409 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2410 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412 spoe_unset_var(ctx, scope, str, sz, &smp);
2413
2414 ret = (p - *buf);
2415 *buf = p;
2416 return ret;
2417 skip:
2418 return 0;
2419}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002420
Christopher Faulet8ef75252017-02-20 22:56:03 +01002421/* Process SPOE actions for a specific event. It returns 1 on success. If an
2422 * error occurred, 0 is returned. */
2423static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002424spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002425{
2426 char *p, *end;
2427 int ret;
2428
2429 p = ctx->buffer->p;
2430 end = p + ctx->buffer->i;
2431
2432 while (p < end) {
2433 enum spoe_action_type type;
2434
2435 type = *p++;
2436 switch (type) {
2437 case SPOE_ACT_T_SET_VAR:
2438 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2439 if (!ret)
2440 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002441 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443 case SPOE_ACT_T_UNSET_VAR:
2444 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2445 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002446 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002447 break;
2448
2449 default:
2450 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002451 }
2452 }
2453
2454 return 1;
2455 skip:
2456 return 0;
2457}
2458
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002459/***************************************************************************
2460 * Functions that process SPOE events
2461 **************************************************************************/
2462static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002463spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002464{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002465 /* If a process is already started for this SPOE context, retry
2466 * later. */
2467 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002468 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002469
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002470 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002471 ctx->stats.tv_start = now;
2472 ctx->stats.tv_request = now;
2473 ctx->stats.t_request = -1;
2474 ctx->stats.t_queue = -1;
2475 ctx->stats.t_waiting = -1;
2476 ctx->stats.t_response = -1;
2477 ctx->stats.t_process = -1;
2478
2479 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002480
Christopher Fauleta1cda022016-12-21 08:58:06 +01002481 /* Set the right flag to prevent request and response processing
2482 * in same time. */
2483 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2484 ? SPOE_CTX_FL_REQ_PROCESS
2485 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002486 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002487}
2488
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002489static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002490spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002491{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002492 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002493
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002494 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2495 return;
2496
Christopher Faulet879dca92018-03-23 11:53:24 +01002497 if (sa) {
2498 if (sa->frag_ctx.ctx == ctx) {
2499 sa->frag_ctx.ctx = NULL;
2500 spoe_wakeup_appctx(sa->owner);
2501 }
2502 else
2503 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002504 }
2505
Christopher Fauleta1cda022016-12-21 08:58:06 +01002506 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002507 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002508 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002509
2510 /* Reset processing timer */
2511 ctx->process_exp = TICK_ETERNITY;
2512
Christopher Faulet8ef75252017-02-20 22:56:03 +01002513 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002514
Christopher Fauletfce747b2018-01-24 15:59:32 +01002515 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002516 ctx->frag_ctx.curmsg = NULL;
2517 ctx->frag_ctx.curarg = NULL;
2518 ctx->frag_ctx.curoff = 0;
2519 ctx->frag_ctx.flags = 0;
2520
Christopher Fauleta1cda022016-12-21 08:58:06 +01002521 if (!LIST_ISEMPTY(&ctx->list)) {
2522 LIST_DEL(&ctx->list);
2523 LIST_INIT(&ctx->list);
2524 }
2525}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002526
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002527static void
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002528spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2529 struct spoe_context *ctx, int dir)
2530{
2531 if (!tv_iszero(&ctx->stats.tv_start)) {
2532 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2533 ctx->stats.t_total += ctx->stats.t_process;
2534 tv_zero(&ctx->stats.tv_request);
2535 tv_zero(&ctx->stats.tv_queue);
2536 tv_zero(&ctx->stats.tv_wait);
2537 tv_zero(&ctx->stats.tv_response);
2538 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01002539
2540 if (agent->var_t_process) {
2541 struct sample smp;
2542
2543 memset(&smp, 0, sizeof(smp));
2544 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2545 smp.data.u.sint = ctx->stats.t_process;
2546 smp.data.type = SMP_T_SINT;
2547
2548 spoe_set_var(ctx, "txn", agent->var_t_process,
2549 strlen(agent->var_t_process), &smp);
2550 }
2551
2552 if (agent->var_t_total) {
2553 struct sample smp;
2554
2555 memset(&smp, 0, sizeof(smp));
2556 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2557 smp.data.u.sint = ctx->stats.t_total;
2558 smp.data.type = SMP_T_SINT;
2559
2560 spoe_set_var(ctx, "txn", agent->var_t_total,
2561 strlen(agent->var_t_total), &smp);
2562 }
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002563}
2564
2565static void
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002566spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2567 struct spoe_context *ctx, int dir)
2568{
2569 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002570 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002571
2572 if (agent->var_on_error) {
2573 struct sample smp;
2574
2575 memset(&smp, 0, sizeof(smp));
2576 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2577 smp.data.u.sint = ctx->status_code;
2578 smp.data.type = SMP_T_BOOL;
2579
2580 spoe_set_var(ctx, "txn", agent->var_on_error,
2581 strlen(agent->var_on_error), &smp);
2582 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002583
2584 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2585 ? SPOE_CTX_ST_READY
2586 : SPOE_CTX_ST_NONE);
2587}
2588
Christopher Faulet58d03682017-09-21 16:57:24 +02002589/* Process a list of SPOE messages. First, this functions will process messages
2590 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2591 * to process corresponding actions. During all the processing, it returns 0
2592 * and it returns 1 when the processing is finished. If an error occurred, -1
2593 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002594static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002595spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2596 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002597{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002598 struct spoe_config *conf = FLT_CONF(ctx->filter);
2599 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002600 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002601
2602 if (ctx->state == SPOE_CTX_ST_ERROR)
2603 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002604
2605 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2606 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002607 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002608 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002609 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002610 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002611 goto error;
2612 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002613
2614 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002616 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002617 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002618 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002619 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002620 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002621 goto skip;
2622 }
2623 }
2624
Christopher Fauletf7a30922016-11-10 15:04:51 +01002625 if (!tick_isset(ctx->process_exp)) {
2626 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2627 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2628 ctx->process_exp);
2629 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002630 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002631 if (!ret)
2632 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002633
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002634 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002636 }
2637
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002638 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002639 if (!tv_iszero(&ctx->stats.tv_request))
2640 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002641 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002642 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002643 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002644 if (ret < 0)
2645 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002646 if (!ret)
2647 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002648 if (spoe_queue_context(ctx) < 0)
2649 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002650 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2651 }
2652
2653 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002654 if (ctx->spoe_appctx)
2655 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002656 ret = 0;
2657 goto out;
2658 }
2659
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002660 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2661 ret = 0;
2662 goto out;
2663 }
2664
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002665 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002666 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002667 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002668 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002669 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002670 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002671 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002672 }
2673
2674 out:
2675 return ret;
2676
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002678 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002679 ret = 1;
2680 goto end;
2681
2682 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002683 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002684 ctx->state = SPOE_CTX_ST_READY;
2685 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002686
Christopher Fauleta1cda022016-12-21 08:58:06 +01002687 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002688 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002689 spoe_stop_processing(agent, ctx);
Christopher Faulet58d03682017-09-21 16:57:24 +02002690 return ret;
2691}
2692
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002693/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2694 * See spoe_process_message for details. */
2695static int
2696spoe_process_group(struct stream *s, struct spoe_context *ctx,
2697 struct spoe_group *group, int dir)
2698{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002699 struct spoe_config *conf = FLT_CONF(ctx->filter);
2700 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002701 int ret;
2702
2703 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2704 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002705 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002706 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2707 group->id);
2708
2709 if (LIST_ISEMPTY(&group->messages))
2710 return 1;
2711
2712 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002713 if (ret && ctx->stats.t_process != -1) {
2714 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2715 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2716 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2717 __FUNCTION__, s, s->uniq_id, ctx->status_code,
2718 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2719 ctx->stats.t_response, ctx->stats.t_process);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002720 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002721 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2722 agent->id, group->id, s->uniq_id, ctx->status_code,
2723 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2724 ctx->stats.t_response, ctx->stats.t_process);
2725 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002726 return ret;
2727}
2728
Christopher Faulet58d03682017-09-21 16:57:24 +02002729/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2730 * See spoe_process_message for details. */
2731static int
2732spoe_process_event(struct stream *s, struct spoe_context *ctx,
2733 enum spoe_event ev)
2734{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002735 struct spoe_config *conf = FLT_CONF(ctx->filter);
2736 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002737 int dir, ret;
2738
2739 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002740 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002741 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002742 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2743 spoe_event_str[ev]);
2744
2745 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2746
2747 if (LIST_ISEMPTY(&(ctx->events[ev])))
2748 return 1;
2749
2750 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002751 if (ret && ctx->stats.t_process != -1) {
2752 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2753 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2754 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2755 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2756 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2757 ctx->stats.t_response, ctx->stats.t_process);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002758 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002759 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2760 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2761 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2762 ctx->stats.t_response, ctx->stats.t_process);
2763 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002764 return ret;
2765}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766
2767/***************************************************************************
2768 * Functions that create/destroy SPOE contexts
2769 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002770static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002771spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002772{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002773 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002774 return 1;
2775
Christopher Faulet4596fb72017-01-11 14:05:19 +01002776 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002777 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002778 LIST_DEL(&buffer_wait->list);
2779 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002780 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002781 }
2782
Christopher Faulet4596fb72017-01-11 14:05:19 +01002783 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002784 return 1;
2785
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002786 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002787 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002788 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002789 return 0;
2790}
2791
2792static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002793spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002794{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002795 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002796 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002797 LIST_DEL(&buffer_wait->list);
2798 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002799 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002800 }
2801
2802 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002803 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002804 b_free(buf);
2805 offer_buffers(buffer_wait->target,
2806 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002807 }
2808}
2809
Christopher Faulet4596fb72017-01-11 14:05:19 +01002810static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002811spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002812{
2813 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2814 return 1;
2815}
2816
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002817static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002818spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002819{
2820 struct spoe_config *conf = FLT_CONF(filter);
2821 struct spoe_context *ctx;
2822
Willy Tarreaubafbe012017-11-24 17:34:44 +01002823 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002824 if (ctx == NULL) {
2825 return NULL;
2826 }
2827 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002828 ctx->filter = filter;
2829 ctx->state = SPOE_CTX_ST_NONE;
2830 ctx->status_code = SPOE_CTX_ERR_NONE;
2831 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002832 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002833 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002834 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002835 LIST_INIT(&ctx->buffer_wait.list);
2836 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002837 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002838 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002839
Christopher Fauletf7a30922016-11-10 15:04:51 +01002840 ctx->stream_id = 0;
2841 ctx->frame_id = 1;
2842 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002843
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002844 tv_zero(&ctx->stats.tv_start);
2845 tv_zero(&ctx->stats.tv_request);
2846 tv_zero(&ctx->stats.tv_queue);
2847 tv_zero(&ctx->stats.tv_wait);
2848 tv_zero(&ctx->stats.tv_response);
2849 ctx->stats.t_request = -1;
2850 ctx->stats.t_queue = -1;
2851 ctx->stats.t_waiting = -1;
2852 ctx->stats.t_response = -1;
2853 ctx->stats.t_process = -1;
2854 ctx->stats.t_total = 0;
2855
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002856 ctx->strm = s;
2857 ctx->state = SPOE_CTX_ST_READY;
2858 filter->ctx = ctx;
2859
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002860 return ctx;
2861}
2862
2863static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002864spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002865{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002866 struct spoe_config *conf = FLT_CONF(filter);
2867 struct spoe_context *ctx = filter->ctx;
2868
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002869 if (!ctx)
2870 return;
2871
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002872 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002873 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002874 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002875}
2876
2877static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002878spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002879{
2880 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002881 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002882
2883 tv_zero(&ctx->stats.tv_start);
2884 tv_zero(&ctx->stats.tv_request);
2885 tv_zero(&ctx->stats.tv_queue);
2886 tv_zero(&ctx->stats.tv_wait);
2887 tv_zero(&ctx->stats.tv_response);
2888 ctx->stats.t_request = -1;
2889 ctx->stats.t_queue = -1;
2890 ctx->stats.t_waiting = -1;
2891 ctx->stats.t_response = -1;
2892 ctx->stats.t_process = -1;
2893 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002894}
2895
2896
2897/***************************************************************************
2898 * Hooks that manage the filter lifecycle (init/check/deinit)
2899 **************************************************************************/
2900/* Signal handler: Do a soft stop, wakeup SPOE applet */
2901static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002902spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002903{
2904 struct proxy *p;
2905
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002906 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002907 while (p) {
2908 struct flt_conf *fconf;
2909
2910 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002911 struct spoe_config *conf;
2912 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002913 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002914 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002915
Christopher Faulet3b386a32017-02-23 10:17:15 +01002916 if (fconf->id != spoe_filter_id)
2917 continue;
2918
2919 conf = fconf->conf;
2920 agent = conf->agent;
2921
Christopher Faulet24289f22017-09-25 14:48:02 +02002922 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002923 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002924 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2925 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002926 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002927 }
2928 }
2929 p = p->next;
2930 }
2931}
2932
2933
2934/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2935static int
2936spoe_init(struct proxy *px, struct flt_conf *fconf)
2937{
2938 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002939
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002940 /* conf->agent_fe was already initialized during the config
2941 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002943 conf->agent_fe.cap = PR_CAP_FE;
2944 conf->agent_fe.mode = PR_MODE_TCP;
2945 conf->agent_fe.maxconn = 0;
2946 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2947 conf->agent_fe.conn_retries = CONN_RETRIES;
2948 conf->agent_fe.accept = frontend_accept;
2949 conf->agent_fe.srv = NULL;
2950 conf->agent_fe.timeout.client = TICK_ETERNITY;
2951 conf->agent_fe.default_target = &spoe_applet.obj_type;
2952 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2953
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002954 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002955 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956 sighandler_registered = 1;
2957 }
2958
2959 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002960}
2961
2962/* Free ressources allocated by the SPOE filter. */
2963static void
2964spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2965{
2966 struct spoe_config *conf = fconf->conf;
2967
2968 if (conf) {
2969 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002970
Christopher Faulet8ef75252017-02-20 22:56:03 +01002971 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002972 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002973 free(conf);
2974 }
2975 fconf->conf = NULL;
2976}
2977
2978/* Check configuration of a SPOE filter for a specified proxy.
2979 * Return 1 on error, else 0. */
2980static int
2981spoe_check(struct proxy *px, struct flt_conf *fconf)
2982{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002983 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002984 struct spoe_config *conf = fconf->conf;
2985 struct proxy *target;
2986
Christopher Faulet7ee86672017-09-19 11:08:28 +02002987 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2988 * are uniq */
2989 list_for_each_entry(f, &px->filter_configs, list) {
2990 struct spoe_config *c = f->conf;
2991
2992 /* This is not an SPOE filter */
2993 if (f->id != spoe_filter_id)
2994 continue;
2995 /* This is the current SPOE filter */
2996 if (f == fconf)
2997 continue;
2998
2999 /* Check engine Id. It should be uniq */
3000 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003001 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3002 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003003 return 1;
3004 }
3005 }
3006
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 target = proxy_be_by_name(conf->agent->b.name);
3008 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003009 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3010 " declared at %s:%d.\n",
3011 px->id, conf->agent->b.name, conf->agent->id,
3012 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013 return 1;
3014 }
3015 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003016 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3017 " at %s:%d does not support HTTP mode.\n",
3018 px->id, target->id, conf->agent->id,
3019 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003020 return 1;
3021 }
3022
3023 free(conf->agent->b.name);
3024 conf->agent->b.name = NULL;
3025 conf->agent->b.be = target;
3026 return 0;
3027}
3028
3029/**************************************************************************
3030 * Hooks attached to a stream
3031 *************************************************************************/
3032/* Called when a filter instance is created and attach to a stream. It creates
3033 * the context that will be used to process this stream. */
3034static int
3035spoe_start(struct stream *s, struct filter *filter)
3036{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003037 struct spoe_config *conf = FLT_CONF(filter);
3038 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003039 struct spoe_context *ctx;
3040
3041 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003042 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003043 __FUNCTION__, s);
3044
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003045 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3047 " - failed to create SPOE context\n",
3048 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003049 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003050 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003051 "SPOE: [%s] failed to create SPOE context\n",
3052 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053 return 0;
3054 }
3055
Christopher Faulet11610f32017-09-21 10:23:10 +02003056 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3058
Christopher Faulet11610f32017-09-21 10:23:10 +02003059 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003060 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3061
Christopher Faulet11610f32017-09-21 10:23:10 +02003062 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003063 filter->pre_analyzers |= AN_RES_INSPECT;
3064
Christopher Faulet11610f32017-09-21 10:23:10 +02003065 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003066 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3067
Christopher Faulet11610f32017-09-21 10:23:10 +02003068 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003069 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3070
Christopher Faulet11610f32017-09-21 10:23:10 +02003071 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003072 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3073
3074 return 1;
3075}
3076
3077/* Called when a filter instance is detached from a stream. It release the
3078 * attached SPOE context. */
3079static void
3080spoe_stop(struct stream *s, struct filter *filter)
3081{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3083 (int)now.tv_sec, (int)now.tv_usec,
3084 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3085 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003086 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003087}
3088
Christopher Fauletf7a30922016-11-10 15:04:51 +01003089
3090/*
3091 * Called when the stream is woken up because of expired timer.
3092 */
3093static void
3094spoe_check_timeouts(struct stream *s, struct filter *filter)
3095{
3096 struct spoe_context *ctx = filter->ctx;
3097
Christopher Fauletac580602018-03-20 16:09:20 +01003098 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003099 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003100}
3101
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003102/* Called when we are ready to filter data on a channel */
3103static int
3104spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3105{
3106 struct spoe_context *ctx = filter->ctx;
3107 int ret = 1;
3108
3109 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3110 " - ctx-flags=0x%08x\n",
3111 (int)now.tv_sec, (int)now.tv_usec,
3112 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3113 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3114
Christopher Fauletb067b062017-01-04 16:39:11 +01003115 if (ctx->state == SPOE_CTX_ST_NONE)
3116 goto out;
3117
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003118 if (!(chn->flags & CF_ISRESP)) {
3119 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3120 chn->analysers |= AN_REQ_INSPECT_FE;
3121 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3122 chn->analysers |= AN_REQ_INSPECT_BE;
3123
3124 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3125 goto out;
3126
3127 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003128 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003129 if (!ret)
3130 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003131 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3132 }
3133 else {
3134 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3135 chn->analysers |= AN_RES_INSPECT;
3136
3137 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3138 goto out;
3139
Christopher Faulet8ef75252017-02-20 22:56:03 +01003140 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003141 if (!ret) {
3142 channel_dont_read(chn);
3143 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003144 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003145 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003146 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003147 }
3148
3149 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003150 return ret;
3151}
3152
3153/* Called before a processing happens on a given channel */
3154static int
3155spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3156 struct channel *chn, unsigned an_bit)
3157{
3158 struct spoe_context *ctx = filter->ctx;
3159 int ret = 1;
3160
3161 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3162 " - ctx-flags=0x%08x - ana=0x%08x\n",
3163 (int)now.tv_sec, (int)now.tv_usec,
3164 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3165 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3166 ctx->flags, an_bit);
3167
Christopher Fauletb067b062017-01-04 16:39:11 +01003168 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169 goto out;
3170
3171 switch (an_bit) {
3172 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003173 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003174 break;
3175 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003176 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003177 break;
3178 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003179 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003180 break;
3181 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003182 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003183 break;
3184 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003185 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003186 break;
3187 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003188 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003189 break;
3190 }
3191
3192 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003193 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003194 channel_dont_read(chn);
3195 channel_dont_close(chn);
3196 }
3197 return ret;
3198}
3199
3200/* Called when the filtering on the channel ends. */
3201static int
3202spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3203{
3204 struct spoe_context *ctx = filter->ctx;
3205
3206 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3207 " - ctx-flags=0x%08x\n",
3208 (int)now.tv_sec, (int)now.tv_usec,
3209 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3210 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3211
3212 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003213 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 }
3215
3216 return 1;
3217}
3218
3219/********************************************************************
3220 * Functions that manage the filter initialization
3221 ********************************************************************/
3222struct flt_ops spoe_ops = {
3223 /* Manage SPOE filter, called for each filter declaration */
3224 .init = spoe_init,
3225 .deinit = spoe_deinit,
3226 .check = spoe_check,
3227
3228 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003229 .attach = spoe_start,
3230 .detach = spoe_stop,
3231 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003232
3233 /* Handle channels activity */
3234 .channel_start_analyze = spoe_start_analyze,
3235 .channel_pre_analyze = spoe_chn_pre_analyze,
3236 .channel_end_analyze = spoe_end_analyze,
3237};
3238
3239
3240static int
3241cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3242{
3243 const char *err;
3244 int i, err_code = 0;
3245
3246 if ((cfg_scope == NULL && curengine != NULL) ||
3247 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003248 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003249 goto out;
3250
3251 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3252 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003253 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3254 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 err_code |= ERR_ALERT | ERR_ABORT;
3256 goto out;
3257 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003258 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3259 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 goto out;
3261 }
3262
3263 err = invalid_char(args[1]);
3264 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003265 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3266 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003267 err_code |= ERR_ALERT | ERR_ABORT;
3268 goto out;
3269 }
3270
3271 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003272 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3273 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003274 err_code |= ERR_ALERT | ERR_ABORT;
3275 goto out;
3276 }
3277 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003278 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 err_code |= ERR_ALERT | ERR_ABORT;
3280 goto out;
3281 }
3282
3283 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003284
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003285 curagent->conf.file = strdup(file);
3286 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003287
3288 curagent->timeout.hello = TICK_ETERNITY;
3289 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003290 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003291
3292 curagent->engine_id = NULL;
3293 curagent->var_pfx = NULL;
3294 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003295 curagent->var_t_process = NULL;
3296 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003297 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3298 if (global.nbthread == 1)
3299 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003300 curagent->cps_max = 0;
3301 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003302 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003303 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304
3305 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003306 LIST_INIT(&curagent->events[i]);
3307 LIST_INIT(&curagent->groups);
3308 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003309
Christopher Faulet24289f22017-09-25 14:48:02 +02003310 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003311 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003312 err_code |= ERR_ALERT | ERR_ABORT;
3313 goto out;
3314 }
3315 for (i = 0; i < global.nbthread; ++i) {
3316 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003317 SPOE_DEBUG_STMT(curagent->rt[i].applets_act = 0);
3318 SPOE_DEBUG_STMT(curagent->rt[i].applets_idle = 0);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003319 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003320 LIST_INIT(&curagent->rt[i].applets);
3321 LIST_INIT(&curagent->rt[i].sending_queue);
3322 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003323 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003324 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 }
3326 else if (!strcmp(args[0], "use-backend")) {
3327 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003328 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3329 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003330 err_code |= ERR_ALERT | ERR_FATAL;
3331 goto out;
3332 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003333 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003334 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003335 free(curagent->b.name);
3336 curagent->b.name = strdup(args[1]);
3337 }
3338 else if (!strcmp(args[0], "messages")) {
3339 int cur_arg = 1;
3340 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003341 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342
Christopher Faulet11610f32017-09-21 10:23:10 +02003343 list_for_each_entry(ph, &curmphs, list) {
3344 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003345 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3346 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003347 err_code |= ERR_ALERT | ERR_FATAL;
3348 goto out;
3349 }
3350 }
3351
Christopher Faulet11610f32017-09-21 10:23:10 +02003352 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003353 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003354 err_code |= ERR_ALERT | ERR_ABORT;
3355 goto out;
3356 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003357 ph->id = strdup(args[cur_arg]);
3358 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003359 cur_arg++;
3360 }
3361 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003362 else if (!strcmp(args[0], "groups")) {
3363 int cur_arg = 1;
3364 while (*args[cur_arg]) {
3365 struct spoe_placeholder *ph = NULL;
3366
3367 list_for_each_entry(ph, &curgphs, list) {
3368 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003369 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3370 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003371 err_code |= ERR_ALERT | ERR_FATAL;
3372 goto out;
3373 }
3374 }
3375
3376 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003377 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003378 err_code |= ERR_ALERT | ERR_ABORT;
3379 goto out;
3380 }
3381 ph->id = strdup(args[cur_arg]);
3382 LIST_ADDQ(&curgphs, &ph->list);
3383 cur_arg++;
3384 }
3385 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003386 else if (!strcmp(args[0], "timeout")) {
3387 unsigned int *tv = NULL;
3388 const char *res;
3389 unsigned timeout;
3390
3391 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003392 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3393 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 err_code |= ERR_ALERT | ERR_FATAL;
3395 goto out;
3396 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003397 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3398 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 if (!strcmp(args[1], "hello"))
3400 tv = &curagent->timeout.hello;
3401 else if (!strcmp(args[1], "idle"))
3402 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003403 else if (!strcmp(args[1], "processing"))
3404 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003406 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3407 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003408 err_code |= ERR_ALERT | ERR_FATAL;
3409 goto out;
3410 }
3411 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003412 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3413 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003414 err_code |= ERR_ALERT | ERR_FATAL;
3415 goto out;
3416 }
3417 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3418 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003419 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3420 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003421 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 goto out;
3423 }
3424 *tv = MS_TO_TICKS(timeout);
3425 }
3426 else if (!strcmp(args[0], "option")) {
3427 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003428 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3429 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003430 err_code |= ERR_ALERT | ERR_FATAL;
3431 goto out;
3432 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003433
Christopher Faulet305c6072017-02-23 16:17:53 +01003434 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003435 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003436 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003437 if (kwm == 1)
3438 curagent->flags &= ~SPOE_FL_PIPELINING;
3439 else
3440 curagent->flags |= SPOE_FL_PIPELINING;
3441 goto out;
3442 }
3443 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003444 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003445 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003446 if (kwm == 1)
3447 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003448 else {
3449 if (global.nbthread == 1)
3450 curagent->flags |= SPOE_FL_ASYNC;
3451 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3453 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003454 err_code |= ERR_WARN;
3455 }
3456 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003457 goto out;
3458 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003459 else if (!strcmp(args[1], "send-frag-payload")) {
3460 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3461 goto out;
3462 if (kwm == 1)
3463 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3464 else
3465 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3466 goto out;
3467 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003468 else if (!strcmp(args[1], "dontlog-normal")) {
3469 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3470 goto out;
3471 if (kwm == 1)
3472 curpxopts2 &= ~PR_O2_NOLOGNORM;
3473 else
3474 curpxopts2 |= PR_O2_NOLOGNORM;
3475 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003476
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003477 /* Following options does not support negation */
3478 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003479 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3480 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003481 err_code |= ERR_ALERT | ERR_FATAL;
3482 goto out;
3483 }
3484
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003485 if (!strcmp(args[1], "var-prefix")) {
3486 char *tmp;
3487
3488 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003489 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3490 file, linenum, args[0],
3491 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003492 err_code |= ERR_ALERT | ERR_FATAL;
3493 goto out;
3494 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003495 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3496 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003497 tmp = args[2];
3498 while (*tmp) {
3499 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003500 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3501 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003502 err_code |= ERR_ALERT | ERR_FATAL;
3503 goto out;
3504 }
3505 tmp++;
3506 }
3507 curagent->var_pfx = strdup(args[2]);
3508 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003509 else if (!strcmp(args[1], "force-set-var")) {
3510 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3511 goto out;
3512 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3513 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003514 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003515 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003516 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003517 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3518 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003519 else if (!strcmp(args[1], "set-on-error")) {
3520 char *tmp;
3521
3522 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003523 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3524 file, linenum, args[0],
3525 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003526 err_code |= ERR_ALERT | ERR_FATAL;
3527 goto out;
3528 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003529 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3530 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003531 tmp = args[2];
3532 while (*tmp) {
3533 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003534 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3535 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003536 err_code |= ERR_ALERT | ERR_FATAL;
3537 goto out;
3538 }
3539 tmp++;
3540 }
3541 curagent->var_on_error = strdup(args[2]);
3542 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003543 else if (!strcmp(args[1], "set-process-time")) {
3544 char *tmp;
3545
3546 if (!*args[2]) {
3547 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3548 file, linenum, args[0],
3549 args[1]);
3550 err_code |= ERR_ALERT | ERR_FATAL;
3551 goto out;
3552 }
3553 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3554 goto out;
3555 tmp = args[2];
3556 while (*tmp) {
3557 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3558 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3559 file, linenum, args[0], args[1]);
3560 err_code |= ERR_ALERT | ERR_FATAL;
3561 goto out;
3562 }
3563 tmp++;
3564 }
3565 curagent->var_t_process = strdup(args[2]);
3566 }
3567 else if (!strcmp(args[1], "set-total-time")) {
3568 char *tmp;
3569
3570 if (!*args[2]) {
3571 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3572 file, linenum, args[0],
3573 args[1]);
3574 err_code |= ERR_ALERT | ERR_FATAL;
3575 goto out;
3576 }
3577 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3578 goto out;
3579 tmp = args[2];
3580 while (*tmp) {
3581 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3582 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3583 file, linenum, args[0], args[1]);
3584 err_code |= ERR_ALERT | ERR_FATAL;
3585 goto out;
3586 }
3587 tmp++;
3588 }
3589 curagent->var_t_total = strdup(args[2]);
3590 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003591 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003592 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3593 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003594 err_code |= ERR_ALERT | ERR_FATAL;
3595 goto out;
3596 }
Christopher Faulet48026722016-11-16 15:01:12 +01003597 }
3598 else if (!strcmp(args[0], "maxconnrate")) {
3599 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003600 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3601 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003602 err_code |= ERR_ALERT | ERR_FATAL;
3603 goto out;
3604 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003605 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003606 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003607 curagent->cps_max = atol(args[1]);
3608 }
3609 else if (!strcmp(args[0], "maxerrrate")) {
3610 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003611 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3612 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003613 err_code |= ERR_ALERT | ERR_FATAL;
3614 goto out;
3615 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003616 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003617 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003618 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003619 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003620 else if (!strcmp(args[0], "max-frame-size")) {
3621 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003622 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3623 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003624 err_code |= ERR_ALERT | ERR_FATAL;
3625 goto out;
3626 }
3627 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3628 goto out;
3629 curagent->max_frame_size = atol(args[1]);
3630 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3631 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003632 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3633 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003634 err_code |= ERR_ALERT | ERR_FATAL;
3635 goto out;
3636 }
3637 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003638 else if (!strcmp(args[0], "max-waiting-frames")) {
3639 if (!*args[1]) {
3640 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3641 file, linenum, args[0]);
3642 err_code |= ERR_ALERT | ERR_FATAL;
3643 goto out;
3644 }
3645 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3646 goto out;
3647 curagent->max_fpa = atol(args[1]);
3648 if (curagent->max_fpa < 1) {
3649 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3650 file, linenum, args[0]);
3651 err_code |= ERR_ALERT | ERR_FATAL;
3652 goto out;
3653 }
3654 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003655 else if (!strcmp(args[0], "register-var-names")) {
3656 int cur_arg;
3657
3658 if (!*args[1]) {
3659 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3660 file, linenum, args[0]);
3661 err_code |= ERR_ALERT | ERR_FATAL;
3662 goto out;
3663 }
3664 cur_arg = 1;
3665 while (*args[cur_arg]) {
3666 struct spoe_var_placeholder *vph;
3667
3668 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3669 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3670 err_code |= ERR_ALERT | ERR_ABORT;
3671 goto out;
3672 }
3673 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3674 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3675 err_code |= ERR_ALERT | ERR_ABORT;
3676 goto out;
3677 }
3678 LIST_ADDQ(&curvars, &vph->list);
3679 cur_arg++;
3680 }
3681 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003682 else if (!strcmp(args[0], "log")) {
3683 char *errmsg = NULL;
3684
3685 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3686 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
3690 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003691 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003692 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3693 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
3697 out:
3698 return err_code;
3699}
Christopher Faulet11610f32017-09-21 10:23:10 +02003700static int
3701cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3702{
3703 struct spoe_group *grp;
3704 const char *err;
3705 int err_code = 0;
3706
3707 if ((cfg_scope == NULL && curengine != NULL) ||
3708 (cfg_scope != NULL && curengine == NULL) ||
3709 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3710 goto out;
3711
3712 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3713 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003714 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3715 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003716 err_code |= ERR_ALERT | ERR_ABORT;
3717 goto out;
3718 }
3719 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3720 err_code |= ERR_ABORT;
3721 goto out;
3722 }
3723
3724 err = invalid_char(args[1]);
3725 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003726 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3727 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003728 err_code |= ERR_ALERT | ERR_ABORT;
3729 goto out;
3730 }
3731
3732 list_for_each_entry(grp, &curgrps, list) {
3733 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003734 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3735 " name as another one declared at %s:%d.\n",
3736 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003737 err_code |= ERR_ALERT | ERR_FATAL;
3738 goto out;
3739 }
3740 }
3741
3742 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003743 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003744 err_code |= ERR_ALERT | ERR_ABORT;
3745 goto out;
3746 }
3747
3748 curgrp->id = strdup(args[1]);
3749 curgrp->conf.file = strdup(file);
3750 curgrp->conf.line = linenum;
3751 LIST_INIT(&curgrp->phs);
3752 LIST_INIT(&curgrp->messages);
3753 LIST_ADDQ(&curgrps, &curgrp->list);
3754 }
3755 else if (!strcmp(args[0], "messages")) {
3756 int cur_arg = 1;
3757 while (*args[cur_arg]) {
3758 struct spoe_placeholder *ph = NULL;
3759
3760 list_for_each_entry(ph, &curgrp->phs, list) {
3761 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003762 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3763 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003764 err_code |= ERR_ALERT | ERR_FATAL;
3765 goto out;
3766 }
3767 }
3768
3769 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003770 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003771 err_code |= ERR_ALERT | ERR_ABORT;
3772 goto out;
3773 }
3774 ph->id = strdup(args[cur_arg]);
3775 LIST_ADDQ(&curgrp->phs, &ph->list);
3776 cur_arg++;
3777 }
3778 }
3779 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003780 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3781 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003782 err_code |= ERR_ALERT | ERR_FATAL;
3783 goto out;
3784 }
3785 out:
3786 return err_code;
3787}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003788
3789static int
3790cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3791{
3792 struct spoe_message *msg;
3793 struct spoe_arg *arg;
3794 const char *err;
3795 char *errmsg = NULL;
3796 int err_code = 0;
3797
3798 if ((cfg_scope == NULL && curengine != NULL) ||
3799 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003800 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003801 goto out;
3802
3803 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3804 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003805 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3806 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003807 err_code |= ERR_ALERT | ERR_ABORT;
3808 goto out;
3809 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003810 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3811 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003812 goto out;
3813 }
3814
3815 err = invalid_char(args[1]);
3816 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003817 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3818 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003819 err_code |= ERR_ALERT | ERR_ABORT;
3820 goto out;
3821 }
3822
3823 list_for_each_entry(msg, &curmsgs, list) {
3824 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003825 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3826 " name as another one declared at %s:%d.\n",
3827 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003828 err_code |= ERR_ALERT | ERR_FATAL;
3829 goto out;
3830 }
3831 }
3832
3833 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003834 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003835 err_code |= ERR_ALERT | ERR_ABORT;
3836 goto out;
3837 }
3838
3839 curmsg->id = strdup(args[1]);
3840 curmsg->id_len = strlen(curmsg->id);
3841 curmsg->event = SPOE_EV_NONE;
3842 curmsg->conf.file = strdup(file);
3843 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003844 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003845 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003846 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003847 LIST_INIT(&curmsg->by_evt);
3848 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003849 LIST_ADDQ(&curmsgs, &curmsg->list);
3850 }
3851 else if (!strcmp(args[0], "args")) {
3852 int cur_arg = 1;
3853
3854 curproxy->conf.args.ctx = ARGC_SPOE;
3855 curproxy->conf.args.file = file;
3856 curproxy->conf.args.line = linenum;
3857 while (*args[cur_arg]) {
3858 char *delim = strchr(args[cur_arg], '=');
3859 int idx = 0;
3860
3861 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003863 err_code |= ERR_ALERT | ERR_ABORT;
3864 goto out;
3865 }
3866
3867 if (!delim) {
3868 arg->name = NULL;
3869 arg->name_len = 0;
3870 delim = args[cur_arg];
3871 }
3872 else {
3873 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3874 arg->name_len = delim - args[cur_arg];
3875 delim++;
3876 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003877 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3878 &idx, file, linenum, &errmsg,
3879 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003880 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003881 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003882 err_code |= ERR_ALERT | ERR_FATAL;
3883 free(arg->name);
3884 free(arg);
3885 goto out;
3886 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003887 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003888 LIST_ADDQ(&curmsg->args, &arg->list);
3889 cur_arg++;
3890 }
3891 curproxy->conf.args.file = NULL;
3892 curproxy->conf.args.line = 0;
3893 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003894 else if (!strcmp(args[0], "acl")) {
3895 err = invalid_char(args[1]);
3896 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003897 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3898 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003899 err_code |= ERR_ALERT | ERR_FATAL;
3900 goto out;
3901 }
3902 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003903 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3904 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003905 err_code |= ERR_ALERT | ERR_FATAL;
3906 goto out;
3907 }
3908 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003909 else if (!strcmp(args[0], "event")) {
3910 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003911 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003912 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003913 goto out;
3914 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003915 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3916 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003917
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3919 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3920 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3921 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3922
3923 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3924 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3925 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3926 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3927 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3928 curmsg->event = SPOE_EV_ON_TCP_RSP;
3929
3930 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3931 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3932 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3933 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3934 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3935 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3936 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003937 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3938 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003939 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003940 goto out;
3941 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003942
3943 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3944 struct acl_cond *cond;
3945
3946 cond = build_acl_cond(file, linenum, &curmsg->acls,
3947 curproxy, (const char **)args+2,
3948 &errmsg);
3949 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003950 ha_alert("parsing [%s:%d] : error detected while "
3951 "parsing an 'event %s' condition : %s.\n",
3952 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003953 err_code |= ERR_ALERT | ERR_FATAL;
3954 goto out;
3955 }
3956 curmsg->cond = cond;
3957 }
3958 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003959 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3960 "or 'unless' followed by a condition but found '%s'.\n",
3961 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003962 err_code |= ERR_ALERT | ERR_FATAL;
3963 goto out;
3964 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 }
3966 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003967 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3968 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003969 err_code |= ERR_ALERT | ERR_FATAL;
3970 goto out;
3971 }
3972 out:
3973 free(errmsg);
3974 return err_code;
3975}
3976
3977/* Return -1 on error, else 0 */
3978static int
3979parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3980 struct flt_conf *fconf, char **err, void *private)
3981{
3982 struct list backup_sections;
3983 struct spoe_config *conf;
3984 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003985 struct spoe_group *grp, *grpback;
3986 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003987 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003988 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003989 char *file = NULL, *engine = NULL;
3990 int ret, pos = *cur_arg + 1;
3991
Christopher Faulet84c844e2018-03-23 14:37:14 +01003992 LIST_INIT(&curmsgs);
3993 LIST_INIT(&curgrps);
3994 LIST_INIT(&curmphs);
3995 LIST_INIT(&curgphs);
3996 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003997 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003998 curpxopts = 0;
3999 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004000
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004001 conf = calloc(1, sizeof(*conf));
4002 if (conf == NULL) {
4003 memprintf(err, "%s: out of memory", args[*cur_arg]);
4004 goto error;
4005 }
4006 conf->proxy = px;
4007
4008 while (*args[pos]) {
4009 if (!strcmp(args[pos], "config")) {
4010 if (!*args[pos+1]) {
4011 memprintf(err, "'%s' : '%s' option without value",
4012 args[*cur_arg], args[pos]);
4013 goto error;
4014 }
4015 file = args[pos+1];
4016 pos += 2;
4017 }
4018 else if (!strcmp(args[pos], "engine")) {
4019 if (!*args[pos+1]) {
4020 memprintf(err, "'%s' : '%s' option without value",
4021 args[*cur_arg], args[pos]);
4022 goto error;
4023 }
4024 engine = args[pos+1];
4025 pos += 2;
4026 }
4027 else {
4028 memprintf(err, "unknown keyword '%s'", args[pos]);
4029 goto error;
4030 }
4031 }
4032 if (file == NULL) {
4033 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4034 goto error;
4035 }
4036
4037 /* backup sections and register SPOE sections */
4038 LIST_INIT(&backup_sections);
4039 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004040 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4041 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004042 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004043
4044 /* Parse SPOE filter configuration file */
4045 curengine = engine;
4046 curproxy = px;
4047 curagent = NULL;
4048 curmsg = NULL;
4049 ret = readcfgfile(file);
4050 curproxy = NULL;
4051
4052 /* unregister SPOE sections and restore previous sections */
4053 cfg_unregister_sections();
4054 cfg_restore_sections(&backup_sections);
4055
4056 if (ret == -1) {
4057 memprintf(err, "Could not open configuration file %s : %s",
4058 file, strerror(errno));
4059 goto error;
4060 }
4061 if (ret & (ERR_ABORT|ERR_FATAL)) {
4062 memprintf(err, "Error(s) found in configuration file %s", file);
4063 goto error;
4064 }
4065
4066 /* Check SPOE agent */
4067 if (curagent == NULL) {
4068 memprintf(err, "No SPOE agent found in file %s", file);
4069 goto error;
4070 }
4071 if (curagent->b.name == NULL) {
4072 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4073 curagent->id, curagent->conf.file, curagent->conf.line);
4074 goto error;
4075 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004076 if (curagent->timeout.hello == TICK_ETERNITY ||
4077 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004078 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004079 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4080 " | While not properly invalid, you will certainly encounter various problems\n"
4081 " | with such a configuration. To fix this, please ensure that all following\n"
4082 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4083 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004084 }
4085 if (curagent->var_pfx == NULL) {
4086 char *tmp = curagent->id;
4087
4088 while (*tmp) {
4089 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4090 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4091 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4092 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4093 goto error;
4094 }
4095 tmp++;
4096 }
4097 curagent->var_pfx = strdup(curagent->id);
4098 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004099 if (curagent->engine_id == NULL)
4100 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004101
Christopher Fauletb7426d12018-03-21 14:12:17 +01004102 if (curagent->var_on_error) {
4103 struct arg arg;
4104
4105 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4106 curagent->var_pfx, curagent->var_on_error);
4107
4108 arg.type = ARGT_STR;
4109 arg.data.str.str = trash.str;
4110 arg.data.str.len = trash.len;
4111 if (!vars_check_arg(&arg, err)) {
4112 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4113 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4114 goto error;
4115 }
4116 }
4117
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004118 if (curagent->var_t_process) {
4119 struct arg arg;
4120
4121 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4122 curagent->var_pfx, curagent->var_t_process);
4123
4124 arg.type = ARGT_STR;
4125 arg.data.str.str = trash.str;
4126 arg.data.str.len = trash.len;
4127 if (!vars_check_arg(&arg, err)) {
4128 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4129 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4130 goto error;
4131 }
4132 }
4133
4134 if (curagent->var_t_total) {
4135 struct arg arg;
4136
4137 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4138 curagent->var_pfx, curagent->var_t_total);
4139
4140 arg.type = ARGT_STR;
4141 arg.data.str.str = trash.str;
4142 arg.data.str.len = trash.len;
4143 if (!vars_check_arg(&arg, err)) {
4144 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4145 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4146 goto error;
4147 }
4148 }
4149
Christopher Faulet11610f32017-09-21 10:23:10 +02004150 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004151 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4152 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004153 goto finish;
4154 }
4155
Christopher Faulet11610f32017-09-21 10:23:10 +02004156 /* Replace placeholders by the corresponding messages for the SPOE
4157 * agent */
4158 list_for_each_entry(ph, &curmphs, list) {
4159 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004160 struct spoe_arg *arg;
4161 unsigned int where;
4162
Christopher Faulet11610f32017-09-21 10:23:10 +02004163 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004164 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4165 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4166 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4167 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4168 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4169 }
4170 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4171 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4172 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004173 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4174 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004175 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004176 }
4177 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004178 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4179 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004180 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004181 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004182
4183 where = 0;
4184 switch (msg->event) {
4185 case SPOE_EV_ON_CLIENT_SESS:
4186 where |= SMP_VAL_FE_CON_ACC;
4187 break;
4188
4189 case SPOE_EV_ON_TCP_REQ_FE:
4190 where |= SMP_VAL_FE_REQ_CNT;
4191 break;
4192
4193 case SPOE_EV_ON_HTTP_REQ_FE:
4194 where |= SMP_VAL_FE_HRQ_HDR;
4195 break;
4196
4197 case SPOE_EV_ON_TCP_REQ_BE:
4198 if (px->cap & PR_CAP_FE)
4199 where |= SMP_VAL_FE_REQ_CNT;
4200 if (px->cap & PR_CAP_BE)
4201 where |= SMP_VAL_BE_REQ_CNT;
4202 break;
4203
4204 case SPOE_EV_ON_HTTP_REQ_BE:
4205 if (px->cap & PR_CAP_FE)
4206 where |= SMP_VAL_FE_HRQ_HDR;
4207 if (px->cap & PR_CAP_BE)
4208 where |= SMP_VAL_BE_HRQ_HDR;
4209 break;
4210
4211 case SPOE_EV_ON_SERVER_SESS:
4212 where |= SMP_VAL_BE_SRV_CON;
4213 break;
4214
4215 case SPOE_EV_ON_TCP_RSP:
4216 if (px->cap & PR_CAP_FE)
4217 where |= SMP_VAL_FE_RES_CNT;
4218 if (px->cap & PR_CAP_BE)
4219 where |= SMP_VAL_BE_RES_CNT;
4220 break;
4221
4222 case SPOE_EV_ON_HTTP_RSP:
4223 if (px->cap & PR_CAP_FE)
4224 where |= SMP_VAL_FE_HRS_HDR;
4225 if (px->cap & PR_CAP_BE)
4226 where |= SMP_VAL_BE_HRS_HDR;
4227 break;
4228
4229 default:
4230 break;
4231 }
4232
4233 list_for_each_entry(arg, &msg->args, list) {
4234 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004235 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004236 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004237 "none of which is available here ('%s')",
4238 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004239 sample_ckp_names(arg->expr->fetch->use),
4240 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004241 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004242 }
4243 }
4244
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004245 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004246 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4247 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004248 }
4249 }
4250 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004251 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004252 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004253 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004254 continue;
4255 }
4256
Christopher Faulet11610f32017-09-21 10:23:10 +02004257 /* Replace placeholders by the corresponding groups for the SPOE
4258 * agent */
4259 list_for_each_entry(ph, &curgphs, list) {
4260 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4261 if (!strcmp(grp->id, ph->id)) {
4262 grp->agent = curagent;
4263 LIST_DEL(&grp->list);
4264 LIST_ADDQ(&curagent->groups, &grp->list);
4265 goto next_aph;
4266 }
4267 }
4268 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4269 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4270 goto error;
4271 next_aph:
4272 continue;
4273 }
4274
4275 /* Replace placeholders by the corresponding message for each SPOE
4276 * group of the SPOE agent */
4277 list_for_each_entry(grp, &curagent->groups, list) {
4278 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4279 list_for_each_entry(msg, &curmsgs, list) {
4280 if (!strcmp(msg->id, ph->id)) {
4281 if (msg->group != NULL) {
4282 memprintf(err, "SPOE message '%s' already belongs to "
4283 "the SPOE group '%s' declare at %s:%d",
4284 msg->id, msg->group->id,
4285 msg->group->conf.file,
4286 msg->group->conf.line);
4287 goto error;
4288 }
4289
4290 /* Scope for arguments are not checked for now. We will check
4291 * them only if a rule use the corresponding SPOE group. */
4292 msg->agent = curagent;
4293 msg->group = grp;
4294 LIST_DEL(&ph->list);
4295 LIST_ADDQ(&grp->messages, &msg->by_grp);
4296 goto next_mph_grp;
4297 }
4298 }
4299 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4300 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4301 goto error;
4302 next_mph_grp:
4303 continue;
4304 }
4305 }
4306
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004307 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004308 /* move curmsgs to the agent message list */
4309 curmsgs.n->p = &curagent->messages;
4310 curmsgs.p->n = &curagent->messages;
4311 curagent->messages = curmsgs;
4312 LIST_INIT(&curmsgs);
4313
Christopher Faulet7ee86672017-09-19 11:08:28 +02004314 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004315 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004316
4317 /* Start agent's proxy initialization here. It will be finished during
4318 * the filter init. */
4319 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4320 init_new_proxy(&conf->agent_fe);
4321 conf->agent_fe.id = conf->agent->id;
4322 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004323 conf->agent_fe.options |= curpxopts;
4324 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004325
4326 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4327 LIST_DEL(&logsrv->list);
4328 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4329 }
4330
Christopher Faulet11610f32017-09-21 10:23:10 +02004331 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4332 LIST_DEL(&ph->list);
4333 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004334 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004335 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4336 LIST_DEL(&ph->list);
4337 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004338 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004339 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4340 struct arg arg;
4341
4342 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4343 curagent->var_pfx, vph->name);
4344
4345 arg.type = ARGT_STR;
4346 arg.data.str.str = trash.str;
4347 arg.data.str.len = trash.len;
4348 if (!vars_check_arg(&arg, err)) {
4349 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4350 curagent->id, curagent->var_pfx, vph->name, *err);
4351 goto error;
4352 }
4353
4354 LIST_DEL(&vph->list);
4355 free(vph->name);
4356 free(vph);
4357 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004358 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4359 LIST_DEL(&grp->list);
4360 spoe_release_group(grp);
4361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004362 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004363 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004364 fconf->ops = &spoe_ops;
4365 fconf->conf = conf;
4366 return 0;
4367
4368 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004369 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004370 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4371 LIST_DEL(&ph->list);
4372 spoe_release_placeholder(ph);
4373 }
4374 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4375 LIST_DEL(&ph->list);
4376 spoe_release_placeholder(ph);
4377 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004378 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4379 LIST_DEL(&vph->list);
4380 free(vph->name);
4381 free(vph);
4382 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004383 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4384 LIST_DEL(&grp->list);
4385 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004386 }
4387 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4388 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004389 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004390 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004391 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4392 LIST_DEL(&logsrv->list);
4393 free(logsrv);
4394 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004395 free(conf);
4396 return -1;
4397}
4398
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004399/* Send message of a SPOE group. This is the action_ptr callback of a rule
4400 * associated to a "send-spoe-group" action.
4401 *
4402 * It returns ACT_RET_CONT is processing is finished without error, it returns
4403 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4404 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004405static enum act_return
4406spoe_send_group(struct act_rule *rule, struct proxy *px,
4407 struct session *sess, struct stream *s, int flags)
4408{
4409 struct filter *filter;
4410 struct spoe_agent *agent = NULL;
4411 struct spoe_group *group = NULL;
4412 struct spoe_context *ctx = NULL;
4413 int ret, dir;
4414
4415 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4416 if (filter->config == rule->arg.act.p[0]) {
4417 agent = rule->arg.act.p[2];
4418 group = rule->arg.act.p[3];
4419 ctx = filter->ctx;
4420 break;
4421 }
4422 }
4423 if (agent == NULL || group == NULL || ctx == NULL)
4424 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004425 if (ctx->state == SPOE_CTX_ST_NONE)
4426 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004427
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004428 switch (rule->from) {
4429 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4430 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4431 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4432 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4433 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4434 default:
4435 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4436 " - internal error while execute spoe-send-group\n",
4437 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4438 __FUNCTION__, s);
4439 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4440 agent->id);
4441 return ACT_RET_CONT;
4442 }
4443
4444 ret = spoe_process_group(s, ctx, group, dir);
4445 if (ret == 1)
4446 return ACT_RET_CONT;
4447 else if (ret == 0) {
4448 if (flags & ACT_FLAG_FINAL) {
4449 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4450 " - failed to process group '%s': interrupted by caller\n",
4451 (int)now.tv_sec, (int)now.tv_usec,
4452 agent->id, __FUNCTION__, s, group->id);
4453 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4454 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004455 spoe_stop_processing(agent, ctx);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004456 return ACT_RET_CONT;
4457 }
4458 return ACT_RET_YIELD;
4459 }
4460 else
4461 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004462}
4463
4464/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4465 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4466 * action should be:
4467 *
4468 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4469 *
4470 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4471 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4472 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4473 * group.
4474 *
4475 * The function returns 1 in success case, otherwise, it returns 0 and err is
4476 * filled.
4477 */
4478static int
4479check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4480{
4481 struct flt_conf *fconf;
4482 struct spoe_config *conf;
4483 struct spoe_agent *agent = NULL;
4484 struct spoe_group *group;
4485 struct spoe_message *msg;
4486 char *engine_id = rule->arg.act.p[0];
4487 char *group_id = rule->arg.act.p[1];
4488 unsigned int where = 0;
4489
4490 switch (rule->from) {
4491 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4492 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4493 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4494 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4495 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4496 default:
4497 memprintf(err,
4498 "internal error, unexpected rule->from=%d, please report this bug!",
4499 rule->from);
4500 goto error;
4501 }
4502
4503 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4504 * <px> */
4505 list_for_each_entry(fconf, &px->filter_configs, list) {
4506 conf = fconf->conf;
4507
4508 /* This is not an SPOE filter */
4509 if (fconf->id != spoe_filter_id)
4510 continue;
4511
4512 /* This is the good engine */
4513 if (!strcmp(conf->id, engine_id)) {
4514 agent = conf->agent;
4515 break;
4516 }
4517 }
4518 if (agent == NULL) {
4519 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4520 engine_id, group_id);
4521 goto error;
4522 }
4523
4524 /* Try to find the right group */
4525 list_for_each_entry(group, &agent->groups, list) {
4526 /* This is the good group */
4527 if (!strcmp(group->id, group_id))
4528 break;
4529 }
4530 if (&group->list == &agent->groups) {
4531 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4532 group_id, engine_id);
4533 goto error;
4534 }
4535
4536 /* Ok, we found the group, we need to check messages and their
4537 * arguments */
4538 list_for_each_entry(msg, &group->messages, by_grp) {
4539 struct spoe_arg *arg;
4540
4541 list_for_each_entry(arg, &msg->args, list) {
4542 if (!(arg->expr->fetch->val & where)) {
4543 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4544 "some args extract information from '%s',"
4545 "none of which is available here ('%s')",
4546 msg->id, group->id, msg->conf.file, msg->conf.line,
4547 sample_ckp_names(arg->expr->fetch->use),
4548 sample_ckp_names(where));
4549 goto error;
4550 }
4551 }
4552 }
4553
4554 free(engine_id);
4555 free(group_id);
4556 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4557 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4558 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4559 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4560 return 1;
4561
4562 error:
4563 free(engine_id);
4564 free(group_id);
4565 return 0;
4566}
4567
4568/* Parse 'send-spoe-group' action following the format:
4569 *
4570 * ... send-spoe-group <engine-id> <group-id>
4571 *
4572 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4573 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4574 * ids are saved and used later, when the rule will be checked.
4575 */
4576static enum act_parse_ret
4577parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4578 struct act_rule *rule, char **err)
4579{
4580 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4581 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4582 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4583 return ACT_RET_PRS_ERR;
4584 }
4585 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4586 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4587
4588 (*orig_arg) += 2;
4589
4590 rule->action = ACT_CUSTOM;
4591 rule->action_ptr = spoe_send_group;
4592 rule->check_ptr = check_send_spoe_group;
4593 return ACT_RET_PRS_OK;
4594}
4595
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004596
4597/* Declare the filter parser for "spoe" keyword */
4598static struct flt_kw_list flt_kws = { "SPOE", { }, {
4599 { "spoe", parse_spoe_flt, NULL },
4600 { NULL, NULL, NULL },
4601 }
4602};
4603
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004604/* Delcate the action parser for "spoe-action" keyword */
4605static struct action_kw_list tcp_req_action_kws = { { }, {
4606 { "send-spoe-group", parse_send_spoe_group },
4607 { /* END */ },
4608 }
4609};
4610static struct action_kw_list tcp_res_action_kws = { { }, {
4611 { "send-spoe-group", parse_send_spoe_group },
4612 { /* END */ },
4613 }
4614};
4615static struct action_kw_list http_req_action_kws = { { }, {
4616 { "send-spoe-group", parse_send_spoe_group },
4617 { /* END */ },
4618 }
4619};
4620static struct action_kw_list http_res_action_kws = { { }, {
4621 { "send-spoe-group", parse_send_spoe_group },
4622 { /* END */ },
4623 }
4624};
4625
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004626__attribute__((constructor))
4627static void __spoe_init(void)
4628{
4629 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004630 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4631 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4632 http_req_keywords_register(&http_req_action_kws);
4633 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004634
Willy Tarreaubafbe012017-11-24 17:34:44 +01004635 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4636 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004637}
4638
4639__attribute__((destructor))
4640static void
4641__spoe_deinit(void)
4642{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004643 pool_destroy(pool_head_spoe_ctx);
4644 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004645}