blob: cdb6238f82ef8e40d2dcc4d741d2efbc2e3c1420 [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)
49#else
50#define SPOE_PRINTF(x...)
51#endif
52
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010053/* Reserved 4 bytes to the frame size. So a frame and its size can be written
54 * together in a buffer */
55#define MAX_FRAME_SIZE global.tune.bufsize - 4
56
57/* The minimum size for a frame */
58#define MIN_FRAME_SIZE 256
59
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Reserved for the metadata and the frame type.
61 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010062#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020063
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010065#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
66
Christopher Faulet3b386a32017-02-23 10:17:15 +010067/* SPOE filter id. Used to identify SPOE filters */
68const char *spoe_filter_id = "SPOE filter";
69
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020070/* Set if the handle on SIGUSR1 is registered */
71static int sighandler_registered = 0;
72
73/* proxy used during the parsing */
74struct proxy *curproxy = NULL;
75
76/* The name of the SPOE engine, used during the parsing */
77char *curengine = NULL;
78
79/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020080/* SPOE agent/group/message used during the parsing */
81struct spoe_agent *curagent = NULL;
82struct spoe_group *curgrp = NULL;
83struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020084
85/* list of SPOE messages and placeholders used during the parsing */
86struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020087struct list curgrps;
88struct list curmphs;
89struct list curgphs;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020090
Christopher Faulet42bfa462017-01-04 14:14:19 +010091/* Pools used to allocate SPOE structs */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020092static struct pool_head *pool2_spoe_ctx = NULL;
Christopher Faulet42bfa462017-01-04 14:14:19 +010093static struct pool_head *pool2_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020094
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095struct flt_ops spoe_ops;
96
Christopher Faulet8ef75252017-02-20 22:56:03 +010097static int spoe_queue_context(struct spoe_context *ctx);
98static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
99static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100
101/********************************************************************
102 * helper functions/globals
103 ********************************************************************/
104static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200105spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106{
Christopher Faulet11610f32017-09-21 10:23:10 +0200107 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200108 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200109 free(ph->id);
110 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111}
112
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100114spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200115{
Christopher Faulet57583e42017-09-04 15:41:09 +0200116 struct spoe_arg *arg, *argback;
117 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118
119 if (!msg)
120 return;
121 free(msg->id);
122 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200123 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124 release_sample_expr(arg->expr);
125 free(arg->name);
126 LIST_DEL(&arg->list);
127 free(arg);
128 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200129 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
130 LIST_DEL(&acl->list);
131 prune_acl(acl);
132 free(acl);
133 }
134 if (msg->cond) {
135 prune_acl_cond(msg->cond);
136 free(msg->cond);
137 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200138 free(msg);
139}
140
141static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200142spoe_release_group(struct spoe_group *grp)
143{
144 if (!grp)
145 return;
146 free(grp->id);
147 free(grp->conf.file);
148 free(grp);
149}
150
151static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100152spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200153{
Christopher Faulet11610f32017-09-21 10:23:10 +0200154 struct spoe_message *msg, *msgback;
155 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200156 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200157
158 if (!agent)
159 return;
160 free(agent->id);
161 free(agent->conf.file);
162 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100163 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100164 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200165 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
166 LIST_DEL(&msg->list);
167 spoe_release_message(msg);
168 }
169 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
170 LIST_DEL(&grp->list);
171 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200172 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200173 for (i = 0; i < global.nbthread; ++i)
174 SPIN_DESTROY(&agent->rt[i].lock);
175 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200176 free(agent);
177}
178
179static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100180 [SPOE_FRM_ERR_NONE] = "normal",
181 [SPOE_FRM_ERR_IO] = "I/O error",
182 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
183 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
184 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
185 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
186 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
187 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
188 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
189 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
190 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
191 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100192 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100193 [SPOE_FRM_ERR_RES] = "resource allocation error",
194 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200195};
196
197static const char *spoe_event_str[SPOE_EV_EVENTS] = {
198 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
199 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
200 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
201 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
202 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
203
204 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
205 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
206 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
207};
208
209
210#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
211
212static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100213 [SPOE_CTX_ST_NONE] = "NONE",
214 [SPOE_CTX_ST_READY] = "READY",
215 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
216 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
217 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
218 [SPOE_CTX_ST_DONE] = "DONE",
219 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200220};
221
222static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100223 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
224 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
225 [SPOE_APPCTX_ST_IDLE] = "IDLE",
226 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
227 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
228 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
229 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
230 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
231 [SPOE_APPCTX_ST_EXIT] = "EXIT",
232 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200233};
234
235#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100236
Christopher Faulet8ef75252017-02-20 22:56:03 +0100237/* Used to generates a unique id for an engine. On success, it returns a
238 * allocated string. So it is the caller's reponsibility to release it. If the
239 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100240static char *
241generate_pseudo_uuid()
242{
243 static int init = 0;
244
245 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
246 const char uuid_chr[] = "0123456789ABCDEF-";
247 char *uuid;
248 int i;
249
250 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
251 return NULL;
252
253 if (!init) {
254 srand(now_ms);
255 init = 1;
256 }
257
258 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
259 int r = rand () % 16;
260
261 switch (uuid_fmt[i]) {
262 case 'x' : uuid[i] = uuid_chr[r]; break;
263 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
264 default : uuid[i] = uuid_fmt[i]; break;
265 }
266 }
267 return uuid;
268}
269
Christopher Faulet8ef75252017-02-20 22:56:03 +0100270/* Returns the minimum number of appets alive at a time. This function is used
271 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100272static inline unsigned int
273min_applets_act(struct spoe_agent *agent)
274{
275 unsigned int nbsrv;
276
Christopher Faulet8ef75252017-02-20 22:56:03 +0100277 /* TODO: Add a config parameter to customize this value. Always 0 for
278 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279 if (agent->min_applets)
280 return agent->min_applets;
281
Christopher Faulet8ef75252017-02-20 22:56:03 +0100282 /* Get the number of active servers for the backend */
283 nbsrv = (agent->b.be->srv_act
284 ? agent->b.be->srv_act
285 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100286 return 2*nbsrv;
287}
288
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200289/********************************************************************
290 * Functions that encode/decode SPOE frames
291 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200292/* Helper to get static string length, excluding the terminating null byte */
293#define SLEN(str) (sizeof(str)-1)
294
295/* Predefined key used in HELLO/DISCONNECT frames */
296#define SUPPORTED_VERSIONS_KEY "supported-versions"
297#define VERSION_KEY "version"
298#define MAX_FRAME_SIZE_KEY "max-frame-size"
299#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100300#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100301#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200302#define STATUS_CODE_KEY "status-code"
303#define MSG_KEY "message"
304
305struct spoe_version {
306 char *str;
307 int min;
308 int max;
309};
310
311/* All supported versions */
312static struct spoe_version supported_versions[] = {
313 {"1.0", 1000, 1000},
314 {NULL, 0, 0}
315};
316
317/* Comma-separated list of supported versions */
318#define SUPPORTED_VERSIONS_VAL "1.0"
319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320/* Convert a string to a SPOE version value. The string must follow the format
321 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
322 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200323static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200325{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100326 const char *p, *end;
327 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329 p = str;
330 end = str+len;
331 maj = min = 0;
332 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200333
Christopher Faulet8ef75252017-02-20 22:56:03 +0100334 /* skip leading spaces */
335 while (p < end && isspace(*p))
336 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 /* parse Major number, until the '.' */
339 while (*p != '.') {
340 if (p >= end || *p < '0' || *p > '9')
341 goto out;
342 maj *= 10;
343 maj += (*p - '0');
344 p++;
345 }
346
347 /* check Major version */
348 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200349 goto out;
350
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351 p++; /* skip the '.' */
352 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
353 goto out;
354
355 /* Parse Minor number */
356 while (p < end) {
357 if (*p < '0' || *p > '9')
358 break;
359 min *= 10;
360 min += (*p - '0');
361 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363
364 /* check Minor number */
365 if (min > 999)
366 goto out;
367
368 /* skip trailing spaces */
369 while (p < end && isspace(*p))
370 p++;
371 if (p != end)
372 goto out;
373
374 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375 out:
376 return vsn;
377}
378
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
380 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
381 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384{
Christopher Faulet305c6072017-02-23 16:17:53 +0100385 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100386 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387 char *p, *end;
388 unsigned int flags = SPOE_FRM_FL_FIN;
389 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200390
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 p = frame;
392 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 /* Set Frame type */
395 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200396
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100397 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 memcpy(p, (char *)&flags, 4);
399 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
401 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200403
404 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
405 * and "capabilities" */
406
407 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 sz = SLEN(SUPPORTED_VERSIONS_KEY);
409 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
410 goto too_big;
411
412 *p++ = SPOE_DATA_T_STR;
413 sz = SLEN(SUPPORTED_VERSIONS_VAL);
414 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
415 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200416
417 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100418 sz = SLEN(MAX_FRAME_SIZE_KEY);
419 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
420 goto too_big;
421
422 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200423 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100424 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200425
426 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100427 sz = SLEN(CAPABILITIES_KEY);
428 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100432 chk = get_trash_chunk();
433 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
434 memcpy(chk->str, "pipelining", 10);
435 chk->len += 10;
436 }
437 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
438 if (chk->len) chk->str[chk->len++] = ',';
439 memcpy(chk->str+chk->len, "async", 5);
440 chk->len += 5;
441 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100442 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
443 if (chk->len) chk->str[chk->len++] = ',';
444 memcpy(chk->str+chk->len, "fragmentation", 13);
445 chk->len += 5;
446 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100447 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448 goto too_big;
449
450 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100451 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452 sz = SLEN(ENGINE_ID_KEY);
453 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
454 goto too_big;
455
456 *p++ = SPOE_DATA_T_STR;
457 sz = strlen(agent->engine_id);
458 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
459 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100460 }
461
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 return (p - frame);
463
464 too_big:
465 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
466 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467}
468
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
470 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
471 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200472static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 const char *reason;
476 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100477 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 p = frame;
481 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 /* Set Frame type */
484 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200485
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100486 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 memcpy(p, (char *)&flags, 4);
488 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
490 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 *p++ = 0; *p++ = 0;
492
493 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
494 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
496 /* There are 2 mandatory items: "status-code" and "message" */
497
498 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499 sz = SLEN(STATUS_CODE_KEY);
500 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
501 goto too_big;
502
503 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200504 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
507 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100508 sz = SLEN(MSG_KEY);
509 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
510 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200511
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512 /*Get the message corresponding to the status code */
513 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
514
515 *p++ = SPOE_DATA_T_STR;
516 sz = strlen(reason);
517 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
518 goto too_big;
519
520 return (p - frame);
521
522 too_big:
523 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
524 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525}
526
Christopher Faulet8ef75252017-02-20 22:56:03 +0100527/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
528 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
529 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200530static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100531spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100532 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200533{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100534 char *p, *end;
535 unsigned int stream_id, frame_id;
536 unsigned int flags = SPOE_FRM_FL_FIN;
537 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200538
Christopher Faulet8ef75252017-02-20 22:56:03 +0100539 p = frame;
540 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200541
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100542 stream_id = ctx->stream_id;
543 frame_id = ctx->frame_id;
544
545 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
546 /* The fragmentation is not supported by the applet */
547 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
548 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
549 return -1;
550 }
551 flags = ctx->frag_ctx.flags;
552 }
553
554 /* Set Frame type */
555 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
556
557 /* Set flags */
558 memcpy(p, (char *)&flags, 4);
559 p += 4;
560
561 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200562 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100563 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200564 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100565 goto too_big;
566
567 /* Copy encoded messages, if possible */
568 sz = ctx->buffer->i;
569 if (p + sz >= end)
570 goto too_big;
571 memcpy(p, ctx->buffer->p, sz);
572 p += sz;
573
574 return (p - frame);
575
576 too_big:
577 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
578 return 0;
579}
580
581/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
582 * returns the number of encoded bytes in the frame on success, 0 if an encoding
583 * error occurred and -1 if a fatal error occurred. */
584static int
585spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
586 char *frame, size_t size)
587{
588 char *p, *end;
589 unsigned int stream_id, frame_id;
590 unsigned int flags;
591 size_t sz;
592
593 p = frame;
594 end = frame+size;
595
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 /* <ctx> is null when the stream has aborted the processing of a
597 * fragmented frame. In this case, we must notify the corresponding
598 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100599 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100600 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
602 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
603 }
604 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100605 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100606 stream_id = ctx->stream_id;
607 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 }
609
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100611 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 memcpy(p, (char *)&flags, 4);
615 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200616
617 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200618 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200620 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200622
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100623 if (ctx == NULL)
624 goto end;
625
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628 if (p + sz >= end)
629 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100630 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100632
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100633 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 return (p - frame);
635
636 too_big:
637 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
638 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639}
640
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641/* Decode and process the HELLO frame sent by an agent. It returns the number of
642 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
643 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646{
Christopher Faulet305c6072017-02-23 16:17:53 +0100647 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
648 char *p, *end;
649 int vsn, max_frame_size;
650 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651
652 p = frame;
653 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654
655 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200660
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661 if (size < 7 /* TYPE + METADATA */) {
662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
663 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664 }
665
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100666 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 memcpy((char *)&flags, p, 4);
668 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100670 /* Fragmentation is not supported for HELLO frame */
671 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100673 return -1;
674 }
675
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200676 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 if (*p != 0 || *(p+1) != 0) {
678 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
679 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682
683 /* There are 3 mandatory items: "version", "max-frame-size" and
684 * "capabilities" */
685
686 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100687 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 while (p < end) {
689 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200690 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692
693 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 ret = spoe_decode_buffer(&p, end, &str, &sz);
695 if (ret == -1 || !sz) {
696 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
697 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200698 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 /* Check "version" K/V item */
701 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 int i, type = *p++;
703
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
707 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
713
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200715 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 return -1;
718 }
719 for (i = 0; supported_versions[i].str != NULL; ++i) {
720 if (vsn >= supported_versions[i].min &&
721 vsn <= supported_versions[i].max)
722 break;
723 }
724 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 return -1;
727 }
728 }
729 /* Check "max-frame-size" K/V item */
730 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100731 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732
733 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
735 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
736 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
737 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100738 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
739 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200740 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200741 if (decode_varint(&p, end, &sz) == -1) {
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 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if (sz < MIN_FRAME_SIZE ||
746 sz > SPOE_APPCTX(appctx)->max_frame_size) {
747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 return -1;
749 }
750 max_frame_size = sz;
751 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752 /* Check "capabilities" K/V item */
753 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
756 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100757 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
758 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
759 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
763 return 0;
764 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767 char *delim;
768
769 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
773 str += 10; sz -= 10;
774 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775 flags |= SPOE_APPCTX_FL_PIPELINING;
776 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 else if (sz >= 5 && !strncmp(str, "async", 5)) {
778 str += 5; sz -= 5;
779 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780 flags |= SPOE_APPCTX_FL_ASYNC;
781 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100782 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
783 str += 13; sz -= 13;
784 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100785 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
786 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100787
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 /* Get the next comma or break */
789 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100790 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 delim++;
792 sz -= (delim - str);
793 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100794 }
795 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 else {
797 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 if (spoe_skip_data(&p, end) == -1) {
799 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
800 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200801 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802 }
803 }
804
805 /* Final checks */
806 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200808 return -1;
809 }
810 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812 return -1;
813 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100814 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
815 flags &= ~SPOE_APPCTX_FL_PIPELINING;
816 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
817 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818
Christopher Faulet42bfa462017-01-04 14:14:19 +0100819 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
820 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
821 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822
823 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200824}
825
826/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
827 * bytes on success, 0 if the frame can be ignored and -1 if an error
828 * occurred. */
829static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100833 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834
835 p = frame;
836 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837
838 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 if (size < 7 /* TYPE + METADATA */) {
845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
846 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847 }
848
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100849 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 memcpy((char *)&flags, p, 4);
851 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100853 /* Fragmentation is not supported for DISCONNECT frame */
854 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100856 return -1;
857 }
858
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 if (*p != 0 || *(p+1) != 0) {
861 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
862 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
866 /* There are 2 mandatory items: "status-code" and "message" */
867
868 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 while (p < end) {
870 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200871 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873
874 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 ret = spoe_decode_buffer(&p, end, &str, &sz);
876 if (ret == -1 || !sz) {
877 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
878 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879 }
880
881 /* Check "status-code" K/V item */
882 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884
885 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
887 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
888 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
889 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100890 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
891 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200893 if (decode_varint(&p, end, &sz) == -1) {
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 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 }
899
900 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 else if (!memcmp(str, MSG_KEY, sz)) {
902 int type = *p++;
903
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200904 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 ret = spoe_decode_buffer(&p, end, &str, &sz);
910 if (ret == -1 || sz > 255) {
911 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
912 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100914#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
915 SPOE_APPCTX(appctx)->reason = str;
916 SPOE_APPCTX(appctx)->rlen = sz;
917#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
919 else {
920 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 if (spoe_skip_data(&p, end) == -1) {
922 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
923 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 }
926 }
927
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929}
930
931
Christopher Fauleta1cda022016-12-21 08:58:06 +0100932/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933 * success, 0 if the frame can be ignored and -1 if an error occurred. */
934static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100935spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100936 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100938 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100940 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100942 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943
944 p = frame;
945 end = frame + size;
946 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947
948 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200953
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 if (size < 7 /* TYPE + METADATA */) {
955 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
956 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957 }
958
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 memcpy((char *)&flags, p, 4);
961 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 /* Fragmentation is not supported for now */
964 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100966 return -1;
967 }
968
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200970 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100972 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200974 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100978
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100980 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200981 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 if ((*ctx)->stream_id == (unsigned int)stream_id &&
983 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100984 goto found;
985 }
986 }
987 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100988 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
989 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100990 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100991 goto found;
992 }
993 }
994
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100995 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
996 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
997 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
998
999 /* ABRT bit is set for an unfinished fragmented frame */
1000 if (flags & SPOE_FRM_FL_ABRT) {
1001 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1002 (*ctx)->frag_ctx.spoe_appctx = NULL;
1003 (*ctx)->state = SPOE_CTX_ST_ERROR;
1004 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1005 /* Ignore the payload */
1006 goto end;
1007 }
1008 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1009 /* For now, we ignore the ack */
1010 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1011 return 0;
1012 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001013
Christopher Fauleta1cda022016-12-21 08:58:06 +01001014 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001015 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1016 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001017 " - stream-id=%u - frame-id=%u\n",
1018 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1019 __FUNCTION__, appctx,
1020 (unsigned int)stream_id, (unsigned int)frame_id);
1021
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001022 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001023 return 0;
1024
1025 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001026 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1027 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001028 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001030 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001031
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001032 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001033 len = (end - p);
1034 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1035 SPOE_APPCTX(appctx)->buffer->i = len;
1036 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001037
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001038 /* Transfer the buffer ownership to the SPOE context */
1039 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1040 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001041
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 (*ctx)->state = SPOE_CTX_ST_DONE;
1043
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001044 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001045 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001046 " - ACK frame received"
1047 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1050 (*ctx)->frame_id, flags);
1051 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001052}
1053
Christopher Fauletba7bc162016-11-07 21:07:38 +01001054/* This function is used in cfgparse.c and declared in proto/checks.h. It
1055 * prepare the request to send to agents during a healthcheck. It returns 0 on
1056 * success and -1 if an error occurred. */
1057int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001058spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 struct appctx appctx;
1061 struct spoe_appctx spoe_appctx;
1062 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1063 size_t sz;
1064 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065
Christopher Faulet42bfa462017-01-04 14:14:19 +01001066 memset(&appctx, 0, sizeof(appctx));
1067 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069
1070 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001071 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001072
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1074 end = frame + MAX_FRAME_SIZE;
1075
1076 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1077 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080
Christopher Faulet8ef75252017-02-20 22:56:03 +01001081 /* Add "healthcheck" K/V item */
1082 sz = SLEN(HEALTHCHECK_KEY);
1083 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1084 return -1;
1085 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 *len = frame - buf;
1088 sz = htonl(*len - 4);
1089 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001094 return 0;
1095}
1096
1097/* This function is used in checks.c and declared in proto/checks.h. It decode
1098 * the response received from an agent during a healthcheck. It returns 0 on
1099 * success and -1 if an error occurred. */
1100int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001103 struct appctx appctx;
1104 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 memset(&appctx, 0, sizeof(appctx));
1107 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001110 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001111
Christopher Faulet8ef75252017-02-20 22:56:03 +01001112 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1113 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114 goto error;
1115 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1117 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118
1119 return 0;
1120
1121 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1123 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1124 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001125 return -1;
1126}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001127
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1129 * the frame can be ignored, 1 to retry later, and the frame legnth on
1130 * success. */
1131static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001132spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133{
1134 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135 int ret;
1136 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001137
1138 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 goto retry;
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
1147 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 if (ret == -1) {
1149 retry:
1150 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001151 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001152 }
1153 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154 return -1; /* error */
1155 }
1156 return framesz;
1157}
1158
1159/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1160 * when the frame can be ignored, 1 to retry later and the frame length on
1161 * success. */
1162static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001163spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164{
1165 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001166 int ret;
1167 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168
1169 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171
Willy Tarreau06d80a92017-10-19 14:32:15 +02001172 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 if (ret > 0) {
1174 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001175 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 return -1;
1178 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001179 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180 }
1181 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 if (ret == 0) {
1183 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001185 }
1186 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001187 return -1; /* error */
1188 }
1189 return framesz;
1190}
1191
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001192/********************************************************************
1193 * Functions that manage the SPOE applet
1194 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001195static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001196spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001197{
1198 si_applet_want_get(appctx->owner);
1199 si_applet_want_put(appctx->owner);
1200 appctx_wakeup(appctx);
1201 return 1;
1202}
1203
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204/* Callback function that catches applet timeouts. If a timeout occurred, we set
1205 * <appctx->st1> flag and the SPOE applet is woken up. */
1206static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001207spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208{
1209 struct appctx *appctx = task->context;
1210
1211 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1212 if (tick_is_expired(task->expire, now_ms)) {
1213 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001217 return task;
1218}
1219
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220/* Callback function that releases a SPOE applet. This happens when the
1221 * connection with the agent is closed. */
1222static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001224{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001225 struct stream_interface *si = appctx->owner;
1226 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1227 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001228 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229
1230 if (spoe_appctx == NULL)
1231 return;
1232
1233 appctx->ctx.spoe.ptr = NULL;
1234 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001235
1236 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1237 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1238 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001239
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 /* Remove applet from the list of running applets */
Christopher Faulet24289f22017-09-25 14:48:02 +02001241 agent->rt[tid].applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1243 LIST_DEL(&spoe_appctx->list);
1244 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001245 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001246
Christopher Faulet8ef75252017-02-20 22:56:03 +01001247 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001249 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
Christopher Faulet24289f22017-09-25 14:48:02 +02001250 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001251
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001253 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1254 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001255
1256 si_shutw(si);
1257 si_shutr(si);
1258 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001259 }
1260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Destroy the task attached to this applet */
1262 if (spoe_appctx->task) {
1263 task_delete(spoe_appctx->task);
1264 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001265 }
1266
Christopher Faulet8ef75252017-02-20 22:56:03 +01001267 /* Notify all waiting streams */
1268 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001269 LIST_DEL(&ctx->list);
1270 LIST_INIT(&ctx->list);
1271 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001273 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001274 }
1275
Christopher Faulet8ef75252017-02-20 22:56:03 +01001276 /* If the applet was processing a fragmented frame, notify the
1277 * corresponding stream. */
1278 if (spoe_appctx->frag_ctx.ctx) {
1279 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001280 ctx->frag_ctx.spoe_appctx = NULL;
1281 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001283 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1284 }
1285
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 /* Release allocated memory */
1287 spoe_release_buffer(&spoe_appctx->buffer,
1288 &spoe_appctx->buffer_wait);
1289 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001290
Christopher Faulet24289f22017-09-25 14:48:02 +02001291 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001292 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001295 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001296 LIST_DEL(&ctx->list);
1297 LIST_INIT(&ctx->list);
1298 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);
1305 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001306 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1308 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001309
1310 end:
1311 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001312 agent->rt[tid].frame_size = agent->max_frame_size;
1313 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1314 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001315}
1316
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001317static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001318spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001319{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001321 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001322 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001323 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001324
Christopher Fauleta1cda022016-12-21 08:58:06 +01001325 if (si->state <= SI_ST_CON) {
1326 si_applet_want_put(si);
1327 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1328 goto stop;
1329 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001330 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001331 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001332 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001333 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001334
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001336 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1337 " - Connection timed out\n",
1338 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1339 __FUNCTION__, appctx);
1340 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001342 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343
Christopher Faulet42bfa462017-01-04 14:14:19 +01001344 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345 SPOE_APPCTX(appctx)->task->expire =
1346 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1349 * length. */
1350 buf = trash.str; frame = buf+4;
1351 ret = spoe_prepare_hahello_frame(appctx, frame,
1352 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355
1356 switch (ret) {
1357 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 case 0: /* ignore => an error, cannot be ignored */
1359 goto exit;
1360
1361 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001362 goto stop;
1363
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 default:
1365 /* HELLO frame successfully sent, now wait for the
1366 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001367 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1368 goto next;
1369 }
1370
1371 next:
1372 return 0;
1373 stop:
1374 return 1;
1375 exit:
1376 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1377 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378}
1379
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001380static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001382{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001384 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 char *frame;
1386 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001387
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauletb067b062017-01-04 16:39:11 +01001389 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001390 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001392 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1396 " - Connection timed out\n",
1397 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1398 __FUNCTION__, appctx);
1399 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 goto exit;
1401 }
1402
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 frame = trash.str; trash.len = 0;
1404 ret = spoe_recv_frame(appctx, frame,
1405 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (ret > 1) {
1407 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1408 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1409 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 trash.len = ret + 4;
1412 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 switch (ret) {
1416 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1419 goto next;
1420
1421 case 1: /* retry later */
1422 goto stop;
1423
1424 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 /* HELLO handshake is finished, set the idle timeout and
1426 * add the applet in the list of running applets. */
Christopher Faulet24289f22017-09-25 14:48:02 +02001427 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet24289f22017-09-25 14:48:02 +02001429 SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001430 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001431 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
1432 SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001433
1434 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001435 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001437 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001438
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001440 /* Do not forget to remove processed frame from the output buffer */
1441 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001442 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001443
1444 SPOE_APPCTX(appctx)->task->expire =
1445 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001446 return 0;
1447 stop:
1448 return 1;
1449 exit:
1450 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1451 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452}
1453
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001454
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001456spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001458 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1459 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 char *frame, *buf;
1461 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001462
Christopher Faulet8ef75252017-02-20 22:56:03 +01001463 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1464 * length. */
1465 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001466
1467 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1468 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1469 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1470 SPOE_APPCTX(appctx)->max_frame_size);
1471 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001472 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473 *skip = 1;
1474 ret = 1;
1475 goto end;
1476 }
1477 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001478 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001479 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1480 SPOE_APPCTX(appctx)->max_frame_size);
1481
1482 }
1483
Christopher Faulet8ef75252017-02-20 22:56:03 +01001484 if (ret > 1)
1485 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486
Christopher Faulet8ef75252017-02-20 22:56:03 +01001487 switch (ret) {
1488 case -1: /* error */
1489 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1490 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001491
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 case 0: /* ignore */
1493 if (ctx == NULL)
1494 goto abort_frag_frame;
1495
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001496 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001497 LIST_DEL(&ctx->list);
1498 LIST_INIT(&ctx->list);
1499 ctx->state = SPOE_CTX_ST_ERROR;
1500 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1501 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1502 break;
1503
1504 case 1: /* retry */
1505 *skip = 1;
1506 break;
1507
1508 default:
1509 if (ctx == NULL)
1510 goto abort_frag_frame;
1511
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001512 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001513 LIST_DEL(&ctx->list);
1514 LIST_INIT(&ctx->list);
1515 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1516 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1517 goto no_frag_frame_sent;
1518 else {
1519 *skip = 1;
1520 goto frag_frame_sent;
1521 }
1522 }
1523 goto end;
1524
1525 frag_frame_sent:
1526 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1527 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1528 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1529 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1530
1531 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1532 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1533 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1534 goto end;
1535
1536 no_frag_frame_sent:
1537 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1538 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001539 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 }
1541 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1542 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1543 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1544 }
1545 else {
1546 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1547 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1548 }
1549 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1550 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1551 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1552
1553 ctx->frag_ctx.spoe_appctx = NULL;
1554 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1555 goto end;
1556
1557 abort_frag_frame:
1558 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1559 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1560 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1561 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1562 goto end;
1563
1564 end:
1565 return ret;
1566}
1567
1568static int
1569spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1570{
1571 struct spoe_context *ctx = NULL;
1572 char *frame;
1573 int ret;
1574
1575 frame = trash.str; trash.len = 0;
1576 ret = spoe_recv_frame(appctx, frame,
1577 SPOE_APPCTX(appctx)->max_frame_size);
1578 if (ret > 1) {
1579 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1580 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1581 goto end;
1582 }
1583 trash.len = ret + 4;
1584 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1585 }
1586 switch (ret) {
1587 case -1: /* error */
1588 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1589 break;
1590
1591 case 0: /* ignore */
1592 break;
1593
1594 case 1: /* retry */
1595 *skip = 1;
1596 break;
1597
1598 default:
1599 LIST_DEL(&ctx->list);
1600 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001601
1602 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1603 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1604 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1605 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1606 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1607 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1608 }
1609 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1610 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1611
Christopher Faulet8ef75252017-02-20 22:56:03 +01001612 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1613 break;
1614 }
1615
1616 /* Do not forget to remove processed frame from the output buffer */
1617 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001618 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001619 end:
1620 return ret;
1621}
1622
1623static int
1624spoe_handle_processing_appctx(struct appctx *appctx)
1625{
1626 struct stream_interface *si = appctx->owner;
1627 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 unsigned int fpa = 0;
1629 int ret, skip_sending = 0, skip_receiving = 0;
1630
1631 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1632 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1633 goto exit;
1634 }
1635
1636 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1637 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1638 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1639 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1640 goto next;
1641 }
1642
1643 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001644 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1645 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1646 " - appctx-state=%s\n",
1647 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1648 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001649 skip_sending, skip_receiving,
1650 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001651
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001652 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001653 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001654 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001655 if (skip_receiving)
1656 goto stop;
1657 goto recv_frame;
1658 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001659
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001660 /* send_frame */
1661 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001662 switch (ret) {
1663 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001664 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001665
Christopher Fauleta1cda022016-12-21 08:58:06 +01001666 case 0: /* ignore */
Christopher Faulet24289f22017-09-25 14:48:02 +02001667 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001668 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001669 break;
1670
Christopher Fauleta1cda022016-12-21 08:58:06 +01001671 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001672 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001673
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 default:
Christopher Faulet24289f22017-09-25 14:48:02 +02001675 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001676 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001677 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001678 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679 if (fpa > agent->max_fpa)
1680 goto stop;
1681
1682 recv_frame:
1683 if (skip_receiving)
1684 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001685 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686 switch (ret) {
1687 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001688 goto next;
1689
1690 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001691 fpa++;
1692 break;
1693
1694 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 break;
1696
1697 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001698 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001700 }
1701 goto process;
1702
1703 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001704 SPOE_APPCTX(appctx)->task->expire =
1705 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 return 0;
1707 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001708 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001709 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet24289f22017-09-25 14:48:02 +02001710 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001711 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001712 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
Christopher Faulet24289f22017-09-25 14:48:02 +02001713 SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001714 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001715 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
1716 SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001718 SPOE_APPCTX(appctx)->task->expire =
1719 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720 }
1721 return 1;
1722
1723 exit:
1724 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1725 return 0;
1726}
1727
1728static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001729spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001730{
1731 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001732 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001733 char *frame, *buf;
1734 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001735
Christopher Fauleta1cda022016-12-21 08:58:06 +01001736 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1737 goto exit;
1738
1739 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1740 goto exit;
1741
Christopher Faulet8ef75252017-02-20 22:56:03 +01001742 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1743 * length. */
1744 buf = trash.str; frame = buf+4;
1745 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1746 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749
1750 switch (ret) {
1751 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001752 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753 goto exit;
1754
1755 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 goto stop;
1757
1758 default:
1759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1760 " - disconnected by HAProxy (%d): %s\n",
1761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 __FUNCTION__, appctx,
1763 SPOE_APPCTX(appctx)->status_code,
1764 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765
1766 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1767 goto next;
1768 }
1769
1770 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 SPOE_APPCTX(appctx)->task->expire =
1772 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 return 0;
1774 stop:
1775 return 1;
1776 exit:
1777 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1778 return 0;
1779}
1780
1781static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783{
1784 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001785 char *frame;
1786 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787
Christopher Fauletb067b062017-01-04 16:39:11 +01001788 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001791 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792
Christopher Fauletb067b062017-01-04 16:39:11 +01001793 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001796 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 frame = trash.str; trash.len = 0;
1799 ret = spoe_recv_frame(appctx, frame,
1800 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802 trash.len = ret + 4;
1803 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 }
1805
1806 switch (ret) {
1807 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001808 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1809 " - error on frame (%s)\n",
1810 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001811 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 goto exit;
1815
1816 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 goto next;
1818
1819 case 1: /* retry */
1820 goto stop;
1821
1822 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001826 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1828 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 goto exit;
1830 }
1831
1832 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 /* Do not forget to remove processed frame from the output buffer */
1834 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001835 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 return 0;
1838 stop:
1839 return 1;
1840 exit:
1841 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1842 return 0;
1843}
1844
1845/* I/O Handler processing messages exchanged with the agent */
1846static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001849 struct stream_interface *si = appctx->owner;
1850 struct spoe_agent *agent;
1851
1852 if (SPOE_APPCTX(appctx) == NULL)
1853 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001854
Christopher Faulet8ef75252017-02-20 22:56:03 +01001855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1856 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001857
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 switchstate:
1859 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1860 " - appctx-state=%s\n",
1861 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1862 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1863
1864 switch (appctx->st0) {
1865 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 goto out;
1868 goto switchstate;
1869
1870 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001872 goto out;
1873 goto switchstate;
1874
1875 case SPOE_APPCTX_ST_IDLE:
1876 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001877 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001878 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001879 SPOE_APPCTX(appctx)->task->expire =
1880 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001882 goto switchstate;
1883 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001884 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1886 /* fall through */
1887
1888 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001889 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1890 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001891 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 goto out;
1893 goto switchstate;
1894
1895 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001896 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897 goto out;
1898 goto switchstate;
1899
1900 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001901 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001902 goto out;
1903 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001904
1905 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001906 appctx->st0 = SPOE_APPCTX_ST_END;
1907 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1908
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001909 si_shutw(si);
1910 si_shutr(si);
1911 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001912 /* fall through */
1913
1914 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001915 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001918 if (stopping)
1919 spoe_wakeup_appctx(appctx);
1920
Christopher Faulet42bfa462017-01-04 14:14:19 +01001921 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1922 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001923 si_oc(si)->flags |= CF_READ_DONTWAIT;
1924 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925}
1926
1927struct applet spoe_applet = {
1928 .obj_type = OBJ_TYPE_APPLET,
1929 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 .fct = spoe_handle_appctx,
1931 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932};
1933
1934/* Create a SPOE applet. On success, the created applet is returned, else
1935 * NULL. */
1936static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001937spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938{
1939 struct appctx *appctx;
1940 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942
Emeric Brun1138fd02017-06-19 12:38:55 +02001943 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 goto out_error;
1945
Christopher Faulet42bfa462017-01-04 14:14:19 +01001946 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1947 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001949 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001950
Christopher Faulet42bfa462017-01-04 14:14:19 +01001951 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001952 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001953 goto out_free_spoe_appctx;
1954
1955 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001956 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001957 SPOE_APPCTX(appctx)->task->context = appctx;
1958 SPOE_APPCTX(appctx)->agent = conf->agent;
1959 SPOE_APPCTX(appctx)->version = 0;
1960 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1961 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001963 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1964
1965 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1966 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001967 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001968
1969 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1970 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971
Willy Tarreau5820a362016-12-22 15:59:02 +01001972 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973 if (!sess)
1974 goto out_free_spoe;
1975
Willy Tarreau87787ac2017-08-28 16:22:54 +02001976 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977 goto out_free_sess;
1978
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979 stream_set_backend(strm, conf->agent->b.be);
1980
1981 /* applet is waiting for data */
1982 si_applet_cant_get(&strm->si[0]);
1983 appctx_wakeup(appctx);
1984
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985 strm->do_log = NULL;
1986 strm->res.flags |= CF_READ_DONTWAIT;
1987
Christopher Faulet24289f22017-09-25 14:48:02 +02001988 SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
1989 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
1990 SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
1991 conf->agent->rt[tid].applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001992
Emeric Brunc60def82017-09-27 14:59:38 +02001993 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001994 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995 return appctx;
1996
1997 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998 out_free_sess:
1999 session_free(sess);
2000 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002001 task_free(SPOE_APPCTX(appctx)->task);
2002 out_free_spoe_appctx:
2003 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004 out_free_appctx:
2005 appctx_free(appctx);
2006 out_error:
2007 return NULL;
2008}
2009
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002010static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002011spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012{
2013 struct spoe_config *conf = FLT_CONF(ctx->filter);
2014 struct spoe_agent *agent = conf->agent;
2015 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002016 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002017 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018
Christopher Fauleta1cda022016-12-21 08:58:06 +01002019 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020
Christopher Fauleta1cda022016-12-21 08:58:06 +01002021 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002022 if (agent->rt[tid].applets_act >= min_applets &&
2023 agent->rt[tid].applets_idle &&
2024 agent->rt[tid].sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026
2027 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002028 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2030 ctx->strm);
2031
Christopher Fauleta1cda022016-12-21 08:58:06 +01002032 /* Do not try to create a new applet if there is no server up for the
2033 * agent's backend. */
2034 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2035 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2036 " - cannot create SPOE appctx: no server up\n",
2037 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2038 __FUNCTION__, ctx->strm);
2039 goto end;
2040 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 /* Do not try to create a new applet if we have reached the maximum of
2043 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002044 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002045 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2047 " - cannot create SPOE appctx: max CPS reached\n",
2048 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2049 __FUNCTION__, ctx->strm);
2050 goto end;
2051 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002052 }
2053
Christopher Faulet8ef75252017-02-20 22:56:03 +01002054 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 if (appctx == NULL) {
2056 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2057 " - failed to create SPOE appctx\n",
2058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2059 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002060 send_log(ctx->strm->be, LOG_EMERG,
2061 "SPOE: [%s] failed to create SPOE applet\n",
2062 agent->id);
2063
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065 }
Christopher Faulet24289f22017-09-25 14:48:02 +02002066 if (agent->rt[tid].applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002067 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 /* Increase the per-process number of cumulated connections */
2070 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002071 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072
Christopher Fauleta1cda022016-12-21 08:58:06 +01002073 end:
2074 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002075 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002076 ctx->status_code = SPOE_CTX_ERR_RES;
2077 return -1;
2078 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 /* Add the SPOE context in the sending queue and update all running
2081 * info */
Christopher Faulet24289f22017-09-25 14:48:02 +02002082 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
2083 if (agent->rt[tid].sending_rate)
2084 agent->rt[tid].sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085
2086 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002087 " - Add stream in sending queue"
2088 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002090 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
2091 agent->rt[tid].sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002092
Christopher Fauleta1cda022016-12-21 08:58:06 +01002093 /* Finally try to wakeup the first IDLE applet found and move it at the
2094 * end of the list. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002095 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) {
Christopher Faulet42bfa462017-01-04 14:14:19 +01002096 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002097 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002098 spoe_wakeup_appctx(appctx);
Christopher Faulet24289f22017-09-25 14:48:02 +02002099 SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002100 LIST_DEL(&spoe_appctx->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02002101 LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list);
2102 SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002103 break;
2104 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002105 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002106 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107}
2108
2109/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002110 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002111 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002112/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2113 * fragmented_content. If the next message can be processed, it returns 0. If
2114 * the message is too big, it returns -1.*/
2115static int
2116spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2117 struct spoe_message *msg, int dir,
2118 char **buf, char *end)
2119{
2120 struct sample *smp;
2121 struct spoe_arg *arg;
2122 int ret;
2123
2124 if (msg->cond) {
2125 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2126 ret = acl_pass(ret);
2127 if (msg->cond->pol == ACL_COND_UNLESS)
2128 ret = !ret;
2129
2130 /* the rule does not match */
2131 if (!ret)
2132 goto next;
2133 }
2134
2135 /* Resume encoding of a SPOE argument */
2136 if (ctx->frag_ctx.curarg != NULL) {
2137 arg = ctx->frag_ctx.curarg;
2138 goto encode_argument;
2139 }
2140
2141 if (ctx->frag_ctx.curoff != UINT_MAX)
2142 goto encode_msg_payload;
2143
2144 /* Check if there is enough space for the message name and the
2145 * number of arguments. It implies <msg->id_len> is encoded on 2
2146 * bytes, at most (< 2288). */
2147 if (*buf + 2 + msg->id_len + 1 > end)
2148 goto too_big;
2149
2150 /* Encode the message name */
2151 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2152 goto too_big;
2153
2154 /* Set the number of arguments for this message */
2155 **buf = msg->nargs;
2156 (*buf)++;
2157
2158 ctx->frag_ctx.curoff = 0;
2159 encode_msg_payload:
2160
2161 /* Loop on arguments */
2162 list_for_each_entry(arg, &msg->args, list) {
2163 ctx->frag_ctx.curarg = arg;
2164 ctx->frag_ctx.curoff = UINT_MAX;
2165
2166 encode_argument:
2167 if (ctx->frag_ctx.curoff != UINT_MAX)
2168 goto encode_arg_value;
2169
2170 /* Encode the arguement name as a string. It can by NULL */
2171 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2172 goto too_big;
2173
2174 ctx->frag_ctx.curoff = 0;
2175 encode_arg_value:
2176
2177 /* Fetch the arguement value */
2178 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2179 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2180 if (ret == -1 || ctx->frag_ctx.curoff)
2181 goto too_big;
2182 }
2183
2184 next:
2185 return 0;
2186
2187 too_big:
2188 return -1;
2189}
2190
Christopher Fauletc718b822017-09-21 16:50:56 +02002191/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2192 * handle fragmented content. On success it returns 1. If an error occurred, -1
2193 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2194 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002195static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002196spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002197 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002198{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002199 struct spoe_config *conf = FLT_CONF(ctx->filter);
2200 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002201 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002202 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002203
Christopher Faulet8ef75252017-02-20 22:56:03 +01002204 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002205 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206
Christopher Fauletc718b822017-09-21 16:50:56 +02002207 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2208 /* Resume encoding of a SPOE message */
2209 if (ctx->frag_ctx.curmsg != NULL) {
2210 msg = ctx->frag_ctx.curmsg;
2211 goto encode_evt_message;
2212 }
2213
2214 list_for_each_entry(msg, messages, by_evt) {
2215 ctx->frag_ctx.curmsg = msg;
2216 ctx->frag_ctx.curarg = NULL;
2217 ctx->frag_ctx.curoff = UINT_MAX;
2218
2219 encode_evt_message:
2220 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2221 goto too_big;
2222 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002223 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002224 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2225 /* Resume encoding of a SPOE message */
2226 if (ctx->frag_ctx.curmsg != NULL) {
2227 msg = ctx->frag_ctx.curmsg;
2228 goto encode_grp_message;
2229 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002230
Christopher Fauletc718b822017-09-21 16:50:56 +02002231 list_for_each_entry(msg, messages, by_grp) {
2232 ctx->frag_ctx.curmsg = msg;
2233 ctx->frag_ctx.curarg = NULL;
2234 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235
Christopher Fauletc718b822017-09-21 16:50:56 +02002236 encode_grp_message:
2237 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2238 goto too_big;
2239 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002240 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002241 else
2242 goto skip;
2243
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244
Christopher Faulet57583e42017-09-04 15:41:09 +02002245 /* nothing has been encoded for an unfragmented payload */
2246 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2247 goto skip;
2248
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002250 " - encode %s messages - spoe_appctx=%p"
2251 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252 (int)now.tv_sec, (int)now.tv_usec,
2253 agent->id, __FUNCTION__, s,
2254 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet24289f22017-09-25 14:48:02 +02002255 ctx->frag_ctx.spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002256 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259 ctx->frag_ctx.curmsg = NULL;
2260 ctx->frag_ctx.curarg = NULL;
2261 ctx->frag_ctx.curoff = 0;
2262 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002263
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264 return 1;
2265
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002267 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2268 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2269 return -1;
2270 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002271
2272 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002273 " - encode fragmented messages - spoe_appctx=%p"
2274 " - curmsg=%p - curarg=%p - curoff=%u"
2275 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002276 (int)now.tv_sec, (int)now.tv_usec,
2277 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2278 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002279 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280
Christopher Faulet8ef75252017-02-20 22:56:03 +01002281 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2283 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2284 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002285
2286 skip:
2287 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2288 " - skip the frame because nothing has been encoded\n",
2289 (int)now.tv_sec, (int)now.tv_usec,
2290 agent->id, __FUNCTION__, s);
2291 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002292}
2293
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294
2295/***************************************************************************
2296 * Functions that handle SPOE actions
2297 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298/* Helper function to set a variable */
2299static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002300spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301 struct sample *smp)
2302{
2303 struct spoe_config *conf = FLT_CONF(ctx->filter);
2304 struct spoe_agent *agent = conf->agent;
2305 char varname[64];
2306
2307 memset(varname, 0, sizeof(varname));
2308 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2309 scope, agent->var_pfx, len, name);
2310 vars_set_by_name_ifexist(varname, len, smp);
2311}
2312
2313/* Helper function to unset a variable */
2314static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002316 struct sample *smp)
2317{
2318 struct spoe_config *conf = FLT_CONF(ctx->filter);
2319 struct spoe_agent *agent = conf->agent;
2320 char varname[64];
2321
2322 memset(varname, 0, sizeof(varname));
2323 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2324 scope, agent->var_pfx, len, name);
2325 vars_unset_by_name_ifexist(varname, len, smp);
2326}
2327
2328
Christopher Faulet8ef75252017-02-20 22:56:03 +01002329static inline int
2330spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2331 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002332{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333 char *str, *scope, *p = *buf;
2334 struct sample smp;
2335 uint64_t sz;
2336 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002337
Christopher Faulet8ef75252017-02-20 22:56:03 +01002338 if (p + 2 >= end)
2339 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002340
Christopher Faulet8ef75252017-02-20 22:56:03 +01002341 /* SET-VAR requires 3 arguments */
2342 if (*p++ != 3)
2343 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345 switch (*p++) {
2346 case SPOE_SCOPE_PROC: scope = "proc"; break;
2347 case SPOE_SCOPE_SESS: scope = "sess"; break;
2348 case SPOE_SCOPE_TXN : scope = "txn"; break;
2349 case SPOE_SCOPE_REQ : scope = "req"; break;
2350 case SPOE_SCOPE_RES : scope = "res"; break;
2351 default: goto skip;
2352 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2355 goto skip;
2356 memset(&smp, 0, sizeof(smp));
2357 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002358
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359 if (spoe_decode_data(&p, end, &smp) == -1)
2360 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002361
Christopher Faulet8ef75252017-02-20 22:56:03 +01002362 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2363 " - set-var '%s.%s.%.*s'\n",
2364 (int)now.tv_sec, (int)now.tv_usec,
2365 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2366 __FUNCTION__, s, scope,
2367 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2368 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369
Christopher Faulet8ef75252017-02-20 22:56:03 +01002370 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 ret = (p - *buf);
2373 *buf = p;
2374 return ret;
2375 skip:
2376 return 0;
2377}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378
Christopher Faulet8ef75252017-02-20 22:56:03 +01002379static inline int
2380spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2381 char **buf, char *end, int dir)
2382{
2383 char *str, *scope, *p = *buf;
2384 struct sample smp;
2385 uint64_t sz;
2386 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388 if (p + 2 >= end)
2389 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002390
Christopher Faulet8ef75252017-02-20 22:56:03 +01002391 /* UNSET-VAR requires 2 arguments */
2392 if (*p++ != 2)
2393 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 switch (*p++) {
2396 case SPOE_SCOPE_PROC: scope = "proc"; break;
2397 case SPOE_SCOPE_SESS: scope = "sess"; break;
2398 case SPOE_SCOPE_TXN : scope = "txn"; break;
2399 case SPOE_SCOPE_REQ : scope = "req"; break;
2400 case SPOE_SCOPE_RES : scope = "res"; break;
2401 default: goto skip;
2402 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2405 goto skip;
2406 memset(&smp, 0, sizeof(smp));
2407 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2410 " - unset-var '%s.%s.%.*s'\n",
2411 (int)now.tv_sec, (int)now.tv_usec,
2412 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2413 __FUNCTION__, s, scope,
2414 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2415 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002416
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417 spoe_unset_var(ctx, scope, str, sz, &smp);
2418
2419 ret = (p - *buf);
2420 *buf = p;
2421 return ret;
2422 skip:
2423 return 0;
2424}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002425
Christopher Faulet8ef75252017-02-20 22:56:03 +01002426/* Process SPOE actions for a specific event. It returns 1 on success. If an
2427 * error occurred, 0 is returned. */
2428static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002429spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002430{
2431 char *p, *end;
2432 int ret;
2433
2434 p = ctx->buffer->p;
2435 end = p + ctx->buffer->i;
2436
2437 while (p < end) {
2438 enum spoe_action_type type;
2439
2440 type = *p++;
2441 switch (type) {
2442 case SPOE_ACT_T_SET_VAR:
2443 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2444 if (!ret)
2445 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002446 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002447
Christopher Faulet8ef75252017-02-20 22:56:03 +01002448 case SPOE_ACT_T_UNSET_VAR:
2449 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2450 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002451 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002452 break;
2453
2454 default:
2455 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002456 }
2457 }
2458
2459 return 1;
2460 skip:
2461 return 0;
2462}
2463
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002464/***************************************************************************
2465 * Functions that process SPOE events
2466 **************************************************************************/
2467static inline int
Christopher Faulet58d03682017-09-21 16:57:24 +02002468spoe_start_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002469{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002470 /* If a process is already started for this SPOE context, retry
2471 * later. */
2472 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002473 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002474
2475 /* Set the right flag to prevent request and response processing
2476 * in same time. */
2477 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2478 ? SPOE_CTX_FL_REQ_PROCESS
2479 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002480 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002481}
2482
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002483static inline void
Christopher Faulet58d03682017-09-21 16:57:24 +02002484spoe_stop_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002485{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002486 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2487
2488 if (sa) {
2489 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002490 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002491 }
2492
Christopher Fauleta1cda022016-12-21 08:58:06 +01002493 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002494 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002495
Christopher Fauletb067b062017-01-04 16:39:11 +01002496 ctx->status_code = 0;
2497
Christopher Fauleta1cda022016-12-21 08:58:06 +01002498 /* Reset processing timer */
2499 ctx->process_exp = TICK_ETERNITY;
2500
Christopher Faulet8ef75252017-02-20 22:56:03 +01002501 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002502
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002503 ctx->frag_ctx.spoe_appctx = NULL;
2504 ctx->frag_ctx.curmsg = NULL;
2505 ctx->frag_ctx.curarg = NULL;
2506 ctx->frag_ctx.curoff = 0;
2507 ctx->frag_ctx.flags = 0;
2508
Christopher Fauleta1cda022016-12-21 08:58:06 +01002509 if (!LIST_ISEMPTY(&ctx->list)) {
2510 LIST_DEL(&ctx->list);
2511 LIST_INIT(&ctx->list);
2512 }
2513}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002514
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002515static void
2516spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2517 struct spoe_context *ctx, int dir)
2518{
2519 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002520 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002521
2522 if (agent->var_on_error) {
2523 struct sample smp;
2524
2525 memset(&smp, 0, sizeof(smp));
2526 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2527 smp.data.u.sint = ctx->status_code;
2528 smp.data.type = SMP_T_BOOL;
2529
2530 spoe_set_var(ctx, "txn", agent->var_on_error,
2531 strlen(agent->var_on_error), &smp);
2532 }
2533 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2534 " - failed to process messages: code=%u\n",
2535 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2536 __FUNCTION__, s, ctx->status_code);
2537 send_log(ctx->strm->be, LOG_WARNING,
2538 "SPOE: [%s] failed to process messages: code=%u\n",
2539 agent->id, ctx->status_code);
2540
2541 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2542 ? SPOE_CTX_ST_READY
2543 : SPOE_CTX_ST_NONE);
2544}
2545
Christopher Faulet58d03682017-09-21 16:57:24 +02002546/* Process a list of SPOE messages. First, this functions will process messages
2547 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2548 * to process corresponding actions. During all the processing, it returns 0
2549 * and it returns 1 when the processing is finished. If an error occurred, -1
2550 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002551static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002552spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2553 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002554{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002555 struct spoe_config *conf = FLT_CONF(ctx->filter);
2556 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002557 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002558
2559 if (ctx->state == SPOE_CTX_ST_ERROR)
2560 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002561
2562 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2563 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002564 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002565 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002566 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002567 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002568 goto error;
2569 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002570
2571 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002572 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002573 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002574 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002575 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002577 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002578 goto skip;
2579 }
2580 }
2581
Christopher Fauletf7a30922016-11-10 15:04:51 +01002582 if (!tick_isset(ctx->process_exp)) {
2583 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2584 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2585 ctx->process_exp);
2586 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002587 ret = spoe_start_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002588 if (!ret)
2589 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002590
Christopher Faulet8ef75252017-02-20 22:56:03 +01002591 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592 goto error;
2593
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002595 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002596 }
2597
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002598 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002599 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002600 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002601 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002602 if (ret < 0)
2603 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002604 if (!ret)
2605 goto skip;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002606 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2607 }
2608
2609 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2610 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002611 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002612 ret = 0;
2613 goto out;
2614 }
2615
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002616 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2617 ret = 0;
2618 goto out;
2619 }
2620
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002621 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002622 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002623 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002624 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002625 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002626 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002627 }
2628
2629 out:
2630 return ret;
2631
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002632 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002633 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002634 ret = 1;
2635 goto end;
2636
2637 skip:
2638 ctx->state = SPOE_CTX_ST_READY;
2639 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002640
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641 end:
Christopher Faulet58d03682017-09-21 16:57:24 +02002642 spoe_stop_processing(ctx);
2643 return ret;
2644}
2645
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002646/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2647 * See spoe_process_message for details. */
2648static int
2649spoe_process_group(struct stream *s, struct spoe_context *ctx,
2650 struct spoe_group *group, int dir)
2651{
2652 int ret;
2653
2654 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2655 " - ctx-state=%s - Process messages for group=%s\n",
2656 (int)now.tv_sec, (int)now.tv_usec,
2657 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2658 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2659 group->id);
2660
2661 if (LIST_ISEMPTY(&group->messages))
2662 return 1;
2663
2664 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2665 return ret;
2666}
2667
Christopher Faulet58d03682017-09-21 16:57:24 +02002668/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2669 * See spoe_process_message for details. */
2670static int
2671spoe_process_event(struct stream *s, struct spoe_context *ctx,
2672 enum spoe_event ev)
2673{
2674 int dir, ret;
2675
2676 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002677 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002678 (int)now.tv_sec, (int)now.tv_usec,
2679 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2680 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2681 spoe_event_str[ev]);
2682
2683 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2684
2685 if (LIST_ISEMPTY(&(ctx->events[ev])))
2686 return 1;
2687
2688 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002689 return ret;
2690}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691
2692/***************************************************************************
2693 * Functions that create/destroy SPOE contexts
2694 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002695static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002696spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002697{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002698 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699 return 1;
2700
Christopher Faulet4596fb72017-01-11 14:05:19 +01002701 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Emeric Bruna1dd2432017-06-21 15:42:52 +02002702 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002703 LIST_DEL(&buffer_wait->list);
2704 LIST_INIT(&buffer_wait->list);
Emeric Bruna1dd2432017-06-21 15:42:52 +02002705 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002706 }
2707
Christopher Faulet4596fb72017-01-11 14:05:19 +01002708 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002709 return 1;
2710
Emeric Bruna1dd2432017-06-21 15:42:52 +02002711 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002712 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Emeric Bruna1dd2432017-06-21 15:42:52 +02002713 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002714 return 0;
2715}
2716
2717static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002718spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002719{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002720 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Emeric Bruna1dd2432017-06-21 15:42:52 +02002721 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002722 LIST_DEL(&buffer_wait->list);
2723 LIST_INIT(&buffer_wait->list);
Emeric Bruna1dd2432017-06-21 15:42:52 +02002724 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002725 }
2726
2727 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002728 if (*buf != &buf_empty) {
2729 b_free(buf);
2730 offer_buffers(buffer_wait->target,
2731 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002732 }
2733}
2734
Christopher Faulet4596fb72017-01-11 14:05:19 +01002735static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002736spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002737{
2738 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2739 return 1;
2740}
2741
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002742static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002743spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744{
2745 struct spoe_config *conf = FLT_CONF(filter);
2746 struct spoe_context *ctx;
2747
2748 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2749 if (ctx == NULL) {
2750 return NULL;
2751 }
2752 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002753 ctx->filter = filter;
2754 ctx->state = SPOE_CTX_ST_NONE;
2755 ctx->status_code = SPOE_CTX_ERR_NONE;
2756 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002757 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002758 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002759 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002760 LIST_INIT(&ctx->buffer_wait.list);
2761 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002762 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002763 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002764
Christopher Fauletf7a30922016-11-10 15:04:51 +01002765 ctx->stream_id = 0;
2766 ctx->frame_id = 1;
2767 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002768
2769 return ctx;
2770}
2771
2772static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002773spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002774{
2775 if (!ctx)
2776 return;
2777
Christopher Faulet58d03682017-09-21 16:57:24 +02002778 spoe_stop_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002779 pool_free2(pool2_spoe_ctx, ctx);
2780}
2781
2782static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002783spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002784{
2785 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002786 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002787}
2788
2789
2790/***************************************************************************
2791 * Hooks that manage the filter lifecycle (init/check/deinit)
2792 **************************************************************************/
2793/* Signal handler: Do a soft stop, wakeup SPOE applet */
2794static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002795spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002796{
2797 struct proxy *p;
2798
2799 p = proxy;
2800 while (p) {
2801 struct flt_conf *fconf;
2802
2803 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002804 struct spoe_config *conf;
2805 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002806 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002807 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002808
Christopher Faulet3b386a32017-02-23 10:17:15 +01002809 if (fconf->id != spoe_filter_id)
2810 continue;
2811
2812 conf = fconf->conf;
2813 agent = conf->agent;
2814
Christopher Faulet24289f22017-09-25 14:48:02 +02002815 for (i = 0; i < global.nbthread; ++i) {
2816 SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
2817 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2818 spoe_wakeup_appctx(spoe_appctx->owner);
2819 SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002820 }
2821 }
2822 p = p->next;
2823 }
2824}
2825
2826
2827/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2828static int
2829spoe_init(struct proxy *px, struct flt_conf *fconf)
2830{
2831 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002832
2833 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2834 init_new_proxy(&conf->agent_fe);
2835 conf->agent_fe.parent = conf->agent;
2836 conf->agent_fe.last_change = now.tv_sec;
2837 conf->agent_fe.id = conf->agent->id;
2838 conf->agent_fe.cap = PR_CAP_FE;
2839 conf->agent_fe.mode = PR_MODE_TCP;
2840 conf->agent_fe.maxconn = 0;
2841 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2842 conf->agent_fe.conn_retries = CONN_RETRIES;
2843 conf->agent_fe.accept = frontend_accept;
2844 conf->agent_fe.srv = NULL;
2845 conf->agent_fe.timeout.client = TICK_ETERNITY;
2846 conf->agent_fe.default_target = &spoe_applet.obj_type;
2847 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2848
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002849 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002850 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002851 sighandler_registered = 1;
2852 }
2853
2854 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002855}
2856
2857/* Free ressources allocated by the SPOE filter. */
2858static void
2859spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2860{
2861 struct spoe_config *conf = fconf->conf;
2862
2863 if (conf) {
2864 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002865
Christopher Faulet8ef75252017-02-20 22:56:03 +01002866 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002867 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002868 free(conf);
2869 }
2870 fconf->conf = NULL;
2871}
2872
2873/* Check configuration of a SPOE filter for a specified proxy.
2874 * Return 1 on error, else 0. */
2875static int
2876spoe_check(struct proxy *px, struct flt_conf *fconf)
2877{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002878 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002879 struct spoe_config *conf = fconf->conf;
2880 struct proxy *target;
2881
Christopher Faulet7ee86672017-09-19 11:08:28 +02002882 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2883 * are uniq */
2884 list_for_each_entry(f, &px->filter_configs, list) {
2885 struct spoe_config *c = f->conf;
2886
2887 /* This is not an SPOE filter */
2888 if (f->id != spoe_filter_id)
2889 continue;
2890 /* This is the current SPOE filter */
2891 if (f == fconf)
2892 continue;
2893
2894 /* Check engine Id. It should be uniq */
2895 if (!strcmp(conf->id, c->id)) {
2896 Alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2897 px->id, conf->id);
2898 return 1;
2899 }
2900 }
2901
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 target = proxy_be_by_name(conf->agent->b.name);
2903 if (target == NULL) {
2904 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2905 " declared at %s:%d.\n",
2906 px->id, conf->agent->b.name, conf->agent->id,
2907 conf->agent->conf.file, conf->agent->conf.line);
2908 return 1;
2909 }
2910 if (target->mode != PR_MODE_TCP) {
2911 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2912 " at %s:%d does not support HTTP mode.\n",
2913 px->id, target->id, conf->agent->id,
2914 conf->agent->conf.file, conf->agent->conf.line);
2915 return 1;
2916 }
2917
2918 free(conf->agent->b.name);
2919 conf->agent->b.name = NULL;
2920 conf->agent->b.be = target;
2921 return 0;
2922}
2923
2924/**************************************************************************
2925 * Hooks attached to a stream
2926 *************************************************************************/
2927/* Called when a filter instance is created and attach to a stream. It creates
2928 * the context that will be used to process this stream. */
2929static int
2930spoe_start(struct stream *s, struct filter *filter)
2931{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002932 struct spoe_config *conf = FLT_CONF(filter);
2933 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002934 struct spoe_context *ctx;
2935
2936 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002937 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002938 __FUNCTION__, s);
2939
Christopher Faulet8ef75252017-02-20 22:56:03 +01002940 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002941 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002942 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2943 " - failed to create SPOE context\n",
2944 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002945 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002947 "SPOE: [%s] failed to create SPOE context\n",
2948 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002949 return 0;
2950 }
2951
2952 ctx->strm = s;
2953 ctx->state = SPOE_CTX_ST_READY;
2954 filter->ctx = ctx;
2955
Christopher Faulet11610f32017-09-21 10:23:10 +02002956 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2958
Christopher Faulet11610f32017-09-21 10:23:10 +02002959 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002960 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2961
Christopher Faulet11610f32017-09-21 10:23:10 +02002962 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963 filter->pre_analyzers |= AN_RES_INSPECT;
2964
Christopher Faulet11610f32017-09-21 10:23:10 +02002965 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002966 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2967
Christopher Faulet11610f32017-09-21 10:23:10 +02002968 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002969 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2970
Christopher Faulet11610f32017-09-21 10:23:10 +02002971 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002972 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2973
2974 return 1;
2975}
2976
2977/* Called when a filter instance is detached from a stream. It release the
2978 * attached SPOE context. */
2979static void
2980spoe_stop(struct stream *s, struct filter *filter)
2981{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2983 (int)now.tv_sec, (int)now.tv_usec,
2984 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2985 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002986 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002987}
2988
Christopher Fauletf7a30922016-11-10 15:04:51 +01002989
2990/*
2991 * Called when the stream is woken up because of expired timer.
2992 */
2993static void
2994spoe_check_timeouts(struct stream *s, struct filter *filter)
2995{
2996 struct spoe_context *ctx = filter->ctx;
2997
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002998 if (tick_is_expired(ctx->process_exp, now_ms)) {
2999 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003000 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003001 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003002}
3003
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003004/* Called when we are ready to filter data on a channel */
3005static int
3006spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3007{
3008 struct spoe_context *ctx = filter->ctx;
3009 int ret = 1;
3010
3011 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3012 " - ctx-flags=0x%08x\n",
3013 (int)now.tv_sec, (int)now.tv_usec,
3014 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3015 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3016
Christopher Fauletb067b062017-01-04 16:39:11 +01003017 if (ctx->state == SPOE_CTX_ST_NONE)
3018 goto out;
3019
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003020 if (!(chn->flags & CF_ISRESP)) {
3021 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3022 chn->analysers |= AN_REQ_INSPECT_FE;
3023 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3024 chn->analysers |= AN_REQ_INSPECT_BE;
3025
3026 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3027 goto out;
3028
3029 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003030 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003031 if (!ret)
3032 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003033 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3034 }
3035 else {
3036 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3037 chn->analysers |= AN_RES_INSPECT;
3038
3039 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3040 goto out;
3041
Christopher Faulet8ef75252017-02-20 22:56:03 +01003042 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003043 if (!ret) {
3044 channel_dont_read(chn);
3045 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003046 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003047 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003048 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049 }
3050
3051 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 return ret;
3053}
3054
3055/* Called before a processing happens on a given channel */
3056static int
3057spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3058 struct channel *chn, unsigned an_bit)
3059{
3060 struct spoe_context *ctx = filter->ctx;
3061 int ret = 1;
3062
3063 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3064 " - ctx-flags=0x%08x - ana=0x%08x\n",
3065 (int)now.tv_sec, (int)now.tv_usec,
3066 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3067 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3068 ctx->flags, an_bit);
3069
Christopher Fauletb067b062017-01-04 16:39:11 +01003070 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003071 goto out;
3072
3073 switch (an_bit) {
3074 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003075 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 break;
3077 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003078 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003079 break;
3080 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003081 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082 break;
3083 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003084 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003085 break;
3086 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003087 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003088 break;
3089 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003090 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091 break;
3092 }
3093
3094 out:
3095 if (!ret) {
3096 channel_dont_read(chn);
3097 channel_dont_close(chn);
3098 }
3099 return ret;
3100}
3101
3102/* Called when the filtering on the channel ends. */
3103static int
3104spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3105{
3106 struct spoe_context *ctx = filter->ctx;
3107
3108 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3109 " - ctx-flags=0x%08x\n",
3110 (int)now.tv_sec, (int)now.tv_usec,
3111 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3112 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3113
3114 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003115 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003116 }
3117
3118 return 1;
3119}
3120
3121/********************************************************************
3122 * Functions that manage the filter initialization
3123 ********************************************************************/
3124struct flt_ops spoe_ops = {
3125 /* Manage SPOE filter, called for each filter declaration */
3126 .init = spoe_init,
3127 .deinit = spoe_deinit,
3128 .check = spoe_check,
3129
3130 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003131 .attach = spoe_start,
3132 .detach = spoe_stop,
3133 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003134
3135 /* Handle channels activity */
3136 .channel_start_analyze = spoe_start_analyze,
3137 .channel_pre_analyze = spoe_chn_pre_analyze,
3138 .channel_end_analyze = spoe_end_analyze,
3139};
3140
3141
3142static int
3143cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3144{
3145 const char *err;
3146 int i, err_code = 0;
3147
3148 if ((cfg_scope == NULL && curengine != NULL) ||
3149 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003150 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003151 goto out;
3152
3153 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3154 if (!*args[1]) {
3155 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3156 file, linenum);
3157 err_code |= ERR_ALERT | ERR_ABORT;
3158 goto out;
3159 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003160 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3161 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162 goto out;
3163 }
3164
3165 err = invalid_char(args[1]);
3166 if (err) {
3167 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3168 file, linenum, *err, args[0], args[1]);
3169 err_code |= ERR_ALERT | ERR_ABORT;
3170 goto out;
3171 }
3172
3173 if (curagent != NULL) {
3174 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3175 file, linenum);
3176 err_code |= ERR_ALERT | ERR_ABORT;
3177 goto out;
3178 }
3179 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3180 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3181 err_code |= ERR_ALERT | ERR_ABORT;
3182 goto out;
3183 }
3184
3185 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003186
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 curagent->conf.file = strdup(file);
3188 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003189
3190 curagent->timeout.hello = TICK_ETERNITY;
3191 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003192 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003193
3194 curagent->engine_id = NULL;
3195 curagent->var_pfx = NULL;
3196 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003197 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3198 if (global.nbthread == 1)
3199 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003200 curagent->cps_max = 0;
3201 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003202 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003203 curagent->min_applets = 0;
3204 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205
3206 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003207 LIST_INIT(&curagent->events[i]);
3208 LIST_INIT(&curagent->groups);
3209 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003210
Christopher Faulet24289f22017-09-25 14:48:02 +02003211 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
3212 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3213 err_code |= ERR_ALERT | ERR_ABORT;
3214 goto out;
3215 }
3216 for (i = 0; i < global.nbthread; ++i) {
3217 curagent->rt[i].frame_size = curagent->max_frame_size;
3218 curagent->rt[i].applets_act = 0;
3219 curagent->rt[i].applets_idle = 0;
3220 curagent->rt[i].sending_rate = 0;
3221 LIST_INIT(&curagent->rt[i].applets);
3222 LIST_INIT(&curagent->rt[i].sending_queue);
3223 LIST_INIT(&curagent->rt[i].waiting_queue);
3224 SPIN_INIT(&curagent->rt[i].lock);
3225 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003226 }
3227 else if (!strcmp(args[0], "use-backend")) {
3228 if (!*args[1]) {
3229 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3230 file, linenum, args[0]);
3231 err_code |= ERR_ALERT | ERR_FATAL;
3232 goto out;
3233 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003234 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003235 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003236 free(curagent->b.name);
3237 curagent->b.name = strdup(args[1]);
3238 }
3239 else if (!strcmp(args[0], "messages")) {
3240 int cur_arg = 1;
3241 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003242 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003243
Christopher Faulet11610f32017-09-21 10:23:10 +02003244 list_for_each_entry(ph, &curmphs, list) {
3245 if (!strcmp(ph->id, args[cur_arg])) {
3246 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247 file, linenum, args[cur_arg]);
3248 err_code |= ERR_ALERT | ERR_FATAL;
3249 goto out;
3250 }
3251 }
3252
Christopher Faulet11610f32017-09-21 10:23:10 +02003253 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003254 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3255 err_code |= ERR_ALERT | ERR_ABORT;
3256 goto out;
3257 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003258 ph->id = strdup(args[cur_arg]);
3259 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003260 cur_arg++;
3261 }
3262 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003263 else if (!strcmp(args[0], "groups")) {
3264 int cur_arg = 1;
3265 while (*args[cur_arg]) {
3266 struct spoe_placeholder *ph = NULL;
3267
3268 list_for_each_entry(ph, &curgphs, list) {
3269 if (!strcmp(ph->id, args[cur_arg])) {
3270 Alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3271 file, linenum, args[cur_arg]);
3272 err_code |= ERR_ALERT | ERR_FATAL;
3273 goto out;
3274 }
3275 }
3276
3277 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3278 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3279 err_code |= ERR_ALERT | ERR_ABORT;
3280 goto out;
3281 }
3282 ph->id = strdup(args[cur_arg]);
3283 LIST_ADDQ(&curgphs, &ph->list);
3284 cur_arg++;
3285 }
3286 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003287 else if (!strcmp(args[0], "timeout")) {
3288 unsigned int *tv = NULL;
3289 const char *res;
3290 unsigned timeout;
3291
3292 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003293 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003294 file, linenum);
3295 err_code |= ERR_ALERT | ERR_FATAL;
3296 goto out;
3297 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003298 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3299 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003300 if (!strcmp(args[1], "hello"))
3301 tv = &curagent->timeout.hello;
3302 else if (!strcmp(args[1], "idle"))
3303 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003304 else if (!strcmp(args[1], "processing"))
3305 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003306 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003307 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003308 file, linenum, args[1]);
3309 err_code |= ERR_ALERT | ERR_FATAL;
3310 goto out;
3311 }
3312 if (!*args[2]) {
3313 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3314 file, linenum, args[1]);
3315 err_code |= ERR_ALERT | ERR_FATAL;
3316 goto out;
3317 }
3318 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3319 if (res) {
3320 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3321 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003322 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003323 goto out;
3324 }
3325 *tv = MS_TO_TICKS(timeout);
3326 }
3327 else if (!strcmp(args[0], "option")) {
3328 if (!*args[1]) {
3329 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3330 file, linenum, args[0]);
3331 err_code |= ERR_ALERT | ERR_FATAL;
3332 goto out;
3333 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003334
Christopher Faulet305c6072017-02-23 16:17:53 +01003335 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003336 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003337 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003338 if (kwm == 1)
3339 curagent->flags &= ~SPOE_FL_PIPELINING;
3340 else
3341 curagent->flags |= SPOE_FL_PIPELINING;
3342 goto out;
3343 }
3344 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003345 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003346 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003347 if (kwm == 1)
3348 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003349 else {
3350 if (global.nbthread == 1)
3351 curagent->flags |= SPOE_FL_ASYNC;
3352 else {
3353 Warning("parsing [%s:%d] Async option is not supported with threads.\n",
3354 file, linenum);
3355 err_code |= ERR_WARN;
3356 }
3357 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003358 goto out;
3359 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003360 else if (!strcmp(args[1], "send-frag-payload")) {
3361 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3362 goto out;
3363 if (kwm == 1)
3364 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3365 else
3366 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3367 goto out;
3368 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003369
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003370 /* Following options does not support negation */
3371 if (kwm == 1) {
3372 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3373 file, linenum, args[1]);
3374 err_code |= ERR_ALERT | ERR_FATAL;
3375 goto out;
3376 }
3377
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 if (!strcmp(args[1], "var-prefix")) {
3379 char *tmp;
3380
3381 if (!*args[2]) {
3382 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3383 file, linenum, args[0],
3384 args[1]);
3385 err_code |= ERR_ALERT | ERR_FATAL;
3386 goto out;
3387 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003388 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3389 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003390 tmp = args[2];
3391 while (*tmp) {
3392 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3393 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3394 file, linenum, args[0], args[1]);
3395 err_code |= ERR_ALERT | ERR_FATAL;
3396 goto out;
3397 }
3398 tmp++;
3399 }
3400 curagent->var_pfx = strdup(args[2]);
3401 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003402 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003403 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003404 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003405 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3406 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003407 else if (!strcmp(args[1], "set-on-error")) {
3408 char *tmp;
3409
3410 if (!*args[2]) {
3411 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3412 file, linenum, args[0],
3413 args[1]);
3414 err_code |= ERR_ALERT | ERR_FATAL;
3415 goto out;
3416 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003417 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3418 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003419 tmp = args[2];
3420 while (*tmp) {
3421 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3422 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3423 file, linenum, args[0], args[1]);
3424 err_code |= ERR_ALERT | ERR_FATAL;
3425 goto out;
3426 }
3427 tmp++;
3428 }
3429 curagent->var_on_error = strdup(args[2]);
3430 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003431 else {
3432 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3433 file, linenum, args[1]);
3434 err_code |= ERR_ALERT | ERR_FATAL;
3435 goto out;
3436 }
Christopher Faulet48026722016-11-16 15:01:12 +01003437 }
3438 else if (!strcmp(args[0], "maxconnrate")) {
3439 if (!*args[1]) {
3440 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3441 file, linenum, args[0]);
3442 err_code |= ERR_ALERT | ERR_FATAL;
3443 goto out;
3444 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003445 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003446 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003447 curagent->cps_max = atol(args[1]);
3448 }
3449 else if (!strcmp(args[0], "maxerrrate")) {
3450 if (!*args[1]) {
3451 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3452 file, linenum, args[0]);
3453 err_code |= ERR_ALERT | ERR_FATAL;
3454 goto out;
3455 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003456 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003457 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003458 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003460 else if (!strcmp(args[0], "max-frame-size")) {
3461 if (!*args[1]) {
3462 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3463 file, linenum, args[0]);
3464 err_code |= ERR_ALERT | ERR_FATAL;
3465 goto out;
3466 }
3467 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3468 goto out;
3469 curagent->max_frame_size = atol(args[1]);
3470 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3471 curagent->max_frame_size > MAX_FRAME_SIZE) {
3472 Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3473 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
3474 err_code |= ERR_ALERT | ERR_FATAL;
3475 goto out;
3476 }
3477 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 else if (*args[0]) {
3479 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3480 file, linenum, args[0]);
3481 err_code |= ERR_ALERT | ERR_FATAL;
3482 goto out;
3483 }
3484 out:
3485 return err_code;
3486}
Christopher Faulet11610f32017-09-21 10:23:10 +02003487static int
3488cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3489{
3490 struct spoe_group *grp;
3491 const char *err;
3492 int err_code = 0;
3493
3494 if ((cfg_scope == NULL && curengine != NULL) ||
3495 (cfg_scope != NULL && curengine == NULL) ||
3496 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3497 goto out;
3498
3499 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3500 if (!*args[1]) {
3501 Alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3502 file, linenum);
3503 err_code |= ERR_ALERT | ERR_ABORT;
3504 goto out;
3505 }
3506 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3507 err_code |= ERR_ABORT;
3508 goto out;
3509 }
3510
3511 err = invalid_char(args[1]);
3512 if (err) {
3513 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3514 file, linenum, *err, args[0], args[1]);
3515 err_code |= ERR_ALERT | ERR_ABORT;
3516 goto out;
3517 }
3518
3519 list_for_each_entry(grp, &curgrps, list) {
3520 if (!strcmp(grp->id, args[1])) {
3521 Alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3522 " name as another one declared at %s:%d.\n",
3523 file, linenum, args[1], grp->conf.file, grp->conf.line);
3524 err_code |= ERR_ALERT | ERR_FATAL;
3525 goto out;
3526 }
3527 }
3528
3529 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
3530 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3531 err_code |= ERR_ALERT | ERR_ABORT;
3532 goto out;
3533 }
3534
3535 curgrp->id = strdup(args[1]);
3536 curgrp->conf.file = strdup(file);
3537 curgrp->conf.line = linenum;
3538 LIST_INIT(&curgrp->phs);
3539 LIST_INIT(&curgrp->messages);
3540 LIST_ADDQ(&curgrps, &curgrp->list);
3541 }
3542 else if (!strcmp(args[0], "messages")) {
3543 int cur_arg = 1;
3544 while (*args[cur_arg]) {
3545 struct spoe_placeholder *ph = NULL;
3546
3547 list_for_each_entry(ph, &curgrp->phs, list) {
3548 if (!strcmp(ph->id, args[cur_arg])) {
3549 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3550 file, linenum, args[cur_arg]);
3551 err_code |= ERR_ALERT | ERR_FATAL;
3552 goto out;
3553 }
3554 }
3555
3556 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3557 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3558 err_code |= ERR_ALERT | ERR_ABORT;
3559 goto out;
3560 }
3561 ph->id = strdup(args[cur_arg]);
3562 LIST_ADDQ(&curgrp->phs, &ph->list);
3563 cur_arg++;
3564 }
3565 }
3566 else if (*args[0]) {
3567 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3568 file, linenum, args[0]);
3569 err_code |= ERR_ALERT | ERR_FATAL;
3570 goto out;
3571 }
3572 out:
3573 return err_code;
3574}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003575
3576static int
3577cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3578{
3579 struct spoe_message *msg;
3580 struct spoe_arg *arg;
3581 const char *err;
3582 char *errmsg = NULL;
3583 int err_code = 0;
3584
3585 if ((cfg_scope == NULL && curengine != NULL) ||
3586 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003587 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003588 goto out;
3589
3590 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3591 if (!*args[1]) {
3592 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3593 file, linenum);
3594 err_code |= ERR_ALERT | ERR_ABORT;
3595 goto out;
3596 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003597 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3598 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003599 goto out;
3600 }
3601
3602 err = invalid_char(args[1]);
3603 if (err) {
3604 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3605 file, linenum, *err, args[0], args[1]);
3606 err_code |= ERR_ALERT | ERR_ABORT;
3607 goto out;
3608 }
3609
3610 list_for_each_entry(msg, &curmsgs, list) {
3611 if (!strcmp(msg->id, args[1])) {
3612 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3613 " name as another one declared at %s:%d.\n",
3614 file, linenum, args[1], msg->conf.file, msg->conf.line);
3615 err_code |= ERR_ALERT | ERR_FATAL;
3616 goto out;
3617 }
3618 }
3619
3620 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3621 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3622 err_code |= ERR_ALERT | ERR_ABORT;
3623 goto out;
3624 }
3625
3626 curmsg->id = strdup(args[1]);
3627 curmsg->id_len = strlen(curmsg->id);
3628 curmsg->event = SPOE_EV_NONE;
3629 curmsg->conf.file = strdup(file);
3630 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003631 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003632 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003633 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003634 LIST_INIT(&curmsg->by_evt);
3635 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003636 LIST_ADDQ(&curmsgs, &curmsg->list);
3637 }
3638 else if (!strcmp(args[0], "args")) {
3639 int cur_arg = 1;
3640
3641 curproxy->conf.args.ctx = ARGC_SPOE;
3642 curproxy->conf.args.file = file;
3643 curproxy->conf.args.line = linenum;
3644 while (*args[cur_arg]) {
3645 char *delim = strchr(args[cur_arg], '=');
3646 int idx = 0;
3647
3648 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3649 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3650 err_code |= ERR_ALERT | ERR_ABORT;
3651 goto out;
3652 }
3653
3654 if (!delim) {
3655 arg->name = NULL;
3656 arg->name_len = 0;
3657 delim = args[cur_arg];
3658 }
3659 else {
3660 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3661 arg->name_len = delim - args[cur_arg];
3662 delim++;
3663 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003664 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3665 &idx, file, linenum, &errmsg,
3666 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003667 if (arg->expr == NULL) {
3668 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3669 err_code |= ERR_ALERT | ERR_FATAL;
3670 free(arg->name);
3671 free(arg);
3672 goto out;
3673 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003674 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003675 LIST_ADDQ(&curmsg->args, &arg->list);
3676 cur_arg++;
3677 }
3678 curproxy->conf.args.file = NULL;
3679 curproxy->conf.args.line = 0;
3680 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003681 else if (!strcmp(args[0], "acl")) {
3682 err = invalid_char(args[1]);
3683 if (err) {
3684 Alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3685 file, linenum, *err, args[1]);
3686 err_code |= ERR_ALERT | ERR_FATAL;
3687 goto out;
3688 }
3689 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
3690 Alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3691 file, linenum, args[1], errmsg);
3692 err_code |= ERR_ALERT | ERR_FATAL;
3693 goto out;
3694 }
3695 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003696 else if (!strcmp(args[0], "event")) {
3697 if (!*args[1]) {
3698 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003699 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003700 goto out;
3701 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003702 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3703 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003704
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003705 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3706 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3707 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3708 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3709
3710 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3711 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3712 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3713 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3714 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3715 curmsg->event = SPOE_EV_ON_TCP_RSP;
3716
3717 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3718 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3719 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3720 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3721 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3722 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3723 else {
3724 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3725 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003726 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003727 goto out;
3728 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003729
3730 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3731 struct acl_cond *cond;
3732
3733 cond = build_acl_cond(file, linenum, &curmsg->acls,
3734 curproxy, (const char **)args+2,
3735 &errmsg);
3736 if (cond == NULL) {
3737 Alert("parsing [%s:%d] : error detected while "
3738 "parsing an 'event %s' condition : %s.\n",
3739 file, linenum, args[1], errmsg);
3740 err_code |= ERR_ALERT | ERR_FATAL;
3741 goto out;
3742 }
3743 curmsg->cond = cond;
3744 }
3745 else if (*args[2]) {
3746 Alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3747 "or 'unless' followed by a condition but found '%s'.\n",
3748 file, linenum, args[1], args[2]);
3749 err_code |= ERR_ALERT | ERR_FATAL;
3750 goto out;
3751 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003752 }
3753 else if (!*args[0]) {
3754 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3755 file, linenum, args[0]);
3756 err_code |= ERR_ALERT | ERR_FATAL;
3757 goto out;
3758 }
3759 out:
3760 free(errmsg);
3761 return err_code;
3762}
3763
3764/* Return -1 on error, else 0 */
3765static int
3766parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3767 struct flt_conf *fconf, char **err, void *private)
3768{
3769 struct list backup_sections;
3770 struct spoe_config *conf;
3771 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003772 struct spoe_group *grp, *grpback;
3773 struct spoe_placeholder *ph, *phback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003774 char *file = NULL, *engine = NULL;
3775 int ret, pos = *cur_arg + 1;
3776
3777 conf = calloc(1, sizeof(*conf));
3778 if (conf == NULL) {
3779 memprintf(err, "%s: out of memory", args[*cur_arg]);
3780 goto error;
3781 }
3782 conf->proxy = px;
3783
3784 while (*args[pos]) {
3785 if (!strcmp(args[pos], "config")) {
3786 if (!*args[pos+1]) {
3787 memprintf(err, "'%s' : '%s' option without value",
3788 args[*cur_arg], args[pos]);
3789 goto error;
3790 }
3791 file = args[pos+1];
3792 pos += 2;
3793 }
3794 else if (!strcmp(args[pos], "engine")) {
3795 if (!*args[pos+1]) {
3796 memprintf(err, "'%s' : '%s' option without value",
3797 args[*cur_arg], args[pos]);
3798 goto error;
3799 }
3800 engine = args[pos+1];
3801 pos += 2;
3802 }
3803 else {
3804 memprintf(err, "unknown keyword '%s'", args[pos]);
3805 goto error;
3806 }
3807 }
3808 if (file == NULL) {
3809 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3810 goto error;
3811 }
3812
3813 /* backup sections and register SPOE sections */
3814 LIST_INIT(&backup_sections);
3815 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003816 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3817 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003818 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003819
3820 /* Parse SPOE filter configuration file */
3821 curengine = engine;
3822 curproxy = px;
3823 curagent = NULL;
3824 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003825 LIST_INIT(&curmsgs);
3826 LIST_INIT(&curgrps);
3827 LIST_INIT(&curmphs);
3828 LIST_INIT(&curgphs);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003829 ret = readcfgfile(file);
3830 curproxy = NULL;
3831
3832 /* unregister SPOE sections and restore previous sections */
3833 cfg_unregister_sections();
3834 cfg_restore_sections(&backup_sections);
3835
3836 if (ret == -1) {
3837 memprintf(err, "Could not open configuration file %s : %s",
3838 file, strerror(errno));
3839 goto error;
3840 }
3841 if (ret & (ERR_ABORT|ERR_FATAL)) {
3842 memprintf(err, "Error(s) found in configuration file %s", file);
3843 goto error;
3844 }
3845
3846 /* Check SPOE agent */
3847 if (curagent == NULL) {
3848 memprintf(err, "No SPOE agent found in file %s", file);
3849 goto error;
3850 }
3851 if (curagent->b.name == NULL) {
3852 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3853 curagent->id, curagent->conf.file, curagent->conf.line);
3854 goto error;
3855 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003856 if (curagent->timeout.hello == TICK_ETERNITY ||
3857 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003858 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003859 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3860 " | While not properly invalid, you will certainly encounter various problems\n"
3861 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003862 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003863 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3864 }
3865 if (curagent->var_pfx == NULL) {
3866 char *tmp = curagent->id;
3867
3868 while (*tmp) {
3869 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3870 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3871 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3872 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3873 goto error;
3874 }
3875 tmp++;
3876 }
3877 curagent->var_pfx = strdup(curagent->id);
3878 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003879 if (curagent->engine_id == NULL)
3880 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003881
Christopher Faulet11610f32017-09-21 10:23:10 +02003882 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
3883 Warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003884 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3885 goto finish;
3886 }
3887
Christopher Faulet11610f32017-09-21 10:23:10 +02003888 /* Replace placeholders by the corresponding messages for the SPOE
3889 * agent */
3890 list_for_each_entry(ph, &curmphs, list) {
3891 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003892 struct spoe_arg *arg;
3893 unsigned int where;
3894
Christopher Faulet11610f32017-09-21 10:23:10 +02003895 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003896 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3897 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3898 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3899 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3900 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3901 }
3902 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3903 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3904 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3905 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3906 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003907 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003908 }
3909 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003910 Warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3911 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003912 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003913 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003914
3915 where = 0;
3916 switch (msg->event) {
3917 case SPOE_EV_ON_CLIENT_SESS:
3918 where |= SMP_VAL_FE_CON_ACC;
3919 break;
3920
3921 case SPOE_EV_ON_TCP_REQ_FE:
3922 where |= SMP_VAL_FE_REQ_CNT;
3923 break;
3924
3925 case SPOE_EV_ON_HTTP_REQ_FE:
3926 where |= SMP_VAL_FE_HRQ_HDR;
3927 break;
3928
3929 case SPOE_EV_ON_TCP_REQ_BE:
3930 if (px->cap & PR_CAP_FE)
3931 where |= SMP_VAL_FE_REQ_CNT;
3932 if (px->cap & PR_CAP_BE)
3933 where |= SMP_VAL_BE_REQ_CNT;
3934 break;
3935
3936 case SPOE_EV_ON_HTTP_REQ_BE:
3937 if (px->cap & PR_CAP_FE)
3938 where |= SMP_VAL_FE_HRQ_HDR;
3939 if (px->cap & PR_CAP_BE)
3940 where |= SMP_VAL_BE_HRQ_HDR;
3941 break;
3942
3943 case SPOE_EV_ON_SERVER_SESS:
3944 where |= SMP_VAL_BE_SRV_CON;
3945 break;
3946
3947 case SPOE_EV_ON_TCP_RSP:
3948 if (px->cap & PR_CAP_FE)
3949 where |= SMP_VAL_FE_RES_CNT;
3950 if (px->cap & PR_CAP_BE)
3951 where |= SMP_VAL_BE_RES_CNT;
3952 break;
3953
3954 case SPOE_EV_ON_HTTP_RSP:
3955 if (px->cap & PR_CAP_FE)
3956 where |= SMP_VAL_FE_HRS_HDR;
3957 if (px->cap & PR_CAP_BE)
3958 where |= SMP_VAL_BE_HRS_HDR;
3959 break;
3960
3961 default:
3962 break;
3963 }
3964
3965 list_for_each_entry(arg, &msg->args, list) {
3966 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003967 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003968 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003969 "none of which is available here ('%s')",
3970 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003971 sample_ckp_names(arg->expr->fetch->use),
3972 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003973 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003974 }
3975 }
3976
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003977 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003978 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3979 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 }
3981 }
3982 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003983 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003984 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003985 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003986 continue;
3987 }
3988
Christopher Faulet11610f32017-09-21 10:23:10 +02003989 /* Replace placeholders by the corresponding groups for the SPOE
3990 * agent */
3991 list_for_each_entry(ph, &curgphs, list) {
3992 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3993 if (!strcmp(grp->id, ph->id)) {
3994 grp->agent = curagent;
3995 LIST_DEL(&grp->list);
3996 LIST_ADDQ(&curagent->groups, &grp->list);
3997 goto next_aph;
3998 }
3999 }
4000 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4001 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4002 goto error;
4003 next_aph:
4004 continue;
4005 }
4006
4007 /* Replace placeholders by the corresponding message for each SPOE
4008 * group of the SPOE agent */
4009 list_for_each_entry(grp, &curagent->groups, list) {
4010 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4011 list_for_each_entry(msg, &curmsgs, list) {
4012 if (!strcmp(msg->id, ph->id)) {
4013 if (msg->group != NULL) {
4014 memprintf(err, "SPOE message '%s' already belongs to "
4015 "the SPOE group '%s' declare at %s:%d",
4016 msg->id, msg->group->id,
4017 msg->group->conf.file,
4018 msg->group->conf.line);
4019 goto error;
4020 }
4021
4022 /* Scope for arguments are not checked for now. We will check
4023 * them only if a rule use the corresponding SPOE group. */
4024 msg->agent = curagent;
4025 msg->group = grp;
4026 LIST_DEL(&ph->list);
4027 LIST_ADDQ(&grp->messages, &msg->by_grp);
4028 goto next_mph_grp;
4029 }
4030 }
4031 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4032 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4033 goto error;
4034 next_mph_grp:
4035 continue;
4036 }
4037 }
4038
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004039 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004040 /* move curmsgs to the agent message list */
4041 curmsgs.n->p = &curagent->messages;
4042 curmsgs.p->n = &curagent->messages;
4043 curagent->messages = curmsgs;
4044 LIST_INIT(&curmsgs);
4045
Christopher Faulet7ee86672017-09-19 11:08:28 +02004046 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004047 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004048 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4049 LIST_DEL(&ph->list);
4050 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004051 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004052 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4053 LIST_DEL(&ph->list);
4054 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004056 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4057 LIST_DEL(&grp->list);
4058 spoe_release_group(grp);
4059 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004060 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004061 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004062 fconf->ops = &spoe_ops;
4063 fconf->conf = conf;
4064 return 0;
4065
4066 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004067 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004068 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4069 LIST_DEL(&ph->list);
4070 spoe_release_placeholder(ph);
4071 }
4072 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4073 LIST_DEL(&ph->list);
4074 spoe_release_placeholder(ph);
4075 }
4076 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4077 LIST_DEL(&grp->list);
4078 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004079 }
4080 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4081 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004082 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004083 }
4084 free(conf);
4085 return -1;
4086}
4087
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004088/* Send message of a SPOE group. This is the action_ptr callback of a rule
4089 * associated to a "send-spoe-group" action.
4090 *
4091 * It returns ACT_RET_CONT is processing is finished without error, it returns
4092 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4093 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004094static enum act_return
4095spoe_send_group(struct act_rule *rule, struct proxy *px,
4096 struct session *sess, struct stream *s, int flags)
4097{
4098 struct filter *filter;
4099 struct spoe_agent *agent = NULL;
4100 struct spoe_group *group = NULL;
4101 struct spoe_context *ctx = NULL;
4102 int ret, dir;
4103
4104 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4105 if (filter->config == rule->arg.act.p[0]) {
4106 agent = rule->arg.act.p[2];
4107 group = rule->arg.act.p[3];
4108 ctx = filter->ctx;
4109 break;
4110 }
4111 }
4112 if (agent == NULL || group == NULL || ctx == NULL)
4113 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004114 if (ctx->state == SPOE_CTX_ST_NONE)
4115 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004116
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004117 switch (rule->from) {
4118 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4119 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4120 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4121 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4122 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4123 default:
4124 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4125 " - internal error while execute spoe-send-group\n",
4126 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4127 __FUNCTION__, s);
4128 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4129 agent->id);
4130 return ACT_RET_CONT;
4131 }
4132
4133 ret = spoe_process_group(s, ctx, group, dir);
4134 if (ret == 1)
4135 return ACT_RET_CONT;
4136 else if (ret == 0) {
4137 if (flags & ACT_FLAG_FINAL) {
4138 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4139 " - failed to process group '%s': interrupted by caller\n",
4140 (int)now.tv_sec, (int)now.tv_usec,
4141 agent->id, __FUNCTION__, s, group->id);
4142 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4143 spoe_handle_processing_error(s, agent, ctx, dir);
4144 spoe_stop_processing(ctx);
4145 return ACT_RET_CONT;
4146 }
4147 return ACT_RET_YIELD;
4148 }
4149 else
4150 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004151}
4152
4153/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4154 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4155 * action should be:
4156 *
4157 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4158 *
4159 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4160 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4161 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4162 * group.
4163 *
4164 * The function returns 1 in success case, otherwise, it returns 0 and err is
4165 * filled.
4166 */
4167static int
4168check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4169{
4170 struct flt_conf *fconf;
4171 struct spoe_config *conf;
4172 struct spoe_agent *agent = NULL;
4173 struct spoe_group *group;
4174 struct spoe_message *msg;
4175 char *engine_id = rule->arg.act.p[0];
4176 char *group_id = rule->arg.act.p[1];
4177 unsigned int where = 0;
4178
4179 switch (rule->from) {
4180 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4181 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4182 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4183 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4184 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4185 default:
4186 memprintf(err,
4187 "internal error, unexpected rule->from=%d, please report this bug!",
4188 rule->from);
4189 goto error;
4190 }
4191
4192 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4193 * <px> */
4194 list_for_each_entry(fconf, &px->filter_configs, list) {
4195 conf = fconf->conf;
4196
4197 /* This is not an SPOE filter */
4198 if (fconf->id != spoe_filter_id)
4199 continue;
4200
4201 /* This is the good engine */
4202 if (!strcmp(conf->id, engine_id)) {
4203 agent = conf->agent;
4204 break;
4205 }
4206 }
4207 if (agent == NULL) {
4208 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4209 engine_id, group_id);
4210 goto error;
4211 }
4212
4213 /* Try to find the right group */
4214 list_for_each_entry(group, &agent->groups, list) {
4215 /* This is the good group */
4216 if (!strcmp(group->id, group_id))
4217 break;
4218 }
4219 if (&group->list == &agent->groups) {
4220 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4221 group_id, engine_id);
4222 goto error;
4223 }
4224
4225 /* Ok, we found the group, we need to check messages and their
4226 * arguments */
4227 list_for_each_entry(msg, &group->messages, by_grp) {
4228 struct spoe_arg *arg;
4229
4230 list_for_each_entry(arg, &msg->args, list) {
4231 if (!(arg->expr->fetch->val & where)) {
4232 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4233 "some args extract information from '%s',"
4234 "none of which is available here ('%s')",
4235 msg->id, group->id, msg->conf.file, msg->conf.line,
4236 sample_ckp_names(arg->expr->fetch->use),
4237 sample_ckp_names(where));
4238 goto error;
4239 }
4240 }
4241 }
4242
4243 free(engine_id);
4244 free(group_id);
4245 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4246 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4247 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4248 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4249 return 1;
4250
4251 error:
4252 free(engine_id);
4253 free(group_id);
4254 return 0;
4255}
4256
4257/* Parse 'send-spoe-group' action following the format:
4258 *
4259 * ... send-spoe-group <engine-id> <group-id>
4260 *
4261 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4262 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4263 * ids are saved and used later, when the rule will be checked.
4264 */
4265static enum act_parse_ret
4266parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4267 struct act_rule *rule, char **err)
4268{
4269 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4270 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4271 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4272 return ACT_RET_PRS_ERR;
4273 }
4274 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4275 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4276
4277 (*orig_arg) += 2;
4278
4279 rule->action = ACT_CUSTOM;
4280 rule->action_ptr = spoe_send_group;
4281 rule->check_ptr = check_send_spoe_group;
4282 return ACT_RET_PRS_OK;
4283}
4284
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004285
4286/* Declare the filter parser for "spoe" keyword */
4287static struct flt_kw_list flt_kws = { "SPOE", { }, {
4288 { "spoe", parse_spoe_flt, NULL },
4289 { NULL, NULL, NULL },
4290 }
4291};
4292
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004293/* Delcate the action parser for "spoe-action" keyword */
4294static struct action_kw_list tcp_req_action_kws = { { }, {
4295 { "send-spoe-group", parse_send_spoe_group },
4296 { /* END */ },
4297 }
4298};
4299static struct action_kw_list tcp_res_action_kws = { { }, {
4300 { "send-spoe-group", parse_send_spoe_group },
4301 { /* END */ },
4302 }
4303};
4304static struct action_kw_list http_req_action_kws = { { }, {
4305 { "send-spoe-group", parse_send_spoe_group },
4306 { /* END */ },
4307 }
4308};
4309static struct action_kw_list http_res_action_kws = { { }, {
4310 { "send-spoe-group", parse_send_spoe_group },
4311 { /* END */ },
4312 }
4313};
4314
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004315__attribute__((constructor))
4316static void __spoe_init(void)
4317{
4318 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004319 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4320 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4321 http_req_keywords_register(&http_req_action_kws);
4322 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004323
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004324 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004325 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004326}
4327
4328__attribute__((destructor))
4329static void
4330__spoe_deinit(void)
4331{
4332 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004333 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004334}