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