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