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