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