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