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