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