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; |
| 1690 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1691 | int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1692 | |
| 1693 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1694 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1695 | goto exit; |
| 1696 | } |
| 1697 | |
| 1698 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1699 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1700 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1701 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1702 | goto next; |
| 1703 | } |
| 1704 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1705 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1706 | " - 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] | 1707 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1708 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1709 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1710 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1711 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1712 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1713 | skip_sending = 1; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1714 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1715 | /* receiving_frame loop */ |
| 1716 | while (!skip_receiving) { |
| 1717 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1718 | switch (ret) { |
| 1719 | case -1: /* error */ |
| 1720 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1721 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1722 | case 0: /* ignore */ |
| 1723 | active_r = 1; |
| 1724 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1725 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1726 | case 1: /* retry */ |
| 1727 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1728 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1729 | default: |
| 1730 | active_r = 1; |
| 1731 | break; |
| 1732 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1733 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1734 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1735 | /* send_frame loop */ |
| 1736 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1737 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1738 | switch (ret) { |
| 1739 | case -1: /* error */ |
| 1740 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1741 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1742 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1743 | if (SPOE_APPCTX(appctx)->node.key) |
| 1744 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1745 | active_s++; |
| 1746 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1747 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1748 | case 1: /* retry */ |
| 1749 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1750 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1751 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1752 | if (SPOE_APPCTX(appctx)->node.key) |
| 1753 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1754 | active_s++; |
| 1755 | break; |
| 1756 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1757 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1758 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1759 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1760 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1761 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1762 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1763 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1764 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
Christopher Faulet | e7c50f1 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1765 | struct server *srv = objt_server(si_strm(si)->target); |
| 1766 | |
| 1767 | /* With several threads, close the applet if there are pending |
| 1768 | * connections or if the server is full. Otherwise, add the |
| 1769 | * applet in the idle list. |
| 1770 | */ |
| 1771 | if (global.nbthread > 1 && |
| 1772 | (agent->b.be->nbpend || |
| 1773 | (srv && (srv->nbpend || (srv->maxconn && srv->served >=srv_dynamic_maxconn(srv)))))) { |
| 1774 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1775 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1776 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1777 | goto next; |
| 1778 | } |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1779 | _HA_ATOMIC_ADD(&agent->counters.idles, 1); |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1780 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1781 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1782 | } |
| 1783 | return 1; |
| 1784 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1785 | next: |
| 1786 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1787 | return 0; |
| 1788 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1789 | exit: |
| 1790 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1795 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1796 | { |
| 1797 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1798 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1799 | char *frame, *buf; |
| 1800 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1801 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1802 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1803 | goto exit; |
| 1804 | |
| 1805 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1806 | goto exit; |
| 1807 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1808 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1809 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1810 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1811 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1812 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1813 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1814 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1815 | |
| 1816 | switch (ret) { |
| 1817 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1818 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1819 | goto exit; |
| 1820 | |
| 1821 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1822 | goto stop; |
| 1823 | |
| 1824 | default: |
| 1825 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1826 | " - disconnected by HAProxy (%d): %s\n", |
| 1827 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1828 | __FUNCTION__, appctx, |
| 1829 | SPOE_APPCTX(appctx)->status_code, |
| 1830 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1831 | |
| 1832 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1833 | goto next; |
| 1834 | } |
| 1835 | |
| 1836 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1837 | SPOE_APPCTX(appctx)->task->expire = |
| 1838 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1839 | return 0; |
| 1840 | stop: |
| 1841 | return 1; |
| 1842 | exit: |
| 1843 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1848 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1849 | { |
| 1850 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1851 | char *frame; |
| 1852 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1853 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1854 | 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] | 1855 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1856 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1857 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1858 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1859 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1860 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1861 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1862 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1863 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1864 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1865 | ret = spoe_recv_frame(appctx, frame, |
| 1866 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1867 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1868 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1869 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | switch (ret) { |
| 1873 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1874 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1875 | " - error on frame (%s)\n", |
| 1876 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1877 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1878 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1879 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1880 | goto exit; |
| 1881 | |
| 1882 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1883 | goto next; |
| 1884 | |
| 1885 | case 1: /* retry */ |
| 1886 | goto stop; |
| 1887 | |
| 1888 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1889 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1890 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 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 | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1893 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1894 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1895 | goto exit; |
| 1896 | } |
| 1897 | |
| 1898 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1899 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1900 | if (trash.data) |
| 1901 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1902 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1903 | return 0; |
| 1904 | stop: |
| 1905 | return 1; |
| 1906 | exit: |
| 1907 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
| 1911 | /* I/O Handler processing messages exchanged with the agent */ |
| 1912 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1913 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1914 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1915 | struct stream_interface *si = appctx->owner; |
| 1916 | struct spoe_agent *agent; |
| 1917 | |
| 1918 | if (SPOE_APPCTX(appctx) == NULL) |
| 1919 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1920 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1921 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1922 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1923 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1924 | switchstate: |
| 1925 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1926 | " - appctx-state=%s\n", |
| 1927 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1928 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1929 | |
| 1930 | switch (appctx->st0) { |
| 1931 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1932 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1933 | goto out; |
| 1934 | goto switchstate; |
| 1935 | |
| 1936 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1937 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1938 | goto out; |
| 1939 | goto switchstate; |
| 1940 | |
| 1941 | case SPOE_APPCTX_ST_IDLE: |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1942 | _HA_ATOMIC_SUB(&agent->counters.idles, 1); |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1943 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1944 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1945 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1946 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1947 | SPOE_APPCTX(appctx)->task->expire = |
| 1948 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1949 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1950 | goto switchstate; |
| 1951 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1952 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1953 | /* fall through */ |
| 1954 | |
| 1955 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1956 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1957 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1958 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1959 | goto out; |
| 1960 | goto switchstate; |
| 1961 | |
| 1962 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1963 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1964 | goto out; |
| 1965 | goto switchstate; |
| 1966 | |
| 1967 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1968 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1969 | goto out; |
| 1970 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1971 | |
| 1972 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1973 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1974 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1975 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1976 | si_shutw(si); |
| 1977 | si_shutr(si); |
| 1978 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1979 | /* fall through */ |
| 1980 | |
| 1981 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1982 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1983 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1984 | out: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1985 | if (stopping) |
| 1986 | spoe_wakeup_appctx(appctx); |
| 1987 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1988 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 1989 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | struct applet spoe_applet = { |
| 1993 | .obj_type = OBJ_TYPE_APPLET, |
| 1994 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1995 | .fct = spoe_handle_appctx, |
| 1996 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1997 | }; |
| 1998 | |
| 1999 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 2000 | * NULL. */ |
| 2001 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2002 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2003 | { |
| 2004 | struct appctx *appctx; |
| 2005 | struct session *sess; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2006 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2007 | |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 2008 | if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2009 | goto out_error; |
| 2010 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2011 | appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2012 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2013 | goto out_free_appctx; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2014 | memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2015 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2016 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2017 | if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2018 | goto out_free_spoe_appctx; |
| 2019 | |
| 2020 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2021 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2022 | SPOE_APPCTX(appctx)->task->context = appctx; |
| 2023 | SPOE_APPCTX(appctx)->agent = conf->agent; |
| 2024 | SPOE_APPCTX(appctx)->version = 0; |
| 2025 | SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size; |
| 2026 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2027 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2028 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 2029 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2030 | |
| 2031 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
| 2032 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2033 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2034 | |
| 2035 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 2036 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2037 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 2038 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2039 | if (!sess) |
| 2040 | goto out_free_spoe; |
| 2041 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2042 | if ((strm = stream_new(sess, &appctx->obj_type)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2043 | goto out_free_sess; |
| 2044 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2045 | stream_set_backend(strm, conf->agent->b.be); |
| 2046 | |
| 2047 | /* applet is waiting for data */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2048 | si_cant_get(&strm->si[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2049 | appctx_wakeup(appctx); |
| 2050 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2051 | strm->do_log = NULL; |
| 2052 | strm->res.flags |= CF_READ_DONTWAIT; |
| 2053 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2054 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2055 | LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2056 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2057 | _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1); |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 2058 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2059 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2060 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2061 | return appctx; |
| 2062 | |
| 2063 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2064 | out_free_sess: |
| 2065 | session_free(sess); |
| 2066 | out_free_spoe: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2067 | task_destroy(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2068 | out_free_spoe_appctx: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2069 | pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2070 | out_free_appctx: |
| 2071 | appctx_free(appctx); |
| 2072 | out_error: |
| 2073 | return NULL; |
| 2074 | } |
| 2075 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2076 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2077 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2078 | { |
| 2079 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2080 | struct spoe_agent *agent = conf->agent; |
| 2081 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2082 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2083 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2084 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2085 | if (!eb_is_empty(&agent->rt[tid].idle_applets) && |
Christopher Faulet | d6cd2b3 | 2020-06-22 15:32:14 +0200 | [diff] [blame] | 2086 | (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] | 2087 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2088 | |
| 2089 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2090 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2091 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2092 | ctx->strm); |
| 2093 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2094 | /* Do not try to create a new applet if there is no server up for the |
| 2095 | * agent's backend. */ |
| 2096 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2097 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2098 | " - cannot create SPOE appctx: no server up\n", |
| 2099 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2100 | __FUNCTION__, ctx->strm); |
| 2101 | goto end; |
| 2102 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2103 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2104 | /* Do not try to create a new applet if we have reached the maximum of |
| 2105 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2106 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2107 | 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] | 2108 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2109 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2110 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2111 | __FUNCTION__, ctx->strm); |
| 2112 | goto end; |
| 2113 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2114 | } |
| 2115 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2116 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2117 | if (appctx == NULL) { |
| 2118 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2119 | " - failed to create SPOE appctx\n", |
| 2120 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2121 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 2122 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2123 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2124 | agent->id); |
| 2125 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2126 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2127 | } |
| 2128 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2129 | /* Increase the per-process number of cumulated connections */ |
| 2130 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2131 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2132 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2133 | end: |
| 2134 | /* 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] | 2135 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2136 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2137 | return -1; |
| 2138 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2139 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2140 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2141 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2142 | * it. */ |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2143 | _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2144 | spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request); |
| 2145 | ctx->stats.tv_queue = now; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2146 | if (ctx->spoe_appctx) |
| 2147 | return 1; |
| 2148 | LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2149 | |
| 2150 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2151 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2152 | " - applets=%u - idles=%u - processing=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2153 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2154 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2155 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2156 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2157 | /* Finally try to wakeup an IDLE applet. */ |
| 2158 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2159 | struct eb32_node *node; |
| 2160 | |
| 2161 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2162 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2163 | if (node && spoe_appctx) { |
| 2164 | eb32_delete(&spoe_appctx->node); |
| 2165 | spoe_appctx->node.key++; |
| 2166 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2167 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2168 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2169 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2170 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2174 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2175 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2176 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2177 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2178 | * the message is too big, it returns -1.*/ |
| 2179 | static int |
| 2180 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2181 | struct spoe_message *msg, int dir, |
| 2182 | char **buf, char *end) |
| 2183 | { |
| 2184 | struct sample *smp; |
| 2185 | struct spoe_arg *arg; |
| 2186 | int ret; |
| 2187 | |
| 2188 | if (msg->cond) { |
| 2189 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2190 | ret = acl_pass(ret); |
| 2191 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2192 | ret = !ret; |
| 2193 | |
| 2194 | /* the rule does not match */ |
| 2195 | if (!ret) |
| 2196 | goto next; |
| 2197 | } |
| 2198 | |
| 2199 | /* Resume encoding of a SPOE argument */ |
| 2200 | if (ctx->frag_ctx.curarg != NULL) { |
| 2201 | arg = ctx->frag_ctx.curarg; |
| 2202 | goto encode_argument; |
| 2203 | } |
| 2204 | |
| 2205 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2206 | goto encode_msg_payload; |
| 2207 | |
| 2208 | /* Check if there is enough space for the message name and the |
| 2209 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2210 | * bytes, at most (< 2288). */ |
| 2211 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2212 | goto too_big; |
| 2213 | |
| 2214 | /* Encode the message name */ |
| 2215 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2216 | goto too_big; |
| 2217 | |
| 2218 | /* Set the number of arguments for this message */ |
| 2219 | **buf = msg->nargs; |
| 2220 | (*buf)++; |
| 2221 | |
| 2222 | ctx->frag_ctx.curoff = 0; |
| 2223 | encode_msg_payload: |
| 2224 | |
| 2225 | /* Loop on arguments */ |
| 2226 | list_for_each_entry(arg, &msg->args, list) { |
| 2227 | ctx->frag_ctx.curarg = arg; |
| 2228 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2229 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2230 | |
| 2231 | encode_argument: |
| 2232 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2233 | goto encode_arg_value; |
| 2234 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2235 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2236 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2237 | goto too_big; |
| 2238 | |
| 2239 | ctx->frag_ctx.curoff = 0; |
| 2240 | encode_arg_value: |
| 2241 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2242 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2243 | 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] | 2244 | if (smp) { |
| 2245 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2246 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2247 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2248 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2249 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2250 | goto too_big; |
| 2251 | } |
| 2252 | |
| 2253 | next: |
| 2254 | return 0; |
| 2255 | |
| 2256 | too_big: |
| 2257 | return -1; |
| 2258 | } |
| 2259 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2260 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2261 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2262 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2263 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2264 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2265 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2266 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2267 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2268 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2269 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2270 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2271 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2272 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2273 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2274 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2275 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2276 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2277 | /* Resume encoding of a SPOE message */ |
| 2278 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2279 | msg = ctx->frag_ctx.curmsg; |
| 2280 | goto encode_evt_message; |
| 2281 | } |
| 2282 | |
| 2283 | list_for_each_entry(msg, messages, by_evt) { |
| 2284 | ctx->frag_ctx.curmsg = msg; |
| 2285 | ctx->frag_ctx.curarg = NULL; |
| 2286 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2287 | |
| 2288 | encode_evt_message: |
| 2289 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2290 | goto too_big; |
| 2291 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2292 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2293 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2294 | /* Resume encoding of a SPOE message */ |
| 2295 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2296 | msg = ctx->frag_ctx.curmsg; |
| 2297 | goto encode_grp_message; |
| 2298 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2299 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2300 | list_for_each_entry(msg, messages, by_grp) { |
| 2301 | ctx->frag_ctx.curmsg = msg; |
| 2302 | ctx->frag_ctx.curarg = NULL; |
| 2303 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2304 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2305 | encode_grp_message: |
| 2306 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2307 | goto too_big; |
| 2308 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2309 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2310 | else |
| 2311 | goto skip; |
| 2312 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2313 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2314 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2315 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2316 | goto skip; |
| 2317 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2318 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2319 | " - encode %s messages - spoe_appctx=%p" |
| 2320 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2321 | (int)now.tv_sec, (int)now.tv_usec, |
| 2322 | agent->id, __FUNCTION__, s, |
| 2323 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2324 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2325 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2326 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2327 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2328 | ctx->frag_ctx.curmsg = NULL; |
| 2329 | ctx->frag_ctx.curarg = NULL; |
| 2330 | ctx->frag_ctx.curoff = 0; |
| 2331 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2332 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2333 | return 1; |
| 2334 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2335 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2336 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2337 | * been encoded because its too big and not splittable. */ |
| 2338 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2339 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2340 | return -1; |
| 2341 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2342 | |
| 2343 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2344 | " - encode fragmented messages - spoe_appctx=%p" |
| 2345 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2346 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2347 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2348 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2349 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2350 | (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] | 2351 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2352 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2353 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2354 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2355 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2356 | |
| 2357 | skip: |
| 2358 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2359 | " - skip the frame because nothing has been encoded\n", |
| 2360 | (int)now.tv_sec, (int)now.tv_usec, |
| 2361 | agent->id, __FUNCTION__, s); |
| 2362 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2363 | } |
| 2364 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2365 | |
| 2366 | /*************************************************************************** |
| 2367 | * Functions that handle SPOE actions |
| 2368 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2369 | /* Helper function to set a variable */ |
| 2370 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2371 | 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] | 2372 | struct sample *smp) |
| 2373 | { |
| 2374 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2375 | struct spoe_agent *agent = conf->agent; |
| 2376 | char varname[64]; |
| 2377 | |
| 2378 | memset(varname, 0, sizeof(varname)); |
| 2379 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2380 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2381 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2382 | vars_set_by_name(varname, len, smp); |
| 2383 | else |
| 2384 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | /* Helper function to unset a variable */ |
| 2388 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2389 | 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] | 2390 | struct sample *smp) |
| 2391 | { |
| 2392 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2393 | struct spoe_agent *agent = conf->agent; |
| 2394 | char varname[64]; |
| 2395 | |
| 2396 | memset(varname, 0, sizeof(varname)); |
| 2397 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2398 | scope, agent->var_pfx, len, name); |
| 2399 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2400 | } |
| 2401 | |
| 2402 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2403 | static inline int |
| 2404 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2405 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2406 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2407 | char *str, *scope, *p = *buf; |
| 2408 | struct sample smp; |
| 2409 | uint64_t sz; |
| 2410 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2411 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2412 | if (p + 2 >= end) |
| 2413 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2414 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2415 | /* SET-VAR requires 3 arguments */ |
| 2416 | if (*p++ != 3) |
| 2417 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2418 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2419 | switch (*p++) { |
| 2420 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2421 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2422 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2423 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2424 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2425 | default: goto skip; |
| 2426 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2427 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2428 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2429 | goto skip; |
| 2430 | memset(&smp, 0, sizeof(smp)); |
| 2431 | 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] | 2432 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2433 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2434 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2435 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2436 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2437 | " - set-var '%s.%s.%.*s'\n", |
| 2438 | (int)now.tv_sec, (int)now.tv_usec, |
| 2439 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2440 | __FUNCTION__, s, scope, |
| 2441 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2442 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2443 | |
Christopher Faulet | b135abc | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2444 | if (smp.data.type == SMP_T_ANY) |
| 2445 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2446 | else |
| 2447 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2448 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2449 | ret = (p - *buf); |
| 2450 | *buf = p; |
| 2451 | return ret; |
| 2452 | skip: |
| 2453 | return 0; |
| 2454 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2455 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2456 | static inline int |
| 2457 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2458 | char **buf, char *end, int dir) |
| 2459 | { |
| 2460 | char *str, *scope, *p = *buf; |
| 2461 | struct sample smp; |
| 2462 | uint64_t sz; |
| 2463 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2464 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2465 | if (p + 2 >= end) |
| 2466 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2467 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2468 | /* UNSET-VAR requires 2 arguments */ |
| 2469 | if (*p++ != 2) |
| 2470 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2471 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2472 | switch (*p++) { |
| 2473 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2474 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2475 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2476 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2477 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2478 | default: goto skip; |
| 2479 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2480 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2481 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2482 | goto skip; |
| 2483 | memset(&smp, 0, sizeof(smp)); |
| 2484 | 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] | 2485 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2486 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2487 | " - unset-var '%s.%s.%.*s'\n", |
| 2488 | (int)now.tv_sec, (int)now.tv_usec, |
| 2489 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2490 | __FUNCTION__, s, scope, |
| 2491 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2492 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2493 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2494 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2495 | |
| 2496 | ret = (p - *buf); |
| 2497 | *buf = p; |
| 2498 | return ret; |
| 2499 | skip: |
| 2500 | return 0; |
| 2501 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2502 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2503 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2504 | * error occurred, 0 is returned. */ |
| 2505 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2506 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2507 | { |
| 2508 | char *p, *end; |
| 2509 | int ret; |
| 2510 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2511 | p = b_head(&ctx->buffer); |
| 2512 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2513 | |
| 2514 | while (p < end) { |
| 2515 | enum spoe_action_type type; |
| 2516 | |
| 2517 | type = *p++; |
| 2518 | switch (type) { |
| 2519 | case SPOE_ACT_T_SET_VAR: |
| 2520 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2521 | if (!ret) |
| 2522 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2523 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2524 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2525 | case SPOE_ACT_T_UNSET_VAR: |
| 2526 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2527 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2528 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2529 | break; |
| 2530 | |
| 2531 | default: |
| 2532 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | return 1; |
| 2537 | skip: |
| 2538 | return 0; |
| 2539 | } |
| 2540 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2541 | /*************************************************************************** |
| 2542 | * Functions that process SPOE events |
| 2543 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2544 | static void |
| 2545 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2546 | struct spoe_context *ctx, int dir) |
| 2547 | { |
| 2548 | if (!tv_iszero(&ctx->stats.tv_start)) { |
| 2549 | spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process); |
| 2550 | ctx->stats.t_total += ctx->stats.t_process; |
| 2551 | tv_zero(&ctx->stats.tv_request); |
| 2552 | tv_zero(&ctx->stats.tv_queue); |
| 2553 | tv_zero(&ctx->stats.tv_wait); |
| 2554 | tv_zero(&ctx->stats.tv_response); |
| 2555 | } |
| 2556 | |
| 2557 | if (agent->var_t_process) { |
| 2558 | struct sample smp; |
| 2559 | |
| 2560 | memset(&smp, 0, sizeof(smp)); |
| 2561 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2562 | smp.data.u.sint = ctx->stats.t_process; |
| 2563 | smp.data.type = SMP_T_SINT; |
| 2564 | |
| 2565 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2566 | strlen(agent->var_t_process), &smp); |
| 2567 | } |
| 2568 | |
| 2569 | if (agent->var_t_total) { |
| 2570 | struct sample smp; |
| 2571 | |
| 2572 | memset(&smp, 0, sizeof(smp)); |
| 2573 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2574 | smp.data.u.sint = ctx->stats.t_total; |
| 2575 | smp.data.type = SMP_T_SINT; |
| 2576 | |
| 2577 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2578 | strlen(agent->var_t_total), &smp); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | static void |
| 2583 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2584 | struct spoe_context *ctx, int dir) |
| 2585 | { |
| 2586 | if (agent->eps_max > 0) |
| 2587 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2588 | |
| 2589 | if (agent->var_on_error) { |
| 2590 | struct sample smp; |
| 2591 | |
| 2592 | memset(&smp, 0, sizeof(smp)); |
| 2593 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2594 | smp.data.u.sint = ctx->status_code; |
| 2595 | smp.data.type = SMP_T_BOOL; |
| 2596 | |
| 2597 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2598 | strlen(agent->var_on_error), &smp); |
| 2599 | } |
| 2600 | |
| 2601 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2602 | ? SPOE_CTX_ST_READY |
| 2603 | : SPOE_CTX_ST_NONE); |
| 2604 | } |
| 2605 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2606 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2607 | 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] | 2608 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2609 | /* If a process is already started for this SPOE context, retry |
| 2610 | * later. */ |
| 2611 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2612 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2613 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2614 | agent->rt[tid].processing++; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2615 | ctx->stats.tv_start = now; |
| 2616 | ctx->stats.tv_request = now; |
| 2617 | ctx->stats.t_request = -1; |
| 2618 | ctx->stats.t_queue = -1; |
| 2619 | ctx->stats.t_waiting = -1; |
| 2620 | ctx->stats.t_response = -1; |
| 2621 | ctx->stats.t_process = -1; |
| 2622 | |
| 2623 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2624 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2625 | /* Set the right flag to prevent request and response processing |
| 2626 | * in same time. */ |
| 2627 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2628 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2629 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2630 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2633 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2634 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2635 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2636 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2637 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2638 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2639 | return; |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2640 | _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2641 | if (sa) { |
| 2642 | if (sa->frag_ctx.ctx == ctx) { |
| 2643 | sa->frag_ctx.ctx = NULL; |
| 2644 | spoe_wakeup_appctx(sa->owner); |
| 2645 | } |
| 2646 | else |
| 2647 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2650 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2651 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2652 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2653 | |
| 2654 | /* Reset processing timer */ |
| 2655 | ctx->process_exp = TICK_ETERNITY; |
| 2656 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2657 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2658 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2659 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2660 | ctx->frag_ctx.curmsg = NULL; |
| 2661 | ctx->frag_ctx.curarg = NULL; |
| 2662 | ctx->frag_ctx.curoff = 0; |
| 2663 | ctx->frag_ctx.flags = 0; |
| 2664 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2665 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2666 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2667 | _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2668 | else |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2669 | _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2670 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2671 | LIST_DEL(&ctx->list); |
| 2672 | LIST_INIT(&ctx->list); |
| 2673 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2676 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2677 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2678 | * to process corresponding actions. During all the processing, it returns 0 |
| 2679 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2680 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2681 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2682 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2683 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2684 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2685 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2686 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2687 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2688 | |
| 2689 | if (ctx->state == SPOE_CTX_ST_ERROR) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2690 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2691 | |
| 2692 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2693 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2694 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2695 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2696 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2697 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2698 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2699 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2700 | |
| 2701 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2702 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2703 | 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] | 2704 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2705 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2706 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2707 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2708 | goto skip; |
| 2709 | } |
| 2710 | } |
| 2711 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2712 | if (!tick_isset(ctx->process_exp)) { |
| 2713 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2714 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2715 | ctx->process_exp); |
| 2716 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2717 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2718 | if (!ret) |
| 2719 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2720 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2721 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2722 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2725 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 63263e5 | 2019-04-10 14:47:18 +0200 | [diff] [blame] | 2726 | if (tv_iszero(&ctx->stats.tv_request)) |
| 2727 | ctx->stats.tv_request = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2728 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2729 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2730 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2731 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2732 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2733 | if (!ret) |
| 2734 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2735 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2736 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2737 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2738 | } |
| 2739 | |
| 2740 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2741 | if (ctx->spoe_appctx) |
| 2742 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2743 | ret = 0; |
| 2744 | goto out; |
| 2745 | } |
| 2746 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2747 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2748 | ret = 0; |
| 2749 | goto out; |
| 2750 | } |
| 2751 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2752 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2753 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2754 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2755 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2756 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2757 | spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2758 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | out: |
| 2762 | return ret; |
| 2763 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2764 | skip: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2765 | tv_zero(&ctx->stats.tv_start); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2766 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2767 | spoe_stop_processing(agent, ctx); |
| 2768 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2769 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2770 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2771 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2772 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2773 | if (ctx->status_code) { |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2774 | _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2775 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2776 | ret = 1; |
| 2777 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2778 | return ret; |
| 2779 | } |
| 2780 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2781 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2782 | * See spoe_process_message for details. */ |
| 2783 | static int |
| 2784 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2785 | struct spoe_group *group, int dir) |
| 2786 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2787 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2788 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2789 | int ret; |
| 2790 | |
| 2791 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2792 | " - ctx-state=%s - Process messages for group=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2793 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2794 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2795 | group->id); |
| 2796 | |
| 2797 | if (LIST_ISEMPTY(&group->messages)) |
| 2798 | return 1; |
| 2799 | |
| 2800 | 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] | 2801 | if (ret && ctx->stats.t_process != -1) { |
| 2802 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2803 | " - <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] | 2804 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2805 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2806 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2807 | ctx->stats.t_response, ctx->stats.t_process, |
| 2808 | agent->counters.idles, agent->counters.applets, |
| 2809 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2810 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2811 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2812 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2813 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2814 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2815 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2816 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2817 | ctx->stats.t_response, ctx->stats.t_process, |
| 2818 | agent->counters.idles, agent->counters.applets, |
| 2819 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2820 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2821 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2822 | return ret; |
| 2823 | } |
| 2824 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2825 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2826 | * See spoe_process_message for details. */ |
| 2827 | static int |
| 2828 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2829 | enum spoe_event ev) |
| 2830 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2831 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2832 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2833 | int dir, ret; |
| 2834 | |
| 2835 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2836 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2837 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2838 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2839 | spoe_event_str[ev]); |
| 2840 | |
| 2841 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2842 | |
| 2843 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2844 | return 1; |
| 2845 | |
| 2846 | 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] | 2847 | if (ret && ctx->stats.t_process != -1) { |
| 2848 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2849 | " - <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] | 2850 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2851 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2852 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2853 | ctx->stats.t_response, ctx->stats.t_process, |
| 2854 | agent->counters.idles, agent->counters.applets, |
| 2855 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2856 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2857 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2858 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2859 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2860 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2861 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2862 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2863 | ctx->stats.t_response, ctx->stats.t_process, |
| 2864 | agent->counters.idles, agent->counters.applets, |
| 2865 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2866 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2867 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2868 | return ret; |
| 2869 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2870 | |
| 2871 | /*************************************************************************** |
| 2872 | * Functions that create/destroy SPOE contexts |
| 2873 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2874 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2875 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2876 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2877 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2878 | return 1; |
| 2879 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2880 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2881 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2882 | LIST_DEL(&buffer_wait->list); |
| 2883 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2884 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2885 | } |
| 2886 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2887 | if (b_alloc_margin(buf, global.tune.reserved_bufs)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2888 | return 1; |
| 2889 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2890 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2891 | LIST_ADDQ(&buffer_wq, &buffer_wait->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2892 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2893 | return 0; |
| 2894 | } |
| 2895 | |
| 2896 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2897 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2898 | { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2899 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2900 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2901 | LIST_DEL(&buffer_wait->list); |
| 2902 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2903 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2907 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2908 | b_free(buf); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 2909 | offer_buffers(buffer_wait->target, tasks_run_queue); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2910 | } |
| 2911 | } |
| 2912 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2913 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2914 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2915 | { |
| 2916 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2917 | return 1; |
| 2918 | } |
| 2919 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2920 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2921 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2922 | { |
| 2923 | struct spoe_config *conf = FLT_CONF(filter); |
| 2924 | struct spoe_context *ctx; |
| 2925 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2926 | ctx = pool_alloc_dirty(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2927 | if (ctx == NULL) { |
| 2928 | return NULL; |
| 2929 | } |
| 2930 | memset(ctx, 0, sizeof(*ctx)); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2931 | ctx->filter = filter; |
| 2932 | ctx->state = SPOE_CTX_ST_NONE; |
| 2933 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2934 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2935 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2936 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2937 | ctx->buffer = BUF_NULL; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2938 | LIST_INIT(&ctx->buffer_wait.list); |
| 2939 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2940 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2941 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2942 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2943 | ctx->stream_id = 0; |
| 2944 | ctx->frame_id = 1; |
| 2945 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2946 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2947 | tv_zero(&ctx->stats.tv_start); |
| 2948 | tv_zero(&ctx->stats.tv_request); |
| 2949 | tv_zero(&ctx->stats.tv_queue); |
| 2950 | tv_zero(&ctx->stats.tv_wait); |
| 2951 | tv_zero(&ctx->stats.tv_response); |
| 2952 | ctx->stats.t_request = -1; |
| 2953 | ctx->stats.t_queue = -1; |
| 2954 | ctx->stats.t_waiting = -1; |
| 2955 | ctx->stats.t_response = -1; |
| 2956 | ctx->stats.t_process = -1; |
| 2957 | ctx->stats.t_total = 0; |
| 2958 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2959 | ctx->strm = s; |
| 2960 | ctx->state = SPOE_CTX_ST_READY; |
| 2961 | filter->ctx = ctx; |
| 2962 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2963 | return ctx; |
| 2964 | } |
| 2965 | |
| 2966 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2967 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2968 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2969 | struct spoe_config *conf = FLT_CONF(filter); |
| 2970 | struct spoe_context *ctx = filter->ctx; |
| 2971 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2972 | if (!ctx) |
| 2973 | return; |
| 2974 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2975 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2976 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2977 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2981 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2982 | { |
| 2983 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2984 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2985 | |
| 2986 | tv_zero(&ctx->stats.tv_start); |
| 2987 | tv_zero(&ctx->stats.tv_request); |
| 2988 | tv_zero(&ctx->stats.tv_queue); |
| 2989 | tv_zero(&ctx->stats.tv_wait); |
| 2990 | tv_zero(&ctx->stats.tv_response); |
| 2991 | ctx->stats.t_request = -1; |
| 2992 | ctx->stats.t_queue = -1; |
| 2993 | ctx->stats.t_waiting = -1; |
| 2994 | ctx->stats.t_response = -1; |
| 2995 | ctx->stats.t_process = -1; |
| 2996 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | |
| 3000 | /*************************************************************************** |
| 3001 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 3002 | **************************************************************************/ |
| 3003 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 3004 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3005 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3006 | { |
| 3007 | struct proxy *p; |
| 3008 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 3009 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3010 | while (p) { |
| 3011 | struct flt_conf *fconf; |
| 3012 | |
| 3013 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3014 | struct spoe_config *conf; |
| 3015 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 3016 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3017 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3018 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3019 | if (fconf->id != spoe_filter_id) |
| 3020 | continue; |
| 3021 | |
| 3022 | conf = fconf->conf; |
| 3023 | agent = conf->agent; |
| 3024 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3025 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3026 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3027 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 3028 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3029 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | p = p->next; |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | |
| 3037 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 3038 | static int |
| 3039 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 3040 | { |
| 3041 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3042 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3043 | /* conf->agent_fe was already initialized during the config |
| 3044 | * parsing. Finish initialization. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3045 | conf->agent_fe.last_change = now.tv_sec; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3046 | conf->agent_fe.cap = PR_CAP_FE; |
| 3047 | conf->agent_fe.mode = PR_MODE_TCP; |
| 3048 | conf->agent_fe.maxconn = 0; |
| 3049 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 3050 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 3051 | conf->agent_fe.accept = frontend_accept; |
| 3052 | conf->agent_fe.srv = NULL; |
| 3053 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 3054 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 3055 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3056 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3057 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3058 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3059 | sighandler_registered = 1; |
| 3060 | } |
| 3061 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3062 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3063 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | /* Free ressources allocated by the SPOE filter. */ |
| 3067 | static void |
| 3068 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3069 | { |
| 3070 | struct spoe_config *conf = fconf->conf; |
| 3071 | |
| 3072 | if (conf) { |
| 3073 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3074 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3075 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3076 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3077 | free(conf); |
| 3078 | } |
| 3079 | fconf->conf = NULL; |
| 3080 | } |
| 3081 | |
| 3082 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3083 | * Return 1 on error, else 0. */ |
| 3084 | static int |
| 3085 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3086 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3087 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3088 | struct spoe_config *conf = fconf->conf; |
| 3089 | struct proxy *target; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3090 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3091 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3092 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3093 | * are uniq */ |
| 3094 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3095 | struct spoe_config *c = f->conf; |
| 3096 | |
| 3097 | /* This is not an SPOE filter */ |
| 3098 | if (f->id != spoe_filter_id) |
| 3099 | continue; |
| 3100 | /* This is the current SPOE filter */ |
| 3101 | if (f == fconf) |
| 3102 | continue; |
| 3103 | |
| 3104 | /* Check engine Id. It should be uniq */ |
| 3105 | if (!strcmp(conf->id, c->id)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3106 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3107 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3108 | return 1; |
| 3109 | } |
| 3110 | } |
| 3111 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3112 | target = proxy_be_by_name(conf->agent->b.name); |
| 3113 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3114 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3115 | " declared at %s:%d.\n", |
| 3116 | px->id, conf->agent->b.name, conf->agent->id, |
| 3117 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3118 | return 1; |
| 3119 | } |
| 3120 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3121 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3122 | " at %s:%d does not support HTTP mode.\n", |
| 3123 | px->id, target->id, conf->agent->id, |
| 3124 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3125 | return 1; |
| 3126 | } |
| 3127 | |
Willy Tarreau | 2bdcfde | 2019-02-05 13:37:19 +0100 | [diff] [blame] | 3128 | if (px->bind_proc & ~target->bind_proc) { |
| 3129 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3130 | " at %s:%d does not cover all of its processes.\n", |
| 3131 | px->id, target->id, conf->agent->id, |
| 3132 | conf->agent->conf.file, conf->agent->conf.line); |
| 3133 | return 1; |
| 3134 | } |
| 3135 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3136 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3137 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3138 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3139 | return 1; |
| 3140 | } |
| 3141 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 46c7682 | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3142 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3143 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3144 | conf->agent->rt[i].processing = 0; |
| 3145 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3146 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3147 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3148 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3149 | } |
| 3150 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3151 | free(conf->agent->b.name); |
| 3152 | conf->agent->b.name = NULL; |
| 3153 | conf->agent->b.be = target; |
| 3154 | return 0; |
| 3155 | } |
| 3156 | |
Kevin Zhu | 652c8e2 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3157 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3158 | * Returns a negative value if an error occurs. */ |
| 3159 | static int |
| 3160 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3161 | { |
| 3162 | struct spoe_config *conf = fconf->conf; |
| 3163 | struct spoe_agent *agent = conf->agent; |
| 3164 | |
Christopher Faulet | 46c7682 | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3165 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3166 | if (agent->rt[tid].engine_id == NULL) |
| 3167 | return -1; |
Kevin Zhu | 652c8e2 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3168 | return 0; |
| 3169 | } |
| 3170 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3171 | /************************************************************************** |
| 3172 | * Hooks attached to a stream |
| 3173 | *************************************************************************/ |
| 3174 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3175 | * the context that will be used to process this stream. */ |
| 3176 | static int |
| 3177 | spoe_start(struct stream *s, struct filter *filter) |
| 3178 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3179 | struct spoe_config *conf = FLT_CONF(filter); |
| 3180 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3181 | struct spoe_context *ctx; |
| 3182 | |
| 3183 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3184 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3185 | __FUNCTION__, s); |
| 3186 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3187 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3188 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3189 | " - failed to create SPOE context\n", |
| 3190 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3191 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3192 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3193 | "SPOE: [%s] failed to create SPOE context\n", |
| 3194 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3195 | return 0; |
| 3196 | } |
| 3197 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3198 | return 1; |
| 3199 | } |
| 3200 | |
| 3201 | /* Called when a filter instance is detached from a stream. It release the |
| 3202 | * attached SPOE context. */ |
| 3203 | static void |
| 3204 | spoe_stop(struct stream *s, struct filter *filter) |
| 3205 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3206 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 3207 | (int)now.tv_sec, (int)now.tv_usec, |
| 3208 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3209 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3210 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3211 | } |
| 3212 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3213 | |
| 3214 | /* |
| 3215 | * Called when the stream is woken up because of expired timer. |
| 3216 | */ |
| 3217 | static void |
| 3218 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3219 | { |
| 3220 | struct spoe_context *ctx = filter->ctx; |
| 3221 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3222 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3223 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3224 | } |
| 3225 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3226 | /* Called when we are ready to filter data on a channel */ |
| 3227 | static int |
| 3228 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3229 | { |
| 3230 | struct spoe_context *ctx = filter->ctx; |
| 3231 | int ret = 1; |
| 3232 | |
| 3233 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3234 | " - ctx-flags=0x%08x\n", |
| 3235 | (int)now.tv_sec, (int)now.tv_usec, |
| 3236 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3237 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3238 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3239 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3240 | goto out; |
| 3241 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3242 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | bebe255 | 2021-06-25 19:11:49 +0200 | [diff] [blame] | 3243 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
| 3244 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3245 | |
| 3246 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
| 3247 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3248 | |
| 3249 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
| 3250 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3251 | |
| 3252 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
| 3253 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3254 | |
| 3255 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
| 3256 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3257 | |
| 3258 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
| 3259 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3260 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3261 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3262 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3263 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3264 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3265 | |
| 3266 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3267 | goto out; |
| 3268 | |
| 3269 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3270 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3271 | if (!ret) |
| 3272 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3273 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3274 | } |
| 3275 | else { |
Miroslav Zagorac | d995d5f | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3276 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3277 | chn->analysers |= AN_RES_INSPECT; |
| 3278 | |
| 3279 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3280 | goto out; |
| 3281 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3282 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3283 | if (!ret) { |
| 3284 | channel_dont_read(chn); |
| 3285 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3286 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3287 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3288 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3292 | return ret; |
| 3293 | } |
| 3294 | |
| 3295 | /* Called before a processing happens on a given channel */ |
| 3296 | static int |
| 3297 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3298 | struct channel *chn, unsigned an_bit) |
| 3299 | { |
| 3300 | struct spoe_context *ctx = filter->ctx; |
| 3301 | int ret = 1; |
| 3302 | |
| 3303 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3304 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3305 | (int)now.tv_sec, (int)now.tv_usec, |
| 3306 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3307 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3308 | ctx->flags, an_bit); |
| 3309 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3310 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3311 | goto out; |
| 3312 | |
| 3313 | switch (an_bit) { |
| 3314 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3315 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3316 | break; |
| 3317 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3318 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3319 | break; |
| 3320 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3321 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3322 | break; |
| 3323 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3324 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3325 | break; |
| 3326 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3327 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3328 | break; |
| 3329 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3330 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3335 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3336 | channel_dont_read(chn); |
| 3337 | channel_dont_close(chn); |
| 3338 | } |
| 3339 | return ret; |
| 3340 | } |
| 3341 | |
| 3342 | /* Called when the filtering on the channel ends. */ |
| 3343 | static int |
| 3344 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3345 | { |
| 3346 | struct spoe_context *ctx = filter->ctx; |
| 3347 | |
| 3348 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3349 | " - ctx-flags=0x%08x\n", |
| 3350 | (int)now.tv_sec, (int)now.tv_usec, |
| 3351 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3352 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3353 | |
| 3354 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3355 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | return 1; |
| 3359 | } |
| 3360 | |
| 3361 | /******************************************************************** |
| 3362 | * Functions that manage the filter initialization |
| 3363 | ********************************************************************/ |
| 3364 | struct flt_ops spoe_ops = { |
| 3365 | /* Manage SPOE filter, called for each filter declaration */ |
| 3366 | .init = spoe_init, |
| 3367 | .deinit = spoe_deinit, |
| 3368 | .check = spoe_check, |
Kevin Zhu | 652c8e2 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3369 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3370 | |
| 3371 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3372 | .attach = spoe_start, |
| 3373 | .detach = spoe_stop, |
| 3374 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3375 | |
| 3376 | /* Handle channels activity */ |
| 3377 | .channel_start_analyze = spoe_start_analyze, |
| 3378 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3379 | .channel_end_analyze = spoe_end_analyze, |
| 3380 | }; |
| 3381 | |
| 3382 | |
| 3383 | static int |
| 3384 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3385 | { |
| 3386 | const char *err; |
| 3387 | int i, err_code = 0; |
| 3388 | |
| 3389 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3390 | (cfg_scope != NULL && curengine == NULL) || |
Christopher Faulet | e1405e5 | 2017-09-19 10:35:35 +0200 | [diff] [blame] | 3391 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3392 | goto out; |
| 3393 | |
| 3394 | if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */ |
| 3395 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3396 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3397 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3398 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3399 | goto out; |
| 3400 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3401 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3402 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3403 | goto out; |
| 3404 | } |
| 3405 | |
| 3406 | err = invalid_char(args[1]); |
| 3407 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3408 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3409 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3410 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3411 | goto out; |
| 3412 | } |
| 3413 | |
| 3414 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3415 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3416 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3417 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3418 | goto out; |
| 3419 | } |
| 3420 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3421 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3422 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3423 | goto out; |
| 3424 | } |
| 3425 | |
| 3426 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3427 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3428 | curagent->conf.file = strdup(file); |
| 3429 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3430 | |
| 3431 | curagent->timeout.hello = TICK_ETERNITY; |
| 3432 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3433 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3434 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3435 | curagent->var_pfx = NULL; |
| 3436 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3437 | curagent->var_t_process = NULL; |
| 3438 | curagent->var_t_total = NULL; |
Christopher Faulet | 46c7682 | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3439 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3440 | curagent->cps_max = 0; |
| 3441 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3442 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3443 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3444 | |
| 3445 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3446 | LIST_INIT(&curagent->events[i]); |
| 3447 | LIST_INIT(&curagent->groups); |
| 3448 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3449 | } |
| 3450 | else if (!strcmp(args[0], "use-backend")) { |
| 3451 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3452 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3453 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3454 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3455 | goto out; |
| 3456 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3457 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3458 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3459 | free(curagent->b.name); |
| 3460 | curagent->b.name = strdup(args[1]); |
| 3461 | } |
| 3462 | else if (!strcmp(args[0], "messages")) { |
| 3463 | int cur_arg = 1; |
| 3464 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3465 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3466 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3467 | list_for_each_entry(ph, &curmphs, list) { |
| 3468 | if (!strcmp(ph->id, args[cur_arg])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3469 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3470 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3471 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3472 | goto out; |
| 3473 | } |
| 3474 | } |
| 3475 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3476 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3477 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3478 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3479 | goto out; |
| 3480 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3481 | ph->id = strdup(args[cur_arg]); |
| 3482 | LIST_ADDQ(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3483 | cur_arg++; |
| 3484 | } |
| 3485 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3486 | else if (!strcmp(args[0], "groups")) { |
| 3487 | int cur_arg = 1; |
| 3488 | while (*args[cur_arg]) { |
| 3489 | struct spoe_placeholder *ph = NULL; |
| 3490 | |
| 3491 | list_for_each_entry(ph, &curgphs, list) { |
| 3492 | if (!strcmp(ph->id, args[cur_arg])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3493 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3494 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3495 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3496 | goto out; |
| 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3501 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3502 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3503 | goto out; |
| 3504 | } |
| 3505 | ph->id = strdup(args[cur_arg]); |
| 3506 | LIST_ADDQ(&curgphs, &ph->list); |
| 3507 | cur_arg++; |
| 3508 | } |
| 3509 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3510 | else if (!strcmp(args[0], "timeout")) { |
| 3511 | unsigned int *tv = NULL; |
| 3512 | const char *res; |
| 3513 | unsigned timeout; |
| 3514 | |
| 3515 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3516 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3517 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3518 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3519 | goto out; |
| 3520 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3521 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3522 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3523 | if (!strcmp(args[1], "hello")) |
| 3524 | tv = &curagent->timeout.hello; |
| 3525 | else if (!strcmp(args[1], "idle")) |
| 3526 | tv = &curagent->timeout.idle; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3527 | else if (!strcmp(args[1], "processing")) |
| 3528 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3529 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3530 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3531 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3532 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3533 | goto out; |
| 3534 | } |
| 3535 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3536 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3537 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3538 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3539 | goto out; |
| 3540 | } |
| 3541 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3542 | if (res == PARSE_TIME_OVER) { |
| 3543 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3544 | file, linenum, args[2], args[0], args[1]); |
| 3545 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3546 | goto out; |
| 3547 | } |
| 3548 | else if (res == PARSE_TIME_UNDER) { |
| 3549 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3550 | file, linenum, args[2], args[0], args[1]); |
| 3551 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3552 | goto out; |
| 3553 | } |
| 3554 | else if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3555 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3556 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3557 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3558 | goto out; |
| 3559 | } |
| 3560 | *tv = MS_TO_TICKS(timeout); |
| 3561 | } |
| 3562 | else if (!strcmp(args[0], "option")) { |
| 3563 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3564 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3565 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3566 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3567 | goto out; |
| 3568 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3569 | |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3570 | if (!strcmp(args[1], "pipelining")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3571 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3572 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3573 | if (kwm == 1) |
| 3574 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3575 | else |
| 3576 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3577 | goto out; |
| 3578 | } |
| 3579 | else if (!strcmp(args[1], "async")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3580 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3581 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3582 | if (kwm == 1) |
| 3583 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | 46c7682 | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3584 | else |
| 3585 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3586 | goto out; |
| 3587 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3588 | else if (!strcmp(args[1], "send-frag-payload")) { |
| 3589 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3590 | goto out; |
| 3591 | if (kwm == 1) |
| 3592 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3593 | else |
| 3594 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3595 | goto out; |
| 3596 | } |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3597 | else if (!strcmp(args[1], "dontlog-normal")) { |
| 3598 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3599 | goto out; |
| 3600 | if (kwm == 1) |
| 3601 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3602 | else |
| 3603 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3604 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3605 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3606 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3607 | /* Following options does not support negation */ |
| 3608 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3609 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3610 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3611 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3612 | goto out; |
| 3613 | } |
| 3614 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3615 | if (!strcmp(args[1], "var-prefix")) { |
| 3616 | char *tmp; |
| 3617 | |
| 3618 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3619 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3620 | file, linenum, args[0], |
| 3621 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3622 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3623 | goto out; |
| 3624 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3625 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3626 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3627 | tmp = args[2]; |
| 3628 | while (*tmp) { |
| 3629 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3630 | 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] | 3631 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3632 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3633 | goto out; |
| 3634 | } |
| 3635 | tmp++; |
| 3636 | } |
| 3637 | curagent->var_pfx = strdup(args[2]); |
| 3638 | } |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3639 | else if (!strcmp(args[1], "force-set-var")) { |
| 3640 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3641 | goto out; |
| 3642 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3643 | } |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3644 | else if (!strcmp(args[1], "continue-on-error")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3645 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3646 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3647 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3648 | } |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3649 | else if (!strcmp(args[1], "set-on-error")) { |
| 3650 | char *tmp; |
| 3651 | |
| 3652 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3653 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3654 | file, linenum, args[0], |
| 3655 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3656 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3657 | goto out; |
| 3658 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3659 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3660 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3661 | tmp = args[2]; |
| 3662 | while (*tmp) { |
| 3663 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3664 | 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] | 3665 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3666 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3667 | goto out; |
| 3668 | } |
| 3669 | tmp++; |
| 3670 | } |
| 3671 | curagent->var_on_error = strdup(args[2]); |
| 3672 | } |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3673 | else if (!strcmp(args[1], "set-process-time")) { |
| 3674 | char *tmp; |
| 3675 | |
| 3676 | if (!*args[2]) { |
| 3677 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3678 | file, linenum, args[0], |
| 3679 | args[1]); |
| 3680 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3681 | goto out; |
| 3682 | } |
| 3683 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3684 | goto out; |
| 3685 | tmp = args[2]; |
| 3686 | while (*tmp) { |
| 3687 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3688 | 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] | 3689 | file, linenum, args[0], args[1]); |
| 3690 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3691 | goto out; |
| 3692 | } |
| 3693 | tmp++; |
| 3694 | } |
| 3695 | curagent->var_t_process = strdup(args[2]); |
| 3696 | } |
| 3697 | else if (!strcmp(args[1], "set-total-time")) { |
| 3698 | char *tmp; |
| 3699 | |
| 3700 | if (!*args[2]) { |
| 3701 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3702 | file, linenum, args[0], |
| 3703 | args[1]); |
| 3704 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3705 | goto out; |
| 3706 | } |
| 3707 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3708 | goto out; |
| 3709 | tmp = args[2]; |
| 3710 | while (*tmp) { |
| 3711 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3712 | 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] | 3713 | file, linenum, args[0], args[1]); |
| 3714 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3715 | goto out; |
| 3716 | } |
| 3717 | tmp++; |
| 3718 | } |
| 3719 | curagent->var_t_total = strdup(args[2]); |
| 3720 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3721 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3722 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3723 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3724 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3725 | goto out; |
| 3726 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3727 | } |
| 3728 | else if (!strcmp(args[0], "maxconnrate")) { |
| 3729 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3730 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3731 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3732 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3733 | goto out; |
| 3734 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3735 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3736 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3737 | curagent->cps_max = atol(args[1]); |
| 3738 | } |
| 3739 | else if (!strcmp(args[0], "maxerrrate")) { |
| 3740 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3741 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3742 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3743 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3744 | goto out; |
| 3745 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3746 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3747 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3748 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3749 | } |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3750 | else if (!strcmp(args[0], "max-frame-size")) { |
| 3751 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3752 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3753 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3754 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3755 | goto out; |
| 3756 | } |
| 3757 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3758 | goto out; |
| 3759 | curagent->max_frame_size = atol(args[1]); |
| 3760 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3761 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3762 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3763 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3764 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3765 | goto out; |
| 3766 | } |
| 3767 | } |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3768 | else if (!strcmp(args[0], "max-waiting-frames")) { |
| 3769 | if (!*args[1]) { |
| 3770 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3771 | file, linenum, args[0]); |
| 3772 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3773 | goto out; |
| 3774 | } |
| 3775 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3776 | goto out; |
| 3777 | curagent->max_fpa = atol(args[1]); |
| 3778 | if (curagent->max_fpa < 1) { |
| 3779 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3780 | file, linenum, args[0]); |
| 3781 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3782 | goto out; |
| 3783 | } |
| 3784 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3785 | else if (!strcmp(args[0], "register-var-names")) { |
| 3786 | int cur_arg; |
| 3787 | |
| 3788 | if (!*args[1]) { |
| 3789 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3790 | file, linenum, args[0]); |
| 3791 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3792 | goto out; |
| 3793 | } |
| 3794 | cur_arg = 1; |
| 3795 | while (*args[cur_arg]) { |
| 3796 | struct spoe_var_placeholder *vph; |
| 3797 | |
| 3798 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3799 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3800 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3801 | goto out; |
| 3802 | } |
| 3803 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | f818ea5 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3804 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3805 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3806 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3807 | goto out; |
| 3808 | } |
| 3809 | LIST_ADDQ(&curvars, &vph->list); |
| 3810 | cur_arg++; |
| 3811 | } |
| 3812 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3813 | else if (!strcmp(args[0], "log")) { |
| 3814 | char *errmsg = NULL; |
| 3815 | |
| 3816 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) { |
| 3817 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3818 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3819 | goto out; |
| 3820 | } |
| 3821 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3822 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3823 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3824 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3825 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3826 | goto out; |
| 3827 | } |
| 3828 | out: |
| 3829 | return err_code; |
| 3830 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3831 | static int |
| 3832 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3833 | { |
| 3834 | struct spoe_group *grp; |
| 3835 | const char *err; |
| 3836 | int err_code = 0; |
| 3837 | |
| 3838 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3839 | (cfg_scope != NULL && curengine == NULL) || |
| 3840 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
| 3841 | goto out; |
| 3842 | |
| 3843 | if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */ |
| 3844 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3845 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3846 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3847 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3848 | goto out; |
| 3849 | } |
| 3850 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3851 | err_code |= ERR_ABORT; |
| 3852 | goto out; |
| 3853 | } |
| 3854 | |
| 3855 | err = invalid_char(args[1]); |
| 3856 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3857 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3858 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3859 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3860 | goto out; |
| 3861 | } |
| 3862 | |
| 3863 | list_for_each_entry(grp, &curgrps, list) { |
| 3864 | if (!strcmp(grp->id, args[1])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3865 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3866 | " name as another one declared at %s:%d.\n", |
| 3867 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3868 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3869 | goto out; |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3874 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3875 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3876 | goto out; |
| 3877 | } |
| 3878 | |
| 3879 | curgrp->id = strdup(args[1]); |
| 3880 | curgrp->conf.file = strdup(file); |
| 3881 | curgrp->conf.line = linenum; |
| 3882 | LIST_INIT(&curgrp->phs); |
| 3883 | LIST_INIT(&curgrp->messages); |
| 3884 | LIST_ADDQ(&curgrps, &curgrp->list); |
| 3885 | } |
| 3886 | else if (!strcmp(args[0], "messages")) { |
| 3887 | int cur_arg = 1; |
| 3888 | while (*args[cur_arg]) { |
| 3889 | struct spoe_placeholder *ph = NULL; |
| 3890 | |
| 3891 | list_for_each_entry(ph, &curgrp->phs, list) { |
| 3892 | if (!strcmp(ph->id, args[cur_arg])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3893 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3894 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3895 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3896 | goto out; |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3901 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3902 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3903 | goto out; |
| 3904 | } |
| 3905 | ph->id = strdup(args[cur_arg]); |
| 3906 | LIST_ADDQ(&curgrp->phs, &ph->list); |
| 3907 | cur_arg++; |
| 3908 | } |
| 3909 | } |
| 3910 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3911 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3912 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3913 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3914 | goto out; |
| 3915 | } |
| 3916 | out: |
| 3917 | return err_code; |
| 3918 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3919 | |
| 3920 | static int |
| 3921 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3922 | { |
| 3923 | struct spoe_message *msg; |
| 3924 | struct spoe_arg *arg; |
| 3925 | const char *err; |
| 3926 | char *errmsg = NULL; |
| 3927 | int err_code = 0; |
| 3928 | |
| 3929 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3930 | (cfg_scope != NULL && curengine == NULL) || |
Christopher Faulet | e1405e5 | 2017-09-19 10:35:35 +0200 | [diff] [blame] | 3931 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope))) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3932 | goto out; |
| 3933 | |
| 3934 | if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */ |
| 3935 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3936 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3937 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3938 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3939 | goto out; |
| 3940 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3941 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3942 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3943 | goto out; |
| 3944 | } |
| 3945 | |
| 3946 | err = invalid_char(args[1]); |
| 3947 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3948 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3949 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3950 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3951 | goto out; |
| 3952 | } |
| 3953 | |
| 3954 | list_for_each_entry(msg, &curmsgs, list) { |
| 3955 | if (!strcmp(msg->id, args[1])) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3956 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3957 | " name as another one declared at %s:%d.\n", |
| 3958 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3959 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3960 | goto out; |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3965 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3966 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3967 | goto out; |
| 3968 | } |
| 3969 | |
| 3970 | curmsg->id = strdup(args[1]); |
| 3971 | curmsg->id_len = strlen(curmsg->id); |
| 3972 | curmsg->event = SPOE_EV_NONE; |
| 3973 | curmsg->conf.file = strdup(file); |
| 3974 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3975 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3976 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3977 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3978 | LIST_INIT(&curmsg->by_evt); |
| 3979 | LIST_INIT(&curmsg->by_grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3980 | LIST_ADDQ(&curmsgs, &curmsg->list); |
| 3981 | } |
| 3982 | else if (!strcmp(args[0], "args")) { |
| 3983 | int cur_arg = 1; |
| 3984 | |
| 3985 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3986 | curproxy->conf.args.file = file; |
| 3987 | curproxy->conf.args.line = linenum; |
| 3988 | while (*args[cur_arg]) { |
| 3989 | char *delim = strchr(args[cur_arg], '='); |
| 3990 | int idx = 0; |
| 3991 | |
| 3992 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3993 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3994 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3995 | goto out; |
| 3996 | } |
| 3997 | |
| 3998 | if (!delim) { |
| 3999 | arg->name = NULL; |
| 4000 | arg->name_len = 0; |
| 4001 | delim = args[cur_arg]; |
| 4002 | } |
| 4003 | else { |
| 4004 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 4005 | arg->name_len = delim - args[cur_arg]; |
| 4006 | delim++; |
| 4007 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 4008 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 4009 | &idx, file, linenum, &errmsg, |
| 4010 | &curproxy->conf.args); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4011 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4012 | 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] | 4013 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4014 | free(arg->name); |
| 4015 | free(arg); |
| 4016 | goto out; |
| 4017 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 4018 | curmsg->nargs++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4019 | LIST_ADDQ(&curmsg->args, &arg->list); |
| 4020 | cur_arg++; |
| 4021 | } |
| 4022 | curproxy->conf.args.file = NULL; |
| 4023 | curproxy->conf.args.line = 0; |
| 4024 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4025 | else if (!strcmp(args[0], "acl")) { |
| 4026 | err = invalid_char(args[1]); |
| 4027 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4028 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 4029 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4030 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4031 | goto out; |
| 4032 | } |
Tim Duesterhus | 6b7ce1e | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 4033 | if (strcasecmp(args[1], "or") == 0) { |
| 4034 | ha_warning("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a " |
| 4035 | "logical disjunction within a condition.\n", |
| 4036 | file, linenum, args[1]); |
| 4037 | err_code |= ERR_WARN; |
| 4038 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4039 | 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] | 4040 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4041 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4042 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4043 | goto out; |
| 4044 | } |
| 4045 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4046 | else if (!strcmp(args[0], "event")) { |
| 4047 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4048 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4049 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4050 | goto out; |
| 4051 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4052 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4053 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4054 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4055 | if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS])) |
| 4056 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
| 4057 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS])) |
| 4058 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4059 | |
| 4060 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE])) |
| 4061 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4062 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE])) |
| 4063 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
| 4064 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP])) |
| 4065 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4066 | |
| 4067 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE])) |
| 4068 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4069 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE])) |
| 4070 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
| 4071 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP])) |
| 4072 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4073 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4074 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4075 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4076 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4077 | goto out; |
| 4078 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4079 | |
| 4080 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4081 | struct acl_cond *cond; |
| 4082 | |
| 4083 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4084 | curproxy, (const char **)args+2, |
| 4085 | &errmsg); |
| 4086 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4087 | ha_alert("parsing [%s:%d] : error detected while " |
| 4088 | "parsing an 'event %s' condition : %s.\n", |
| 4089 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4090 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4091 | goto out; |
| 4092 | } |
| 4093 | curmsg->cond = cond; |
| 4094 | } |
| 4095 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4096 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4097 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4098 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4099 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4100 | goto out; |
| 4101 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4102 | } |
| 4103 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4104 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4105 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4106 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4107 | goto out; |
| 4108 | } |
| 4109 | out: |
| 4110 | free(errmsg); |
| 4111 | return err_code; |
| 4112 | } |
| 4113 | |
| 4114 | /* Return -1 on error, else 0 */ |
| 4115 | static int |
| 4116 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4117 | struct flt_conf *fconf, char **err, void *private) |
| 4118 | { |
| 4119 | struct list backup_sections; |
| 4120 | struct spoe_config *conf; |
| 4121 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4122 | struct spoe_group *grp, *grpback; |
| 4123 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4124 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4125 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4126 | char *file = NULL, *engine = NULL; |
| 4127 | int ret, pos = *cur_arg + 1; |
| 4128 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4129 | LIST_INIT(&curmsgs); |
| 4130 | LIST_INIT(&curgrps); |
| 4131 | LIST_INIT(&curmphs); |
| 4132 | LIST_INIT(&curgphs); |
| 4133 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4134 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4135 | curpxopts = 0; |
| 4136 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4137 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4138 | conf = calloc(1, sizeof(*conf)); |
| 4139 | if (conf == NULL) { |
| 4140 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4141 | goto error; |
| 4142 | } |
| 4143 | conf->proxy = px; |
| 4144 | |
| 4145 | while (*args[pos]) { |
| 4146 | if (!strcmp(args[pos], "config")) { |
| 4147 | if (!*args[pos+1]) { |
| 4148 | memprintf(err, "'%s' : '%s' option without value", |
| 4149 | args[*cur_arg], args[pos]); |
| 4150 | goto error; |
| 4151 | } |
| 4152 | file = args[pos+1]; |
| 4153 | pos += 2; |
| 4154 | } |
| 4155 | else if (!strcmp(args[pos], "engine")) { |
| 4156 | if (!*args[pos+1]) { |
| 4157 | memprintf(err, "'%s' : '%s' option without value", |
| 4158 | args[*cur_arg], args[pos]); |
| 4159 | goto error; |
| 4160 | } |
| 4161 | engine = args[pos+1]; |
| 4162 | pos += 2; |
| 4163 | } |
| 4164 | else { |
| 4165 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4166 | goto error; |
| 4167 | } |
| 4168 | } |
| 4169 | if (file == NULL) { |
| 4170 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4171 | goto error; |
| 4172 | } |
| 4173 | |
| 4174 | /* backup sections and register SPOE sections */ |
| 4175 | LIST_INIT(&backup_sections); |
| 4176 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4177 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4178 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4179 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4180 | |
| 4181 | /* Parse SPOE filter configuration file */ |
| 4182 | curengine = engine; |
| 4183 | curproxy = px; |
| 4184 | curagent = NULL; |
| 4185 | curmsg = NULL; |
| 4186 | ret = readcfgfile(file); |
| 4187 | curproxy = NULL; |
| 4188 | |
| 4189 | /* unregister SPOE sections and restore previous sections */ |
| 4190 | cfg_unregister_sections(); |
| 4191 | cfg_restore_sections(&backup_sections); |
| 4192 | |
| 4193 | if (ret == -1) { |
| 4194 | memprintf(err, "Could not open configuration file %s : %s", |
| 4195 | file, strerror(errno)); |
| 4196 | goto error; |
| 4197 | } |
| 4198 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4199 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4200 | goto error; |
| 4201 | } |
| 4202 | |
| 4203 | /* Check SPOE agent */ |
| 4204 | if (curagent == NULL) { |
| 4205 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4206 | goto error; |
| 4207 | } |
| 4208 | if (curagent->b.name == NULL) { |
| 4209 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4210 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4211 | goto error; |
| 4212 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4213 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4214 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4215 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4216 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4217 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4218 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4219 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4220 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4221 | } |
| 4222 | if (curagent->var_pfx == NULL) { |
| 4223 | char *tmp = curagent->id; |
| 4224 | |
| 4225 | while (*tmp) { |
| 4226 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 4227 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4228 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4229 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4230 | goto error; |
| 4231 | } |
| 4232 | tmp++; |
| 4233 | } |
| 4234 | curagent->var_pfx = strdup(curagent->id); |
| 4235 | } |
| 4236 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4237 | if (curagent->var_on_error) { |
| 4238 | struct arg arg; |
| 4239 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4240 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4241 | curagent->var_pfx, curagent->var_on_error); |
| 4242 | |
| 4243 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4244 | arg.data.str.area = trash.area; |
| 4245 | arg.data.str.data = trash.data; |
Christopher Faulet | bf9bcb0 | 2019-05-13 10:39:36 +0200 | [diff] [blame] | 4246 | 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] | 4247 | if (!vars_check_arg(&arg, err)) { |
| 4248 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4249 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4250 | goto error; |
| 4251 | } |
| 4252 | } |
| 4253 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4254 | if (curagent->var_t_process) { |
| 4255 | struct arg arg; |
| 4256 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4257 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4258 | curagent->var_pfx, curagent->var_t_process); |
| 4259 | |
| 4260 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4261 | arg.data.str.area = trash.area; |
| 4262 | arg.data.str.data = trash.data; |
Christopher Faulet | bf9bcb0 | 2019-05-13 10:39:36 +0200 | [diff] [blame] | 4263 | 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] | 4264 | if (!vars_check_arg(&arg, err)) { |
| 4265 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4266 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4267 | goto error; |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | if (curagent->var_t_total) { |
| 4272 | struct arg arg; |
| 4273 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4274 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4275 | curagent->var_pfx, curagent->var_t_total); |
| 4276 | |
| 4277 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4278 | arg.data.str.area = trash.area; |
| 4279 | arg.data.str.data = trash.data; |
Christopher Faulet | bf9bcb0 | 2019-05-13 10:39:36 +0200 | [diff] [blame] | 4280 | 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] | 4281 | if (!vars_check_arg(&arg, err)) { |
| 4282 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4283 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4284 | goto error; |
| 4285 | } |
| 4286 | } |
| 4287 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4288 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4289 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4290 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4291 | goto finish; |
| 4292 | } |
| 4293 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4294 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4295 | * agent */ |
| 4296 | list_for_each_entry(ph, &curmphs, list) { |
| 4297 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4298 | struct spoe_arg *arg; |
| 4299 | unsigned int where; |
| 4300 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4301 | if (!strcmp(msg->id, ph->id)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4302 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4303 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4304 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4305 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4306 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4307 | } |
| 4308 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4309 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4310 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4311 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4312 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4313 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4314 | } |
| 4315 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4316 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4317 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4318 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4319 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4320 | |
| 4321 | where = 0; |
| 4322 | switch (msg->event) { |
| 4323 | case SPOE_EV_ON_CLIENT_SESS: |
| 4324 | where |= SMP_VAL_FE_CON_ACC; |
| 4325 | break; |
| 4326 | |
| 4327 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4328 | where |= SMP_VAL_FE_REQ_CNT; |
| 4329 | break; |
| 4330 | |
| 4331 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4332 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4333 | break; |
| 4334 | |
| 4335 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4336 | if (px->cap & PR_CAP_FE) |
| 4337 | where |= SMP_VAL_FE_REQ_CNT; |
| 4338 | if (px->cap & PR_CAP_BE) |
| 4339 | where |= SMP_VAL_BE_REQ_CNT; |
| 4340 | break; |
| 4341 | |
| 4342 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4343 | if (px->cap & PR_CAP_FE) |
| 4344 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4345 | if (px->cap & PR_CAP_BE) |
| 4346 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4347 | break; |
| 4348 | |
| 4349 | case SPOE_EV_ON_SERVER_SESS: |
| 4350 | where |= SMP_VAL_BE_SRV_CON; |
| 4351 | break; |
| 4352 | |
| 4353 | case SPOE_EV_ON_TCP_RSP: |
| 4354 | if (px->cap & PR_CAP_FE) |
| 4355 | where |= SMP_VAL_FE_RES_CNT; |
| 4356 | if (px->cap & PR_CAP_BE) |
| 4357 | where |= SMP_VAL_BE_RES_CNT; |
| 4358 | break; |
| 4359 | |
| 4360 | case SPOE_EV_ON_HTTP_RSP: |
| 4361 | if (px->cap & PR_CAP_FE) |
| 4362 | where |= SMP_VAL_FE_HRS_HDR; |
| 4363 | if (px->cap & PR_CAP_BE) |
| 4364 | where |= SMP_VAL_BE_HRS_HDR; |
| 4365 | break; |
| 4366 | |
| 4367 | default: |
| 4368 | break; |
| 4369 | } |
| 4370 | |
| 4371 | list_for_each_entry(arg, &msg->args, list) { |
| 4372 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4373 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4374 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4375 | "none of which is available here ('%s')", |
| 4376 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4377 | sample_ckp_names(arg->expr->fetch->use), |
| 4378 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4379 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4383 | msg->agent = curagent; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4384 | LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt); |
| 4385 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4386 | } |
| 4387 | } |
| 4388 | 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] | 4389 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4390 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4391 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4392 | continue; |
| 4393 | } |
| 4394 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4395 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4396 | * agent */ |
| 4397 | list_for_each_entry(ph, &curgphs, list) { |
| 4398 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4399 | if (!strcmp(grp->id, ph->id)) { |
| 4400 | grp->agent = curagent; |
| 4401 | LIST_DEL(&grp->list); |
| 4402 | LIST_ADDQ(&curagent->groups, &grp->list); |
| 4403 | goto next_aph; |
| 4404 | } |
| 4405 | } |
| 4406 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4407 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4408 | goto error; |
| 4409 | next_aph: |
| 4410 | continue; |
| 4411 | } |
| 4412 | |
| 4413 | /* Replace placeholders by the corresponding message for each SPOE |
| 4414 | * group of the SPOE agent */ |
| 4415 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4416 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4417 | list_for_each_entry(msg, &curmsgs, list) { |
| 4418 | if (!strcmp(msg->id, ph->id)) { |
| 4419 | if (msg->group != NULL) { |
| 4420 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4421 | "the SPOE group '%s' declare at %s:%d", |
| 4422 | msg->id, msg->group->id, |
| 4423 | msg->group->conf.file, |
| 4424 | msg->group->conf.line); |
| 4425 | goto error; |
| 4426 | } |
| 4427 | |
| 4428 | /* Scope for arguments are not checked for now. We will check |
| 4429 | * them only if a rule use the corresponding SPOE group. */ |
| 4430 | msg->agent = curagent; |
| 4431 | msg->group = grp; |
| 4432 | LIST_DEL(&ph->list); |
| 4433 | LIST_ADDQ(&grp->messages, &msg->by_grp); |
| 4434 | goto next_mph_grp; |
| 4435 | } |
| 4436 | } |
| 4437 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4438 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4439 | goto error; |
| 4440 | next_mph_grp: |
| 4441 | continue; |
| 4442 | } |
| 4443 | } |
| 4444 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4445 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4446 | /* move curmsgs to the agent message list */ |
| 4447 | curmsgs.n->p = &curagent->messages; |
| 4448 | curmsgs.p->n = &curagent->messages; |
| 4449 | curagent->messages = curmsgs; |
| 4450 | LIST_INIT(&curmsgs); |
| 4451 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4452 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4453 | conf->agent = curagent; |
Christopher Faulet | 75fc5f3 | 2021-08-02 17:51:01 +0200 | [diff] [blame] | 4454 | curagent->spoe_conf = conf; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4455 | |
| 4456 | /* Start agent's proxy initialization here. It will be finished during |
| 4457 | * the filter init. */ |
| 4458 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4459 | init_new_proxy(&conf->agent_fe); |
| 4460 | conf->agent_fe.id = conf->agent->id; |
| 4461 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4462 | conf->agent_fe.options |= curpxopts; |
| 4463 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4464 | |
| 4465 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
| 4466 | LIST_DEL(&logsrv->list); |
| 4467 | LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list); |
| 4468 | } |
| 4469 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4470 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 4471 | LIST_DEL(&ph->list); |
| 4472 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4473 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4474 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 4475 | LIST_DEL(&ph->list); |
| 4476 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4477 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4478 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4479 | struct arg arg; |
| 4480 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4481 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4482 | curagent->var_pfx, vph->name); |
| 4483 | |
| 4484 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4485 | arg.data.str.area = trash.area; |
| 4486 | arg.data.str.data = trash.data; |
Christopher Faulet | bf9bcb0 | 2019-05-13 10:39:36 +0200 | [diff] [blame] | 4487 | 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] | 4488 | if (!vars_check_arg(&arg, err)) { |
| 4489 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4490 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4491 | goto error; |
| 4492 | } |
| 4493 | |
| 4494 | LIST_DEL(&vph->list); |
| 4495 | free(vph->name); |
| 4496 | free(vph); |
| 4497 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4498 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4499 | LIST_DEL(&grp->list); |
| 4500 | spoe_release_group(grp); |
| 4501 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4502 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4503 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4504 | fconf->ops = &spoe_ops; |
| 4505 | fconf->conf = conf; |
| 4506 | return 0; |
| 4507 | |
| 4508 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4509 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4510 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 4511 | LIST_DEL(&ph->list); |
| 4512 | spoe_release_placeholder(ph); |
| 4513 | } |
| 4514 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 4515 | LIST_DEL(&ph->list); |
| 4516 | spoe_release_placeholder(ph); |
| 4517 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4518 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4519 | LIST_DEL(&vph->list); |
| 4520 | free(vph->name); |
| 4521 | free(vph); |
| 4522 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4523 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4524 | LIST_DEL(&grp->list); |
| 4525 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4526 | } |
| 4527 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
| 4528 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4529 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4530 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4531 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
| 4532 | LIST_DEL(&logsrv->list); |
| 4533 | free(logsrv); |
| 4534 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4535 | free(conf); |
| 4536 | return -1; |
| 4537 | } |
| 4538 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4539 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4540 | * associated to a "send-spoe-group" action. |
| 4541 | * |
| 4542 | * It returns ACT_RET_CONT is processing is finished without error, it returns |
| 4543 | * ACT_RET_YIELD if the action is in progress. Otherwise it returns |
| 4544 | * ACT_RET_ERR. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4545 | static enum act_return |
| 4546 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4547 | struct session *sess, struct stream *s, int flags) |
| 4548 | { |
| 4549 | struct filter *filter; |
| 4550 | struct spoe_agent *agent = NULL; |
| 4551 | struct spoe_group *group = NULL; |
| 4552 | struct spoe_context *ctx = NULL; |
| 4553 | int ret, dir; |
| 4554 | |
| 4555 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4556 | if (filter->config == rule->arg.act.p[0]) { |
| 4557 | agent = rule->arg.act.p[2]; |
| 4558 | group = rule->arg.act.p[3]; |
| 4559 | ctx = filter->ctx; |
| 4560 | break; |
| 4561 | } |
| 4562 | } |
| 4563 | if (agent == NULL || group == NULL || ctx == NULL) |
| 4564 | return ACT_RET_ERR; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4565 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4566 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4567 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4568 | switch (rule->from) { |
| 4569 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4570 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4571 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4572 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4573 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4574 | default: |
| 4575 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4576 | " - internal error while execute spoe-send-group\n", |
| 4577 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4578 | __FUNCTION__, s); |
| 4579 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4580 | agent->id); |
| 4581 | return ACT_RET_CONT; |
| 4582 | } |
| 4583 | |
| 4584 | ret = spoe_process_group(s, ctx, group, dir); |
| 4585 | if (ret == 1) |
| 4586 | return ACT_RET_CONT; |
| 4587 | else if (ret == 0) { |
| 4588 | if (flags & ACT_FLAG_FINAL) { |
| 4589 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4590 | " - failed to process group '%s': interrupted by caller\n", |
| 4591 | (int)now.tv_sec, (int)now.tv_usec, |
| 4592 | agent->id, __FUNCTION__, s, group->id); |
| 4593 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4594 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4595 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4596 | return ACT_RET_CONT; |
| 4597 | } |
| 4598 | return ACT_RET_YIELD; |
| 4599 | } |
| 4600 | else |
| 4601 | return ACT_RET_ERR; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4605 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4606 | * action should be: |
| 4607 | * |
| 4608 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4609 | * |
| 4610 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4611 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4612 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4613 | * group. |
| 4614 | * |
| 4615 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4616 | * filled. |
| 4617 | */ |
| 4618 | static int |
| 4619 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4620 | { |
| 4621 | struct flt_conf *fconf; |
| 4622 | struct spoe_config *conf; |
| 4623 | struct spoe_agent *agent = NULL; |
| 4624 | struct spoe_group *group; |
| 4625 | struct spoe_message *msg; |
| 4626 | char *engine_id = rule->arg.act.p[0]; |
| 4627 | char *group_id = rule->arg.act.p[1]; |
| 4628 | unsigned int where = 0; |
| 4629 | |
| 4630 | switch (rule->from) { |
| 4631 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4632 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4633 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4634 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4635 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4636 | default: |
| 4637 | memprintf(err, |
| 4638 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4639 | rule->from); |
| 4640 | goto error; |
| 4641 | } |
| 4642 | |
| 4643 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4644 | * <px> */ |
| 4645 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4646 | conf = fconf->conf; |
| 4647 | |
| 4648 | /* This is not an SPOE filter */ |
| 4649 | if (fconf->id != spoe_filter_id) |
| 4650 | continue; |
| 4651 | |
| 4652 | /* This is the good engine */ |
| 4653 | if (!strcmp(conf->id, engine_id)) { |
| 4654 | agent = conf->agent; |
| 4655 | break; |
| 4656 | } |
| 4657 | } |
| 4658 | if (agent == NULL) { |
| 4659 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4660 | engine_id, group_id); |
| 4661 | goto error; |
| 4662 | } |
| 4663 | |
| 4664 | /* Try to find the right group */ |
| 4665 | list_for_each_entry(group, &agent->groups, list) { |
| 4666 | /* This is the good group */ |
| 4667 | if (!strcmp(group->id, group_id)) |
| 4668 | break; |
| 4669 | } |
| 4670 | if (&group->list == &agent->groups) { |
| 4671 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4672 | group_id, engine_id); |
| 4673 | goto error; |
| 4674 | } |
| 4675 | |
| 4676 | /* Ok, we found the group, we need to check messages and their |
| 4677 | * arguments */ |
| 4678 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4679 | struct spoe_arg *arg; |
| 4680 | |
| 4681 | list_for_each_entry(arg, &msg->args, list) { |
| 4682 | if (!(arg->expr->fetch->val & where)) { |
| 4683 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4684 | "some args extract information from '%s'," |
| 4685 | "none of which is available here ('%s')", |
| 4686 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4687 | sample_ckp_names(arg->expr->fetch->use), |
| 4688 | sample_ckp_names(where)); |
| 4689 | goto error; |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | |
| 4694 | free(engine_id); |
| 4695 | free(group_id); |
| 4696 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4697 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4698 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4699 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4700 | return 1; |
| 4701 | |
| 4702 | error: |
| 4703 | free(engine_id); |
| 4704 | free(group_id); |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | /* Parse 'send-spoe-group' action following the format: |
| 4709 | * |
| 4710 | * ... send-spoe-group <engine-id> <group-id> |
| 4711 | * |
| 4712 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4713 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4714 | * ids are saved and used later, when the rule will be checked. |
| 4715 | */ |
| 4716 | static enum act_parse_ret |
| 4717 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4718 | struct act_rule *rule, char **err) |
| 4719 | { |
| 4720 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4721 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4722 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4723 | return ACT_RET_PRS_ERR; |
| 4724 | } |
| 4725 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4726 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4727 | |
| 4728 | (*orig_arg) += 2; |
| 4729 | |
| 4730 | rule->action = ACT_CUSTOM; |
| 4731 | rule->action_ptr = spoe_send_group; |
| 4732 | rule->check_ptr = check_send_spoe_group; |
| 4733 | return ACT_RET_PRS_OK; |
| 4734 | } |
| 4735 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4736 | |
| 4737 | /* Declare the filter parser for "spoe" keyword */ |
| 4738 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4739 | { "spoe", parse_spoe_flt, NULL }, |
| 4740 | { NULL, NULL, NULL }, |
| 4741 | } |
| 4742 | }; |
| 4743 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4744 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4745 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4746 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4747 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4748 | { "send-spoe-group", parse_send_spoe_group }, |
| 4749 | { /* END */ }, |
| 4750 | } |
| 4751 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4752 | |
| 4753 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws); |
| 4754 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4755 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4756 | { "send-spoe-group", parse_send_spoe_group }, |
| 4757 | { /* END */ }, |
| 4758 | } |
| 4759 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4760 | |
| 4761 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4762 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4763 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4764 | { "send-spoe-group", parse_send_spoe_group }, |
| 4765 | { /* END */ }, |
| 4766 | } |
| 4767 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4768 | |
| 4769 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4770 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4771 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4772 | { "send-spoe-group", parse_send_spoe_group }, |
| 4773 | { /* END */ }, |
| 4774 | } |
| 4775 | }; |
| 4776 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4777 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |