Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1 | /* |
| 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 Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 15 | #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 Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 21 | #include <common/hathreads.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 22 | |
| 23 | #include <types/arg.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 24 | #include <types/global.h> |
Christopher Faulet | 1f40b91 | 2017-02-17 09:32:19 +0100 | [diff] [blame] | 25 | #include <types/spoe.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 26 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 27 | #include <proto/acl.h> |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 28 | #include <proto/action.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 29 | #include <proto/arg.h> |
| 30 | #include <proto/backend.h> |
| 31 | #include <proto/filters.h> |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 32 | #include <proto/freq_ctr.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 33 | #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 Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 40 | #include <proto/spoe.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 41 | #include <proto/stream.h> |
| 42 | #include <proto/stream_interface.h> |
| 43 | #include <proto/task.h> |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 44 | #include <proto/tcp_rules.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 45 | #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 Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 53 | /* 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 Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 60 | /* Reserved for the metadata and the frame type. |
| 61 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 62 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 63 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 64 | /* Helper to get SPOE ctx inside an appctx */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 65 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr)) |
| 66 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 67 | /* SPOE filter id. Used to identify SPOE filters */ |
| 68 | const char *spoe_filter_id = "SPOE filter"; |
| 69 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 70 | /* Set if the handle on SIGUSR1 is registered */ |
| 71 | static int sighandler_registered = 0; |
| 72 | |
| 73 | /* proxy used during the parsing */ |
| 74 | struct proxy *curproxy = NULL; |
| 75 | |
| 76 | /* The name of the SPOE engine, used during the parsing */ |
| 77 | char *curengine = NULL; |
| 78 | |
| 79 | /* SPOE agent used during the parsing */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 80 | /* SPOE agent/group/message used during the parsing */ |
| 81 | struct spoe_agent *curagent = NULL; |
| 82 | struct spoe_group *curgrp = NULL; |
| 83 | struct spoe_message *curmsg = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 84 | |
| 85 | /* list of SPOE messages and placeholders used during the parsing */ |
| 86 | struct list curmsgs; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 87 | struct list curgrps; |
| 88 | struct list curmphs; |
| 89 | struct list curgphs; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 90 | struct list curvars; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 91 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 92 | /* Pools used to allocate SPOE structs */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 93 | static struct pool_head *pool_head_spoe_ctx = NULL; |
| 94 | static struct pool_head *pool_head_spoe_appctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 95 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 96 | struct flt_ops spoe_ops; |
| 97 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 98 | static int spoe_queue_context(struct spoe_context *ctx); |
| 99 | static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait); |
| 100 | static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 101 | |
| 102 | /******************************************************************** |
| 103 | * helper functions/globals |
| 104 | ********************************************************************/ |
| 105 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 106 | spoe_release_placeholder(struct spoe_placeholder *ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 107 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 108 | if (!ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 109 | return; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 110 | free(ph->id); |
| 111 | free(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 114 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 115 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 116 | { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 117 | struct spoe_arg *arg, *argback; |
| 118 | struct acl *acl, *aclback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 119 | |
| 120 | if (!msg) |
| 121 | return; |
| 122 | free(msg->id); |
| 123 | free(msg->conf.file); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 124 | list_for_each_entry_safe(arg, argback, &msg->args, list) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 125 | release_sample_expr(arg->expr); |
| 126 | free(arg->name); |
| 127 | LIST_DEL(&arg->list); |
| 128 | free(arg); |
| 129 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 130 | list_for_each_entry_safe(acl, aclback, &msg->acls, list) { |
| 131 | LIST_DEL(&acl->list); |
| 132 | prune_acl(acl); |
| 133 | free(acl); |
| 134 | } |
| 135 | if (msg->cond) { |
| 136 | prune_acl_cond(msg->cond); |
| 137 | free(msg->cond); |
| 138 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 139 | free(msg); |
| 140 | } |
| 141 | |
| 142 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 143 | spoe_release_group(struct spoe_group *grp) |
| 144 | { |
| 145 | if (!grp) |
| 146 | return; |
| 147 | free(grp->id); |
| 148 | free(grp->conf.file); |
| 149 | free(grp); |
| 150 | } |
| 151 | |
| 152 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 153 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 154 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 155 | struct spoe_message *msg, *msgback; |
| 156 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 157 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 158 | |
| 159 | if (!agent) |
| 160 | return; |
| 161 | free(agent->id); |
| 162 | free(agent->conf.file); |
| 163 | free(agent->var_pfx); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 164 | free(agent->engine_id); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 165 | free(agent->var_on_error); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 166 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
| 167 | LIST_DEL(&msg->list); |
| 168 | spoe_release_message(msg); |
| 169 | } |
| 170 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
| 171 | LIST_DEL(&grp->list); |
| 172 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 173 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 174 | for (i = 0; i < global.nbthread; ++i) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 175 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 176 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 177 | free(agent); |
| 178 | } |
| 179 | |
| 180 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 181 | [SPOE_FRM_ERR_NONE] = "normal", |
| 182 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 183 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 184 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 185 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 186 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 187 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 188 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 189 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 190 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 191 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 192 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 193 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 194 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 195 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 199 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 200 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 201 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 202 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 203 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 204 | |
| 205 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 206 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 207 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 208 | }; |
| 209 | |
| 210 | |
| 211 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 212 | |
| 213 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 214 | [SPOE_CTX_ST_NONE] = "NONE", |
| 215 | [SPOE_CTX_ST_READY] = "READY", |
| 216 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 217 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 218 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 219 | [SPOE_CTX_ST_DONE] = "DONE", |
| 220 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 224 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 225 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 226 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 227 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 228 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 229 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 230 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 231 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 232 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 233 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 237 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 238 | /* Used to generates a unique id for an engine. On success, it returns a |
| 239 | * allocated string. So it is the caller's reponsibility to release it. If the |
| 240 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 241 | static char * |
| 242 | generate_pseudo_uuid() |
| 243 | { |
| 244 | static int init = 0; |
| 245 | |
| 246 | const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; |
| 247 | const char uuid_chr[] = "0123456789ABCDEF-"; |
| 248 | char *uuid; |
| 249 | int i; |
| 250 | |
| 251 | if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL) |
| 252 | return NULL; |
| 253 | |
| 254 | if (!init) { |
| 255 | srand(now_ms); |
| 256 | init = 1; |
| 257 | } |
| 258 | |
| 259 | for (i = 0; i < sizeof(uuid_fmt)-1; i++) { |
| 260 | int r = rand () % 16; |
| 261 | |
| 262 | switch (uuid_fmt[i]) { |
| 263 | case 'x' : uuid[i] = uuid_chr[r]; break; |
| 264 | case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break; |
| 265 | default : uuid[i] = uuid_fmt[i]; break; |
| 266 | } |
| 267 | } |
| 268 | return uuid; |
| 269 | } |
| 270 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 271 | /******************************************************************** |
| 272 | * Functions that encode/decode SPOE frames |
| 273 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 274 | /* Helper to get static string length, excluding the terminating null byte */ |
| 275 | #define SLEN(str) (sizeof(str)-1) |
| 276 | |
| 277 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 278 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 279 | #define VERSION_KEY "version" |
| 280 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 281 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 282 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 283 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 284 | #define STATUS_CODE_KEY "status-code" |
| 285 | #define MSG_KEY "message" |
| 286 | |
| 287 | struct spoe_version { |
| 288 | char *str; |
| 289 | int min; |
| 290 | int max; |
| 291 | }; |
| 292 | |
| 293 | /* All supported versions */ |
| 294 | static struct spoe_version supported_versions[] = { |
| 295 | {"1.0", 1000, 1000}, |
| 296 | {NULL, 0, 0} |
| 297 | }; |
| 298 | |
| 299 | /* Comma-separated list of supported versions */ |
| 300 | #define SUPPORTED_VERSIONS_VAL "1.0" |
| 301 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 302 | /* Convert a string to a SPOE version value. The string must follow the format |
| 303 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 304 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 305 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 306 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 307 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 308 | const char *p, *end; |
| 309 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 310 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 311 | p = str; |
| 312 | end = str+len; |
| 313 | maj = min = 0; |
| 314 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 315 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 316 | /* skip leading spaces */ |
| 317 | while (p < end && isspace(*p)) |
| 318 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 319 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 320 | /* parse Major number, until the '.' */ |
| 321 | while (*p != '.') { |
| 322 | if (p >= end || *p < '0' || *p > '9') |
| 323 | goto out; |
| 324 | maj *= 10; |
| 325 | maj += (*p - '0'); |
| 326 | p++; |
| 327 | } |
| 328 | |
| 329 | /* check Major version */ |
| 330 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 331 | goto out; |
| 332 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 333 | p++; /* skip the '.' */ |
| 334 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 335 | goto out; |
| 336 | |
| 337 | /* Parse Minor number */ |
| 338 | while (p < end) { |
| 339 | if (*p < '0' || *p > '9') |
| 340 | break; |
| 341 | min *= 10; |
| 342 | min += (*p - '0'); |
| 343 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 344 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 345 | |
| 346 | /* check Minor number */ |
| 347 | if (min > 999) |
| 348 | goto out; |
| 349 | |
| 350 | /* skip trailing spaces */ |
| 351 | while (p < end && isspace(*p)) |
| 352 | p++; |
| 353 | if (p != end) |
| 354 | goto out; |
| 355 | |
| 356 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 357 | out: |
| 358 | return vsn; |
| 359 | } |
| 360 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 361 | /* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of |
| 362 | * encoded bytes in the frame on success, 0 if an encoding error occured and -1 |
| 363 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 364 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 365 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 366 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 367 | struct chunk *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 368 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 369 | char *p, *end; |
| 370 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 371 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 372 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 373 | p = frame; |
| 374 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 376 | /* Set Frame type */ |
| 377 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 378 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 379 | /* Set flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 380 | memcpy(p, (char *)&flags, 4); |
| 381 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 382 | |
| 383 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 384 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 385 | |
| 386 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 387 | * and "capabilities" */ |
| 388 | |
| 389 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 390 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 391 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 392 | goto too_big; |
| 393 | |
| 394 | *p++ = SPOE_DATA_T_STR; |
| 395 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 396 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 397 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 398 | |
| 399 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 400 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 401 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 402 | goto too_big; |
| 403 | |
| 404 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 405 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 406 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 407 | |
| 408 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 409 | sz = SLEN(CAPABILITIES_KEY); |
| 410 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 411 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 412 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 413 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 414 | chk = get_trash_chunk(); |
| 415 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
| 416 | memcpy(chk->str, "pipelining", 10); |
| 417 | chk->len += 10; |
| 418 | } |
| 419 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
| 420 | if (chk->len) chk->str[chk->len++] = ','; |
| 421 | memcpy(chk->str+chk->len, "async", 5); |
| 422 | chk->len += 5; |
| 423 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 424 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
| 425 | if (chk->len) chk->str[chk->len++] = ','; |
| 426 | memcpy(chk->str+chk->len, "fragmentation", 13); |
| 427 | chk->len += 5; |
| 428 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 429 | if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 430 | goto too_big; |
| 431 | |
| 432 | /* (optionnal) "engine-id" K/V item, if present */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 433 | if (agent != NULL && agent->engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 434 | sz = SLEN(ENGINE_ID_KEY); |
| 435 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 436 | goto too_big; |
| 437 | |
| 438 | *p++ = SPOE_DATA_T_STR; |
| 439 | sz = strlen(agent->engine_id); |
| 440 | if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1) |
| 441 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 444 | return (p - frame); |
| 445 | |
| 446 | too_big: |
| 447 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 448 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 451 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 452 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 453 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 454 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 455 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 456 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 457 | const char *reason; |
| 458 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 459 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 460 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 461 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 462 | p = frame; |
| 463 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 464 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 465 | /* Set Frame type */ |
| 466 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 467 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 468 | /* Set flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 469 | memcpy(p, (char *)&flags, 4); |
| 470 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 471 | |
| 472 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 473 | *p++ = 0; *p++ = 0; |
| 474 | |
| 475 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 476 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 477 | |
| 478 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 479 | |
| 480 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 481 | sz = SLEN(STATUS_CODE_KEY); |
| 482 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 483 | goto too_big; |
| 484 | |
| 485 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 486 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 487 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 488 | |
| 489 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 490 | sz = SLEN(MSG_KEY); |
| 491 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 492 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 493 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 494 | /*Get the message corresponding to the status code */ |
| 495 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 496 | |
| 497 | *p++ = SPOE_DATA_T_STR; |
| 498 | sz = strlen(reason); |
| 499 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 500 | goto too_big; |
| 501 | |
| 502 | return (p - frame); |
| 503 | |
| 504 | too_big: |
| 505 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 506 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 509 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 510 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 511 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 512 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 513 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 514 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 515 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 516 | char *p, *end; |
| 517 | unsigned int stream_id, frame_id; |
| 518 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 519 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 520 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 521 | p = frame; |
| 522 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 523 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 524 | stream_id = ctx->stream_id; |
| 525 | frame_id = ctx->frame_id; |
| 526 | |
| 527 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 528 | /* The fragmentation is not supported by the applet */ |
| 529 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 530 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 531 | return -1; |
| 532 | } |
| 533 | flags = ctx->frag_ctx.flags; |
| 534 | } |
| 535 | |
| 536 | /* Set Frame type */ |
| 537 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 538 | |
| 539 | /* Set flags */ |
| 540 | memcpy(p, (char *)&flags, 4); |
| 541 | p += 4; |
| 542 | |
| 543 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 544 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 545 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 546 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 547 | goto too_big; |
| 548 | |
| 549 | /* Copy encoded messages, if possible */ |
| 550 | sz = ctx->buffer->i; |
| 551 | if (p + sz >= end) |
| 552 | goto too_big; |
| 553 | memcpy(p, ctx->buffer->p, sz); |
| 554 | p += sz; |
| 555 | |
| 556 | return (p - frame); |
| 557 | |
| 558 | too_big: |
| 559 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 564 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 565 | * error occurred and -1 if a fatal error occurred. */ |
| 566 | static int |
| 567 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 568 | char *frame, size_t size) |
| 569 | { |
| 570 | char *p, *end; |
| 571 | unsigned int stream_id, frame_id; |
| 572 | unsigned int flags; |
| 573 | size_t sz; |
| 574 | |
| 575 | p = frame; |
| 576 | end = frame+size; |
| 577 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 578 | /* <ctx> is null when the stream has aborted the processing of a |
| 579 | * fragmented frame. In this case, we must notify the corresponding |
| 580 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 581 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 582 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 583 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 584 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 585 | } |
| 586 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 587 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 588 | stream_id = ctx->stream_id; |
| 589 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 592 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 593 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 594 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 595 | /* Set flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 596 | memcpy(p, (char *)&flags, 4); |
| 597 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 598 | |
| 599 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 600 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 601 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 602 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 603 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 604 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 605 | if (ctx == NULL) |
| 606 | goto end; |
| 607 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 608 | /* Copy encoded messages, if possible */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 609 | sz = ctx->buffer->i; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 610 | if (p + sz >= end) |
| 611 | goto too_big; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 612 | memcpy(p, ctx->buffer->p, sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 613 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 614 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 615 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 616 | return (p - frame); |
| 617 | |
| 618 | too_big: |
| 619 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 620 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 623 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 624 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 625 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 626 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 627 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 628 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 629 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 630 | char *p, *end; |
| 631 | int vsn, max_frame_size; |
| 632 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 633 | |
| 634 | p = frame; |
| 635 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 636 | |
| 637 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 638 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 639 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 640 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 641 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 642 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 643 | if (size < 7 /* TYPE + METADATA */) { |
| 644 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 645 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 648 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 649 | memcpy((char *)&flags, p, 4); |
| 650 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 651 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 652 | /* Fragmentation is not supported for HELLO frame */ |
| 653 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 654 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 655 | return -1; |
| 656 | } |
| 657 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 658 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 659 | if (*p != 0 || *(p+1) != 0) { |
| 660 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 661 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 662 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 663 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 664 | |
| 665 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 666 | * "capabilities" */ |
| 667 | |
| 668 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 669 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 670 | while (p < end) { |
| 671 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 672 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 673 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 674 | |
| 675 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 676 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 677 | if (ret == -1 || !sz) { |
| 678 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 679 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 680 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 681 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 682 | /* Check "version" K/V item */ |
| 683 | if (!memcmp(str, VERSION_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 684 | int i, type = *p++; |
| 685 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 686 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 687 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 688 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 689 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 690 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 691 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 692 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 693 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 696 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 697 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 698 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 699 | return -1; |
| 700 | } |
| 701 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 702 | if (vsn >= supported_versions[i].min && |
| 703 | vsn <= supported_versions[i].max) |
| 704 | break; |
| 705 | } |
| 706 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 707 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 708 | return -1; |
| 709 | } |
| 710 | } |
| 711 | /* Check "max-frame-size" K/V item */ |
| 712 | else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 713 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 714 | |
| 715 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 716 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 717 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 718 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 719 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 720 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 721 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 722 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 723 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 724 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 725 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 726 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 727 | if (sz < MIN_FRAME_SIZE || |
| 728 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 729 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 730 | return -1; |
| 731 | } |
| 732 | max_frame_size = sz; |
| 733 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 734 | /* Check "capabilities" K/V item */ |
| 735 | else if (!memcmp(str, CAPABILITIES_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 736 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 737 | |
| 738 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 739 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 740 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 741 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 742 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 743 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 744 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 745 | return 0; |
| 746 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 747 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 748 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 749 | char *delim; |
| 750 | |
| 751 | /* Skip leading spaces */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 752 | for (; isspace(*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 753 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 754 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 755 | str += 10; sz -= 10; |
| 756 | if (!sz || isspace(*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 757 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 758 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 759 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 760 | str += 5; sz -= 5; |
| 761 | if (!sz || isspace(*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 762 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 763 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 764 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 765 | str += 13; sz -= 13; |
| 766 | if (!sz || isspace(*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 767 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 768 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 769 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 770 | /* Get the next comma or break */ |
| 771 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 772 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 773 | delim++; |
| 774 | sz -= (delim - str); |
| 775 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 776 | } |
| 777 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 778 | else { |
| 779 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 780 | if (spoe_skip_data(&p, end) == -1) { |
| 781 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 782 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 783 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | /* Final checks */ |
| 788 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 789 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 790 | return -1; |
| 791 | } |
| 792 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 793 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 794 | return -1; |
| 795 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 796 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 797 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 798 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 799 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 800 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 801 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 802 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 803 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 804 | |
| 805 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 809 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 810 | * occurred. */ |
| 811 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 812 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 813 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 814 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 815 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 816 | |
| 817 | p = frame; |
| 818 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 819 | |
| 820 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 821 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 822 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 823 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 824 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 825 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 826 | if (size < 7 /* TYPE + METADATA */) { |
| 827 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 828 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 831 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 832 | memcpy((char *)&flags, p, 4); |
| 833 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 834 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 835 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 836 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 837 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 838 | return -1; |
| 839 | } |
| 840 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 841 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 842 | if (*p != 0 || *(p+1) != 0) { |
| 843 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 844 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 845 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 846 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 847 | |
| 848 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 849 | |
| 850 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 851 | while (p < end) { |
| 852 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 853 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 854 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 855 | |
| 856 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 857 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 858 | if (ret == -1 || !sz) { |
| 859 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 860 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* Check "status-code" K/V item */ |
| 864 | if (!memcmp(str, STATUS_CODE_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 865 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 866 | |
| 867 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 868 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 869 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 870 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 871 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 872 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 873 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 874 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 875 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 876 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 877 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 878 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 879 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* Check "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 883 | else if (!memcmp(str, MSG_KEY, sz)) { |
| 884 | int type = *p++; |
| 885 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 886 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 887 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 888 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 889 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 890 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 891 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 892 | if (ret == -1 || sz > 255) { |
| 893 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 894 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 895 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 896 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 897 | SPOE_APPCTX(appctx)->reason = str; |
| 898 | SPOE_APPCTX(appctx)->rlen = sz; |
| 899 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 900 | } |
| 901 | else { |
| 902 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 903 | if (spoe_skip_data(&p, end) == -1) { |
| 904 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 905 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 906 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 910 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 914 | /* Decode ACK frame sent by an agent. It returns the number of read bytes on |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 915 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 916 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 917 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 918 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 919 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 920 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 921 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 922 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 923 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 924 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 925 | |
| 926 | p = frame; |
| 927 | end = frame + size; |
| 928 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 929 | |
| 930 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 931 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 932 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 933 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 934 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 935 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 936 | if (size < 7 /* TYPE + METADATA */) { |
| 937 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 938 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 939 | } |
| 940 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 941 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 942 | memcpy((char *)&flags, p, 4); |
| 943 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 944 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 945 | /* Fragmentation is not supported for now */ |
| 946 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 947 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 948 | return -1; |
| 949 | } |
| 950 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 951 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 952 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 953 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 954 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 955 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 956 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 957 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 958 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 959 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 960 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 961 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 962 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 963 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 964 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 965 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 966 | goto found; |
| 967 | } |
| 968 | } |
| 969 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 970 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 971 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 972 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 973 | goto found; |
| 974 | } |
| 975 | } |
| 976 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 977 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 978 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 979 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 980 | |
| 981 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 982 | if (flags & SPOE_FRM_FL_ABRT) { |
| 983 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 984 | (*ctx)->frag_ctx.spoe_appctx = NULL; |
| 985 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 986 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 987 | /* Ignore the payload */ |
| 988 | goto end; |
| 989 | } |
| 990 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 991 | /* For now, we ignore the ack */ |
| 992 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 993 | return 0; |
| 994 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 995 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 996 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 997 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 998 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 999 | " - stream-id=%u - frame-id=%u\n", |
| 1000 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1001 | __FUNCTION__, appctx, |
| 1002 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1003 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1004 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1005 | return 0; |
| 1006 | |
| 1007 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1008 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1009 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1010 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1011 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1012 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1013 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1014 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1015 | len = (end - p); |
| 1016 | memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len); |
| 1017 | SPOE_APPCTX(appctx)->buffer->i = len; |
| 1018 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1019 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1020 | /* Transfer the buffer ownership to the SPOE context */ |
| 1021 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
| 1022 | SPOE_APPCTX(appctx)->buffer = &buf_empty; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1023 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1024 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1025 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1026 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1027 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1028 | " - ACK frame received" |
| 1029 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1030 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1031 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1032 | (*ctx)->frame_id, flags); |
| 1033 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1036 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1037 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1038 | * success and -1 if an error occurred. */ |
| 1039 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1040 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1041 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1042 | struct appctx appctx; |
| 1043 | struct spoe_appctx spoe_appctx; |
| 1044 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1045 | size_t sz; |
| 1046 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1047 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1048 | memset(&appctx, 0, sizeof(appctx)); |
| 1049 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1050 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1051 | |
| 1052 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1053 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1054 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1055 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1056 | end = frame + MAX_FRAME_SIZE; |
| 1057 | |
| 1058 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1059 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1060 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1061 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1062 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1063 | /* Add "healthcheck" K/V item */ |
| 1064 | sz = SLEN(HEALTHCHECK_KEY); |
| 1065 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1066 | return -1; |
| 1067 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1068 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1069 | *len = frame - buf; |
| 1070 | sz = htonl(*len - 4); |
| 1071 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1072 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1073 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1074 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1075 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1080 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1081 | * success and -1 if an error occurred. */ |
| 1082 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1083 | spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1084 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1085 | struct appctx appctx; |
| 1086 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1087 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1088 | memset(&appctx, 0, sizeof(appctx)); |
| 1089 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1090 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1091 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1092 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1093 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1094 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1095 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1096 | goto error; |
| 1097 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1098 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1099 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1100 | |
| 1101 | return 0; |
| 1102 | |
| 1103 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1104 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1105 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1106 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1107 | return -1; |
| 1108 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1109 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1110 | /* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when |
| 1111 | * the frame can be ignored, 1 to retry later, and the frame legnth on |
| 1112 | * success. */ |
| 1113 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1114 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1115 | { |
| 1116 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1117 | int ret; |
| 1118 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1119 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1120 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1121 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1122 | netint = htonl(framesz); |
| 1123 | memcpy(buf, (char *)&netint, 4); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1124 | ret = ci_putblk(si_ic(si), buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1125 | if (ret <= 0) { |
Christopher Faulet | d5216d4 | 2018-02-01 08:45:22 +0100 | [diff] [blame] | 1126 | if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1127 | retry: |
| 1128 | si_applet_cant_put(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1129 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1130 | } |
| 1131 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1132 | return -1; /* error */ |
| 1133 | } |
| 1134 | return framesz; |
| 1135 | } |
| 1136 | |
| 1137 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1138 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1139 | * success. */ |
| 1140 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1141 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1142 | { |
| 1143 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1144 | int ret; |
| 1145 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1146 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1147 | ret = co_getblk(si_oc(si), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1148 | if (ret > 0) { |
| 1149 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1150 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1151 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1152 | return -1; |
| 1153 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1154 | ret = co_getblk(si_oc(si), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1155 | } |
| 1156 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1157 | if (ret == 0) { |
| 1158 | retry: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1159 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1160 | } |
| 1161 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1162 | return -1; /* error */ |
| 1163 | } |
| 1164 | return framesz; |
| 1165 | } |
| 1166 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1167 | /******************************************************************** |
| 1168 | * Functions that manage the SPOE applet |
| 1169 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1170 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1171 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1172 | { |
| 1173 | si_applet_want_get(appctx->owner); |
| 1174 | si_applet_want_put(appctx->owner); |
| 1175 | appctx_wakeup(appctx); |
| 1176 | return 1; |
| 1177 | } |
| 1178 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1179 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1180 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1181 | static struct task * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1182 | spoe_process_appctx(struct task * task) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1183 | { |
| 1184 | struct appctx *appctx = task->context; |
| 1185 | |
| 1186 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1187 | if (tick_is_expired(task->expire, now_ms)) { |
| 1188 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1189 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1190 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1191 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1192 | return task; |
| 1193 | } |
| 1194 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1195 | /* Callback function that releases a SPOE applet. This happens when the |
| 1196 | * connection with the agent is closed. */ |
| 1197 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1198 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1199 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1200 | struct stream_interface *si = appctx->owner; |
| 1201 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1202 | struct spoe_agent *agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1203 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1204 | |
| 1205 | if (spoe_appctx == NULL) |
| 1206 | return; |
| 1207 | |
| 1208 | appctx->ctx.spoe.ptr = NULL; |
| 1209 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1210 | |
| 1211 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1212 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1213 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1214 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1215 | /* Remove applet from the list of running applets */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1216 | agent->rt[tid].applets_act--; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1217 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
| 1218 | LIST_DEL(&spoe_appctx->list); |
| 1219 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1220 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1221 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1222 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1223 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1224 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1225 | agent->rt[tid].applets_idle--; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1226 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1227 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1228 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1229 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1230 | |
| 1231 | si_shutw(si); |
| 1232 | si_shutr(si); |
| 1233 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1236 | /* Destroy the task attached to this applet */ |
| 1237 | if (spoe_appctx->task) { |
| 1238 | task_delete(spoe_appctx->task); |
| 1239 | task_free(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1242 | /* Notify all waiting streams */ |
| 1243 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1244 | LIST_DEL(&ctx->list); |
| 1245 | LIST_INIT(&ctx->list); |
| 1246 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1247 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1248 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1251 | /* If the applet was processing a fragmented frame, notify the |
| 1252 | * corresponding stream. */ |
| 1253 | if (spoe_appctx->frag_ctx.ctx) { |
| 1254 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1255 | ctx->frag_ctx.spoe_appctx = NULL; |
| 1256 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1257 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1258 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1259 | } |
| 1260 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1261 | /* Release allocated memory */ |
| 1262 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1263 | &spoe_appctx->buffer_wait); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1264 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1265 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1266 | if (!LIST_ISEMPTY(&agent->rt[tid].applets)) |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1267 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1268 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1269 | /* If this was the last running applet, notify all waiting streams */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1270 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1271 | LIST_DEL(&ctx->list); |
| 1272 | LIST_INIT(&ctx->list); |
| 1273 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1274 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1275 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1276 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1277 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1278 | LIST_DEL(&ctx->list); |
| 1279 | LIST_INIT(&ctx->list); |
| 1280 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1281 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1282 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1283 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1284 | |
| 1285 | end: |
| 1286 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1287 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1288 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1289 | HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1292 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1293 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1294 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1295 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1296 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1297 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1298 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1299 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1300 | if (si->state <= SI_ST_CON) { |
| 1301 | si_applet_want_put(si); |
| 1302 | task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG); |
| 1303 | goto stop; |
| 1304 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1305 | if (si->state != SI_ST_EST) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1306 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1307 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1308 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1309 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1310 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1311 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1312 | " - Connection timed out\n", |
| 1313 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1314 | __FUNCTION__, appctx); |
| 1315 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1316 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1317 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1318 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1319 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1320 | SPOE_APPCTX(appctx)->task->expire = |
| 1321 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1322 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1323 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1324 | * length. */ |
| 1325 | buf = trash.str; frame = buf+4; |
| 1326 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1327 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1328 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1329 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1330 | |
| 1331 | switch (ret) { |
| 1332 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1333 | case 0: /* ignore => an error, cannot be ignored */ |
| 1334 | goto exit; |
| 1335 | |
| 1336 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1337 | goto stop; |
| 1338 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1339 | default: |
| 1340 | /* HELLO frame successfully sent, now wait for the |
| 1341 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1342 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1343 | goto next; |
| 1344 | } |
| 1345 | |
| 1346 | next: |
| 1347 | return 0; |
| 1348 | stop: |
| 1349 | return 1; |
| 1350 | exit: |
| 1351 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1352 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1355 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1356 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1357 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1358 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1359 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1360 | char *frame; |
| 1361 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1362 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1363 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1364 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1365 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1366 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1367 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1368 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1369 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1370 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1371 | " - Connection timed out\n", |
| 1372 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1373 | __FUNCTION__, appctx); |
| 1374 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1375 | goto exit; |
| 1376 | } |
| 1377 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1378 | frame = trash.str; trash.len = 0; |
| 1379 | ret = spoe_recv_frame(appctx, frame, |
| 1380 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1381 | if (ret > 1) { |
| 1382 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1383 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1384 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1385 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1386 | trash.len = ret + 4; |
| 1387 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1388 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1389 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1390 | switch (ret) { |
| 1391 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1392 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1393 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1394 | goto next; |
| 1395 | |
| 1396 | case 1: /* retry later */ |
| 1397 | goto stop; |
| 1398 | |
| 1399 | default: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1400 | /* HELLO handshake is finished, set the idle timeout and |
| 1401 | * add the applet in the list of running applets. */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1402 | agent->rt[tid].applets_idle++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1403 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1404 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1405 | LIST_DEL(&SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1406 | LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1407 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1408 | |
| 1409 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1410 | HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1411 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1412 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1413 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1414 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1415 | /* Do not forget to remove processed frame from the output buffer */ |
| 1416 | if (trash.len) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1417 | co_skip(si_oc(si), trash.len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1418 | |
| 1419 | SPOE_APPCTX(appctx)->task->expire = |
| 1420 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1421 | return 0; |
| 1422 | stop: |
| 1423 | return 1; |
| 1424 | exit: |
| 1425 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1426 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1429 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1430 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1431 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1432 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1433 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1434 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1435 | char *frame, *buf; |
| 1436 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1437 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1438 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1439 | * length. */ |
| 1440 | buf = trash.str; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1441 | |
| 1442 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1443 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1444 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1445 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1446 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1447 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1448 | *skip = 1; |
| 1449 | ret = 1; |
| 1450 | goto end; |
| 1451 | } |
| 1452 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1453 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1454 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1455 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1456 | |
| 1457 | } |
| 1458 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1459 | if (ret > 1) |
| 1460 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1461 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1462 | switch (ret) { |
| 1463 | case -1: /* error */ |
| 1464 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1465 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1466 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1467 | case 0: /* ignore */ |
| 1468 | if (ctx == NULL) |
| 1469 | goto abort_frag_frame; |
| 1470 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1471 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1472 | LIST_DEL(&ctx->list); |
| 1473 | LIST_INIT(&ctx->list); |
| 1474 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1475 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1476 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1477 | break; |
| 1478 | |
| 1479 | case 1: /* retry */ |
| 1480 | *skip = 1; |
| 1481 | break; |
| 1482 | |
| 1483 | default: |
| 1484 | if (ctx == NULL) |
| 1485 | goto abort_frag_frame; |
| 1486 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1487 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1488 | LIST_DEL(&ctx->list); |
| 1489 | LIST_INIT(&ctx->list); |
| 1490 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1491 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1492 | goto no_frag_frame_sent; |
| 1493 | else { |
| 1494 | *skip = 1; |
| 1495 | goto frag_frame_sent; |
| 1496 | } |
| 1497 | } |
| 1498 | goto end; |
| 1499 | |
| 1500 | frag_frame_sent: |
| 1501 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
| 1502 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1503 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1504 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
| 1505 | |
| 1506 | ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx); |
| 1507 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1508 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1509 | goto end; |
| 1510 | |
| 1511 | no_frag_frame_sent: |
| 1512 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1513 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1514 | LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1515 | } |
| 1516 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1517 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1518 | LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
| 1519 | } |
| 1520 | else { |
| 1521 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
| 1522 | LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
| 1523 | } |
| 1524 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1525 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1526 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1527 | |
| 1528 | ctx->frag_ctx.spoe_appctx = NULL; |
| 1529 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1530 | goto end; |
| 1531 | |
| 1532 | abort_frag_frame: |
| 1533 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1534 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1535 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1536 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1537 | goto end; |
| 1538 | |
| 1539 | end: |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | static int |
| 1544 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1545 | { |
| 1546 | struct spoe_context *ctx = NULL; |
| 1547 | char *frame; |
| 1548 | int ret; |
| 1549 | |
| 1550 | frame = trash.str; trash.len = 0; |
| 1551 | ret = spoe_recv_frame(appctx, frame, |
| 1552 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1553 | if (ret > 1) { |
| 1554 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1555 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1556 | goto end; |
| 1557 | } |
| 1558 | trash.len = ret + 4; |
| 1559 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1560 | } |
| 1561 | switch (ret) { |
| 1562 | case -1: /* error */ |
| 1563 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1564 | break; |
| 1565 | |
| 1566 | case 0: /* ignore */ |
| 1567 | break; |
| 1568 | |
| 1569 | case 1: /* retry */ |
| 1570 | *skip = 1; |
| 1571 | break; |
| 1572 | |
| 1573 | default: |
| 1574 | LIST_DEL(&ctx->list); |
| 1575 | LIST_INIT(&ctx->list); |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1576 | |
| 1577 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1578 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1579 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1580 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1581 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1582 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1583 | } |
| 1584 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1585 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1586 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1587 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | /* Do not forget to remove processed frame from the output buffer */ |
| 1592 | if (trash.len) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1593 | co_skip(si_oc(appctx->owner), trash.len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1594 | end: |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | static int |
| 1599 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1600 | { |
| 1601 | struct stream_interface *si = appctx->owner; |
| 1602 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1603 | unsigned int fpa = 0; |
| 1604 | int ret, skip_sending = 0, skip_receiving = 0; |
| 1605 | |
| 1606 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1607 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1608 | goto exit; |
| 1609 | } |
| 1610 | |
| 1611 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1612 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1613 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1614 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1615 | goto next; |
| 1616 | } |
| 1617 | |
| 1618 | process: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1619 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1620 | " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d" |
| 1621 | " - appctx-state=%s\n", |
| 1622 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1623 | __FUNCTION__, appctx, fpa, agent->max_fpa, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1624 | skip_sending, skip_receiving, |
| 1625 | spoe_appctx_state_str[appctx->st0]); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1626 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1627 | if (fpa > agent->max_fpa) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1628 | goto stop; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1629 | else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1630 | if (skip_receiving) |
| 1631 | goto stop; |
| 1632 | goto recv_frame; |
| 1633 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1634 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1635 | /* send_frame */ |
| 1636 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1637 | switch (ret) { |
| 1638 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1639 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1640 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1641 | case 0: /* ignore */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1642 | agent->rt[tid].sending_rate++; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1643 | fpa++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1644 | break; |
| 1645 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1646 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1647 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1648 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1649 | default: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1650 | agent->rt[tid].sending_rate++; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1651 | fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1652 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1653 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1654 | if (fpa > agent->max_fpa) |
| 1655 | goto stop; |
| 1656 | |
| 1657 | recv_frame: |
| 1658 | if (skip_receiving) |
| 1659 | goto process; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1660 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1661 | switch (ret) { |
| 1662 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1663 | goto next; |
| 1664 | |
| 1665 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1666 | fpa++; |
| 1667 | break; |
| 1668 | |
| 1669 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1670 | break; |
| 1671 | |
| 1672 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1673 | fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1674 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1675 | } |
| 1676 | goto process; |
| 1677 | |
| 1678 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1679 | SPOE_APPCTX(appctx)->task->expire = |
| 1680 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1681 | return 0; |
| 1682 | stop: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1683 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1684 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1685 | agent->rt[tid].applets_idle++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1686 | } |
Christopher Faulet | 4209779 | 2018-01-24 15:49:45 +0100 | [diff] [blame^] | 1687 | if (fpa) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1688 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1689 | LIST_DEL(&SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1690 | LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1691 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1692 | if (fpa) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1693 | SPOE_APPCTX(appctx)->task->expire = |
| 1694 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1695 | } |
| 1696 | return 1; |
| 1697 | |
| 1698 | exit: |
| 1699 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1704 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1705 | { |
| 1706 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1707 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1708 | char *frame, *buf; |
| 1709 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1710 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1711 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1712 | goto exit; |
| 1713 | |
| 1714 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1715 | goto exit; |
| 1716 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1717 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1718 | * length. */ |
| 1719 | buf = trash.str; frame = buf+4; |
| 1720 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1721 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1722 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1723 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1724 | |
| 1725 | switch (ret) { |
| 1726 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1727 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1728 | goto exit; |
| 1729 | |
| 1730 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1731 | goto stop; |
| 1732 | |
| 1733 | default: |
| 1734 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1735 | " - disconnected by HAProxy (%d): %s\n", |
| 1736 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1737 | __FUNCTION__, appctx, |
| 1738 | SPOE_APPCTX(appctx)->status_code, |
| 1739 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1740 | |
| 1741 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1742 | goto next; |
| 1743 | } |
| 1744 | |
| 1745 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1746 | SPOE_APPCTX(appctx)->task->expire = |
| 1747 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1748 | return 0; |
| 1749 | stop: |
| 1750 | return 1; |
| 1751 | exit: |
| 1752 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1757 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1758 | { |
| 1759 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1760 | char *frame; |
| 1761 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1762 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1763 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1764 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1765 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1766 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1767 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1768 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1769 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1770 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1771 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1772 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1773 | frame = trash.str; trash.len = 0; |
| 1774 | ret = spoe_recv_frame(appctx, frame, |
| 1775 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1776 | if (ret > 1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1777 | trash.len = ret + 4; |
| 1778 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | switch (ret) { |
| 1782 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1783 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1784 | " - error on frame (%s)\n", |
| 1785 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1786 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1787 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1788 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1789 | goto exit; |
| 1790 | |
| 1791 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1792 | goto next; |
| 1793 | |
| 1794 | case 1: /* retry */ |
| 1795 | goto stop; |
| 1796 | |
| 1797 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1798 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1799 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1800 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1801 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1802 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1803 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1804 | goto exit; |
| 1805 | } |
| 1806 | |
| 1807 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1808 | /* Do not forget to remove processed frame from the output buffer */ |
| 1809 | if (trash.len) |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1810 | co_skip(si_oc(appctx->owner), trash.len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1811 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1812 | return 0; |
| 1813 | stop: |
| 1814 | return 1; |
| 1815 | exit: |
| 1816 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
| 1820 | /* I/O Handler processing messages exchanged with the agent */ |
| 1821 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1822 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1823 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1824 | struct stream_interface *si = appctx->owner; |
| 1825 | struct spoe_agent *agent; |
| 1826 | |
| 1827 | if (SPOE_APPCTX(appctx) == NULL) |
| 1828 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1829 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1830 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1831 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1832 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1833 | switchstate: |
| 1834 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1835 | " - appctx-state=%s\n", |
| 1836 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1837 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1838 | |
| 1839 | switch (appctx->st0) { |
| 1840 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1841 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1842 | goto out; |
| 1843 | goto switchstate; |
| 1844 | |
| 1845 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1846 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1847 | goto out; |
| 1848 | goto switchstate; |
| 1849 | |
| 1850 | case SPOE_APPCTX_ST_IDLE: |
| 1851 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1852 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1853 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1854 | SPOE_APPCTX(appctx)->task->expire = |
| 1855 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1856 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1857 | goto switchstate; |
| 1858 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1859 | agent->rt[tid].applets_idle--; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1860 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1861 | /* fall through */ |
| 1862 | |
| 1863 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1864 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1865 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1866 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1867 | goto out; |
| 1868 | goto switchstate; |
| 1869 | |
| 1870 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1871 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1872 | goto out; |
| 1873 | goto switchstate; |
| 1874 | |
| 1875 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1876 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1877 | goto out; |
| 1878 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1879 | |
| 1880 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1881 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1882 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1883 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1884 | si_shutw(si); |
| 1885 | si_shutr(si); |
| 1886 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1887 | /* fall through */ |
| 1888 | |
| 1889 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1890 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1891 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1892 | out: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1893 | if (stopping) |
| 1894 | spoe_wakeup_appctx(appctx); |
| 1895 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1896 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 1897 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1898 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
| 1899 | task_wakeup(si_strm(si)->task, TASK_WOKEN_IO); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | struct applet spoe_applet = { |
| 1903 | .obj_type = OBJ_TYPE_APPLET, |
| 1904 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1905 | .fct = spoe_handle_appctx, |
| 1906 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1907 | }; |
| 1908 | |
| 1909 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 1910 | * NULL. */ |
| 1911 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1912 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1913 | { |
| 1914 | struct appctx *appctx; |
| 1915 | struct session *sess; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1916 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1917 | |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 1918 | if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1919 | goto out_error; |
| 1920 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1921 | appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1922 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1923 | goto out_free_appctx; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1924 | memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1925 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1926 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 1927 | if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1928 | goto out_free_spoe_appctx; |
| 1929 | |
| 1930 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1931 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1932 | SPOE_APPCTX(appctx)->task->context = appctx; |
| 1933 | SPOE_APPCTX(appctx)->agent = conf->agent; |
| 1934 | SPOE_APPCTX(appctx)->version = 0; |
| 1935 | SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size; |
| 1936 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1937 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1938 | SPOE_APPCTX(appctx)->buffer = &buf_empty; |
| 1939 | |
| 1940 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
| 1941 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1942 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1943 | |
| 1944 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 1945 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1946 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 1947 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1948 | if (!sess) |
| 1949 | goto out_free_spoe; |
| 1950 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 1951 | if ((strm = stream_new(sess, &appctx->obj_type)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1952 | goto out_free_sess; |
| 1953 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1954 | stream_set_backend(strm, conf->agent->b.be); |
| 1955 | |
| 1956 | /* applet is waiting for data */ |
| 1957 | si_applet_cant_get(&strm->si[0]); |
| 1958 | appctx_wakeup(appctx); |
| 1959 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1960 | strm->do_log = NULL; |
| 1961 | strm->res.flags |= CF_READ_DONTWAIT; |
| 1962 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1963 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1964 | LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1965 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1966 | conf->agent->rt[tid].applets_act++; |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 1967 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 1968 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 1969 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1970 | return appctx; |
| 1971 | |
| 1972 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1973 | out_free_sess: |
| 1974 | session_free(sess); |
| 1975 | out_free_spoe: |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1976 | task_free(SPOE_APPCTX(appctx)->task); |
| 1977 | out_free_spoe_appctx: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1978 | pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1979 | out_free_appctx: |
| 1980 | appctx_free(appctx); |
| 1981 | out_error: |
| 1982 | return NULL; |
| 1983 | } |
| 1984 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1985 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1986 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1987 | { |
| 1988 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 1989 | struct spoe_agent *agent = conf->agent; |
| 1990 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1991 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1992 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1993 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | 4209779 | 2018-01-24 15:49:45 +0100 | [diff] [blame^] | 1994 | if (agent->rt[tid].applets_idle && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1995 | agent->rt[tid].sending_rate) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1996 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1997 | |
| 1998 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1999 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2000 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2001 | ctx->strm); |
| 2002 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2003 | /* Do not try to create a new applet if there is no server up for the |
| 2004 | * agent's backend. */ |
| 2005 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2006 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2007 | " - cannot create SPOE appctx: no server up\n", |
| 2008 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2009 | __FUNCTION__, ctx->strm); |
| 2010 | goto end; |
| 2011 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2012 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2013 | /* Do not try to create a new applet if we have reached the maximum of |
| 2014 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2015 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2016 | if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2017 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2018 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2019 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2020 | __FUNCTION__, ctx->strm); |
| 2021 | goto end; |
| 2022 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2025 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2026 | if (appctx == NULL) { |
| 2027 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2028 | " - failed to create SPOE appctx\n", |
| 2029 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2030 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2031 | send_log(ctx->strm->be, LOG_EMERG, |
| 2032 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2033 | agent->id); |
| 2034 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2035 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2036 | } |
| 2037 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2038 | /* Increase the per-process number of cumulated connections */ |
| 2039 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2040 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2041 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2042 | end: |
| 2043 | /* The only reason to return an error is when there is no applet */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2044 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2045 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2046 | return -1; |
| 2047 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2048 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2049 | /* Add the SPOE context in the sending queue and update all running |
| 2050 | * info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2051 | LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list); |
| 2052 | if (agent->rt[tid].sending_rate) |
| 2053 | agent->rt[tid].sending_rate--; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2054 | |
| 2055 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2056 | " - Add stream in sending queue" |
| 2057 | " - applets_act=%u - applets_idle=%u - sending_rate=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2058 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2059 | ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle, |
| 2060 | agent->rt[tid].sending_rate); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2061 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2062 | /* Finally try to wakeup the first IDLE applet found and move it at the |
| 2063 | * end of the list. */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2064 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2065 | appctx = spoe_appctx->owner; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2066 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2067 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2068 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2069 | LIST_DEL(&spoe_appctx->list); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2070 | LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2071 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2072 | break; |
| 2073 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2074 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2075 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2079 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2080 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2081 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2082 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2083 | * the message is too big, it returns -1.*/ |
| 2084 | static int |
| 2085 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2086 | struct spoe_message *msg, int dir, |
| 2087 | char **buf, char *end) |
| 2088 | { |
| 2089 | struct sample *smp; |
| 2090 | struct spoe_arg *arg; |
| 2091 | int ret; |
| 2092 | |
| 2093 | if (msg->cond) { |
| 2094 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2095 | ret = acl_pass(ret); |
| 2096 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2097 | ret = !ret; |
| 2098 | |
| 2099 | /* the rule does not match */ |
| 2100 | if (!ret) |
| 2101 | goto next; |
| 2102 | } |
| 2103 | |
| 2104 | /* Resume encoding of a SPOE argument */ |
| 2105 | if (ctx->frag_ctx.curarg != NULL) { |
| 2106 | arg = ctx->frag_ctx.curarg; |
| 2107 | goto encode_argument; |
| 2108 | } |
| 2109 | |
| 2110 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2111 | goto encode_msg_payload; |
| 2112 | |
| 2113 | /* Check if there is enough space for the message name and the |
| 2114 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2115 | * bytes, at most (< 2288). */ |
| 2116 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2117 | goto too_big; |
| 2118 | |
| 2119 | /* Encode the message name */ |
| 2120 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2121 | goto too_big; |
| 2122 | |
| 2123 | /* Set the number of arguments for this message */ |
| 2124 | **buf = msg->nargs; |
| 2125 | (*buf)++; |
| 2126 | |
| 2127 | ctx->frag_ctx.curoff = 0; |
| 2128 | encode_msg_payload: |
| 2129 | |
| 2130 | /* Loop on arguments */ |
| 2131 | list_for_each_entry(arg, &msg->args, list) { |
| 2132 | ctx->frag_ctx.curarg = arg; |
| 2133 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2134 | |
| 2135 | encode_argument: |
| 2136 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2137 | goto encode_arg_value; |
| 2138 | |
| 2139 | /* Encode the arguement name as a string. It can by NULL */ |
| 2140 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2141 | goto too_big; |
| 2142 | |
| 2143 | ctx->frag_ctx.curoff = 0; |
| 2144 | encode_arg_value: |
| 2145 | |
| 2146 | /* Fetch the arguement value */ |
| 2147 | smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL); |
| 2148 | ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end); |
| 2149 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2150 | goto too_big; |
| 2151 | } |
| 2152 | |
| 2153 | next: |
| 2154 | return 0; |
| 2155 | |
| 2156 | too_big: |
| 2157 | return -1; |
| 2158 | } |
| 2159 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2160 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2161 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2162 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2163 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2164 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2165 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2166 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2167 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2168 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2169 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2170 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2171 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2172 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2173 | p = ctx->buffer->p; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2174 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2175 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2176 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2177 | /* Resume encoding of a SPOE message */ |
| 2178 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2179 | msg = ctx->frag_ctx.curmsg; |
| 2180 | goto encode_evt_message; |
| 2181 | } |
| 2182 | |
| 2183 | list_for_each_entry(msg, messages, by_evt) { |
| 2184 | ctx->frag_ctx.curmsg = msg; |
| 2185 | ctx->frag_ctx.curarg = NULL; |
| 2186 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2187 | |
| 2188 | encode_evt_message: |
| 2189 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2190 | goto too_big; |
| 2191 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2192 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2193 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2194 | /* Resume encoding of a SPOE message */ |
| 2195 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2196 | msg = ctx->frag_ctx.curmsg; |
| 2197 | goto encode_grp_message; |
| 2198 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2199 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2200 | list_for_each_entry(msg, messages, by_grp) { |
| 2201 | ctx->frag_ctx.curmsg = msg; |
| 2202 | ctx->frag_ctx.curarg = NULL; |
| 2203 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2204 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2205 | encode_grp_message: |
| 2206 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2207 | goto too_big; |
| 2208 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2209 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2210 | else |
| 2211 | goto skip; |
| 2212 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2213 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2214 | /* nothing has been encoded for an unfragmented payload */ |
| 2215 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p) |
| 2216 | goto skip; |
| 2217 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2218 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2219 | " - encode %s messages - spoe_appctx=%p" |
| 2220 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2221 | (int)now.tv_sec, (int)now.tv_usec, |
| 2222 | agent->id, __FUNCTION__, s, |
| 2223 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2224 | ctx->frag_ctx.spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2225 | p - ctx->buffer->p); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2226 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2227 | ctx->buffer->i = p - ctx->buffer->p; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2228 | ctx->frag_ctx.curmsg = NULL; |
| 2229 | ctx->frag_ctx.curarg = NULL; |
| 2230 | ctx->frag_ctx.curoff = 0; |
| 2231 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2232 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2233 | return 1; |
| 2234 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2235 | too_big: |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2236 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) { |
| 2237 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2238 | return -1; |
| 2239 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2240 | |
| 2241 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2242 | " - encode fragmented messages - spoe_appctx=%p" |
| 2243 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2244 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2245 | (int)now.tv_sec, (int)now.tv_usec, |
| 2246 | agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx, |
| 2247 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2248 | (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2249 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2250 | ctx->buffer->i = p - ctx->buffer->p; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2251 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2252 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2253 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2254 | |
| 2255 | skip: |
| 2256 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2257 | " - skip the frame because nothing has been encoded\n", |
| 2258 | (int)now.tv_sec, (int)now.tv_usec, |
| 2259 | agent->id, __FUNCTION__, s); |
| 2260 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2263 | |
| 2264 | /*************************************************************************** |
| 2265 | * Functions that handle SPOE actions |
| 2266 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2267 | /* Helper function to set a variable */ |
| 2268 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2269 | spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2270 | struct sample *smp) |
| 2271 | { |
| 2272 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2273 | struct spoe_agent *agent = conf->agent; |
| 2274 | char varname[64]; |
| 2275 | |
| 2276 | memset(varname, 0, sizeof(varname)); |
| 2277 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2278 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2279 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2280 | vars_set_by_name(varname, len, smp); |
| 2281 | else |
| 2282 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /* Helper function to unset a variable */ |
| 2286 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2287 | spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2288 | struct sample *smp) |
| 2289 | { |
| 2290 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2291 | struct spoe_agent *agent = conf->agent; |
| 2292 | char varname[64]; |
| 2293 | |
| 2294 | memset(varname, 0, sizeof(varname)); |
| 2295 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2296 | scope, agent->var_pfx, len, name); |
| 2297 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2298 | } |
| 2299 | |
| 2300 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2301 | static inline int |
| 2302 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2303 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2304 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2305 | char *str, *scope, *p = *buf; |
| 2306 | struct sample smp; |
| 2307 | uint64_t sz; |
| 2308 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2309 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2310 | if (p + 2 >= end) |
| 2311 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2312 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2313 | /* SET-VAR requires 3 arguments */ |
| 2314 | if (*p++ != 3) |
| 2315 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2316 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2317 | switch (*p++) { |
| 2318 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2319 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2320 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2321 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2322 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2323 | default: goto skip; |
| 2324 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2325 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2326 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2327 | goto skip; |
| 2328 | memset(&smp, 0, sizeof(smp)); |
| 2329 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2330 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2331 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2332 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2333 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2334 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2335 | " - set-var '%s.%s.%.*s'\n", |
| 2336 | (int)now.tv_sec, (int)now.tv_usec, |
| 2337 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2338 | __FUNCTION__, s, scope, |
| 2339 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2340 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2341 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2342 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2343 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2344 | ret = (p - *buf); |
| 2345 | *buf = p; |
| 2346 | return ret; |
| 2347 | skip: |
| 2348 | return 0; |
| 2349 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2350 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2351 | static inline int |
| 2352 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2353 | char **buf, char *end, int dir) |
| 2354 | { |
| 2355 | char *str, *scope, *p = *buf; |
| 2356 | struct sample smp; |
| 2357 | uint64_t sz; |
| 2358 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2359 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2360 | if (p + 2 >= end) |
| 2361 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2362 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2363 | /* UNSET-VAR requires 2 arguments */ |
| 2364 | if (*p++ != 2) |
| 2365 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2366 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2367 | switch (*p++) { |
| 2368 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2369 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2370 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2371 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2372 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2373 | default: goto skip; |
| 2374 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2376 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2377 | goto skip; |
| 2378 | memset(&smp, 0, sizeof(smp)); |
| 2379 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2380 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2381 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2382 | " - unset-var '%s.%s.%.*s'\n", |
| 2383 | (int)now.tv_sec, (int)now.tv_usec, |
| 2384 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2385 | __FUNCTION__, s, scope, |
| 2386 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2387 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2388 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2389 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2390 | |
| 2391 | ret = (p - *buf); |
| 2392 | *buf = p; |
| 2393 | return ret; |
| 2394 | skip: |
| 2395 | return 0; |
| 2396 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2397 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2398 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2399 | * error occurred, 0 is returned. */ |
| 2400 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2401 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2402 | { |
| 2403 | char *p, *end; |
| 2404 | int ret; |
| 2405 | |
| 2406 | p = ctx->buffer->p; |
| 2407 | end = p + ctx->buffer->i; |
| 2408 | |
| 2409 | while (p < end) { |
| 2410 | enum spoe_action_type type; |
| 2411 | |
| 2412 | type = *p++; |
| 2413 | switch (type) { |
| 2414 | case SPOE_ACT_T_SET_VAR: |
| 2415 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2416 | if (!ret) |
| 2417 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2418 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2419 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2420 | case SPOE_ACT_T_UNSET_VAR: |
| 2421 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2422 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2423 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2424 | break; |
| 2425 | |
| 2426 | default: |
| 2427 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | return 1; |
| 2432 | skip: |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2436 | /*************************************************************************** |
| 2437 | * Functions that process SPOE events |
| 2438 | **************************************************************************/ |
| 2439 | static inline int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2440 | spoe_start_processing(struct spoe_context *ctx, int dir) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2441 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2442 | /* If a process is already started for this SPOE context, retry |
| 2443 | * later. */ |
| 2444 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2445 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2446 | |
| 2447 | /* Set the right flag to prevent request and response processing |
| 2448 | * in same time. */ |
| 2449 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2450 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2451 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2452 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2453 | } |
| 2454 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2455 | static inline void |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2456 | spoe_stop_processing(struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2457 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2458 | struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx; |
| 2459 | |
| 2460 | if (sa) { |
| 2461 | sa->frag_ctx.ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2462 | spoe_wakeup_appctx(sa->owner); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2465 | /* Reset the flag to allow next processing */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2466 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2467 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2468 | ctx->status_code = 0; |
| 2469 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2470 | /* Reset processing timer */ |
| 2471 | ctx->process_exp = TICK_ETERNITY; |
| 2472 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2473 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2474 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2475 | ctx->frag_ctx.spoe_appctx = NULL; |
| 2476 | ctx->frag_ctx.curmsg = NULL; |
| 2477 | ctx->frag_ctx.curarg = NULL; |
| 2478 | ctx->frag_ctx.curoff = 0; |
| 2479 | ctx->frag_ctx.flags = 0; |
| 2480 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2481 | if (!LIST_ISEMPTY(&ctx->list)) { |
| 2482 | LIST_DEL(&ctx->list); |
| 2483 | LIST_INIT(&ctx->list); |
| 2484 | } |
| 2485 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2486 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2487 | static void |
| 2488 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2489 | struct spoe_context *ctx, int dir) |
| 2490 | { |
| 2491 | if (agent->eps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2492 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2493 | |
| 2494 | if (agent->var_on_error) { |
| 2495 | struct sample smp; |
| 2496 | |
| 2497 | memset(&smp, 0, sizeof(smp)); |
| 2498 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2499 | smp.data.u.sint = ctx->status_code; |
| 2500 | smp.data.type = SMP_T_BOOL; |
| 2501 | |
| 2502 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2503 | strlen(agent->var_on_error), &smp); |
| 2504 | } |
| 2505 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2506 | " - failed to process messages: code=%u\n", |
| 2507 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2508 | __FUNCTION__, s, ctx->status_code); |
| 2509 | send_log(ctx->strm->be, LOG_WARNING, |
| 2510 | "SPOE: [%s] failed to process messages: code=%u\n", |
| 2511 | agent->id, ctx->status_code); |
| 2512 | |
| 2513 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2514 | ? SPOE_CTX_ST_READY |
| 2515 | : SPOE_CTX_ST_NONE); |
| 2516 | } |
| 2517 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2518 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2519 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2520 | * to process corresponding actions. During all the processing, it returns 0 |
| 2521 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2522 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2523 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2524 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2525 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2526 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2527 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2528 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2529 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2530 | |
| 2531 | if (ctx->state == SPOE_CTX_ST_ERROR) |
| 2532 | goto error; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2533 | |
| 2534 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2535 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2536 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2537 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2538 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2539 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2540 | goto error; |
| 2541 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2542 | |
| 2543 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2544 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2545 | if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2546 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2547 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2548 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2549 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2550 | goto skip; |
| 2551 | } |
| 2552 | } |
| 2553 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2554 | if (!tick_isset(ctx->process_exp)) { |
| 2555 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2556 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2557 | ctx->process_exp); |
| 2558 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2559 | ret = spoe_start_processing(ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2560 | if (!ret) |
| 2561 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2562 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2563 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2564 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2565 | } |
| 2566 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2567 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2568 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2569 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2570 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2571 | if (ret < 0) |
| 2572 | goto error; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2573 | if (!ret) |
| 2574 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2575 | if (spoe_queue_context(ctx) < 0) |
| 2576 | goto error; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2577 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2578 | } |
| 2579 | |
| 2580 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
| 2581 | if (ctx->frag_ctx.spoe_appctx) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2582 | spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2583 | ret = 0; |
| 2584 | goto out; |
| 2585 | } |
| 2586 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2587 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2588 | ret = 0; |
| 2589 | goto out; |
| 2590 | } |
| 2591 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2592 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2593 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2594 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2595 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2596 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2597 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | out: |
| 2601 | return ret; |
| 2602 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2603 | error: |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2604 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2605 | ret = 1; |
| 2606 | goto end; |
| 2607 | |
| 2608 | skip: |
| 2609 | ctx->state = SPOE_CTX_ST_READY; |
| 2610 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2611 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2612 | end: |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2613 | spoe_stop_processing(ctx); |
| 2614 | return ret; |
| 2615 | } |
| 2616 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2617 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2618 | * See spoe_process_message for details. */ |
| 2619 | static int |
| 2620 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2621 | struct spoe_group *group, int dir) |
| 2622 | { |
| 2623 | int ret; |
| 2624 | |
| 2625 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2626 | " - ctx-state=%s - Process messages for group=%s\n", |
| 2627 | (int)now.tv_sec, (int)now.tv_usec, |
| 2628 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2629 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2630 | group->id); |
| 2631 | |
| 2632 | if (LIST_ISEMPTY(&group->messages)) |
| 2633 | return 1; |
| 2634 | |
| 2635 | ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP); |
| 2636 | return ret; |
| 2637 | } |
| 2638 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2639 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2640 | * See spoe_process_message for details. */ |
| 2641 | static int |
| 2642 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2643 | enum spoe_event ev) |
| 2644 | { |
| 2645 | int dir, ret; |
| 2646 | |
| 2647 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2648 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2649 | (int)now.tv_sec, (int)now.tv_usec, |
| 2650 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2651 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2652 | spoe_event_str[ev]); |
| 2653 | |
| 2654 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2655 | |
| 2656 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2657 | return 1; |
| 2658 | |
| 2659 | ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2660 | return ret; |
| 2661 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2662 | |
| 2663 | /*************************************************************************** |
| 2664 | * Functions that create/destroy SPOE contexts |
| 2665 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2666 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2667 | spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2668 | { |
Christopher Faulet | 600d37e | 2017-11-10 11:54:58 +0100 | [diff] [blame] | 2669 | if ((*buf)->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2670 | return 1; |
| 2671 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2672 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2673 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2674 | LIST_DEL(&buffer_wait->list); |
| 2675 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2676 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2679 | if (b_alloc_margin(buf, global.tune.reserved_bufs)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2680 | return 1; |
| 2681 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2682 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2683 | LIST_ADDQ(&buffer_wq, &buffer_wait->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2684 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2689 | spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2690 | { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2691 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2692 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2693 | LIST_DEL(&buffer_wait->list); |
| 2694 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2695 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | /* Release the buffer if needed */ |
Christopher Faulet | 600d37e | 2017-11-10 11:54:58 +0100 | [diff] [blame] | 2699 | if ((*buf)->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2700 | b_free(buf); |
| 2701 | offer_buffers(buffer_wait->target, |
| 2702 | tasks_run_queue + applets_active_queue); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2703 | } |
| 2704 | } |
| 2705 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2706 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2707 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2708 | { |
| 2709 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2710 | return 1; |
| 2711 | } |
| 2712 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2713 | static struct spoe_context * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2714 | spoe_create_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2715 | { |
| 2716 | struct spoe_config *conf = FLT_CONF(filter); |
| 2717 | struct spoe_context *ctx; |
| 2718 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2719 | ctx = pool_alloc_dirty(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2720 | if (ctx == NULL) { |
| 2721 | return NULL; |
| 2722 | } |
| 2723 | memset(ctx, 0, sizeof(*ctx)); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2724 | ctx->filter = filter; |
| 2725 | ctx->state = SPOE_CTX_ST_NONE; |
| 2726 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2727 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2728 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2729 | ctx->groups = &conf->agent->groups; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2730 | ctx->buffer = &buf_empty; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2731 | LIST_INIT(&ctx->buffer_wait.list); |
| 2732 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2733 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2734 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2735 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2736 | ctx->stream_id = 0; |
| 2737 | ctx->frame_id = 1; |
| 2738 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2739 | |
| 2740 | return ctx; |
| 2741 | } |
| 2742 | |
| 2743 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2744 | spoe_destroy_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2745 | { |
| 2746 | if (!ctx) |
| 2747 | return; |
| 2748 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2749 | spoe_stop_processing(ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2750 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2754 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2755 | { |
| 2756 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2757 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | |
| 2761 | /*************************************************************************** |
| 2762 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2763 | **************************************************************************/ |
| 2764 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2765 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2766 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2767 | { |
| 2768 | struct proxy *p; |
| 2769 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2770 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2771 | while (p) { |
| 2772 | struct flt_conf *fconf; |
| 2773 | |
| 2774 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2775 | struct spoe_config *conf; |
| 2776 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2777 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2778 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2779 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2780 | if (fconf->id != spoe_filter_id) |
| 2781 | continue; |
| 2782 | |
| 2783 | conf = fconf->conf; |
| 2784 | agent = conf->agent; |
| 2785 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2786 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2787 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2788 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 2789 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2790 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | p = p->next; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | |
| 2798 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 2799 | static int |
| 2800 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 2801 | { |
| 2802 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2803 | |
| 2804 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 2805 | init_new_proxy(&conf->agent_fe); |
| 2806 | conf->agent_fe.parent = conf->agent; |
| 2807 | conf->agent_fe.last_change = now.tv_sec; |
| 2808 | conf->agent_fe.id = conf->agent->id; |
| 2809 | conf->agent_fe.cap = PR_CAP_FE; |
| 2810 | conf->agent_fe.mode = PR_MODE_TCP; |
| 2811 | conf->agent_fe.maxconn = 0; |
| 2812 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 2813 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 2814 | conf->agent_fe.accept = frontend_accept; |
| 2815 | conf->agent_fe.srv = NULL; |
| 2816 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 2817 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 2818 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 2819 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2820 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2821 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2822 | sighandler_registered = 1; |
| 2823 | } |
| 2824 | |
| 2825 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | /* Free ressources allocated by the SPOE filter. */ |
| 2829 | static void |
| 2830 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 2831 | { |
| 2832 | struct spoe_config *conf = fconf->conf; |
| 2833 | |
| 2834 | if (conf) { |
| 2835 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2836 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2837 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2838 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2839 | free(conf); |
| 2840 | } |
| 2841 | fconf->conf = NULL; |
| 2842 | } |
| 2843 | |
| 2844 | /* Check configuration of a SPOE filter for a specified proxy. |
| 2845 | * Return 1 on error, else 0. */ |
| 2846 | static int |
| 2847 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 2848 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2849 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2850 | struct spoe_config *conf = fconf->conf; |
| 2851 | struct proxy *target; |
| 2852 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2853 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 2854 | * are uniq */ |
| 2855 | list_for_each_entry(f, &px->filter_configs, list) { |
| 2856 | struct spoe_config *c = f->conf; |
| 2857 | |
| 2858 | /* This is not an SPOE filter */ |
| 2859 | if (f->id != spoe_filter_id) |
| 2860 | continue; |
| 2861 | /* This is the current SPOE filter */ |
| 2862 | if (f == fconf) |
| 2863 | continue; |
| 2864 | |
| 2865 | /* Check engine Id. It should be uniq */ |
| 2866 | if (!strcmp(conf->id, c->id)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2867 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 2868 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2869 | return 1; |
| 2870 | } |
| 2871 | } |
| 2872 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2873 | target = proxy_be_by_name(conf->agent->b.name); |
| 2874 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2875 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 2876 | " declared at %s:%d.\n", |
| 2877 | px->id, conf->agent->b.name, conf->agent->id, |
| 2878 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2879 | return 1; |
| 2880 | } |
| 2881 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2882 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 2883 | " at %s:%d does not support HTTP mode.\n", |
| 2884 | px->id, target->id, conf->agent->id, |
| 2885 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2886 | return 1; |
| 2887 | } |
| 2888 | |
| 2889 | free(conf->agent->b.name); |
| 2890 | conf->agent->b.name = NULL; |
| 2891 | conf->agent->b.be = target; |
| 2892 | return 0; |
| 2893 | } |
| 2894 | |
| 2895 | /************************************************************************** |
| 2896 | * Hooks attached to a stream |
| 2897 | *************************************************************************/ |
| 2898 | /* Called when a filter instance is created and attach to a stream. It creates |
| 2899 | * the context that will be used to process this stream. */ |
| 2900 | static int |
| 2901 | spoe_start(struct stream *s, struct filter *filter) |
| 2902 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2903 | struct spoe_config *conf = FLT_CONF(filter); |
| 2904 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2905 | struct spoe_context *ctx; |
| 2906 | |
| 2907 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2908 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2909 | __FUNCTION__, s); |
| 2910 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2911 | ctx = spoe_create_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2912 | if (ctx == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2913 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2914 | " - failed to create SPOE context\n", |
| 2915 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 2916 | __FUNCTION__, s); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2917 | send_log(s->be, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2918 | "SPOE: [%s] failed to create SPOE context\n", |
| 2919 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2920 | return 0; |
| 2921 | } |
| 2922 | |
| 2923 | ctx->strm = s; |
| 2924 | ctx->state = SPOE_CTX_ST_READY; |
| 2925 | filter->ctx = ctx; |
| 2926 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2927 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2928 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 2929 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2930 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2931 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 2932 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2933 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2934 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 2935 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2936 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2937 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 2938 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2939 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2940 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 2941 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2942 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2943 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 2944 | |
| 2945 | return 1; |
| 2946 | } |
| 2947 | |
| 2948 | /* Called when a filter instance is detached from a stream. It release the |
| 2949 | * attached SPOE context. */ |
| 2950 | static void |
| 2951 | spoe_stop(struct stream *s, struct filter *filter) |
| 2952 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2953 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 2954 | (int)now.tv_sec, (int)now.tv_usec, |
| 2955 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2956 | __FUNCTION__, s); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2957 | spoe_destroy_context(filter->ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2958 | } |
| 2959 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2960 | |
| 2961 | /* |
| 2962 | * Called when the stream is woken up because of expired timer. |
| 2963 | */ |
| 2964 | static void |
| 2965 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 2966 | { |
| 2967 | struct spoe_context *ctx = filter->ctx; |
| 2968 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2969 | if (tick_is_expired(ctx->process_exp, now_ms)) { |
| 2970 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2971 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2972 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2973 | } |
| 2974 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2975 | /* Called when we are ready to filter data on a channel */ |
| 2976 | static int |
| 2977 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 2978 | { |
| 2979 | struct spoe_context *ctx = filter->ctx; |
| 2980 | int ret = 1; |
| 2981 | |
| 2982 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 2983 | " - ctx-flags=0x%08x\n", |
| 2984 | (int)now.tv_sec, (int)now.tv_usec, |
| 2985 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2986 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 2987 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2988 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 2989 | goto out; |
| 2990 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2991 | if (!(chn->flags & CF_ISRESP)) { |
| 2992 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 2993 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 2994 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 2995 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 2996 | |
| 2997 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 2998 | goto out; |
| 2999 | |
| 3000 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3001 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3002 | if (!ret) |
| 3003 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3004 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3005 | } |
| 3006 | else { |
| 3007 | if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP) |
| 3008 | chn->analysers |= AN_RES_INSPECT; |
| 3009 | |
| 3010 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3011 | goto out; |
| 3012 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3013 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3014 | if (!ret) { |
| 3015 | channel_dont_read(chn); |
| 3016 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3017 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3018 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3019 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3023 | return ret; |
| 3024 | } |
| 3025 | |
| 3026 | /* Called before a processing happens on a given channel */ |
| 3027 | static int |
| 3028 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3029 | struct channel *chn, unsigned an_bit) |
| 3030 | { |
| 3031 | struct spoe_context *ctx = filter->ctx; |
| 3032 | int ret = 1; |
| 3033 | |
| 3034 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3035 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3036 | (int)now.tv_sec, (int)now.tv_usec, |
| 3037 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3038 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3039 | ctx->flags, an_bit); |
| 3040 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3041 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3042 | goto out; |
| 3043 | |
| 3044 | switch (an_bit) { |
| 3045 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3046 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3047 | break; |
| 3048 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3049 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3050 | break; |
| 3051 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3052 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3053 | break; |
| 3054 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3055 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3056 | break; |
| 3057 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3058 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3059 | break; |
| 3060 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3061 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3062 | break; |
| 3063 | } |
| 3064 | |
| 3065 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3066 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3067 | channel_dont_read(chn); |
| 3068 | channel_dont_close(chn); |
| 3069 | } |
| 3070 | return ret; |
| 3071 | } |
| 3072 | |
| 3073 | /* Called when the filtering on the channel ends. */ |
| 3074 | static int |
| 3075 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3076 | { |
| 3077 | struct spoe_context *ctx = filter->ctx; |
| 3078 | |
| 3079 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3080 | " - ctx-flags=0x%08x\n", |
| 3081 | (int)now.tv_sec, (int)now.tv_usec, |
| 3082 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3083 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3084 | |
| 3085 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3086 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | return 1; |
| 3090 | } |
| 3091 | |
| 3092 | /******************************************************************** |
| 3093 | * Functions that manage the filter initialization |
| 3094 | ********************************************************************/ |
| 3095 | struct flt_ops spoe_ops = { |
| 3096 | /* Manage SPOE filter, called for each filter declaration */ |
| 3097 | .init = spoe_init, |
| 3098 | .deinit = spoe_deinit, |
| 3099 | .check = spoe_check, |
| 3100 | |
| 3101 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3102 | .attach = spoe_start, |
| 3103 | .detach = spoe_stop, |
| 3104 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3105 | |
| 3106 | /* Handle channels activity */ |
| 3107 | .channel_start_analyze = spoe_start_analyze, |
| 3108 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3109 | .channel_end_analyze = spoe_end_analyze, |
| 3110 | }; |
| 3111 | |
| 3112 | |
| 3113 | static int |
| 3114 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3115 | { |
| 3116 | const char *err; |
| 3117 | int i, err_code = 0; |
| 3118 | |
| 3119 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3120 | (cfg_scope != NULL && curengine == NULL) || |
Christopher Faulet | e1405e5 | 2017-09-19 10:35:35 +0200 | [diff] [blame] | 3121 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3122 | goto out; |
| 3123 | |
| 3124 | if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */ |
| 3125 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3126 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3127 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3128 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3129 | goto out; |
| 3130 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3131 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3132 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3133 | goto out; |
| 3134 | } |
| 3135 | |
| 3136 | err = invalid_char(args[1]); |
| 3137 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3138 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3139 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3140 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3141 | goto out; |
| 3142 | } |
| 3143 | |
| 3144 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3145 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3146 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3147 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3148 | goto out; |
| 3149 | } |
| 3150 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3151 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3152 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3153 | goto out; |
| 3154 | } |
| 3155 | |
| 3156 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3157 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3158 | curagent->conf.file = strdup(file); |
| 3159 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3160 | |
| 3161 | curagent->timeout.hello = TICK_ETERNITY; |
| 3162 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3163 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3164 | |
| 3165 | curagent->engine_id = NULL; |
| 3166 | curagent->var_pfx = NULL; |
| 3167 | curagent->var_on_error = NULL; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3168 | curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
| 3169 | if (global.nbthread == 1) |
| 3170 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3171 | curagent->cps_max = 0; |
| 3172 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3173 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3174 | curagent->max_fpa = 100; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3175 | |
| 3176 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3177 | LIST_INIT(&curagent->events[i]); |
| 3178 | LIST_INIT(&curagent->groups); |
| 3179 | LIST_INIT(&curagent->messages); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3180 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3181 | if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3182 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3183 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3184 | goto out; |
| 3185 | } |
| 3186 | for (i = 0; i < global.nbthread; ++i) { |
| 3187 | curagent->rt[i].frame_size = curagent->max_frame_size; |
| 3188 | curagent->rt[i].applets_act = 0; |
| 3189 | curagent->rt[i].applets_idle = 0; |
| 3190 | curagent->rt[i].sending_rate = 0; |
| 3191 | LIST_INIT(&curagent->rt[i].applets); |
| 3192 | LIST_INIT(&curagent->rt[i].sending_queue); |
| 3193 | LIST_INIT(&curagent->rt[i].waiting_queue); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3194 | HA_SPIN_INIT(&curagent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3195 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3196 | } |
| 3197 | else if (!strcmp(args[0], "use-backend")) { |
| 3198 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3199 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3200 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3201 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3202 | goto out; |
| 3203 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3204 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3205 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3206 | free(curagent->b.name); |
| 3207 | curagent->b.name = strdup(args[1]); |
| 3208 | } |
| 3209 | else if (!strcmp(args[0], "messages")) { |
| 3210 | int cur_arg = 1; |
| 3211 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3212 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3213 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3214 | list_for_each_entry(ph, &curmphs, list) { |
| 3215 | if (!strcmp(ph->id, args[cur_arg])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3216 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3217 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3218 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3219 | goto out; |
| 3220 | } |
| 3221 | } |
| 3222 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3223 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3224 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3225 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3226 | goto out; |
| 3227 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3228 | ph->id = strdup(args[cur_arg]); |
| 3229 | LIST_ADDQ(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3230 | cur_arg++; |
| 3231 | } |
| 3232 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3233 | else if (!strcmp(args[0], "groups")) { |
| 3234 | int cur_arg = 1; |
| 3235 | while (*args[cur_arg]) { |
| 3236 | struct spoe_placeholder *ph = NULL; |
| 3237 | |
| 3238 | list_for_each_entry(ph, &curgphs, list) { |
| 3239 | if (!strcmp(ph->id, args[cur_arg])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3240 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3241 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3242 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3243 | goto out; |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3248 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3249 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3250 | goto out; |
| 3251 | } |
| 3252 | ph->id = strdup(args[cur_arg]); |
| 3253 | LIST_ADDQ(&curgphs, &ph->list); |
| 3254 | cur_arg++; |
| 3255 | } |
| 3256 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3257 | else if (!strcmp(args[0], "timeout")) { |
| 3258 | unsigned int *tv = NULL; |
| 3259 | const char *res; |
| 3260 | unsigned timeout; |
| 3261 | |
| 3262 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3263 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3264 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3265 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3266 | goto out; |
| 3267 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3268 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3269 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3270 | if (!strcmp(args[1], "hello")) |
| 3271 | tv = &curagent->timeout.hello; |
| 3272 | else if (!strcmp(args[1], "idle")) |
| 3273 | tv = &curagent->timeout.idle; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3274 | else if (!strcmp(args[1], "processing")) |
| 3275 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3276 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3277 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3278 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3279 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3280 | goto out; |
| 3281 | } |
| 3282 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3283 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3284 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3285 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3286 | goto out; |
| 3287 | } |
| 3288 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
| 3289 | if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3290 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3291 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3292 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3293 | goto out; |
| 3294 | } |
| 3295 | *tv = MS_TO_TICKS(timeout); |
| 3296 | } |
| 3297 | else if (!strcmp(args[0], "option")) { |
| 3298 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3299 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3300 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3301 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3302 | goto out; |
| 3303 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3304 | |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3305 | if (!strcmp(args[1], "pipelining")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3306 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3307 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3308 | if (kwm == 1) |
| 3309 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3310 | else |
| 3311 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3312 | goto out; |
| 3313 | } |
| 3314 | else if (!strcmp(args[1], "async")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3315 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3316 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3317 | if (kwm == 1) |
| 3318 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3319 | else { |
| 3320 | if (global.nbthread == 1) |
| 3321 | curagent->flags |= SPOE_FL_ASYNC; |
| 3322 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3323 | ha_warning("parsing [%s:%d] Async option is not supported with threads.\n", |
| 3324 | file, linenum); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3325 | err_code |= ERR_WARN; |
| 3326 | } |
| 3327 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3328 | goto out; |
| 3329 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3330 | else if (!strcmp(args[1], "send-frag-payload")) { |
| 3331 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3332 | goto out; |
| 3333 | if (kwm == 1) |
| 3334 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3335 | else |
| 3336 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3337 | goto out; |
| 3338 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3339 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3340 | /* Following options does not support negation */ |
| 3341 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3342 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3343 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3344 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3345 | goto out; |
| 3346 | } |
| 3347 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3348 | if (!strcmp(args[1], "var-prefix")) { |
| 3349 | char *tmp; |
| 3350 | |
| 3351 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3352 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3353 | file, linenum, args[0], |
| 3354 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3355 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3356 | goto out; |
| 3357 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3358 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3359 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3360 | tmp = args[2]; |
| 3361 | while (*tmp) { |
| 3362 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3363 | ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n", |
| 3364 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3365 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3366 | goto out; |
| 3367 | } |
| 3368 | tmp++; |
| 3369 | } |
| 3370 | curagent->var_pfx = strdup(args[2]); |
| 3371 | } |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3372 | else if (!strcmp(args[1], "force-set-var")) { |
| 3373 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3374 | goto out; |
| 3375 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3376 | } |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3377 | else if (!strcmp(args[1], "continue-on-error")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3378 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3379 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3380 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3381 | } |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3382 | else if (!strcmp(args[1], "set-on-error")) { |
| 3383 | char *tmp; |
| 3384 | |
| 3385 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3386 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3387 | file, linenum, args[0], |
| 3388 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3389 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3390 | goto out; |
| 3391 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3392 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3393 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3394 | tmp = args[2]; |
| 3395 | while (*tmp) { |
| 3396 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3397 | ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n", |
| 3398 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3399 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3400 | goto out; |
| 3401 | } |
| 3402 | tmp++; |
| 3403 | } |
| 3404 | curagent->var_on_error = strdup(args[2]); |
| 3405 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3406 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3407 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3408 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3409 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3410 | goto out; |
| 3411 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3412 | } |
| 3413 | else if (!strcmp(args[0], "maxconnrate")) { |
| 3414 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3415 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3416 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3417 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3418 | goto out; |
| 3419 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3420 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3421 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3422 | curagent->cps_max = atol(args[1]); |
| 3423 | } |
| 3424 | else if (!strcmp(args[0], "maxerrrate")) { |
| 3425 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3426 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3427 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3428 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3429 | goto out; |
| 3430 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3431 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3432 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3433 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3434 | } |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3435 | else if (!strcmp(args[0], "max-frame-size")) { |
| 3436 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3437 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3438 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3439 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3440 | goto out; |
| 3441 | } |
| 3442 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3443 | goto out; |
| 3444 | curagent->max_frame_size = atol(args[1]); |
| 3445 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3446 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3447 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3448 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3449 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3450 | goto out; |
| 3451 | } |
| 3452 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3453 | else if (!strcmp(args[0], "register-var-names")) { |
| 3454 | int cur_arg; |
| 3455 | |
| 3456 | if (!*args[1]) { |
| 3457 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3458 | file, linenum, args[0]); |
| 3459 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3460 | goto out; |
| 3461 | } |
| 3462 | cur_arg = 1; |
| 3463 | while (*args[cur_arg]) { |
| 3464 | struct spoe_var_placeholder *vph; |
| 3465 | |
| 3466 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3467 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3468 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3469 | goto out; |
| 3470 | } |
| 3471 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
| 3472 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3473 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3474 | goto out; |
| 3475 | } |
| 3476 | LIST_ADDQ(&curvars, &vph->list); |
| 3477 | cur_arg++; |
| 3478 | } |
| 3479 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3480 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3481 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3482 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3483 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3484 | goto out; |
| 3485 | } |
| 3486 | out: |
| 3487 | return err_code; |
| 3488 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3489 | static int |
| 3490 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3491 | { |
| 3492 | struct spoe_group *grp; |
| 3493 | const char *err; |
| 3494 | int err_code = 0; |
| 3495 | |
| 3496 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3497 | (cfg_scope != NULL && curengine == NULL) || |
| 3498 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
| 3499 | goto out; |
| 3500 | |
| 3501 | if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */ |
| 3502 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3503 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3504 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3505 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3506 | goto out; |
| 3507 | } |
| 3508 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3509 | err_code |= ERR_ABORT; |
| 3510 | goto out; |
| 3511 | } |
| 3512 | |
| 3513 | err = invalid_char(args[1]); |
| 3514 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3515 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3516 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3517 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3518 | goto out; |
| 3519 | } |
| 3520 | |
| 3521 | list_for_each_entry(grp, &curgrps, list) { |
| 3522 | if (!strcmp(grp->id, args[1])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3523 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3524 | " name as another one declared at %s:%d.\n", |
| 3525 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3526 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3527 | goto out; |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3532 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3533 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3534 | goto out; |
| 3535 | } |
| 3536 | |
| 3537 | curgrp->id = strdup(args[1]); |
| 3538 | curgrp->conf.file = strdup(file); |
| 3539 | curgrp->conf.line = linenum; |
| 3540 | LIST_INIT(&curgrp->phs); |
| 3541 | LIST_INIT(&curgrp->messages); |
| 3542 | LIST_ADDQ(&curgrps, &curgrp->list); |
| 3543 | } |
| 3544 | else if (!strcmp(args[0], "messages")) { |
| 3545 | int cur_arg = 1; |
| 3546 | while (*args[cur_arg]) { |
| 3547 | struct spoe_placeholder *ph = NULL; |
| 3548 | |
| 3549 | list_for_each_entry(ph, &curgrp->phs, list) { |
| 3550 | if (!strcmp(ph->id, args[cur_arg])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3551 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3552 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3553 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3554 | goto out; |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3559 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3560 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3561 | goto out; |
| 3562 | } |
| 3563 | ph->id = strdup(args[cur_arg]); |
| 3564 | LIST_ADDQ(&curgrp->phs, &ph->list); |
| 3565 | cur_arg++; |
| 3566 | } |
| 3567 | } |
| 3568 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3569 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3570 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3571 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3572 | goto out; |
| 3573 | } |
| 3574 | out: |
| 3575 | return err_code; |
| 3576 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3577 | |
| 3578 | static int |
| 3579 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3580 | { |
| 3581 | struct spoe_message *msg; |
| 3582 | struct spoe_arg *arg; |
| 3583 | const char *err; |
| 3584 | char *errmsg = NULL; |
| 3585 | int err_code = 0; |
| 3586 | |
| 3587 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3588 | (cfg_scope != NULL && curengine == NULL) || |
Christopher Faulet | e1405e5 | 2017-09-19 10:35:35 +0200 | [diff] [blame] | 3589 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3590 | goto out; |
| 3591 | |
| 3592 | if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */ |
| 3593 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3594 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3595 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3596 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3597 | goto out; |
| 3598 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3599 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3600 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3601 | goto out; |
| 3602 | } |
| 3603 | |
| 3604 | err = invalid_char(args[1]); |
| 3605 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3606 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3607 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3608 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3609 | goto out; |
| 3610 | } |
| 3611 | |
| 3612 | list_for_each_entry(msg, &curmsgs, list) { |
| 3613 | if (!strcmp(msg->id, args[1])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3614 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3615 | " name as another one declared at %s:%d.\n", |
| 3616 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3617 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3618 | goto out; |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3623 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3624 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3625 | goto out; |
| 3626 | } |
| 3627 | |
| 3628 | curmsg->id = strdup(args[1]); |
| 3629 | curmsg->id_len = strlen(curmsg->id); |
| 3630 | curmsg->event = SPOE_EV_NONE; |
| 3631 | curmsg->conf.file = strdup(file); |
| 3632 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3633 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3634 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3635 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3636 | LIST_INIT(&curmsg->by_evt); |
| 3637 | LIST_INIT(&curmsg->by_grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3638 | LIST_ADDQ(&curmsgs, &curmsg->list); |
| 3639 | } |
| 3640 | else if (!strcmp(args[0], "args")) { |
| 3641 | int cur_arg = 1; |
| 3642 | |
| 3643 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3644 | curproxy->conf.args.file = file; |
| 3645 | curproxy->conf.args.line = linenum; |
| 3646 | while (*args[cur_arg]) { |
| 3647 | char *delim = strchr(args[cur_arg], '='); |
| 3648 | int idx = 0; |
| 3649 | |
| 3650 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3651 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3652 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3653 | goto out; |
| 3654 | } |
| 3655 | |
| 3656 | if (!delim) { |
| 3657 | arg->name = NULL; |
| 3658 | arg->name_len = 0; |
| 3659 | delim = args[cur_arg]; |
| 3660 | } |
| 3661 | else { |
| 3662 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3663 | arg->name_len = delim - args[cur_arg]; |
| 3664 | delim++; |
| 3665 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3666 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3667 | &idx, file, linenum, &errmsg, |
| 3668 | &curproxy->conf.args); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3669 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3670 | ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3671 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3672 | free(arg->name); |
| 3673 | free(arg); |
| 3674 | goto out; |
| 3675 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3676 | curmsg->nargs++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3677 | LIST_ADDQ(&curmsg->args, &arg->list); |
| 3678 | cur_arg++; |
| 3679 | } |
| 3680 | curproxy->conf.args.file = NULL; |
| 3681 | curproxy->conf.args.line = 0; |
| 3682 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3683 | else if (!strcmp(args[0], "acl")) { |
| 3684 | err = invalid_char(args[1]); |
| 3685 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3686 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 3687 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3688 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3689 | goto out; |
| 3690 | } |
| 3691 | if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3692 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 3693 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3694 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3695 | goto out; |
| 3696 | } |
| 3697 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3698 | else if (!strcmp(args[0], "event")) { |
| 3699 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3700 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3701 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3702 | goto out; |
| 3703 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3704 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 3705 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3706 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3707 | if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS])) |
| 3708 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
| 3709 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS])) |
| 3710 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 3711 | |
| 3712 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE])) |
| 3713 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 3714 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE])) |
| 3715 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
| 3716 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP])) |
| 3717 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 3718 | |
| 3719 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE])) |
| 3720 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 3721 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE])) |
| 3722 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
| 3723 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP])) |
| 3724 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 3725 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3726 | ha_alert("parsing [%s:%d] : unkown event '%s'.\n", |
| 3727 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3728 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3729 | goto out; |
| 3730 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3731 | |
| 3732 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 3733 | struct acl_cond *cond; |
| 3734 | |
| 3735 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 3736 | curproxy, (const char **)args+2, |
| 3737 | &errmsg); |
| 3738 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3739 | ha_alert("parsing [%s:%d] : error detected while " |
| 3740 | "parsing an 'event %s' condition : %s.\n", |
| 3741 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3742 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3743 | goto out; |
| 3744 | } |
| 3745 | curmsg->cond = cond; |
| 3746 | } |
| 3747 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3748 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 3749 | "or 'unless' followed by a condition but found '%s'.\n", |
| 3750 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3751 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3752 | goto out; |
| 3753 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3754 | } |
| 3755 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3756 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 3757 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3758 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3759 | goto out; |
| 3760 | } |
| 3761 | out: |
| 3762 | free(errmsg); |
| 3763 | return err_code; |
| 3764 | } |
| 3765 | |
| 3766 | /* Return -1 on error, else 0 */ |
| 3767 | static int |
| 3768 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 3769 | struct flt_conf *fconf, char **err, void *private) |
| 3770 | { |
| 3771 | struct list backup_sections; |
| 3772 | struct spoe_config *conf; |
| 3773 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3774 | struct spoe_group *grp, *grpback; |
| 3775 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3776 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3777 | char *file = NULL, *engine = NULL; |
| 3778 | int ret, pos = *cur_arg + 1; |
| 3779 | |
| 3780 | conf = calloc(1, sizeof(*conf)); |
| 3781 | if (conf == NULL) { |
| 3782 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 3783 | goto error; |
| 3784 | } |
| 3785 | conf->proxy = px; |
| 3786 | |
| 3787 | while (*args[pos]) { |
| 3788 | if (!strcmp(args[pos], "config")) { |
| 3789 | if (!*args[pos+1]) { |
| 3790 | memprintf(err, "'%s' : '%s' option without value", |
| 3791 | args[*cur_arg], args[pos]); |
| 3792 | goto error; |
| 3793 | } |
| 3794 | file = args[pos+1]; |
| 3795 | pos += 2; |
| 3796 | } |
| 3797 | else if (!strcmp(args[pos], "engine")) { |
| 3798 | if (!*args[pos+1]) { |
| 3799 | memprintf(err, "'%s' : '%s' option without value", |
| 3800 | args[*cur_arg], args[pos]); |
| 3801 | goto error; |
| 3802 | } |
| 3803 | engine = args[pos+1]; |
| 3804 | pos += 2; |
| 3805 | } |
| 3806 | else { |
| 3807 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 3808 | goto error; |
| 3809 | } |
| 3810 | } |
| 3811 | if (file == NULL) { |
| 3812 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 3813 | goto error; |
| 3814 | } |
| 3815 | |
| 3816 | /* backup sections and register SPOE sections */ |
| 3817 | LIST_INIT(&backup_sections); |
| 3818 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3819 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 3820 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 3821 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3822 | |
| 3823 | /* Parse SPOE filter configuration file */ |
| 3824 | curengine = engine; |
| 3825 | curproxy = px; |
| 3826 | curagent = NULL; |
| 3827 | curmsg = NULL; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3828 | LIST_INIT(&curmsgs); |
| 3829 | LIST_INIT(&curgrps); |
| 3830 | LIST_INIT(&curmphs); |
| 3831 | LIST_INIT(&curgphs); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3832 | LIST_INIT(&curvars); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3833 | ret = readcfgfile(file); |
| 3834 | curproxy = NULL; |
| 3835 | |
| 3836 | /* unregister SPOE sections and restore previous sections */ |
| 3837 | cfg_unregister_sections(); |
| 3838 | cfg_restore_sections(&backup_sections); |
| 3839 | |
| 3840 | if (ret == -1) { |
| 3841 | memprintf(err, "Could not open configuration file %s : %s", |
| 3842 | file, strerror(errno)); |
| 3843 | goto error; |
| 3844 | } |
| 3845 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 3846 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 3847 | goto error; |
| 3848 | } |
| 3849 | |
| 3850 | /* Check SPOE agent */ |
| 3851 | if (curagent == NULL) { |
| 3852 | memprintf(err, "No SPOE agent found in file %s", file); |
| 3853 | goto error; |
| 3854 | } |
| 3855 | if (curagent->b.name == NULL) { |
| 3856 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 3857 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 3858 | goto error; |
| 3859 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3860 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 3861 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3862 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3863 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 3864 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 3865 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 3866 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 3867 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3868 | } |
| 3869 | if (curagent->var_pfx == NULL) { |
| 3870 | char *tmp = curagent->id; |
| 3871 | |
| 3872 | while (*tmp) { |
| 3873 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3874 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 3875 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 3876 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3877 | goto error; |
| 3878 | } |
| 3879 | tmp++; |
| 3880 | } |
| 3881 | curagent->var_pfx = strdup(curagent->id); |
| 3882 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3883 | if (curagent->engine_id == NULL) |
| 3884 | curagent->engine_id = generate_pseudo_uuid(); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3885 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3886 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3887 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 3888 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3889 | goto finish; |
| 3890 | } |
| 3891 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3892 | /* Replace placeholders by the corresponding messages for the SPOE |
| 3893 | * agent */ |
| 3894 | list_for_each_entry(ph, &curmphs, list) { |
| 3895 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3896 | struct spoe_arg *arg; |
| 3897 | unsigned int where; |
| 3898 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3899 | if (!strcmp(msg->id, ph->id)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3900 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 3901 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 3902 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 3903 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 3904 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 3905 | } |
| 3906 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 3907 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 3908 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3909 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 3910 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3911 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3912 | } |
| 3913 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3914 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 3915 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3916 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3917 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3918 | |
| 3919 | where = 0; |
| 3920 | switch (msg->event) { |
| 3921 | case SPOE_EV_ON_CLIENT_SESS: |
| 3922 | where |= SMP_VAL_FE_CON_ACC; |
| 3923 | break; |
| 3924 | |
| 3925 | case SPOE_EV_ON_TCP_REQ_FE: |
| 3926 | where |= SMP_VAL_FE_REQ_CNT; |
| 3927 | break; |
| 3928 | |
| 3929 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 3930 | where |= SMP_VAL_FE_HRQ_HDR; |
| 3931 | break; |
| 3932 | |
| 3933 | case SPOE_EV_ON_TCP_REQ_BE: |
| 3934 | if (px->cap & PR_CAP_FE) |
| 3935 | where |= SMP_VAL_FE_REQ_CNT; |
| 3936 | if (px->cap & PR_CAP_BE) |
| 3937 | where |= SMP_VAL_BE_REQ_CNT; |
| 3938 | break; |
| 3939 | |
| 3940 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 3941 | if (px->cap & PR_CAP_FE) |
| 3942 | where |= SMP_VAL_FE_HRQ_HDR; |
| 3943 | if (px->cap & PR_CAP_BE) |
| 3944 | where |= SMP_VAL_BE_HRQ_HDR; |
| 3945 | break; |
| 3946 | |
| 3947 | case SPOE_EV_ON_SERVER_SESS: |
| 3948 | where |= SMP_VAL_BE_SRV_CON; |
| 3949 | break; |
| 3950 | |
| 3951 | case SPOE_EV_ON_TCP_RSP: |
| 3952 | if (px->cap & PR_CAP_FE) |
| 3953 | where |= SMP_VAL_FE_RES_CNT; |
| 3954 | if (px->cap & PR_CAP_BE) |
| 3955 | where |= SMP_VAL_BE_RES_CNT; |
| 3956 | break; |
| 3957 | |
| 3958 | case SPOE_EV_ON_HTTP_RSP: |
| 3959 | if (px->cap & PR_CAP_FE) |
| 3960 | where |= SMP_VAL_FE_HRS_HDR; |
| 3961 | if (px->cap & PR_CAP_BE) |
| 3962 | where |= SMP_VAL_BE_HRS_HDR; |
| 3963 | break; |
| 3964 | |
| 3965 | default: |
| 3966 | break; |
| 3967 | } |
| 3968 | |
| 3969 | list_for_each_entry(arg, &msg->args, list) { |
| 3970 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3971 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3972 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3973 | "none of which is available here ('%s')", |
| 3974 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3975 | sample_ckp_names(arg->expr->fetch->use), |
| 3976 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3977 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3981 | msg->agent = curagent; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3982 | LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt); |
| 3983 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3984 | } |
| 3985 | } |
| 3986 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d", |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3987 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3988 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3989 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3990 | continue; |
| 3991 | } |
| 3992 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3993 | /* Replace placeholders by the corresponding groups for the SPOE |
| 3994 | * agent */ |
| 3995 | list_for_each_entry(ph, &curgphs, list) { |
| 3996 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 3997 | if (!strcmp(grp->id, ph->id)) { |
| 3998 | grp->agent = curagent; |
| 3999 | LIST_DEL(&grp->list); |
| 4000 | LIST_ADDQ(&curagent->groups, &grp->list); |
| 4001 | goto next_aph; |
| 4002 | } |
| 4003 | } |
| 4004 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4005 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4006 | goto error; |
| 4007 | next_aph: |
| 4008 | continue; |
| 4009 | } |
| 4010 | |
| 4011 | /* Replace placeholders by the corresponding message for each SPOE |
| 4012 | * group of the SPOE agent */ |
| 4013 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4014 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4015 | list_for_each_entry(msg, &curmsgs, list) { |
| 4016 | if (!strcmp(msg->id, ph->id)) { |
| 4017 | if (msg->group != NULL) { |
| 4018 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4019 | "the SPOE group '%s' declare at %s:%d", |
| 4020 | msg->id, msg->group->id, |
| 4021 | msg->group->conf.file, |
| 4022 | msg->group->conf.line); |
| 4023 | goto error; |
| 4024 | } |
| 4025 | |
| 4026 | /* Scope for arguments are not checked for now. We will check |
| 4027 | * them only if a rule use the corresponding SPOE group. */ |
| 4028 | msg->agent = curagent; |
| 4029 | msg->group = grp; |
| 4030 | LIST_DEL(&ph->list); |
| 4031 | LIST_ADDQ(&grp->messages, &msg->by_grp); |
| 4032 | goto next_mph_grp; |
| 4033 | } |
| 4034 | } |
| 4035 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4036 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4037 | goto error; |
| 4038 | next_mph_grp: |
| 4039 | continue; |
| 4040 | } |
| 4041 | } |
| 4042 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4043 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4044 | /* move curmsgs to the agent message list */ |
| 4045 | curmsgs.n->p = &curagent->messages; |
| 4046 | curmsgs.p->n = &curagent->messages; |
| 4047 | curagent->messages = curmsgs; |
| 4048 | LIST_INIT(&curmsgs); |
| 4049 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4050 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4051 | conf->agent = curagent; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4052 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 4053 | LIST_DEL(&ph->list); |
| 4054 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4055 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4056 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 4057 | LIST_DEL(&ph->list); |
| 4058 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4059 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4060 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4061 | struct arg arg; |
| 4062 | |
| 4063 | trash.len = snprintf(trash.str, trash.size, "proc.%s.%s", |
| 4064 | curagent->var_pfx, vph->name); |
| 4065 | |
| 4066 | arg.type = ARGT_STR; |
| 4067 | arg.data.str.str = trash.str; |
| 4068 | arg.data.str.len = trash.len; |
| 4069 | if (!vars_check_arg(&arg, err)) { |
| 4070 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4071 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4072 | goto error; |
| 4073 | } |
| 4074 | |
| 4075 | LIST_DEL(&vph->list); |
| 4076 | free(vph->name); |
| 4077 | free(vph); |
| 4078 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4079 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4080 | LIST_DEL(&grp->list); |
| 4081 | spoe_release_group(grp); |
| 4082 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4083 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4084 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4085 | fconf->ops = &spoe_ops; |
| 4086 | fconf->conf = conf; |
| 4087 | return 0; |
| 4088 | |
| 4089 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4090 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4091 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 4092 | LIST_DEL(&ph->list); |
| 4093 | spoe_release_placeholder(ph); |
| 4094 | } |
| 4095 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 4096 | LIST_DEL(&ph->list); |
| 4097 | spoe_release_placeholder(ph); |
| 4098 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4099 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4100 | LIST_DEL(&vph->list); |
| 4101 | free(vph->name); |
| 4102 | free(vph); |
| 4103 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4104 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4105 | LIST_DEL(&grp->list); |
| 4106 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4107 | } |
| 4108 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
| 4109 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4110 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4111 | } |
| 4112 | free(conf); |
| 4113 | return -1; |
| 4114 | } |
| 4115 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4116 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4117 | * associated to a "send-spoe-group" action. |
| 4118 | * |
| 4119 | * It returns ACT_RET_CONT is processing is finished without error, it returns |
| 4120 | * ACT_RET_YIELD if the action is in progress. Otherwise it returns |
| 4121 | * ACT_RET_ERR. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4122 | static enum act_return |
| 4123 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4124 | struct session *sess, struct stream *s, int flags) |
| 4125 | { |
| 4126 | struct filter *filter; |
| 4127 | struct spoe_agent *agent = NULL; |
| 4128 | struct spoe_group *group = NULL; |
| 4129 | struct spoe_context *ctx = NULL; |
| 4130 | int ret, dir; |
| 4131 | |
| 4132 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4133 | if (filter->config == rule->arg.act.p[0]) { |
| 4134 | agent = rule->arg.act.p[2]; |
| 4135 | group = rule->arg.act.p[3]; |
| 4136 | ctx = filter->ctx; |
| 4137 | break; |
| 4138 | } |
| 4139 | } |
| 4140 | if (agent == NULL || group == NULL || ctx == NULL) |
| 4141 | return ACT_RET_ERR; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4142 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4143 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4144 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4145 | switch (rule->from) { |
| 4146 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4147 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4148 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4149 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4150 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4151 | default: |
| 4152 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4153 | " - internal error while execute spoe-send-group\n", |
| 4154 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4155 | __FUNCTION__, s); |
| 4156 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4157 | agent->id); |
| 4158 | return ACT_RET_CONT; |
| 4159 | } |
| 4160 | |
| 4161 | ret = spoe_process_group(s, ctx, group, dir); |
| 4162 | if (ret == 1) |
| 4163 | return ACT_RET_CONT; |
| 4164 | else if (ret == 0) { |
| 4165 | if (flags & ACT_FLAG_FINAL) { |
| 4166 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4167 | " - failed to process group '%s': interrupted by caller\n", |
| 4168 | (int)now.tv_sec, (int)now.tv_usec, |
| 4169 | agent->id, __FUNCTION__, s, group->id); |
| 4170 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
| 4171 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 4172 | spoe_stop_processing(ctx); |
| 4173 | return ACT_RET_CONT; |
| 4174 | } |
| 4175 | return ACT_RET_YIELD; |
| 4176 | } |
| 4177 | else |
| 4178 | return ACT_RET_ERR; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4182 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4183 | * action should be: |
| 4184 | * |
| 4185 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4186 | * |
| 4187 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4188 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4189 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4190 | * group. |
| 4191 | * |
| 4192 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4193 | * filled. |
| 4194 | */ |
| 4195 | static int |
| 4196 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4197 | { |
| 4198 | struct flt_conf *fconf; |
| 4199 | struct spoe_config *conf; |
| 4200 | struct spoe_agent *agent = NULL; |
| 4201 | struct spoe_group *group; |
| 4202 | struct spoe_message *msg; |
| 4203 | char *engine_id = rule->arg.act.p[0]; |
| 4204 | char *group_id = rule->arg.act.p[1]; |
| 4205 | unsigned int where = 0; |
| 4206 | |
| 4207 | switch (rule->from) { |
| 4208 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4209 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4210 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4211 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4212 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4213 | default: |
| 4214 | memprintf(err, |
| 4215 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4216 | rule->from); |
| 4217 | goto error; |
| 4218 | } |
| 4219 | |
| 4220 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4221 | * <px> */ |
| 4222 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4223 | conf = fconf->conf; |
| 4224 | |
| 4225 | /* This is not an SPOE filter */ |
| 4226 | if (fconf->id != spoe_filter_id) |
| 4227 | continue; |
| 4228 | |
| 4229 | /* This is the good engine */ |
| 4230 | if (!strcmp(conf->id, engine_id)) { |
| 4231 | agent = conf->agent; |
| 4232 | break; |
| 4233 | } |
| 4234 | } |
| 4235 | if (agent == NULL) { |
| 4236 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4237 | engine_id, group_id); |
| 4238 | goto error; |
| 4239 | } |
| 4240 | |
| 4241 | /* Try to find the right group */ |
| 4242 | list_for_each_entry(group, &agent->groups, list) { |
| 4243 | /* This is the good group */ |
| 4244 | if (!strcmp(group->id, group_id)) |
| 4245 | break; |
| 4246 | } |
| 4247 | if (&group->list == &agent->groups) { |
| 4248 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4249 | group_id, engine_id); |
| 4250 | goto error; |
| 4251 | } |
| 4252 | |
| 4253 | /* Ok, we found the group, we need to check messages and their |
| 4254 | * arguments */ |
| 4255 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4256 | struct spoe_arg *arg; |
| 4257 | |
| 4258 | list_for_each_entry(arg, &msg->args, list) { |
| 4259 | if (!(arg->expr->fetch->val & where)) { |
| 4260 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4261 | "some args extract information from '%s'," |
| 4262 | "none of which is available here ('%s')", |
| 4263 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4264 | sample_ckp_names(arg->expr->fetch->use), |
| 4265 | sample_ckp_names(where)); |
| 4266 | goto error; |
| 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | free(engine_id); |
| 4272 | free(group_id); |
| 4273 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4274 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4275 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4276 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4277 | return 1; |
| 4278 | |
| 4279 | error: |
| 4280 | free(engine_id); |
| 4281 | free(group_id); |
| 4282 | return 0; |
| 4283 | } |
| 4284 | |
| 4285 | /* Parse 'send-spoe-group' action following the format: |
| 4286 | * |
| 4287 | * ... send-spoe-group <engine-id> <group-id> |
| 4288 | * |
| 4289 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4290 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4291 | * ids are saved and used later, when the rule will be checked. |
| 4292 | */ |
| 4293 | static enum act_parse_ret |
| 4294 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4295 | struct act_rule *rule, char **err) |
| 4296 | { |
| 4297 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4298 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4299 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4300 | return ACT_RET_PRS_ERR; |
| 4301 | } |
| 4302 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4303 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4304 | |
| 4305 | (*orig_arg) += 2; |
| 4306 | |
| 4307 | rule->action = ACT_CUSTOM; |
| 4308 | rule->action_ptr = spoe_send_group; |
| 4309 | rule->check_ptr = check_send_spoe_group; |
| 4310 | return ACT_RET_PRS_OK; |
| 4311 | } |
| 4312 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4313 | |
| 4314 | /* Declare the filter parser for "spoe" keyword */ |
| 4315 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4316 | { "spoe", parse_spoe_flt, NULL }, |
| 4317 | { NULL, NULL, NULL }, |
| 4318 | } |
| 4319 | }; |
| 4320 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4321 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4322 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4323 | { "send-spoe-group", parse_send_spoe_group }, |
| 4324 | { /* END */ }, |
| 4325 | } |
| 4326 | }; |
| 4327 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4328 | { "send-spoe-group", parse_send_spoe_group }, |
| 4329 | { /* END */ }, |
| 4330 | } |
| 4331 | }; |
| 4332 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4333 | { "send-spoe-group", parse_send_spoe_group }, |
| 4334 | { /* END */ }, |
| 4335 | } |
| 4336 | }; |
| 4337 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4338 | { "send-spoe-group", parse_send_spoe_group }, |
| 4339 | { /* END */ }, |
| 4340 | } |
| 4341 | }; |
| 4342 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4343 | __attribute__((constructor)) |
| 4344 | static void __spoe_init(void) |
| 4345 | { |
| 4346 | flt_register_keywords(&flt_kws); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4347 | tcp_req_cont_keywords_register(&tcp_req_action_kws); |
| 4348 | tcp_res_cont_keywords_register(&tcp_res_action_kws); |
| 4349 | http_req_keywords_register(&http_req_action_kws); |
| 4350 | http_res_keywords_register(&http_res_action_kws); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4351 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4352 | pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED); |
| 4353 | pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | __attribute__((destructor)) |
| 4357 | static void |
| 4358 | __spoe_deinit(void) |
| 4359 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4360 | pool_destroy(pool_head_spoe_ctx); |
| 4361 | pool_destroy(pool_head_spoe_appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4362 | } |