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 |
| 2415 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, |
| 2416 | enum spoe_event ev, int dir) |
| 2417 | { |
| 2418 | char *p, *end; |
| 2419 | int ret; |
| 2420 | |
| 2421 | p = ctx->buffer->p; |
| 2422 | end = p + ctx->buffer->i; |
| 2423 | |
| 2424 | while (p < end) { |
| 2425 | enum spoe_action_type type; |
| 2426 | |
| 2427 | type = *p++; |
| 2428 | switch (type) { |
| 2429 | case SPOE_ACT_T_SET_VAR: |
| 2430 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2431 | if (!ret) |
| 2432 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2433 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2434 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2435 | case SPOE_ACT_T_UNSET_VAR: |
| 2436 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2437 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2438 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2439 | break; |
| 2440 | |
| 2441 | default: |
| 2442 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | return 1; |
| 2447 | skip: |
| 2448 | return 0; |
| 2449 | } |
| 2450 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2451 | /*************************************************************************** |
| 2452 | * Functions that process SPOE events |
| 2453 | **************************************************************************/ |
| 2454 | static inline int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2455 | spoe_start_event_processing(struct spoe_context *ctx, int dir) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2456 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2457 | /* If a process is already started for this SPOE context, retry |
| 2458 | * later. */ |
| 2459 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2460 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2461 | |
| 2462 | /* Set the right flag to prevent request and response processing |
| 2463 | * in same time. */ |
| 2464 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2465 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2466 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2467 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2468 | } |
| 2469 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2470 | static inline void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2471 | spoe_stop_event_processing(struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2472 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2473 | struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx; |
| 2474 | |
| 2475 | if (sa) { |
| 2476 | sa->frag_ctx.ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2477 | spoe_wakeup_appctx(sa->owner); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2478 | } |
| 2479 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2480 | /* Reset the flag to allow next processing */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2481 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2482 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2483 | ctx->status_code = 0; |
| 2484 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2485 | /* Reset processing timer */ |
| 2486 | ctx->process_exp = TICK_ETERNITY; |
| 2487 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2488 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2489 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2490 | ctx->frag_ctx.spoe_appctx = NULL; |
| 2491 | ctx->frag_ctx.curmsg = NULL; |
| 2492 | ctx->frag_ctx.curarg = NULL; |
| 2493 | ctx->frag_ctx.curoff = 0; |
| 2494 | ctx->frag_ctx.flags = 0; |
| 2495 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2496 | if (!LIST_ISEMPTY(&ctx->list)) { |
| 2497 | LIST_DEL(&ctx->list); |
| 2498 | LIST_INIT(&ctx->list); |
| 2499 | } |
| 2500 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2501 | |
| 2502 | /* Process a SPOE event. First, this functions will process messages attached to |
| 2503 | * this event and send them to an agent in a NOTIFY frame. Then, it will wait a |
| 2504 | * ACK frame to process corresponding actions. During all the processing, it |
| 2505 | * returns 0 and it returns 1 when the processing is finished. If an error |
| 2506 | * occurred, -1 is returned. */ |
| 2507 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2508 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2509 | enum spoe_event ev) |
| 2510 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2511 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2512 | struct spoe_agent *agent = conf->agent; |
| 2513 | int dir, ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2514 | |
| 2515 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2516 | " - ctx-state=%s - event=%s\n", |
| 2517 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2518 | agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2519 | spoe_event_str[ev]); |
| 2520 | |
| 2521 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2522 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2523 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2524 | goto out; |
| 2525 | |
| 2526 | if (ctx->state == SPOE_CTX_ST_ERROR) |
| 2527 | goto error; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2528 | |
| 2529 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2530 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2531 | " - failed to process event '%s': timeout\n", |
| 2532 | (int)now.tv_sec, (int)now.tv_usec, |
| 2533 | agent->id, __FUNCTION__, s, spoe_event_str[ev]); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2534 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2535 | goto error; |
| 2536 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2537 | |
| 2538 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2539 | if (agent->eps_max > 0) { |
| 2540 | if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) { |
| 2541 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2542 | " - skip event '%s': max EPS reached\n", |
| 2543 | (int)now.tv_sec, (int)now.tv_usec, |
| 2544 | agent->id, __FUNCTION__, s, spoe_event_str[ev]); |
| 2545 | goto skip; |
| 2546 | } |
| 2547 | } |
| 2548 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2549 | if (!tick_isset(ctx->process_exp)) { |
| 2550 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2551 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2552 | ctx->process_exp); |
| 2553 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2554 | ret = spoe_start_event_processing(ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2555 | if (!ret) |
| 2556 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2557 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2558 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2559 | goto error; |
| 2560 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2561 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2562 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2563 | } |
| 2564 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2565 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2566 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2567 | goto out; |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame^] | 2568 | ret = spoe_encode_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2569 | if (ret < 0) |
| 2570 | goto error; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2571 | if (!ret) |
| 2572 | goto skip; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2573 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2574 | } |
| 2575 | |
| 2576 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
| 2577 | if (ctx->frag_ctx.spoe_appctx) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2578 | spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2579 | ret = 0; |
| 2580 | goto out; |
| 2581 | } |
| 2582 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2583 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2584 | ret = 0; |
| 2585 | goto out; |
| 2586 | } |
| 2587 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2588 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2589 | spoe_process_actions(s, ctx, ev, dir); |
| 2590 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2591 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2592 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2593 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | out: |
| 2597 | return ret; |
| 2598 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2599 | error: |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2600 | if (agent->eps_max > 0) |
| 2601 | update_freq_ctr(&agent->err_per_sec, 1); |
| 2602 | |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2603 | if (agent->var_on_error) { |
| 2604 | struct sample smp; |
| 2605 | |
| 2606 | memset(&smp, 0, sizeof(smp)); |
| 2607 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2608 | smp.data.u.sint = ctx->status_code; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2609 | smp.data.type = SMP_T_BOOL; |
| 2610 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2611 | spoe_set_var(ctx, "txn", agent->var_on_error, |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2612 | strlen(agent->var_on_error), &smp); |
| 2613 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2614 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2615 | " - failed to create process event '%s': code=%u\n", |
| 2616 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2617 | __FUNCTION__, ctx->strm, spoe_event_str[ev], |
| 2618 | ctx->status_code); |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2619 | send_log(ctx->strm->be, LOG_WARNING, |
| 2620 | "SPOE: [%s] failed to process event '%s': code=%u\n", |
| 2621 | agent->id, spoe_event_str[ev], ctx->status_code); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2622 | |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 2623 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2624 | ? SPOE_CTX_ST_READY |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2625 | : SPOE_CTX_ST_NONE); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2626 | ret = 1; |
| 2627 | goto end; |
| 2628 | |
| 2629 | skip: |
| 2630 | ctx->state = SPOE_CTX_ST_READY; |
| 2631 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2632 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2633 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2634 | spoe_stop_event_processing(ctx); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2635 | return ret; |
| 2636 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2637 | |
| 2638 | /*************************************************************************** |
| 2639 | * Functions that create/destroy SPOE contexts |
| 2640 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2641 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2642 | spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2643 | { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2644 | if (*buf != &buf_empty) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2645 | return 1; |
| 2646 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2647 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
| 2648 | LIST_DEL(&buffer_wait->list); |
| 2649 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2652 | if (b_alloc_margin(buf, global.tune.reserved_bufs)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2653 | return 1; |
| 2654 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2655 | LIST_ADDQ(&buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2656 | return 0; |
| 2657 | } |
| 2658 | |
| 2659 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2660 | spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2661 | { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2662 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
| 2663 | LIST_DEL(&buffer_wait->list); |
| 2664 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | /* Release the buffer if needed */ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2668 | if (*buf != &buf_empty) { |
| 2669 | b_free(buf); |
| 2670 | offer_buffers(buffer_wait->target, |
| 2671 | tasks_run_queue + applets_active_queue); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2675 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2676 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2677 | { |
| 2678 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2679 | return 1; |
| 2680 | } |
| 2681 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2682 | static struct spoe_context * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2683 | spoe_create_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2684 | { |
| 2685 | struct spoe_config *conf = FLT_CONF(filter); |
| 2686 | struct spoe_context *ctx; |
| 2687 | |
| 2688 | ctx = pool_alloc_dirty(pool2_spoe_ctx); |
| 2689 | if (ctx == NULL) { |
| 2690 | return NULL; |
| 2691 | } |
| 2692 | memset(ctx, 0, sizeof(*ctx)); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2693 | ctx->filter = filter; |
| 2694 | ctx->state = SPOE_CTX_ST_NONE; |
| 2695 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2696 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2697 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2698 | ctx->groups = &conf->agent->groups; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2699 | ctx->buffer = &buf_empty; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2700 | LIST_INIT(&ctx->buffer_wait.list); |
| 2701 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2702 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2703 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2704 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2705 | ctx->stream_id = 0; |
| 2706 | ctx->frame_id = 1; |
| 2707 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2708 | |
| 2709 | return ctx; |
| 2710 | } |
| 2711 | |
| 2712 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2713 | spoe_destroy_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2714 | { |
| 2715 | if (!ctx) |
| 2716 | return; |
| 2717 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2718 | spoe_stop_event_processing(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2719 | pool_free2(pool2_spoe_ctx, ctx); |
| 2720 | } |
| 2721 | |
| 2722 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2723 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2724 | { |
| 2725 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2726 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | |
| 2730 | /*************************************************************************** |
| 2731 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2732 | **************************************************************************/ |
| 2733 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2734 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2735 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2736 | { |
| 2737 | struct proxy *p; |
| 2738 | |
| 2739 | p = proxy; |
| 2740 | while (p) { |
| 2741 | struct flt_conf *fconf; |
| 2742 | |
| 2743 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2744 | struct spoe_config *conf; |
| 2745 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2746 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2747 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2748 | if (fconf->id != spoe_filter_id) |
| 2749 | continue; |
| 2750 | |
| 2751 | conf = fconf->conf; |
| 2752 | agent = conf->agent; |
| 2753 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2754 | list_for_each_entry(spoe_appctx, &agent->applets, list) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2755 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | p = p->next; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | |
| 2763 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 2764 | static int |
| 2765 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 2766 | { |
| 2767 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2768 | |
| 2769 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 2770 | init_new_proxy(&conf->agent_fe); |
| 2771 | conf->agent_fe.parent = conf->agent; |
| 2772 | conf->agent_fe.last_change = now.tv_sec; |
| 2773 | conf->agent_fe.id = conf->agent->id; |
| 2774 | conf->agent_fe.cap = PR_CAP_FE; |
| 2775 | conf->agent_fe.mode = PR_MODE_TCP; |
| 2776 | conf->agent_fe.maxconn = 0; |
| 2777 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 2778 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 2779 | conf->agent_fe.accept = frontend_accept; |
| 2780 | conf->agent_fe.srv = NULL; |
| 2781 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 2782 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 2783 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 2784 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2785 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2786 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2787 | sighandler_registered = 1; |
| 2788 | } |
| 2789 | |
| 2790 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | /* Free ressources allocated by the SPOE filter. */ |
| 2794 | static void |
| 2795 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 2796 | { |
| 2797 | struct spoe_config *conf = fconf->conf; |
| 2798 | |
| 2799 | if (conf) { |
| 2800 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2801 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2802 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2803 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2804 | free(conf); |
| 2805 | } |
| 2806 | fconf->conf = NULL; |
| 2807 | } |
| 2808 | |
| 2809 | /* Check configuration of a SPOE filter for a specified proxy. |
| 2810 | * Return 1 on error, else 0. */ |
| 2811 | static int |
| 2812 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 2813 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2814 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2815 | struct spoe_config *conf = fconf->conf; |
| 2816 | struct proxy *target; |
| 2817 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 2818 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 2819 | * are uniq */ |
| 2820 | list_for_each_entry(f, &px->filter_configs, list) { |
| 2821 | struct spoe_config *c = f->conf; |
| 2822 | |
| 2823 | /* This is not an SPOE filter */ |
| 2824 | if (f->id != spoe_filter_id) |
| 2825 | continue; |
| 2826 | /* This is the current SPOE filter */ |
| 2827 | if (f == fconf) |
| 2828 | continue; |
| 2829 | |
| 2830 | /* Check engine Id. It should be uniq */ |
| 2831 | if (!strcmp(conf->id, c->id)) { |
| 2832 | Alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 2833 | px->id, conf->id); |
| 2834 | return 1; |
| 2835 | } |
| 2836 | } |
| 2837 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2838 | target = proxy_be_by_name(conf->agent->b.name); |
| 2839 | if (target == NULL) { |
| 2840 | Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 2841 | " declared at %s:%d.\n", |
| 2842 | px->id, conf->agent->b.name, conf->agent->id, |
| 2843 | conf->agent->conf.file, conf->agent->conf.line); |
| 2844 | return 1; |
| 2845 | } |
| 2846 | if (target->mode != PR_MODE_TCP) { |
| 2847 | Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 2848 | " at %s:%d does not support HTTP mode.\n", |
| 2849 | px->id, target->id, conf->agent->id, |
| 2850 | conf->agent->conf.file, conf->agent->conf.line); |
| 2851 | return 1; |
| 2852 | } |
| 2853 | |
| 2854 | free(conf->agent->b.name); |
| 2855 | conf->agent->b.name = NULL; |
| 2856 | conf->agent->b.be = target; |
| 2857 | return 0; |
| 2858 | } |
| 2859 | |
| 2860 | /************************************************************************** |
| 2861 | * Hooks attached to a stream |
| 2862 | *************************************************************************/ |
| 2863 | /* Called when a filter instance is created and attach to a stream. It creates |
| 2864 | * the context that will be used to process this stream. */ |
| 2865 | static int |
| 2866 | spoe_start(struct stream *s, struct filter *filter) |
| 2867 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2868 | struct spoe_config *conf = FLT_CONF(filter); |
| 2869 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2870 | struct spoe_context *ctx; |
| 2871 | |
| 2872 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2873 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2874 | __FUNCTION__, s); |
| 2875 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2876 | ctx = spoe_create_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2877 | if (ctx == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2878 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2879 | " - failed to create SPOE context\n", |
| 2880 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 2881 | __FUNCTION__, s); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2882 | send_log(s->be, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2883 | "SPOE: [%s] failed to create SPOE context\n", |
| 2884 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2885 | return 0; |
| 2886 | } |
| 2887 | |
| 2888 | ctx->strm = s; |
| 2889 | ctx->state = SPOE_CTX_ST_READY; |
| 2890 | filter->ctx = ctx; |
| 2891 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2892 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2893 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 2894 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2895 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2896 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 2897 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2898 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2899 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 2900 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2901 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2902 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 2903 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2904 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2905 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 2906 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2907 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2908 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 2909 | |
| 2910 | return 1; |
| 2911 | } |
| 2912 | |
| 2913 | /* Called when a filter instance is detached from a stream. It release the |
| 2914 | * attached SPOE context. */ |
| 2915 | static void |
| 2916 | spoe_stop(struct stream *s, struct filter *filter) |
| 2917 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2918 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 2919 | (int)now.tv_sec, (int)now.tv_usec, |
| 2920 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2921 | __FUNCTION__, s); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2922 | spoe_destroy_context(filter->ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2925 | |
| 2926 | /* |
| 2927 | * Called when the stream is woken up because of expired timer. |
| 2928 | */ |
| 2929 | static void |
| 2930 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 2931 | { |
| 2932 | struct spoe_context *ctx = filter->ctx; |
| 2933 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2934 | if (tick_is_expired(ctx->process_exp, now_ms)) { |
| 2935 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2936 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2937 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2938 | } |
| 2939 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2940 | /* Called when we are ready to filter data on a channel */ |
| 2941 | static int |
| 2942 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 2943 | { |
| 2944 | struct spoe_context *ctx = filter->ctx; |
| 2945 | int ret = 1; |
| 2946 | |
| 2947 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 2948 | " - ctx-flags=0x%08x\n", |
| 2949 | (int)now.tv_sec, (int)now.tv_usec, |
| 2950 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2951 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 2952 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2953 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 2954 | goto out; |
| 2955 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2956 | if (!(chn->flags & CF_ISRESP)) { |
| 2957 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 2958 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 2959 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 2960 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 2961 | |
| 2962 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 2963 | goto out; |
| 2964 | |
| 2965 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2966 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2967 | if (!ret) |
| 2968 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2969 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 2970 | } |
| 2971 | else { |
| 2972 | if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP) |
| 2973 | chn->analysers |= AN_RES_INSPECT; |
| 2974 | |
| 2975 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 2976 | goto out; |
| 2977 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2978 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2979 | if (!ret) { |
| 2980 | channel_dont_read(chn); |
| 2981 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2982 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2983 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2984 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2988 | return ret; |
| 2989 | } |
| 2990 | |
| 2991 | /* Called before a processing happens on a given channel */ |
| 2992 | static int |
| 2993 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 2994 | struct channel *chn, unsigned an_bit) |
| 2995 | { |
| 2996 | struct spoe_context *ctx = filter->ctx; |
| 2997 | int ret = 1; |
| 2998 | |
| 2999 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3000 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3001 | (int)now.tv_sec, (int)now.tv_usec, |
| 3002 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3003 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3004 | ctx->flags, an_bit); |
| 3005 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3006 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3007 | goto out; |
| 3008 | |
| 3009 | switch (an_bit) { |
| 3010 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3011 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3012 | break; |
| 3013 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3014 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3015 | break; |
| 3016 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3017 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3018 | break; |
| 3019 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3020 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3021 | break; |
| 3022 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3023 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3024 | break; |
| 3025 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3026 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3027 | break; |
| 3028 | } |
| 3029 | |
| 3030 | out: |
| 3031 | if (!ret) { |
| 3032 | channel_dont_read(chn); |
| 3033 | channel_dont_close(chn); |
| 3034 | } |
| 3035 | return ret; |
| 3036 | } |
| 3037 | |
| 3038 | /* Called when the filtering on the channel ends. */ |
| 3039 | static int |
| 3040 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3041 | { |
| 3042 | struct spoe_context *ctx = filter->ctx; |
| 3043 | |
| 3044 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3045 | " - ctx-flags=0x%08x\n", |
| 3046 | (int)now.tv_sec, (int)now.tv_usec, |
| 3047 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3048 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3049 | |
| 3050 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3051 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | return 1; |
| 3055 | } |
| 3056 | |
| 3057 | /******************************************************************** |
| 3058 | * Functions that manage the filter initialization |
| 3059 | ********************************************************************/ |
| 3060 | struct flt_ops spoe_ops = { |
| 3061 | /* Manage SPOE filter, called for each filter declaration */ |
| 3062 | .init = spoe_init, |
| 3063 | .deinit = spoe_deinit, |
| 3064 | .check = spoe_check, |
| 3065 | |
| 3066 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3067 | .attach = spoe_start, |
| 3068 | .detach = spoe_stop, |
| 3069 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3070 | |
| 3071 | /* Handle channels activity */ |
| 3072 | .channel_start_analyze = spoe_start_analyze, |
| 3073 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3074 | .channel_end_analyze = spoe_end_analyze, |
| 3075 | }; |
| 3076 | |
| 3077 | |
| 3078 | static int |
| 3079 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3080 | { |
| 3081 | const char *err; |
| 3082 | int i, err_code = 0; |
| 3083 | |
| 3084 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3085 | (cfg_scope != NULL && curengine == NULL) || |
Christopher Faulet | e1405e5 | 2017-09-19 10:35:35 +0200 | [diff] [blame] | 3086 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3087 | goto out; |
| 3088 | |
| 3089 | if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */ |
| 3090 | if (!*args[1]) { |
| 3091 | Alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3092 | file, linenum); |
| 3093 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3094 | goto out; |
| 3095 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3096 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3097 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3098 | goto out; |
| 3099 | } |
| 3100 | |
| 3101 | err = invalid_char(args[1]); |
| 3102 | if (err) { |
| 3103 | Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3104 | file, linenum, *err, args[0], args[1]); |
| 3105 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3106 | goto out; |
| 3107 | } |
| 3108 | |
| 3109 | if (curagent != NULL) { |
| 3110 | Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3111 | file, linenum); |
| 3112 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3113 | goto out; |
| 3114 | } |
| 3115 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
| 3116 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3117 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3118 | goto out; |
| 3119 | } |
| 3120 | |
| 3121 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3122 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3123 | curagent->conf.file = strdup(file); |
| 3124 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3125 | |
| 3126 | curagent->timeout.hello = TICK_ETERNITY; |
| 3127 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3128 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3129 | |
| 3130 | curagent->engine_id = NULL; |
| 3131 | curagent->var_pfx = NULL; |
| 3132 | curagent->var_on_error = NULL; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3133 | curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3134 | curagent->cps_max = 0; |
| 3135 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3136 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3137 | curagent->min_applets = 0; |
| 3138 | curagent->max_fpa = 100; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3139 | |
| 3140 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3141 | LIST_INIT(&curagent->events[i]); |
| 3142 | LIST_INIT(&curagent->groups); |
| 3143 | LIST_INIT(&curagent->messages); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3144 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3145 | curagent->frame_size = curagent->max_frame_size; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3146 | curagent->applets_act = 0; |
| 3147 | curagent->applets_idle = 0; |
| 3148 | curagent->sending_rate = 0; |
| 3149 | |
| 3150 | LIST_INIT(&curagent->applets); |
| 3151 | LIST_INIT(&curagent->sending_queue); |
| 3152 | LIST_INIT(&curagent->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3153 | } |
| 3154 | else if (!strcmp(args[0], "use-backend")) { |
| 3155 | if (!*args[1]) { |
| 3156 | Alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3157 | file, linenum, args[0]); |
| 3158 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3159 | goto out; |
| 3160 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3161 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3162 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3163 | free(curagent->b.name); |
| 3164 | curagent->b.name = strdup(args[1]); |
| 3165 | } |
| 3166 | else if (!strcmp(args[0], "messages")) { |
| 3167 | int cur_arg = 1; |
| 3168 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3169 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3170 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3171 | list_for_each_entry(ph, &curmphs, list) { |
| 3172 | if (!strcmp(ph->id, args[cur_arg])) { |
| 3173 | Alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3174 | file, linenum, args[cur_arg]); |
| 3175 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3176 | goto out; |
| 3177 | } |
| 3178 | } |
| 3179 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3180 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3181 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3182 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3183 | goto out; |
| 3184 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3185 | ph->id = strdup(args[cur_arg]); |
| 3186 | LIST_ADDQ(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3187 | cur_arg++; |
| 3188 | } |
| 3189 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3190 | else if (!strcmp(args[0], "groups")) { |
| 3191 | int cur_arg = 1; |
| 3192 | while (*args[cur_arg]) { |
| 3193 | struct spoe_placeholder *ph = NULL; |
| 3194 | |
| 3195 | list_for_each_entry(ph, &curgphs, list) { |
| 3196 | if (!strcmp(ph->id, args[cur_arg])) { |
| 3197 | Alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3198 | file, linenum, args[cur_arg]); |
| 3199 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3200 | goto out; |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
| 3205 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3206 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3207 | goto out; |
| 3208 | } |
| 3209 | ph->id = strdup(args[cur_arg]); |
| 3210 | LIST_ADDQ(&curgphs, &ph->list); |
| 3211 | cur_arg++; |
| 3212 | } |
| 3213 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3214 | else if (!strcmp(args[0], "timeout")) { |
| 3215 | unsigned int *tv = NULL; |
| 3216 | const char *res; |
| 3217 | unsigned timeout; |
| 3218 | |
| 3219 | if (!*args[1]) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3220 | Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3221 | file, linenum); |
| 3222 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3223 | goto out; |
| 3224 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3225 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3226 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3227 | if (!strcmp(args[1], "hello")) |
| 3228 | tv = &curagent->timeout.hello; |
| 3229 | else if (!strcmp(args[1], "idle")) |
| 3230 | tv = &curagent->timeout.idle; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3231 | else if (!strcmp(args[1], "processing")) |
| 3232 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3233 | else { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3234 | 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] | 3235 | file, linenum, args[1]); |
| 3236 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3237 | goto out; |
| 3238 | } |
| 3239 | if (!*args[2]) { |
| 3240 | Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3241 | file, linenum, args[1]); |
| 3242 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3243 | goto out; |
| 3244 | } |
| 3245 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
| 3246 | if (res) { |
| 3247 | Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3248 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3249 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3250 | goto out; |
| 3251 | } |
| 3252 | *tv = MS_TO_TICKS(timeout); |
| 3253 | } |
| 3254 | else if (!strcmp(args[0], "option")) { |
| 3255 | if (!*args[1]) { |
| 3256 | Alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3257 | file, linenum, args[0]); |
| 3258 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3259 | goto out; |
| 3260 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3261 | |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3262 | if (!strcmp(args[1], "pipelining")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3263 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3264 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3265 | if (kwm == 1) |
| 3266 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3267 | else |
| 3268 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3269 | goto out; |
| 3270 | } |
| 3271 | else if (!strcmp(args[1], "async")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3272 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3273 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3274 | if (kwm == 1) |
| 3275 | curagent->flags &= ~SPOE_FL_ASYNC; |
| 3276 | else |
| 3277 | curagent->flags |= SPOE_FL_ASYNC; |
| 3278 | goto out; |
| 3279 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3280 | else if (!strcmp(args[1], "send-frag-payload")) { |
| 3281 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3282 | goto out; |
| 3283 | if (kwm == 1) |
| 3284 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3285 | else |
| 3286 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3287 | goto out; |
| 3288 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3289 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3290 | /* Following options does not support negation */ |
| 3291 | if (kwm == 1) { |
| 3292 | Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3293 | file, linenum, args[1]); |
| 3294 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3295 | goto out; |
| 3296 | } |
| 3297 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3298 | if (!strcmp(args[1], "var-prefix")) { |
| 3299 | char *tmp; |
| 3300 | |
| 3301 | if (!*args[2]) { |
| 3302 | Alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3303 | file, linenum, args[0], |
| 3304 | args[1]); |
| 3305 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3306 | goto out; |
| 3307 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3308 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3309 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3310 | tmp = args[2]; |
| 3311 | while (*tmp) { |
| 3312 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3313 | Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n", |
| 3314 | file, linenum, args[0], args[1]); |
| 3315 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3316 | goto out; |
| 3317 | } |
| 3318 | tmp++; |
| 3319 | } |
| 3320 | curagent->var_pfx = strdup(args[2]); |
| 3321 | } |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3322 | else if (!strcmp(args[1], "continue-on-error")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3323 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3324 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3325 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3326 | } |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3327 | else if (!strcmp(args[1], "set-on-error")) { |
| 3328 | char *tmp; |
| 3329 | |
| 3330 | if (!*args[2]) { |
| 3331 | Alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3332 | file, linenum, args[0], |
| 3333 | args[1]); |
| 3334 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3335 | goto out; |
| 3336 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3337 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3338 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3339 | tmp = args[2]; |
| 3340 | while (*tmp) { |
| 3341 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3342 | Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n", |
| 3343 | file, linenum, args[0], args[1]); |
| 3344 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3345 | goto out; |
| 3346 | } |
| 3347 | tmp++; |
| 3348 | } |
| 3349 | curagent->var_on_error = strdup(args[2]); |
| 3350 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3351 | else { |
| 3352 | Alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3353 | file, linenum, args[1]); |
| 3354 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3355 | goto out; |
| 3356 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3357 | } |
| 3358 | else if (!strcmp(args[0], "maxconnrate")) { |
| 3359 | if (!*args[1]) { |
| 3360 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3361 | file, linenum, args[0]); |
| 3362 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3363 | goto out; |
| 3364 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3365 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3366 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3367 | curagent->cps_max = atol(args[1]); |
| 3368 | } |
| 3369 | else if (!strcmp(args[0], "maxerrrate")) { |
| 3370 | if (!*args[1]) { |
| 3371 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3372 | file, linenum, args[0]); |
| 3373 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3374 | goto out; |
| 3375 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3376 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3377 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3378 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3379 | } |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3380 | else if (!strcmp(args[0], "max-frame-size")) { |
| 3381 | if (!*args[1]) { |
| 3382 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3383 | file, linenum, args[0]); |
| 3384 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3385 | goto out; |
| 3386 | } |
| 3387 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3388 | goto out; |
| 3389 | curagent->max_frame_size = atol(args[1]); |
| 3390 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3391 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
| 3392 | Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3393 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
| 3394 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3395 | goto out; |
| 3396 | } |
| 3397 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3398 | else if (*args[0]) { |
| 3399 | Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3400 | file, linenum, args[0]); |
| 3401 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3402 | goto out; |
| 3403 | } |
| 3404 | out: |
| 3405 | return err_code; |
| 3406 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3407 | static int |
| 3408 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3409 | { |
| 3410 | struct spoe_group *grp; |
| 3411 | const char *err; |
| 3412 | int err_code = 0; |
| 3413 | |
| 3414 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3415 | (cfg_scope != NULL && curengine == NULL) || |
| 3416 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
| 3417 | goto out; |
| 3418 | |
| 3419 | if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */ |
| 3420 | if (!*args[1]) { |
| 3421 | Alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3422 | file, linenum); |
| 3423 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3424 | goto out; |
| 3425 | } |
| 3426 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3427 | err_code |= ERR_ABORT; |
| 3428 | goto out; |
| 3429 | } |
| 3430 | |
| 3431 | err = invalid_char(args[1]); |
| 3432 | if (err) { |
| 3433 | Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3434 | file, linenum, *err, args[0], args[1]); |
| 3435 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3436 | goto out; |
| 3437 | } |
| 3438 | |
| 3439 | list_for_each_entry(grp, &curgrps, list) { |
| 3440 | if (!strcmp(grp->id, args[1])) { |
| 3441 | Alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3442 | " name as another one declared at %s:%d.\n", |
| 3443 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
| 3444 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3445 | goto out; |
| 3446 | } |
| 3447 | } |
| 3448 | |
| 3449 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
| 3450 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3451 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3452 | goto out; |
| 3453 | } |
| 3454 | |
| 3455 | curgrp->id = strdup(args[1]); |
| 3456 | curgrp->conf.file = strdup(file); |
| 3457 | curgrp->conf.line = linenum; |
| 3458 | LIST_INIT(&curgrp->phs); |
| 3459 | LIST_INIT(&curgrp->messages); |
| 3460 | LIST_ADDQ(&curgrps, &curgrp->list); |
| 3461 | } |
| 3462 | else if (!strcmp(args[0], "messages")) { |
| 3463 | int cur_arg = 1; |
| 3464 | while (*args[cur_arg]) { |
| 3465 | struct spoe_placeholder *ph = NULL; |
| 3466 | |
| 3467 | list_for_each_entry(ph, &curgrp->phs, list) { |
| 3468 | if (!strcmp(ph->id, args[cur_arg])) { |
| 3469 | Alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3470 | file, linenum, args[cur_arg]); |
| 3471 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3472 | goto out; |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
| 3477 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3478 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3479 | goto out; |
| 3480 | } |
| 3481 | ph->id = strdup(args[cur_arg]); |
| 3482 | LIST_ADDQ(&curgrp->phs, &ph->list); |
| 3483 | cur_arg++; |
| 3484 | } |
| 3485 | } |
| 3486 | else if (*args[0]) { |
| 3487 | Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3488 | file, linenum, args[0]); |
| 3489 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3490 | goto out; |
| 3491 | } |
| 3492 | out: |
| 3493 | return err_code; |
| 3494 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3495 | |
| 3496 | static int |
| 3497 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3498 | { |
| 3499 | struct spoe_message *msg; |
| 3500 | struct spoe_arg *arg; |
| 3501 | const char *err; |
| 3502 | char *errmsg = NULL; |
| 3503 | int err_code = 0; |
| 3504 | |
| 3505 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3506 | (cfg_scope != NULL && curengine == NULL) || |
Christopher Faulet | e1405e5 | 2017-09-19 10:35:35 +0200 | [diff] [blame] | 3507 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3508 | goto out; |
| 3509 | |
| 3510 | if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */ |
| 3511 | if (!*args[1]) { |
| 3512 | Alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3513 | file, linenum); |
| 3514 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3515 | goto out; |
| 3516 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3517 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3518 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3519 | goto out; |
| 3520 | } |
| 3521 | |
| 3522 | err = invalid_char(args[1]); |
| 3523 | if (err) { |
| 3524 | Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3525 | file, linenum, *err, args[0], args[1]); |
| 3526 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3527 | goto out; |
| 3528 | } |
| 3529 | |
| 3530 | list_for_each_entry(msg, &curmsgs, list) { |
| 3531 | if (!strcmp(msg->id, args[1])) { |
| 3532 | Alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3533 | " name as another one declared at %s:%d.\n", |
| 3534 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
| 3535 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3536 | goto out; |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
| 3541 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3542 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3543 | goto out; |
| 3544 | } |
| 3545 | |
| 3546 | curmsg->id = strdup(args[1]); |
| 3547 | curmsg->id_len = strlen(curmsg->id); |
| 3548 | curmsg->event = SPOE_EV_NONE; |
| 3549 | curmsg->conf.file = strdup(file); |
| 3550 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3551 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3552 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3553 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3554 | LIST_INIT(&curmsg->by_evt); |
| 3555 | LIST_INIT(&curmsg->by_grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3556 | LIST_ADDQ(&curmsgs, &curmsg->list); |
| 3557 | } |
| 3558 | else if (!strcmp(args[0], "args")) { |
| 3559 | int cur_arg = 1; |
| 3560 | |
| 3561 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3562 | curproxy->conf.args.file = file; |
| 3563 | curproxy->conf.args.line = linenum; |
| 3564 | while (*args[cur_arg]) { |
| 3565 | char *delim = strchr(args[cur_arg], '='); |
| 3566 | int idx = 0; |
| 3567 | |
| 3568 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
| 3569 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3570 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3571 | goto out; |
| 3572 | } |
| 3573 | |
| 3574 | if (!delim) { |
| 3575 | arg->name = NULL; |
| 3576 | arg->name_len = 0; |
| 3577 | delim = args[cur_arg]; |
| 3578 | } |
| 3579 | else { |
| 3580 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3581 | arg->name_len = delim - args[cur_arg]; |
| 3582 | delim++; |
| 3583 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3584 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3585 | &idx, file, linenum, &errmsg, |
| 3586 | &curproxy->conf.args); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3587 | if (arg->expr == NULL) { |
| 3588 | Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg); |
| 3589 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3590 | free(arg->name); |
| 3591 | free(arg); |
| 3592 | goto out; |
| 3593 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3594 | curmsg->nargs++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3595 | LIST_ADDQ(&curmsg->args, &arg->list); |
| 3596 | cur_arg++; |
| 3597 | } |
| 3598 | curproxy->conf.args.file = NULL; |
| 3599 | curproxy->conf.args.line = 0; |
| 3600 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3601 | else if (!strcmp(args[0], "acl")) { |
| 3602 | err = invalid_char(args[1]); |
| 3603 | if (err) { |
| 3604 | Alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 3605 | file, linenum, *err, args[1]); |
| 3606 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3607 | goto out; |
| 3608 | } |
| 3609 | if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) { |
| 3610 | Alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 3611 | file, linenum, args[1], errmsg); |
| 3612 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3613 | goto out; |
| 3614 | } |
| 3615 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3616 | else if (!strcmp(args[0], "event")) { |
| 3617 | if (!*args[1]) { |
| 3618 | Alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3619 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3620 | goto out; |
| 3621 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3622 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 3623 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3624 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3625 | if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS])) |
| 3626 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
| 3627 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS])) |
| 3628 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 3629 | |
| 3630 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE])) |
| 3631 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 3632 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE])) |
| 3633 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
| 3634 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP])) |
| 3635 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 3636 | |
| 3637 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE])) |
| 3638 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 3639 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE])) |
| 3640 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
| 3641 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP])) |
| 3642 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 3643 | else { |
| 3644 | Alert("parsing [%s:%d] : unkown event '%s'.\n", |
| 3645 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3646 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3647 | goto out; |
| 3648 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3649 | |
| 3650 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 3651 | struct acl_cond *cond; |
| 3652 | |
| 3653 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 3654 | curproxy, (const char **)args+2, |
| 3655 | &errmsg); |
| 3656 | if (cond == NULL) { |
| 3657 | Alert("parsing [%s:%d] : error detected while " |
| 3658 | "parsing an 'event %s' condition : %s.\n", |
| 3659 | file, linenum, args[1], errmsg); |
| 3660 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3661 | goto out; |
| 3662 | } |
| 3663 | curmsg->cond = cond; |
| 3664 | } |
| 3665 | else if (*args[2]) { |
| 3666 | Alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 3667 | "or 'unless' followed by a condition but found '%s'.\n", |
| 3668 | file, linenum, args[1], args[2]); |
| 3669 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3670 | goto out; |
| 3671 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3672 | } |
| 3673 | else if (!*args[0]) { |
| 3674 | Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 3675 | file, linenum, args[0]); |
| 3676 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3677 | goto out; |
| 3678 | } |
| 3679 | out: |
| 3680 | free(errmsg); |
| 3681 | return err_code; |
| 3682 | } |
| 3683 | |
| 3684 | /* Return -1 on error, else 0 */ |
| 3685 | static int |
| 3686 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 3687 | struct flt_conf *fconf, char **err, void *private) |
| 3688 | { |
| 3689 | struct list backup_sections; |
| 3690 | struct spoe_config *conf; |
| 3691 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3692 | struct spoe_group *grp, *grpback; |
| 3693 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3694 | char *file = NULL, *engine = NULL; |
| 3695 | int ret, pos = *cur_arg + 1; |
| 3696 | |
| 3697 | conf = calloc(1, sizeof(*conf)); |
| 3698 | if (conf == NULL) { |
| 3699 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 3700 | goto error; |
| 3701 | } |
| 3702 | conf->proxy = px; |
| 3703 | |
| 3704 | while (*args[pos]) { |
| 3705 | if (!strcmp(args[pos], "config")) { |
| 3706 | if (!*args[pos+1]) { |
| 3707 | memprintf(err, "'%s' : '%s' option without value", |
| 3708 | args[*cur_arg], args[pos]); |
| 3709 | goto error; |
| 3710 | } |
| 3711 | file = args[pos+1]; |
| 3712 | pos += 2; |
| 3713 | } |
| 3714 | else if (!strcmp(args[pos], "engine")) { |
| 3715 | if (!*args[pos+1]) { |
| 3716 | memprintf(err, "'%s' : '%s' option without value", |
| 3717 | args[*cur_arg], args[pos]); |
| 3718 | goto error; |
| 3719 | } |
| 3720 | engine = args[pos+1]; |
| 3721 | pos += 2; |
| 3722 | } |
| 3723 | else { |
| 3724 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 3725 | goto error; |
| 3726 | } |
| 3727 | } |
| 3728 | if (file == NULL) { |
| 3729 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 3730 | goto error; |
| 3731 | } |
| 3732 | |
| 3733 | /* backup sections and register SPOE sections */ |
| 3734 | LIST_INIT(&backup_sections); |
| 3735 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3736 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 3737 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 3738 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3739 | |
| 3740 | /* Parse SPOE filter configuration file */ |
| 3741 | curengine = engine; |
| 3742 | curproxy = px; |
| 3743 | curagent = NULL; |
| 3744 | curmsg = NULL; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3745 | LIST_INIT(&curmsgs); |
| 3746 | LIST_INIT(&curgrps); |
| 3747 | LIST_INIT(&curmphs); |
| 3748 | LIST_INIT(&curgphs); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3749 | ret = readcfgfile(file); |
| 3750 | curproxy = NULL; |
| 3751 | |
| 3752 | /* unregister SPOE sections and restore previous sections */ |
| 3753 | cfg_unregister_sections(); |
| 3754 | cfg_restore_sections(&backup_sections); |
| 3755 | |
| 3756 | if (ret == -1) { |
| 3757 | memprintf(err, "Could not open configuration file %s : %s", |
| 3758 | file, strerror(errno)); |
| 3759 | goto error; |
| 3760 | } |
| 3761 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 3762 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 3763 | goto error; |
| 3764 | } |
| 3765 | |
| 3766 | /* Check SPOE agent */ |
| 3767 | if (curagent == NULL) { |
| 3768 | memprintf(err, "No SPOE agent found in file %s", file); |
| 3769 | goto error; |
| 3770 | } |
| 3771 | if (curagent->b.name == NULL) { |
| 3772 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 3773 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 3774 | goto error; |
| 3775 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3776 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 3777 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3778 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3779 | Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 3780 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 3781 | " | 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] | 3782 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3783 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3784 | } |
| 3785 | if (curagent->var_pfx == NULL) { |
| 3786 | char *tmp = curagent->id; |
| 3787 | |
| 3788 | while (*tmp) { |
| 3789 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3790 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 3791 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 3792 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3793 | goto error; |
| 3794 | } |
| 3795 | tmp++; |
| 3796 | } |
| 3797 | curagent->var_pfx = strdup(curagent->id); |
| 3798 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3799 | if (curagent->engine_id == NULL) |
| 3800 | curagent->engine_id = generate_pseudo_uuid(); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3801 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3802 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
| 3803 | 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] | 3804 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3805 | goto finish; |
| 3806 | } |
| 3807 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3808 | /* Replace placeholders by the corresponding messages for the SPOE |
| 3809 | * agent */ |
| 3810 | list_for_each_entry(ph, &curmphs, list) { |
| 3811 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3812 | struct spoe_arg *arg; |
| 3813 | unsigned int where; |
| 3814 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3815 | if (!strcmp(msg->id, ph->id)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3816 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 3817 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 3818 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 3819 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 3820 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 3821 | } |
| 3822 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 3823 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 3824 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
| 3825 | Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 3826 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3827 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3828 | } |
| 3829 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3830 | Warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 3831 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3832 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3833 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3834 | |
| 3835 | where = 0; |
| 3836 | switch (msg->event) { |
| 3837 | case SPOE_EV_ON_CLIENT_SESS: |
| 3838 | where |= SMP_VAL_FE_CON_ACC; |
| 3839 | break; |
| 3840 | |
| 3841 | case SPOE_EV_ON_TCP_REQ_FE: |
| 3842 | where |= SMP_VAL_FE_REQ_CNT; |
| 3843 | break; |
| 3844 | |
| 3845 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 3846 | where |= SMP_VAL_FE_HRQ_HDR; |
| 3847 | break; |
| 3848 | |
| 3849 | case SPOE_EV_ON_TCP_REQ_BE: |
| 3850 | if (px->cap & PR_CAP_FE) |
| 3851 | where |= SMP_VAL_FE_REQ_CNT; |
| 3852 | if (px->cap & PR_CAP_BE) |
| 3853 | where |= SMP_VAL_BE_REQ_CNT; |
| 3854 | break; |
| 3855 | |
| 3856 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 3857 | if (px->cap & PR_CAP_FE) |
| 3858 | where |= SMP_VAL_FE_HRQ_HDR; |
| 3859 | if (px->cap & PR_CAP_BE) |
| 3860 | where |= SMP_VAL_BE_HRQ_HDR; |
| 3861 | break; |
| 3862 | |
| 3863 | case SPOE_EV_ON_SERVER_SESS: |
| 3864 | where |= SMP_VAL_BE_SRV_CON; |
| 3865 | break; |
| 3866 | |
| 3867 | case SPOE_EV_ON_TCP_RSP: |
| 3868 | if (px->cap & PR_CAP_FE) |
| 3869 | where |= SMP_VAL_FE_RES_CNT; |
| 3870 | if (px->cap & PR_CAP_BE) |
| 3871 | where |= SMP_VAL_BE_RES_CNT; |
| 3872 | break; |
| 3873 | |
| 3874 | case SPOE_EV_ON_HTTP_RSP: |
| 3875 | if (px->cap & PR_CAP_FE) |
| 3876 | where |= SMP_VAL_FE_HRS_HDR; |
| 3877 | if (px->cap & PR_CAP_BE) |
| 3878 | where |= SMP_VAL_BE_HRS_HDR; |
| 3879 | break; |
| 3880 | |
| 3881 | default: |
| 3882 | break; |
| 3883 | } |
| 3884 | |
| 3885 | list_for_each_entry(arg, &msg->args, list) { |
| 3886 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3887 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3888 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3889 | "none of which is available here ('%s')", |
| 3890 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3891 | sample_ckp_names(arg->expr->fetch->use), |
| 3892 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 3893 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3897 | msg->agent = curagent; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3898 | LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt); |
| 3899 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | 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] | 3903 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3904 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3905 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3906 | continue; |
| 3907 | } |
| 3908 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3909 | /* Replace placeholders by the corresponding groups for the SPOE |
| 3910 | * agent */ |
| 3911 | list_for_each_entry(ph, &curgphs, list) { |
| 3912 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 3913 | if (!strcmp(grp->id, ph->id)) { |
| 3914 | grp->agent = curagent; |
| 3915 | LIST_DEL(&grp->list); |
| 3916 | LIST_ADDQ(&curagent->groups, &grp->list); |
| 3917 | goto next_aph; |
| 3918 | } |
| 3919 | } |
| 3920 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 3921 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 3922 | goto error; |
| 3923 | next_aph: |
| 3924 | continue; |
| 3925 | } |
| 3926 | |
| 3927 | /* Replace placeholders by the corresponding message for each SPOE |
| 3928 | * group of the SPOE agent */ |
| 3929 | list_for_each_entry(grp, &curagent->groups, list) { |
| 3930 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 3931 | list_for_each_entry(msg, &curmsgs, list) { |
| 3932 | if (!strcmp(msg->id, ph->id)) { |
| 3933 | if (msg->group != NULL) { |
| 3934 | memprintf(err, "SPOE message '%s' already belongs to " |
| 3935 | "the SPOE group '%s' declare at %s:%d", |
| 3936 | msg->id, msg->group->id, |
| 3937 | msg->group->conf.file, |
| 3938 | msg->group->conf.line); |
| 3939 | goto error; |
| 3940 | } |
| 3941 | |
| 3942 | /* Scope for arguments are not checked for now. We will check |
| 3943 | * them only if a rule use the corresponding SPOE group. */ |
| 3944 | msg->agent = curagent; |
| 3945 | msg->group = grp; |
| 3946 | LIST_DEL(&ph->list); |
| 3947 | LIST_ADDQ(&grp->messages, &msg->by_grp); |
| 3948 | goto next_mph_grp; |
| 3949 | } |
| 3950 | } |
| 3951 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 3952 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 3953 | goto error; |
| 3954 | next_mph_grp: |
| 3955 | continue; |
| 3956 | } |
| 3957 | } |
| 3958 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3959 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3960 | /* move curmsgs to the agent message list */ |
| 3961 | curmsgs.n->p = &curagent->messages; |
| 3962 | curmsgs.p->n = &curagent->messages; |
| 3963 | curagent->messages = curmsgs; |
| 3964 | LIST_INIT(&curmsgs); |
| 3965 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3966 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3967 | conf->agent = curagent; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3968 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 3969 | LIST_DEL(&ph->list); |
| 3970 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3971 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3972 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 3973 | LIST_DEL(&ph->list); |
| 3974 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3975 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3976 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 3977 | LIST_DEL(&grp->list); |
| 3978 | spoe_release_group(grp); |
| 3979 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3980 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3981 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3982 | fconf->ops = &spoe_ops; |
| 3983 | fconf->conf = conf; |
| 3984 | return 0; |
| 3985 | |
| 3986 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3987 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3988 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 3989 | LIST_DEL(&ph->list); |
| 3990 | spoe_release_placeholder(ph); |
| 3991 | } |
| 3992 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 3993 | LIST_DEL(&ph->list); |
| 3994 | spoe_release_placeholder(ph); |
| 3995 | } |
| 3996 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 3997 | LIST_DEL(&grp->list); |
| 3998 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3999 | } |
| 4000 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
| 4001 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4002 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4003 | } |
| 4004 | free(conf); |
| 4005 | return -1; |
| 4006 | } |
| 4007 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4008 | /* Send a SPOE group. TODO */ |
| 4009 | static enum act_return |
| 4010 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4011 | struct session *sess, struct stream *s, int flags) |
| 4012 | { |
| 4013 | struct filter *filter; |
| 4014 | struct spoe_agent *agent = NULL; |
| 4015 | struct spoe_group *group = NULL; |
| 4016 | struct spoe_context *ctx = NULL; |
| 4017 | int ret, dir; |
| 4018 | |
| 4019 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4020 | if (filter->config == rule->arg.act.p[0]) { |
| 4021 | agent = rule->arg.act.p[2]; |
| 4022 | group = rule->arg.act.p[3]; |
| 4023 | ctx = filter->ctx; |
| 4024 | break; |
| 4025 | } |
| 4026 | } |
| 4027 | if (agent == NULL || group == NULL || ctx == NULL) |
| 4028 | return ACT_RET_ERR; |
| 4029 | |
| 4030 | /* TODO */ |
| 4031 | return ACT_RET_CONT; |
| 4032 | } |
| 4033 | |
| 4034 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4035 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4036 | * action should be: |
| 4037 | * |
| 4038 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4039 | * |
| 4040 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4041 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4042 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4043 | * group. |
| 4044 | * |
| 4045 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4046 | * filled. |
| 4047 | */ |
| 4048 | static int |
| 4049 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4050 | { |
| 4051 | struct flt_conf *fconf; |
| 4052 | struct spoe_config *conf; |
| 4053 | struct spoe_agent *agent = NULL; |
| 4054 | struct spoe_group *group; |
| 4055 | struct spoe_message *msg; |
| 4056 | char *engine_id = rule->arg.act.p[0]; |
| 4057 | char *group_id = rule->arg.act.p[1]; |
| 4058 | unsigned int where = 0; |
| 4059 | |
| 4060 | switch (rule->from) { |
| 4061 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4062 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4063 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4064 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4065 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4066 | default: |
| 4067 | memprintf(err, |
| 4068 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4069 | rule->from); |
| 4070 | goto error; |
| 4071 | } |
| 4072 | |
| 4073 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4074 | * <px> */ |
| 4075 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4076 | conf = fconf->conf; |
| 4077 | |
| 4078 | /* This is not an SPOE filter */ |
| 4079 | if (fconf->id != spoe_filter_id) |
| 4080 | continue; |
| 4081 | |
| 4082 | /* This is the good engine */ |
| 4083 | if (!strcmp(conf->id, engine_id)) { |
| 4084 | agent = conf->agent; |
| 4085 | break; |
| 4086 | } |
| 4087 | } |
| 4088 | if (agent == NULL) { |
| 4089 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4090 | engine_id, group_id); |
| 4091 | goto error; |
| 4092 | } |
| 4093 | |
| 4094 | /* Try to find the right group */ |
| 4095 | list_for_each_entry(group, &agent->groups, list) { |
| 4096 | /* This is the good group */ |
| 4097 | if (!strcmp(group->id, group_id)) |
| 4098 | break; |
| 4099 | } |
| 4100 | if (&group->list == &agent->groups) { |
| 4101 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4102 | group_id, engine_id); |
| 4103 | goto error; |
| 4104 | } |
| 4105 | |
| 4106 | /* Ok, we found the group, we need to check messages and their |
| 4107 | * arguments */ |
| 4108 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4109 | struct spoe_arg *arg; |
| 4110 | |
| 4111 | list_for_each_entry(arg, &msg->args, list) { |
| 4112 | if (!(arg->expr->fetch->val & where)) { |
| 4113 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4114 | "some args extract information from '%s'," |
| 4115 | "none of which is available here ('%s')", |
| 4116 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4117 | sample_ckp_names(arg->expr->fetch->use), |
| 4118 | sample_ckp_names(where)); |
| 4119 | goto error; |
| 4120 | } |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | free(engine_id); |
| 4125 | free(group_id); |
| 4126 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4127 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4128 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4129 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4130 | return 1; |
| 4131 | |
| 4132 | error: |
| 4133 | free(engine_id); |
| 4134 | free(group_id); |
| 4135 | return 0; |
| 4136 | } |
| 4137 | |
| 4138 | /* Parse 'send-spoe-group' action following the format: |
| 4139 | * |
| 4140 | * ... send-spoe-group <engine-id> <group-id> |
| 4141 | * |
| 4142 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4143 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4144 | * ids are saved and used later, when the rule will be checked. |
| 4145 | */ |
| 4146 | static enum act_parse_ret |
| 4147 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4148 | struct act_rule *rule, char **err) |
| 4149 | { |
| 4150 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4151 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4152 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4153 | return ACT_RET_PRS_ERR; |
| 4154 | } |
| 4155 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4156 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4157 | |
| 4158 | (*orig_arg) += 2; |
| 4159 | |
| 4160 | rule->action = ACT_CUSTOM; |
| 4161 | rule->action_ptr = spoe_send_group; |
| 4162 | rule->check_ptr = check_send_spoe_group; |
| 4163 | return ACT_RET_PRS_OK; |
| 4164 | } |
| 4165 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4166 | |
| 4167 | /* Declare the filter parser for "spoe" keyword */ |
| 4168 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4169 | { "spoe", parse_spoe_flt, NULL }, |
| 4170 | { NULL, NULL, NULL }, |
| 4171 | } |
| 4172 | }; |
| 4173 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4174 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4175 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4176 | { "send-spoe-group", parse_send_spoe_group }, |
| 4177 | { /* END */ }, |
| 4178 | } |
| 4179 | }; |
| 4180 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4181 | { "send-spoe-group", parse_send_spoe_group }, |
| 4182 | { /* END */ }, |
| 4183 | } |
| 4184 | }; |
| 4185 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4186 | { "send-spoe-group", parse_send_spoe_group }, |
| 4187 | { /* END */ }, |
| 4188 | } |
| 4189 | }; |
| 4190 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4191 | { "send-spoe-group", parse_send_spoe_group }, |
| 4192 | { /* END */ }, |
| 4193 | } |
| 4194 | }; |
| 4195 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4196 | __attribute__((constructor)) |
| 4197 | static void __spoe_init(void) |
| 4198 | { |
| 4199 | flt_register_keywords(&flt_kws); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4200 | tcp_req_cont_keywords_register(&tcp_req_action_kws); |
| 4201 | tcp_res_cont_keywords_register(&tcp_res_action_kws); |
| 4202 | http_req_keywords_register(&http_req_action_kws); |
| 4203 | http_res_keywords_register(&http_res_action_kws); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4204 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4205 | 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] | 4206 | 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] | 4207 | } |
| 4208 | |
| 4209 | __attribute__((destructor)) |
| 4210 | static void |
| 4211 | __spoe_deinit(void) |
| 4212 | { |
| 4213 | pool_destroy2(pool2_spoe_ctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 4214 | pool_destroy2(pool2_spoe_appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4215 | } |