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