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