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 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 15 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 20 | #include <haproxy/filters.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 21 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 22 | #include <haproxy/frontend.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 23 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 24 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 25 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 26 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 27 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/sample.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 29 | #include <haproxy/session.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/signal.h> |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 31 | #include <haproxy/sink.h> |
Willy Tarreau | 6c58ab0 | 2020-06-04 22:35:49 +0200 | [diff] [blame] | 32 | #include <haproxy/spoe.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 35 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 36 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/thread.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 39 | #include <haproxy/vars.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 40 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 41 | |
| 42 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 43 | #define SPOE_PRINTF(x...) fprintf(x) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 44 | #define SPOE_DEBUG_STMT(statement) statement |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 45 | #else |
| 46 | #define SPOE_PRINTF(x...) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 47 | #define SPOE_DEBUG_STMT(statement) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 50 | /* Reserved 4 bytes to the frame size. So a frame and its size can be written |
| 51 | * together in a buffer */ |
| 52 | #define MAX_FRAME_SIZE global.tune.bufsize - 4 |
| 53 | |
| 54 | /* The minimum size for a frame */ |
| 55 | #define MIN_FRAME_SIZE 256 |
| 56 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 57 | /* Reserved for the metadata and the frame type. |
| 58 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 59 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 60 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 61 | /* Helper to get SPOE ctx inside an appctx */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 62 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr)) |
| 63 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 64 | /* SPOE filter id. Used to identify SPOE filters */ |
| 65 | const char *spoe_filter_id = "SPOE filter"; |
| 66 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 67 | /* Set if the handle on SIGUSR1 is registered */ |
| 68 | static int sighandler_registered = 0; |
| 69 | |
| 70 | /* proxy used during the parsing */ |
| 71 | struct proxy *curproxy = NULL; |
| 72 | |
| 73 | /* The name of the SPOE engine, used during the parsing */ |
| 74 | char *curengine = NULL; |
| 75 | |
| 76 | /* SPOE agent used during the parsing */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 77 | /* SPOE agent/group/message used during the parsing */ |
| 78 | struct spoe_agent *curagent = NULL; |
| 79 | struct spoe_group *curgrp = NULL; |
| 80 | struct spoe_message *curmsg = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 81 | |
| 82 | /* list of SPOE messages and placeholders used during the parsing */ |
| 83 | struct list curmsgs; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 84 | struct list curgrps; |
| 85 | struct list curmphs; |
| 86 | struct list curgphs; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 87 | struct list curvars; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 88 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 89 | /* list of log servers used during the parsing */ |
| 90 | struct list curlogsrvs; |
| 91 | |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 92 | /* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */ |
| 93 | int curpxopts; |
| 94 | int curpxopts2; |
| 95 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 96 | /* Pools used to allocate SPOE structs */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 97 | DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context)); |
| 98 | 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] | 99 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 100 | struct flt_ops spoe_ops; |
| 101 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 102 | static int spoe_queue_context(struct spoe_context *ctx); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 103 | static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
| 104 | 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] | 105 | |
| 106 | /******************************************************************** |
| 107 | * helper functions/globals |
| 108 | ********************************************************************/ |
| 109 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 110 | spoe_release_placeholder(struct spoe_placeholder *ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 111 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 112 | if (!ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 113 | return; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 114 | free(ph->id); |
| 115 | free(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 118 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 119 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 120 | { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 121 | struct spoe_arg *arg, *argback; |
| 122 | struct acl *acl, *aclback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 123 | |
| 124 | if (!msg) |
| 125 | return; |
| 126 | free(msg->id); |
| 127 | free(msg->conf.file); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 128 | list_for_each_entry_safe(arg, argback, &msg->args, list) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 129 | release_sample_expr(arg->expr); |
| 130 | free(arg->name); |
| 131 | LIST_DEL(&arg->list); |
| 132 | free(arg); |
| 133 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 134 | list_for_each_entry_safe(acl, aclback, &msg->acls, list) { |
| 135 | LIST_DEL(&acl->list); |
| 136 | prune_acl(acl); |
| 137 | free(acl); |
| 138 | } |
| 139 | if (msg->cond) { |
| 140 | prune_acl_cond(msg->cond); |
| 141 | free(msg->cond); |
| 142 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 143 | free(msg); |
| 144 | } |
| 145 | |
| 146 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 147 | spoe_release_group(struct spoe_group *grp) |
| 148 | { |
| 149 | if (!grp) |
| 150 | return; |
| 151 | free(grp->id); |
| 152 | free(grp->conf.file); |
| 153 | free(grp); |
| 154 | } |
| 155 | |
| 156 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 157 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 158 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 159 | struct spoe_message *msg, *msgback; |
| 160 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 161 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 162 | |
| 163 | if (!agent) |
| 164 | return; |
| 165 | free(agent->id); |
| 166 | free(agent->conf.file); |
| 167 | free(agent->var_pfx); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 168 | free(agent->var_on_error); |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 169 | free(agent->var_t_process); |
| 170 | free(agent->var_t_total); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 171 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
| 172 | LIST_DEL(&msg->list); |
| 173 | spoe_release_message(msg); |
| 174 | } |
| 175 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
| 176 | LIST_DEL(&grp->list); |
| 177 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 178 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 179 | if (agent->rt) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 180 | for (i = 0; i < global.nbthread; ++i) { |
| 181 | free(agent->rt[i].engine_id); |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 182 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 183 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 184 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 185 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 186 | free(agent); |
| 187 | } |
| 188 | |
| 189 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 190 | [SPOE_FRM_ERR_NONE] = "normal", |
| 191 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 192 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 193 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 194 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 195 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 196 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 197 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 198 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 199 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 200 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 201 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 202 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 203 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 204 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 208 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 209 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 210 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 211 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 212 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 213 | |
| 214 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 215 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 216 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 217 | }; |
| 218 | |
| 219 | |
| 220 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 221 | |
| 222 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 223 | [SPOE_CTX_ST_NONE] = "NONE", |
| 224 | [SPOE_CTX_ST_READY] = "READY", |
| 225 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 226 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 227 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 228 | [SPOE_CTX_ST_DONE] = "DONE", |
| 229 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 233 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 234 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 235 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 236 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 237 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 238 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 239 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 240 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 241 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 242 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 246 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 247 | /* Used to generates a unique id for an engine. On success, it returns a |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 248 | * allocated string. So it is the caller's responsibility to release it. If the |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 249 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 250 | static char * |
| 251 | generate_pseudo_uuid() |
| 252 | { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 253 | ha_generate_uuid(&trash); |
Kevin Zhu | 079f808 | 2020-03-13 10:39:51 +0800 | [diff] [blame] | 254 | return my_strndup(trash.area, trash.data); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 257 | |
| 258 | static inline void |
| 259 | spoe_update_stat_time(struct timeval *tv, long *t) |
| 260 | { |
| 261 | if (*t == -1) |
| 262 | *t = tv_ms_elapsed(tv, &now); |
| 263 | else |
| 264 | *t += tv_ms_elapsed(tv, &now); |
| 265 | tv_zero(tv); |
| 266 | } |
| 267 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 268 | /******************************************************************** |
| 269 | * Functions that encode/decode SPOE frames |
| 270 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 271 | /* Helper to get static string length, excluding the terminating null byte */ |
| 272 | #define SLEN(str) (sizeof(str)-1) |
| 273 | |
| 274 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 275 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 276 | #define VERSION_KEY "version" |
| 277 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 278 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 279 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 280 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 281 | #define STATUS_CODE_KEY "status-code" |
| 282 | #define MSG_KEY "message" |
| 283 | |
| 284 | struct spoe_version { |
| 285 | char *str; |
| 286 | int min; |
| 287 | int max; |
| 288 | }; |
| 289 | |
| 290 | /* All supported versions */ |
| 291 | static struct spoe_version supported_versions[] = { |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 292 | /* 1.0 is now unsupported because of a bug about frame's flags*/ |
| 293 | {"2.0", 2000, 2000}, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 294 | {NULL, 0, 0} |
| 295 | }; |
| 296 | |
| 297 | /* Comma-separated list of supported versions */ |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 298 | #define SUPPORTED_VERSIONS_VAL "2.0" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 299 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 300 | /* Convert a string to a SPOE version value. The string must follow the format |
| 301 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 302 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 303 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 304 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 305 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 306 | const char *p, *end; |
| 307 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 308 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 309 | p = str; |
| 310 | end = str+len; |
| 311 | maj = min = 0; |
| 312 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 313 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 314 | /* skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 315 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 316 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 317 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 318 | /* parse Major number, until the '.' */ |
| 319 | while (*p != '.') { |
| 320 | if (p >= end || *p < '0' || *p > '9') |
| 321 | goto out; |
| 322 | maj *= 10; |
| 323 | maj += (*p - '0'); |
| 324 | p++; |
| 325 | } |
| 326 | |
| 327 | /* check Major version */ |
| 328 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 329 | goto out; |
| 330 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 331 | p++; /* skip the '.' */ |
| 332 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 333 | goto out; |
| 334 | |
| 335 | /* Parse Minor number */ |
| 336 | while (p < end) { |
| 337 | if (*p < '0' || *p > '9') |
| 338 | break; |
| 339 | min *= 10; |
| 340 | min += (*p - '0'); |
| 341 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 342 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 343 | |
| 344 | /* check Minor number */ |
| 345 | if (min > 999) |
| 346 | goto out; |
| 347 | |
| 348 | /* skip trailing spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 349 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 350 | p++; |
| 351 | if (p != end) |
| 352 | goto out; |
| 353 | |
| 354 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 355 | out: |
| 356 | return vsn; |
| 357 | } |
| 358 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 359 | /* 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] | 360 | * 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] | 361 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 362 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 363 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 364 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 365 | struct buffer *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 366 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 367 | char *p, *end; |
| 368 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 369 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 370 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 371 | p = frame; |
| 372 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 373 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 374 | /* Set Frame type */ |
| 375 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 376 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 377 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 378 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 379 | memcpy(p, (char *)&flags, 4); |
| 380 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 381 | |
| 382 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 383 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 384 | |
| 385 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 386 | * and "capabilities" */ |
| 387 | |
| 388 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 389 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 390 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 391 | goto too_big; |
| 392 | |
| 393 | *p++ = SPOE_DATA_T_STR; |
| 394 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 395 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 396 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 397 | |
| 398 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 399 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 400 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 401 | goto too_big; |
| 402 | |
| 403 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 404 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 405 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 406 | |
| 407 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 408 | sz = SLEN(CAPABILITIES_KEY); |
| 409 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 410 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 411 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 412 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 413 | chk = get_trash_chunk(); |
| 414 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 415 | memcpy(chk->area, "pipelining", 10); |
| 416 | chk->data += 10; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 417 | } |
| 418 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 419 | if (chk->data) chk->area[chk->data++] = ','; |
| 420 | memcpy(chk->area+chk->data, "async", 5); |
| 421 | chk->data += 5; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 422 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 423 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 424 | if (chk->data) chk->area[chk->data++] = ','; |
| 425 | memcpy(chk->area+chk->data, "fragmentation", 13); |
Miroslav Zagorac | 6b3690b | 2019-01-13 16:55:01 +0100 | [diff] [blame] | 426 | chk->data += 13; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 427 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 428 | if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 429 | goto too_big; |
| 430 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 431 | /* (optional) "engine-id" K/V item, if present */ |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 432 | if (agent != NULL && agent->rt[tid].engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 433 | sz = SLEN(ENGINE_ID_KEY); |
| 434 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 435 | goto too_big; |
| 436 | |
| 437 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 438 | sz = strlen(agent->rt[tid].engine_id); |
| 439 | 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] | 440 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 443 | return (p - frame); |
| 444 | |
| 445 | too_big: |
| 446 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 447 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 450 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 451 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 452 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 453 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 454 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 455 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 456 | const char *reason; |
| 457 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 458 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 459 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 460 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 461 | p = frame; |
| 462 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 463 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 464 | /* Set Frame type */ |
| 465 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 466 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 467 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 468 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 469 | memcpy(p, (char *)&flags, 4); |
| 470 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 471 | |
| 472 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 473 | *p++ = 0; *p++ = 0; |
| 474 | |
| 475 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 476 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 477 | |
| 478 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 479 | |
| 480 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 481 | sz = SLEN(STATUS_CODE_KEY); |
| 482 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 483 | goto too_big; |
| 484 | |
| 485 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 486 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 487 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 488 | |
| 489 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 490 | sz = SLEN(MSG_KEY); |
| 491 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 492 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 493 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 494 | /*Get the message corresponding to the status code */ |
| 495 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 496 | |
| 497 | *p++ = SPOE_DATA_T_STR; |
| 498 | sz = strlen(reason); |
| 499 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 500 | goto too_big; |
| 501 | |
| 502 | return (p - frame); |
| 503 | |
| 504 | too_big: |
| 505 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 506 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 509 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 510 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 511 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 512 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 513 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 514 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 515 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 516 | char *p, *end; |
| 517 | unsigned int stream_id, frame_id; |
| 518 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 519 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 520 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 521 | p = frame; |
| 522 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 523 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 524 | stream_id = ctx->stream_id; |
| 525 | frame_id = ctx->frame_id; |
| 526 | |
| 527 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 528 | /* The fragmentation is not supported by the applet */ |
| 529 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 530 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 531 | return -1; |
| 532 | } |
| 533 | flags = ctx->frag_ctx.flags; |
| 534 | } |
| 535 | |
| 536 | /* Set Frame type */ |
| 537 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 538 | |
| 539 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 540 | flags = htonl(flags); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 541 | memcpy(p, (char *)&flags, 4); |
| 542 | p += 4; |
| 543 | |
| 544 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 545 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 546 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 547 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 548 | goto too_big; |
| 549 | |
| 550 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 551 | sz = b_data(&ctx->buffer); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 552 | if (p + sz >= end) |
| 553 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 554 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 555 | p += sz; |
| 556 | |
| 557 | return (p - frame); |
| 558 | |
| 559 | too_big: |
| 560 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 565 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 566 | * error occurred and -1 if a fatal error occurred. */ |
| 567 | static int |
| 568 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 569 | char *frame, size_t size) |
| 570 | { |
| 571 | char *p, *end; |
| 572 | unsigned int stream_id, frame_id; |
| 573 | unsigned int flags; |
| 574 | size_t sz; |
| 575 | |
| 576 | p = frame; |
| 577 | end = frame+size; |
| 578 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 579 | /* <ctx> is null when the stream has aborted the processing of a |
| 580 | * fragmented frame. In this case, we must notify the corresponding |
| 581 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 582 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 583 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 584 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 585 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 586 | } |
| 587 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 588 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 589 | stream_id = ctx->stream_id; |
| 590 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 593 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 594 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 595 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 596 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 597 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 598 | memcpy(p, (char *)&flags, 4); |
| 599 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 600 | |
| 601 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 602 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 603 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 604 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 605 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 606 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 607 | if (ctx == NULL) |
| 608 | goto end; |
| 609 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 610 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 611 | sz = b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 612 | if (p + sz >= end) |
| 613 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 614 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 615 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 616 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 617 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 618 | return (p - frame); |
| 619 | |
| 620 | too_big: |
| 621 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 622 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 625 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 626 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 627 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 628 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 629 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 630 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 631 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 632 | char *p, *end; |
| 633 | int vsn, max_frame_size; |
| 634 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 635 | |
| 636 | p = frame; |
| 637 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 638 | |
| 639 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 640 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 641 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 642 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 643 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 644 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 645 | if (size < 7 /* TYPE + METADATA */) { |
| 646 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 647 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 650 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 651 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 652 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 653 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 654 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 655 | /* Fragmentation is not supported for HELLO frame */ |
| 656 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 657 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 658 | return -1; |
| 659 | } |
| 660 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 661 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 662 | if (*p != 0 || *(p+1) != 0) { |
| 663 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 664 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 665 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 666 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 667 | |
| 668 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 669 | * "capabilities" */ |
| 670 | |
| 671 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 672 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 673 | while (p < end) { |
| 674 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 675 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 676 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 677 | |
| 678 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 679 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 680 | if (ret == -1 || !sz) { |
| 681 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 682 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 683 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 684 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 685 | /* Check "version" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 686 | if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 687 | int i, type = *p++; |
| 688 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 689 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 690 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 691 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 692 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 693 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 694 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 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 | } |
| 698 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 699 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 700 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 701 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 702 | return -1; |
| 703 | } |
| 704 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 705 | if (vsn >= supported_versions[i].min && |
| 706 | vsn <= supported_versions[i].max) |
| 707 | break; |
| 708 | } |
| 709 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 710 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 711 | return -1; |
| 712 | } |
| 713 | } |
| 714 | /* Check "max-frame-size" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 715 | 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] | 716 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 717 | |
| 718 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 719 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 720 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 721 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 722 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 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 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 726 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 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 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 730 | if (sz < MIN_FRAME_SIZE || |
| 731 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 732 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 733 | return -1; |
| 734 | } |
| 735 | max_frame_size = sz; |
| 736 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 737 | /* Check "capabilities" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 738 | 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] | 739 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 740 | |
| 741 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 742 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 743 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 744 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 745 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 746 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 747 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 748 | return 0; |
| 749 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 750 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 751 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 752 | char *delim; |
| 753 | |
| 754 | /* Skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 755 | for (; isspace((unsigned char)*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 756 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 757 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 758 | str += 10; sz -= 10; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 759 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 760 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 761 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 762 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 763 | str += 5; sz -= 5; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 764 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 765 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 766 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 767 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 768 | str += 13; sz -= 13; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 769 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 770 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 771 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 772 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 773 | /* Get the next comma or break */ |
| 774 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 775 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 776 | delim++; |
| 777 | sz -= (delim - str); |
| 778 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 779 | } |
| 780 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 781 | else { |
| 782 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 783 | if (spoe_skip_data(&p, end) == -1) { |
| 784 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 785 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 786 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
| 790 | /* Final checks */ |
| 791 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 792 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 793 | return -1; |
| 794 | } |
| 795 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 796 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 797 | return -1; |
| 798 | } |
Christopher Faulet | 1138901 | 2019-02-07 16:13:26 +0100 | [diff] [blame] | 799 | if (!agent) |
| 800 | flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC); |
| 801 | else { |
| 802 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 803 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 804 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 805 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
| 806 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 807 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 808 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 809 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 810 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 811 | |
| 812 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 816 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 817 | * occurred. */ |
| 818 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 819 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 820 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 821 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 822 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 823 | |
| 824 | p = frame; |
| 825 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 826 | |
| 827 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 828 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 829 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 830 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 831 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 832 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 833 | if (size < 7 /* TYPE + METADATA */) { |
| 834 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 835 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 838 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 839 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 840 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 841 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 842 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 843 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 844 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 845 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 846 | return -1; |
| 847 | } |
| 848 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 849 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 850 | if (*p != 0 || *(p+1) != 0) { |
| 851 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 852 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 853 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 854 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 855 | |
| 856 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 857 | |
| 858 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 859 | while (p < end) { |
| 860 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 861 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 862 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 863 | |
| 864 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 865 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 866 | if (ret == -1 || !sz) { |
| 867 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 868 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* Check "status-code" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 872 | 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] | 873 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 874 | |
| 875 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 876 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 877 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 878 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 879 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 880 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 881 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 882 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 883 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 884 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 885 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 886 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 887 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* Check "message" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 891 | 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] | 892 | int type = *p++; |
| 893 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 894 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 895 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 896 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 897 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 898 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 899 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 900 | if (ret == -1 || sz > 255) { |
| 901 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 902 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 903 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 904 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 905 | SPOE_APPCTX(appctx)->reason = str; |
| 906 | SPOE_APPCTX(appctx)->rlen = sz; |
| 907 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 908 | } |
| 909 | else { |
| 910 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 911 | if (spoe_skip_data(&p, end) == -1) { |
| 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 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 918 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 922 | /* 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] | 923 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 924 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 925 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 926 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 927 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 928 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 929 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 930 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 931 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 932 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 933 | |
| 934 | p = frame; |
| 935 | end = frame + size; |
| 936 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 937 | |
| 938 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 939 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 940 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 941 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 942 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 943 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 944 | if (size < 7 /* TYPE + METADATA */) { |
| 945 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 946 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 949 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 950 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 951 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 952 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 953 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 954 | /* Fragmentation is not supported for now */ |
| 955 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 956 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 957 | return -1; |
| 958 | } |
| 959 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 960 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 961 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 962 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 963 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 964 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 965 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 966 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 967 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 968 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 969 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 970 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 971 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 972 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 973 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 974 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 975 | goto found; |
| 976 | } |
| 977 | } |
| 978 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 979 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 980 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 981 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 982 | goto found; |
| 983 | } |
| 984 | } |
| 985 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 986 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 987 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 988 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 989 | |
| 990 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 991 | if (flags & SPOE_FRM_FL_ABRT) { |
| 992 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 993 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 994 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 995 | /* Ignore the payload */ |
| 996 | goto end; |
| 997 | } |
| 998 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 999 | /* For now, we ignore the ack */ |
| 1000 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1001 | return 0; |
| 1002 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1003 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1004 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1005 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1006 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1007 | " - stream-id=%u - frame-id=%u\n", |
| 1008 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1009 | __FUNCTION__, appctx, |
| 1010 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1011 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1012 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | c7ba910 | 2020-11-10 14:31:39 +0100 | [diff] [blame] | 1013 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
| 1014 | /* Report an error if we are waiting the ack for another frame, |
| 1015 | * but not if there is no longer frame waiting for a ack |
| 1016 | * (timeout) |
| 1017 | */ |
| 1018 | if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) || |
| 1019 | SPOE_APPCTX(appctx)->frag_ctx.ctx) |
| 1020 | return -1; |
| 1021 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1022 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
| 1023 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1024 | return 0; |
| 1025 | |
| 1026 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1027 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1028 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1029 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1030 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1031 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1032 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1033 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1034 | len = (end - p); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1035 | memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len); |
| 1036 | b_set_data(&SPOE_APPCTX(appctx)->buffer, len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1037 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1038 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1039 | /* Transfer the buffer ownership to the SPOE context */ |
| 1040 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1041 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1042 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1043 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1044 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1045 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1046 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1047 | " - ACK frame received" |
| 1048 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1049 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1050 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1051 | (*ctx)->frame_id, flags); |
| 1052 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1055 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1056 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1057 | * success and -1 if an error occurred. */ |
| 1058 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1059 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1060 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1061 | struct appctx appctx; |
| 1062 | struct spoe_appctx spoe_appctx; |
| 1063 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1064 | size_t sz; |
| 1065 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1066 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1067 | memset(&appctx, 0, sizeof(appctx)); |
| 1068 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1069 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1070 | |
| 1071 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1072 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1073 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1074 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1075 | end = frame + MAX_FRAME_SIZE; |
| 1076 | |
| 1077 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1078 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1079 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1080 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1081 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1082 | /* Add "healthcheck" K/V item */ |
| 1083 | sz = SLEN(HEALTHCHECK_KEY); |
| 1084 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1085 | return -1; |
| 1086 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1087 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1088 | *len = frame - buf; |
| 1089 | sz = htonl(*len - 4); |
| 1090 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1091 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1092 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1093 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1094 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1099 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1100 | * success and -1 if an error occurred. */ |
| 1101 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1102 | 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] | 1103 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1104 | struct appctx appctx; |
| 1105 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1106 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1107 | memset(&appctx, 0, sizeof(appctx)); |
| 1108 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1109 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1110 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1111 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1112 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1113 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1114 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1115 | goto error; |
| 1116 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1117 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1118 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1119 | |
| 1120 | return 0; |
| 1121 | |
| 1122 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1123 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1124 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1125 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1126 | return -1; |
| 1127 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1128 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1129 | /* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1130 | * the frame can be ignored, 1 to retry later, and the frame length on |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1131 | * success. */ |
| 1132 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1133 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1134 | { |
| 1135 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1136 | int ret; |
| 1137 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1138 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1139 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1140 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1141 | netint = htonl(framesz); |
| 1142 | memcpy(buf, (char *)&netint, 4); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1143 | ret = ci_putblk(si_ic(si), buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1144 | if (ret <= 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1145 | if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1146 | si_rx_room_blk(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1147 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1148 | } |
| 1149 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1150 | return -1; /* error */ |
| 1151 | } |
| 1152 | return framesz; |
| 1153 | } |
| 1154 | |
| 1155 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1156 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1157 | * success. */ |
| 1158 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1159 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1160 | { |
| 1161 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1162 | int ret; |
| 1163 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1164 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1165 | ret = co_getblk(si_oc(si), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1166 | if (ret > 0) { |
| 1167 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1168 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1169 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1170 | return -1; |
| 1171 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1172 | ret = co_getblk(si_oc(si), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1173 | } |
| 1174 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1175 | if (ret == 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1176 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1177 | } |
| 1178 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1179 | return -1; /* error */ |
| 1180 | } |
| 1181 | return framesz; |
| 1182 | } |
| 1183 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1184 | /******************************************************************** |
| 1185 | * Functions that manage the SPOE applet |
| 1186 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1187 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1188 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1189 | { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1190 | si_want_get(appctx->owner); |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1191 | si_rx_endp_more(appctx->owner); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1192 | appctx_wakeup(appctx); |
| 1193 | return 1; |
| 1194 | } |
| 1195 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1196 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1197 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1198 | static struct task * |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1199 | spoe_process_appctx(struct task * task, void *context, unsigned int state) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1200 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1201 | struct appctx *appctx = context; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1202 | |
| 1203 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1204 | if (tick_is_expired(task->expire, now_ms)) { |
| 1205 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1206 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1207 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1208 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1209 | return task; |
| 1210 | } |
| 1211 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1212 | /* Callback function that releases a SPOE applet. This happens when the |
| 1213 | * connection with the agent is closed. */ |
| 1214 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1215 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1216 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1217 | struct stream_interface *si = appctx->owner; |
| 1218 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1219 | struct spoe_agent *agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1220 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1221 | |
| 1222 | if (spoe_appctx == NULL) |
| 1223 | return; |
| 1224 | |
| 1225 | appctx->ctx.spoe.ptr = NULL; |
| 1226 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1227 | |
| 1228 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1229 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1230 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1231 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1232 | /* Remove applet from the list of running applets */ |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1233 | _HA_ATOMIC_SUB(&agent->counters.applets, 1); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1234 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1235 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
| 1236 | LIST_DEL(&spoe_appctx->list); |
| 1237 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1238 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1239 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1240 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1241 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1242 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1243 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
| 1244 | eb32_delete(&spoe_appctx->node); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1245 | _HA_ATOMIC_SUB(&agent->counters.idles, 1); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1246 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1247 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1248 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1249 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1250 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1251 | |
| 1252 | si_shutw(si); |
| 1253 | si_shutr(si); |
| 1254 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1257 | /* Destroy the task attached to this applet */ |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1258 | task_destroy(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1259 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1260 | /* Notify all waiting streams */ |
| 1261 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1262 | LIST_DEL(&ctx->list); |
| 1263 | LIST_INIT(&ctx->list); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1264 | _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1265 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1266 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1267 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1268 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1269 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1272 | /* If the applet was processing a fragmented frame, notify the |
| 1273 | * corresponding stream. */ |
| 1274 | if (spoe_appctx->frag_ctx.ctx) { |
| 1275 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1276 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1277 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1278 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1279 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1280 | } |
| 1281 | |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1282 | if (!LIST_ISEMPTY(&agent->rt[tid].applets)) { |
| 1283 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1284 | if (ctx->spoe_appctx == spoe_appctx) |
| 1285 | ctx->spoe_appctx = NULL; |
| 1286 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1287 | goto end; |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1288 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1289 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1290 | /* If this was the last running applet, notify all waiting streams */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1291 | 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] | 1292 | LIST_DEL(&ctx->list); |
| 1293 | LIST_INIT(&ctx->list); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1294 | _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1295 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1296 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1297 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1298 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1299 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1300 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1301 | 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] | 1302 | LIST_DEL(&ctx->list); |
| 1303 | LIST_INIT(&ctx->list); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1304 | _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1305 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1306 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1307 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1308 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1309 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1310 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1311 | |
| 1312 | end: |
Christopher Faulet | 55ae8a6 | 2019-05-23 22:47:48 +0200 | [diff] [blame] | 1313 | /* Release allocated memory */ |
| 1314 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1315 | &spoe_appctx->buffer_wait); |
| 1316 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
| 1317 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1318 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1319 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1320 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1321 | 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] | 1322 | } |
| 1323 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1324 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1325 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1326 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1327 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1328 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1329 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1330 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1331 | |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1332 | if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) { |
| 1333 | /* closed */ |
| 1334 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1335 | goto exit; |
| 1336 | } |
| 1337 | |
Willy Tarreau | 4f283fa | 2019-06-05 14:34:03 +0200 | [diff] [blame] | 1338 | if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) { |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1339 | /* not connected yet */ |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1340 | si_rx_endp_more(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1341 | task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG); |
| 1342 | goto stop; |
| 1343 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1344 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1345 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1346 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1347 | " - Connection timed out\n", |
| 1348 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1349 | __FUNCTION__, appctx); |
| 1350 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1351 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1352 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1353 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1354 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1355 | SPOE_APPCTX(appctx)->task->expire = |
| 1356 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1357 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1358 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1359 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1360 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1361 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1362 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1363 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1364 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1365 | |
| 1366 | switch (ret) { |
| 1367 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1368 | case 0: /* ignore => an error, cannot be ignored */ |
| 1369 | goto exit; |
| 1370 | |
| 1371 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1372 | goto stop; |
| 1373 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1374 | default: |
| 1375 | /* HELLO frame successfully sent, now wait for the |
| 1376 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1377 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1378 | goto next; |
| 1379 | } |
| 1380 | |
| 1381 | next: |
| 1382 | return 0; |
| 1383 | stop: |
| 1384 | return 1; |
| 1385 | exit: |
| 1386 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1387 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1388 | } |
| 1389 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1390 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1391 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1392 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1393 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1394 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1395 | char *frame; |
| 1396 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1397 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1398 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1399 | 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] | 1400 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1401 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1402 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1403 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1404 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1405 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1406 | " - Connection timed out\n", |
| 1407 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1408 | __FUNCTION__, appctx); |
| 1409 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1410 | goto exit; |
| 1411 | } |
| 1412 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1413 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1414 | ret = spoe_recv_frame(appctx, frame, |
| 1415 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1416 | if (ret > 1) { |
| 1417 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1418 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1419 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1420 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1421 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1422 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1423 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1424 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1425 | switch (ret) { |
| 1426 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1427 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1428 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1429 | goto next; |
| 1430 | |
| 1431 | case 1: /* retry later */ |
| 1432 | goto stop; |
| 1433 | |
| 1434 | default: |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1435 | _HA_ATOMIC_ADD(&agent->counters.idles, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1436 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1437 | SPOE_APPCTX(appctx)->node.key = 0; |
| 1438 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1439 | |
| 1440 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1441 | 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] | 1442 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1443 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1444 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1445 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1446 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1447 | if (trash.data) |
| 1448 | co_skip(si_oc(si), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1449 | |
| 1450 | SPOE_APPCTX(appctx)->task->expire = |
| 1451 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1452 | return 0; |
| 1453 | stop: |
| 1454 | return 1; |
| 1455 | exit: |
| 1456 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1457 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1460 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1461 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1462 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1463 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1464 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1465 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1466 | char *frame, *buf; |
| 1467 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1468 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1469 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1470 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1471 | buf = trash.area; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1472 | |
| 1473 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1474 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1475 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1476 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1477 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1478 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1479 | *skip = 1; |
| 1480 | ret = 1; |
| 1481 | goto end; |
| 1482 | } |
| 1483 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1484 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1485 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1486 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1487 | |
| 1488 | } |
| 1489 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1490 | if (ret > 1) |
| 1491 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1492 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1493 | switch (ret) { |
| 1494 | case -1: /* error */ |
| 1495 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1496 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1497 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1498 | case 0: /* ignore */ |
| 1499 | if (ctx == NULL) |
| 1500 | goto abort_frag_frame; |
| 1501 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1502 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1503 | LIST_DEL(&ctx->list); |
| 1504 | LIST_INIT(&ctx->list); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1505 | _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1506 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1507 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1508 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1509 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1510 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1511 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1512 | break; |
| 1513 | |
| 1514 | case 1: /* retry */ |
| 1515 | *skip = 1; |
| 1516 | break; |
| 1517 | |
| 1518 | default: |
| 1519 | if (ctx == NULL) |
| 1520 | goto abort_frag_frame; |
| 1521 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1522 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1523 | LIST_DEL(&ctx->list); |
| 1524 | LIST_INIT(&ctx->list); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1525 | _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1526 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1527 | ctx->spoe_appctx = SPOE_APPCTX(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1528 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1529 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1530 | goto no_frag_frame_sent; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1531 | else |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1532 | goto frag_frame_sent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1533 | } |
| 1534 | goto end; |
| 1535 | |
| 1536 | frag_frame_sent: |
| 1537 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1538 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1539 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1540 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1541 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1542 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1543 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1544 | goto end; |
| 1545 | |
| 1546 | no_frag_frame_sent: |
| 1547 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1548 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1549 | LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1550 | } |
| 1551 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1552 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1553 | LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
| 1554 | } |
| 1555 | else { |
| 1556 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1557 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1558 | LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
| 1559 | } |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1560 | _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1561 | ctx->stats.tv_wait = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1562 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1563 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1564 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1565 | SPOE_APPCTX(appctx)->cur_fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1566 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1567 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1568 | goto end; |
| 1569 | |
| 1570 | abort_frag_frame: |
| 1571 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1572 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1573 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1574 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1575 | goto end; |
| 1576 | |
| 1577 | end: |
| 1578 | return ret; |
| 1579 | } |
| 1580 | |
| 1581 | static int |
| 1582 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1583 | { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 1584 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1585 | struct spoe_context *ctx = NULL; |
| 1586 | char *frame; |
| 1587 | int ret; |
| 1588 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1589 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1590 | ret = spoe_recv_frame(appctx, frame, |
| 1591 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1592 | if (ret > 1) { |
| 1593 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1594 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1595 | ret = -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1596 | goto end; |
| 1597 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1598 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1599 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1600 | } |
| 1601 | switch (ret) { |
| 1602 | case -1: /* error */ |
| 1603 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1604 | break; |
| 1605 | |
| 1606 | case 0: /* ignore */ |
| 1607 | break; |
| 1608 | |
| 1609 | case 1: /* retry */ |
| 1610 | *skip = 1; |
| 1611 | break; |
| 1612 | |
| 1613 | default: |
| 1614 | LIST_DEL(&ctx->list); |
| 1615 | LIST_INIT(&ctx->list); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1616 | _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1617 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1618 | ctx->stats.tv_response = now; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1619 | if (ctx->spoe_appctx) { |
| 1620 | ctx->spoe_appctx->cur_fpa--; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1621 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1622 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1623 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1624 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1625 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1626 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1627 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1628 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1629 | } |
| 1630 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1631 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1632 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1633 | break; |
| 1634 | } |
| 1635 | |
| 1636 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1637 | if (trash.data) |
| 1638 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1639 | end: |
| 1640 | return ret; |
| 1641 | } |
| 1642 | |
| 1643 | static int |
| 1644 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1645 | { |
| 1646 | struct stream_interface *si = appctx->owner; |
| 1647 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1648 | 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] | 1649 | |
| 1650 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1651 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1652 | goto exit; |
| 1653 | } |
| 1654 | |
| 1655 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1656 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1657 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1658 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1659 | goto next; |
| 1660 | } |
| 1661 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1662 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1663 | " - 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] | 1664 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1665 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1666 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1667 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1668 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1669 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1670 | skip_sending = 1; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1671 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1672 | /* receiving_frame loop */ |
| 1673 | while (!skip_receiving) { |
| 1674 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1675 | switch (ret) { |
| 1676 | case -1: /* error */ |
| 1677 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1678 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1679 | case 0: /* ignore */ |
| 1680 | active_r = 1; |
| 1681 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1682 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1683 | case 1: /* retry */ |
| 1684 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1685 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1686 | default: |
| 1687 | active_r = 1; |
| 1688 | break; |
| 1689 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1690 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1691 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1692 | /* send_frame loop */ |
| 1693 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1694 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1695 | switch (ret) { |
| 1696 | case -1: /* error */ |
| 1697 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1698 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1699 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1700 | if (SPOE_APPCTX(appctx)->node.key) |
| 1701 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1702 | active_s++; |
| 1703 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1704 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1705 | case 1: /* retry */ |
| 1706 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1707 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1708 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1709 | if (SPOE_APPCTX(appctx)->node.key) |
| 1710 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1711 | active_s++; |
| 1712 | break; |
| 1713 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1714 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1715 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1716 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1717 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1718 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1719 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1720 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1721 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1722 | struct server *srv = objt_server(si_strm(si)->target); |
| 1723 | |
| 1724 | /* With several threads, close the applet if there are pending |
| 1725 | * connections or if the server is full. Otherwise, add the |
| 1726 | * applet in the idle list. |
| 1727 | */ |
| 1728 | if (global.nbthread > 1 && |
| 1729 | (agent->b.be->nbpend || |
| 1730 | (srv && (srv->nbpend || (srv->maxconn && srv->served >=srv_dynamic_maxconn(srv)))))) { |
| 1731 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1732 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1733 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1734 | goto next; |
| 1735 | } |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1736 | _HA_ATOMIC_ADD(&agent->counters.idles, 1); |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1737 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1738 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1739 | } |
| 1740 | return 1; |
| 1741 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1742 | next: |
| 1743 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1744 | return 0; |
| 1745 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1746 | exit: |
| 1747 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1752 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1753 | { |
| 1754 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1755 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1756 | char *frame, *buf; |
| 1757 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1758 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1759 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1760 | goto exit; |
| 1761 | |
| 1762 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1763 | goto exit; |
| 1764 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1765 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1766 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1767 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1768 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1769 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1770 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1771 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1772 | |
| 1773 | switch (ret) { |
| 1774 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1775 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1776 | goto exit; |
| 1777 | |
| 1778 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1779 | goto stop; |
| 1780 | |
| 1781 | default: |
| 1782 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1783 | " - disconnected by HAProxy (%d): %s\n", |
| 1784 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1785 | __FUNCTION__, appctx, |
| 1786 | SPOE_APPCTX(appctx)->status_code, |
| 1787 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1788 | |
| 1789 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1790 | goto next; |
| 1791 | } |
| 1792 | |
| 1793 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1794 | SPOE_APPCTX(appctx)->task->expire = |
| 1795 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1796 | return 0; |
| 1797 | stop: |
| 1798 | return 1; |
| 1799 | exit: |
| 1800 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
| 1804 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1805 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1806 | { |
| 1807 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1808 | char *frame; |
| 1809 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1810 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1811 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1812 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1813 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1814 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1815 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1816 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1817 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1818 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1819 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1820 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1821 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1822 | ret = spoe_recv_frame(appctx, frame, |
| 1823 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1824 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1825 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1826 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | switch (ret) { |
| 1830 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1831 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1832 | " - error on frame (%s)\n", |
| 1833 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1834 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1835 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1836 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1837 | goto exit; |
| 1838 | |
| 1839 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1840 | goto next; |
| 1841 | |
| 1842 | case 1: /* retry */ |
| 1843 | goto stop; |
| 1844 | |
| 1845 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1846 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1847 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1848 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1849 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1850 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1851 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1852 | goto exit; |
| 1853 | } |
| 1854 | |
| 1855 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1856 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1857 | if (trash.data) |
| 1858 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1859 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1860 | return 0; |
| 1861 | stop: |
| 1862 | return 1; |
| 1863 | exit: |
| 1864 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | /* I/O Handler processing messages exchanged with the agent */ |
| 1869 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1870 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1871 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1872 | struct stream_interface *si = appctx->owner; |
| 1873 | struct spoe_agent *agent; |
| 1874 | |
| 1875 | if (SPOE_APPCTX(appctx) == NULL) |
| 1876 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1877 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1878 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1879 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1880 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1881 | switchstate: |
| 1882 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1883 | " - appctx-state=%s\n", |
| 1884 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1885 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1886 | |
| 1887 | switch (appctx->st0) { |
| 1888 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1889 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1890 | goto out; |
| 1891 | goto switchstate; |
| 1892 | |
| 1893 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1894 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1895 | goto out; |
| 1896 | goto switchstate; |
| 1897 | |
| 1898 | case SPOE_APPCTX_ST_IDLE: |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 1899 | _HA_ATOMIC_SUB(&agent->counters.idles, 1); |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1900 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1901 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1902 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1903 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1904 | SPOE_APPCTX(appctx)->task->expire = |
| 1905 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1906 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1907 | goto switchstate; |
| 1908 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1909 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1910 | /* fall through */ |
| 1911 | |
| 1912 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1913 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1914 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1915 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1916 | goto out; |
| 1917 | goto switchstate; |
| 1918 | |
| 1919 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1920 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1921 | goto out; |
| 1922 | goto switchstate; |
| 1923 | |
| 1924 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1925 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1926 | goto out; |
| 1927 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1928 | |
| 1929 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1930 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1931 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1932 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1933 | si_shutw(si); |
| 1934 | si_shutr(si); |
| 1935 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1936 | /* fall through */ |
| 1937 | |
| 1938 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1939 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1940 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1941 | out: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1942 | if (stopping) |
| 1943 | spoe_wakeup_appctx(appctx); |
| 1944 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1945 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 1946 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | struct applet spoe_applet = { |
| 1950 | .obj_type = OBJ_TYPE_APPLET, |
| 1951 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1952 | .fct = spoe_handle_appctx, |
| 1953 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1954 | }; |
| 1955 | |
| 1956 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 1957 | * NULL. */ |
| 1958 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1959 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1960 | { |
| 1961 | struct appctx *appctx; |
| 1962 | struct session *sess; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1963 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1964 | |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 1965 | if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1966 | goto out_error; |
| 1967 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1968 | appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1969 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1970 | goto out_free_appctx; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1971 | memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1972 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1973 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 1974 | if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1975 | goto out_free_spoe_appctx; |
| 1976 | |
| 1977 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1978 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1979 | SPOE_APPCTX(appctx)->task->context = appctx; |
| 1980 | SPOE_APPCTX(appctx)->agent = conf->agent; |
| 1981 | SPOE_APPCTX(appctx)->version = 0; |
| 1982 | SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size; |
| 1983 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1984 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1985 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1986 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1987 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 1988 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1989 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1990 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1991 | |
| 1992 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 1993 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 1995 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1996 | if (!sess) |
| 1997 | goto out_free_spoe; |
| 1998 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 1999 | if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2000 | goto out_free_sess; |
| 2001 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2002 | stream_set_backend(strm, conf->agent->b.be); |
| 2003 | |
| 2004 | /* applet is waiting for data */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2005 | si_cant_get(&strm->si[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2006 | appctx_wakeup(appctx); |
| 2007 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2008 | strm->do_log = NULL; |
| 2009 | strm->res.flags |= CF_READ_DONTWAIT; |
| 2010 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2011 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2012 | LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2013 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2014 | _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1); |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 2015 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2016 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2017 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2018 | return appctx; |
| 2019 | |
| 2020 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2021 | out_free_sess: |
| 2022 | session_free(sess); |
| 2023 | out_free_spoe: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2024 | task_destroy(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2025 | out_free_spoe_appctx: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2026 | pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2027 | out_free_appctx: |
| 2028 | appctx_free(appctx); |
| 2029 | out_error: |
| 2030 | return NULL; |
| 2031 | } |
| 2032 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2033 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2034 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2035 | { |
| 2036 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2037 | struct spoe_agent *agent = conf->agent; |
| 2038 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2039 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2040 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2041 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2042 | if (!eb_is_empty(&agent->rt[tid].idle_applets) && |
Christopher Faulet | eccfa61 | 2020-06-22 15:32:14 +0200 | [diff] [blame] | 2043 | (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] | 2044 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2045 | |
| 2046 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2047 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2048 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2049 | ctx->strm); |
| 2050 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2051 | /* Do not try to create a new applet if there is no server up for the |
| 2052 | * agent's backend. */ |
| 2053 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2054 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2055 | " - cannot create SPOE appctx: no server up\n", |
| 2056 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2057 | __FUNCTION__, ctx->strm); |
| 2058 | goto end; |
| 2059 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2060 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2061 | /* Do not try to create a new applet if we have reached the maximum of |
| 2062 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2063 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2064 | if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2065 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2066 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2067 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2068 | __FUNCTION__, ctx->strm); |
| 2069 | goto end; |
| 2070 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2073 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2074 | if (appctx == NULL) { |
| 2075 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2076 | " - failed to create SPOE appctx\n", |
| 2077 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2078 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 2079 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2080 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2081 | agent->id); |
| 2082 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2083 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2086 | /* Increase the per-process number of cumulated connections */ |
| 2087 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2088 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2089 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2090 | end: |
| 2091 | /* The only reason to return an error is when there is no applet */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2092 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2093 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2094 | return -1; |
| 2095 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2096 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2097 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2098 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2099 | * it. */ |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2100 | _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2101 | spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request); |
| 2102 | ctx->stats.tv_queue = now; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2103 | if (ctx->spoe_appctx) |
| 2104 | return 1; |
| 2105 | LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2106 | |
| 2107 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2108 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2109 | " - applets=%u - idles=%u - processing=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2110 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2111 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2112 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2113 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2114 | /* Finally try to wakeup an IDLE applet. */ |
| 2115 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2116 | struct eb32_node *node; |
| 2117 | |
| 2118 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2119 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2120 | if (node && spoe_appctx) { |
| 2121 | eb32_delete(&spoe_appctx->node); |
| 2122 | spoe_appctx->node.key++; |
| 2123 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2124 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2125 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2126 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2127 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2131 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2132 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2133 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2134 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2135 | * the message is too big, it returns -1.*/ |
| 2136 | static int |
| 2137 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2138 | struct spoe_message *msg, int dir, |
| 2139 | char **buf, char *end) |
| 2140 | { |
| 2141 | struct sample *smp; |
| 2142 | struct spoe_arg *arg; |
| 2143 | int ret; |
| 2144 | |
| 2145 | if (msg->cond) { |
| 2146 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2147 | ret = acl_pass(ret); |
| 2148 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2149 | ret = !ret; |
| 2150 | |
| 2151 | /* the rule does not match */ |
| 2152 | if (!ret) |
| 2153 | goto next; |
| 2154 | } |
| 2155 | |
| 2156 | /* Resume encoding of a SPOE argument */ |
| 2157 | if (ctx->frag_ctx.curarg != NULL) { |
| 2158 | arg = ctx->frag_ctx.curarg; |
| 2159 | goto encode_argument; |
| 2160 | } |
| 2161 | |
| 2162 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2163 | goto encode_msg_payload; |
| 2164 | |
| 2165 | /* Check if there is enough space for the message name and the |
| 2166 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2167 | * bytes, at most (< 2288). */ |
| 2168 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2169 | goto too_big; |
| 2170 | |
| 2171 | /* Encode the message name */ |
| 2172 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2173 | goto too_big; |
| 2174 | |
| 2175 | /* Set the number of arguments for this message */ |
| 2176 | **buf = msg->nargs; |
| 2177 | (*buf)++; |
| 2178 | |
| 2179 | ctx->frag_ctx.curoff = 0; |
| 2180 | encode_msg_payload: |
| 2181 | |
| 2182 | /* Loop on arguments */ |
| 2183 | list_for_each_entry(arg, &msg->args, list) { |
| 2184 | ctx->frag_ctx.curarg = arg; |
| 2185 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2186 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2187 | |
| 2188 | encode_argument: |
| 2189 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2190 | goto encode_arg_value; |
| 2191 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2192 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2193 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2194 | goto too_big; |
| 2195 | |
| 2196 | ctx->frag_ctx.curoff = 0; |
| 2197 | encode_arg_value: |
| 2198 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2199 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2200 | smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL); |
Christopher Faulet | 3b1d004 | 2019-05-06 09:53:10 +0200 | [diff] [blame] | 2201 | if (smp) { |
| 2202 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2203 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2204 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2205 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2206 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2207 | goto too_big; |
| 2208 | } |
| 2209 | |
| 2210 | next: |
| 2211 | return 0; |
| 2212 | |
| 2213 | too_big: |
| 2214 | return -1; |
| 2215 | } |
| 2216 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2217 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2218 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2219 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2220 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2221 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2222 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2223 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2224 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2225 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2226 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2227 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2228 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2229 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2230 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2231 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2232 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2233 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2234 | /* Resume encoding of a SPOE message */ |
| 2235 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2236 | msg = ctx->frag_ctx.curmsg; |
| 2237 | goto encode_evt_message; |
| 2238 | } |
| 2239 | |
| 2240 | list_for_each_entry(msg, messages, by_evt) { |
| 2241 | ctx->frag_ctx.curmsg = msg; |
| 2242 | ctx->frag_ctx.curarg = NULL; |
| 2243 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2244 | |
| 2245 | encode_evt_message: |
| 2246 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2247 | goto too_big; |
| 2248 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2249 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2250 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2251 | /* Resume encoding of a SPOE message */ |
| 2252 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2253 | msg = ctx->frag_ctx.curmsg; |
| 2254 | goto encode_grp_message; |
| 2255 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2256 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2257 | list_for_each_entry(msg, messages, by_grp) { |
| 2258 | ctx->frag_ctx.curmsg = msg; |
| 2259 | ctx->frag_ctx.curarg = NULL; |
| 2260 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2261 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2262 | encode_grp_message: |
| 2263 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2264 | goto too_big; |
| 2265 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2266 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2267 | else |
| 2268 | goto skip; |
| 2269 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2270 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2271 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2272 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2273 | goto skip; |
| 2274 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2275 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2276 | " - encode %s messages - spoe_appctx=%p" |
| 2277 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2278 | (int)now.tv_sec, (int)now.tv_usec, |
| 2279 | agent->id, __FUNCTION__, s, |
| 2280 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2281 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2282 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2283 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2284 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2285 | ctx->frag_ctx.curmsg = NULL; |
| 2286 | ctx->frag_ctx.curarg = NULL; |
| 2287 | ctx->frag_ctx.curoff = 0; |
| 2288 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2289 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2290 | return 1; |
| 2291 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2292 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2293 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2294 | * been encoded because its too big and not splittable. */ |
| 2295 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2296 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2297 | return -1; |
| 2298 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2299 | |
| 2300 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2301 | " - encode fragmented messages - spoe_appctx=%p" |
| 2302 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2303 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2304 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2305 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2306 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2307 | (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2308 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2309 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2310 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2311 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2312 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2313 | |
| 2314 | skip: |
| 2315 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2316 | " - skip the frame because nothing has been encoded\n", |
| 2317 | (int)now.tv_sec, (int)now.tv_usec, |
| 2318 | agent->id, __FUNCTION__, s); |
| 2319 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2322 | |
| 2323 | /*************************************************************************** |
| 2324 | * Functions that handle SPOE actions |
| 2325 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2326 | /* Helper function to set a variable */ |
| 2327 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2328 | spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2329 | struct sample *smp) |
| 2330 | { |
| 2331 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2332 | struct spoe_agent *agent = conf->agent; |
| 2333 | char varname[64]; |
| 2334 | |
| 2335 | memset(varname, 0, sizeof(varname)); |
| 2336 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2337 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2338 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2339 | vars_set_by_name(varname, len, smp); |
| 2340 | else |
| 2341 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* Helper function to unset a variable */ |
| 2345 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2346 | spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2347 | struct sample *smp) |
| 2348 | { |
| 2349 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2350 | struct spoe_agent *agent = conf->agent; |
| 2351 | char varname[64]; |
| 2352 | |
| 2353 | memset(varname, 0, sizeof(varname)); |
| 2354 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2355 | scope, agent->var_pfx, len, name); |
| 2356 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2357 | } |
| 2358 | |
| 2359 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2360 | static inline int |
| 2361 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2362 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2363 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2364 | char *str, *scope, *p = *buf; |
| 2365 | struct sample smp; |
| 2366 | uint64_t sz; |
| 2367 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2368 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2369 | if (p + 2 >= end) |
| 2370 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2371 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2372 | /* SET-VAR requires 3 arguments */ |
| 2373 | if (*p++ != 3) |
| 2374 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2376 | switch (*p++) { |
| 2377 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2378 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2379 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2380 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2381 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2382 | default: goto skip; |
| 2383 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2384 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2385 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2386 | goto skip; |
| 2387 | memset(&smp, 0, sizeof(smp)); |
| 2388 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2389 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2390 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2391 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2392 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2393 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2394 | " - set-var '%s.%s.%.*s'\n", |
| 2395 | (int)now.tv_sec, (int)now.tv_usec, |
| 2396 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2397 | __FUNCTION__, s, scope, |
| 2398 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2399 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2400 | |
Christopher Faulet | 2469eba | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2401 | if (smp.data.type == SMP_T_ANY) |
| 2402 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2403 | else |
| 2404 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2405 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2406 | ret = (p - *buf); |
| 2407 | *buf = p; |
| 2408 | return ret; |
| 2409 | skip: |
| 2410 | return 0; |
| 2411 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2412 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2413 | static inline int |
| 2414 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2415 | char **buf, char *end, int dir) |
| 2416 | { |
| 2417 | char *str, *scope, *p = *buf; |
| 2418 | struct sample smp; |
| 2419 | uint64_t sz; |
| 2420 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2421 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2422 | if (p + 2 >= end) |
| 2423 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2424 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2425 | /* UNSET-VAR requires 2 arguments */ |
| 2426 | if (*p++ != 2) |
| 2427 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2428 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2429 | switch (*p++) { |
| 2430 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2431 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2432 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2433 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2434 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2435 | default: goto skip; |
| 2436 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2437 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2438 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2439 | goto skip; |
| 2440 | memset(&smp, 0, sizeof(smp)); |
| 2441 | 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] | 2442 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2443 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2444 | " - unset-var '%s.%s.%.*s'\n", |
| 2445 | (int)now.tv_sec, (int)now.tv_usec, |
| 2446 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2447 | __FUNCTION__, s, scope, |
| 2448 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2449 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2450 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2451 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2452 | |
| 2453 | ret = (p - *buf); |
| 2454 | *buf = p; |
| 2455 | return ret; |
| 2456 | skip: |
| 2457 | return 0; |
| 2458 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2459 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2460 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2461 | * error occurred, 0 is returned. */ |
| 2462 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2463 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2464 | { |
| 2465 | char *p, *end; |
| 2466 | int ret; |
| 2467 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2468 | p = b_head(&ctx->buffer); |
| 2469 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2470 | |
| 2471 | while (p < end) { |
| 2472 | enum spoe_action_type type; |
| 2473 | |
| 2474 | type = *p++; |
| 2475 | switch (type) { |
| 2476 | case SPOE_ACT_T_SET_VAR: |
| 2477 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2478 | if (!ret) |
| 2479 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2480 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2481 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2482 | case SPOE_ACT_T_UNSET_VAR: |
| 2483 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2484 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2485 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2486 | break; |
| 2487 | |
| 2488 | default: |
| 2489 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | return 1; |
| 2494 | skip: |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2498 | /*************************************************************************** |
| 2499 | * Functions that process SPOE events |
| 2500 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2501 | static void |
| 2502 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2503 | struct spoe_context *ctx, int dir) |
| 2504 | { |
| 2505 | if (!tv_iszero(&ctx->stats.tv_start)) { |
| 2506 | spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process); |
| 2507 | ctx->stats.t_total += ctx->stats.t_process; |
| 2508 | tv_zero(&ctx->stats.tv_request); |
| 2509 | tv_zero(&ctx->stats.tv_queue); |
| 2510 | tv_zero(&ctx->stats.tv_wait); |
| 2511 | tv_zero(&ctx->stats.tv_response); |
| 2512 | } |
| 2513 | |
| 2514 | if (agent->var_t_process) { |
| 2515 | struct sample smp; |
| 2516 | |
| 2517 | memset(&smp, 0, sizeof(smp)); |
| 2518 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2519 | smp.data.u.sint = ctx->stats.t_process; |
| 2520 | smp.data.type = SMP_T_SINT; |
| 2521 | |
| 2522 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2523 | strlen(agent->var_t_process), &smp); |
| 2524 | } |
| 2525 | |
| 2526 | if (agent->var_t_total) { |
| 2527 | struct sample smp; |
| 2528 | |
| 2529 | memset(&smp, 0, sizeof(smp)); |
| 2530 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2531 | smp.data.u.sint = ctx->stats.t_total; |
| 2532 | smp.data.type = SMP_T_SINT; |
| 2533 | |
| 2534 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2535 | strlen(agent->var_t_total), &smp); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | static void |
| 2540 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2541 | struct spoe_context *ctx, int dir) |
| 2542 | { |
| 2543 | if (agent->eps_max > 0) |
| 2544 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2545 | |
| 2546 | if (agent->var_on_error) { |
| 2547 | struct sample smp; |
| 2548 | |
| 2549 | memset(&smp, 0, sizeof(smp)); |
| 2550 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2551 | smp.data.u.sint = ctx->status_code; |
| 2552 | smp.data.type = SMP_T_BOOL; |
| 2553 | |
| 2554 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2555 | strlen(agent->var_on_error), &smp); |
| 2556 | } |
| 2557 | |
| 2558 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2559 | ? SPOE_CTX_ST_READY |
| 2560 | : SPOE_CTX_ST_NONE); |
| 2561 | } |
| 2562 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2563 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2564 | 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] | 2565 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2566 | /* If a process is already started for this SPOE context, retry |
| 2567 | * later. */ |
| 2568 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2569 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2570 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2571 | agent->rt[tid].processing++; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2572 | ctx->stats.tv_start = now; |
| 2573 | ctx->stats.tv_request = now; |
| 2574 | ctx->stats.t_request = -1; |
| 2575 | ctx->stats.t_queue = -1; |
| 2576 | ctx->stats.t_waiting = -1; |
| 2577 | ctx->stats.t_response = -1; |
| 2578 | ctx->stats.t_process = -1; |
| 2579 | |
| 2580 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2581 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2582 | /* Set the right flag to prevent request and response processing |
| 2583 | * in same time. */ |
| 2584 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2585 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2586 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2587 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2590 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2591 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2592 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2593 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2594 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2595 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2596 | return; |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2597 | _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2598 | if (sa) { |
| 2599 | if (sa->frag_ctx.ctx == ctx) { |
| 2600 | sa->frag_ctx.ctx = NULL; |
| 2601 | spoe_wakeup_appctx(sa->owner); |
| 2602 | } |
| 2603 | else |
| 2604 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2605 | } |
| 2606 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2607 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2608 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2609 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2610 | |
| 2611 | /* Reset processing timer */ |
| 2612 | ctx->process_exp = TICK_ETERNITY; |
| 2613 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2614 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2615 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2616 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2617 | ctx->frag_ctx.curmsg = NULL; |
| 2618 | ctx->frag_ctx.curarg = NULL; |
| 2619 | ctx->frag_ctx.curoff = 0; |
| 2620 | ctx->frag_ctx.flags = 0; |
| 2621 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2622 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2623 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2624 | _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2625 | else |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2626 | _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2627 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2628 | LIST_DEL(&ctx->list); |
| 2629 | LIST_INIT(&ctx->list); |
| 2630 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2633 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2634 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2635 | * to process corresponding actions. During all the processing, it returns 0 |
| 2636 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2637 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2638 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2639 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2640 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2641 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2642 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2643 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2644 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2645 | |
| 2646 | if (ctx->state == SPOE_CTX_ST_ERROR) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2647 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2648 | |
| 2649 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2650 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2651 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2652 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2653 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2654 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2655 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2656 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2657 | |
| 2658 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2659 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2660 | 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] | 2661 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2662 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +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 | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2665 | goto skip; |
| 2666 | } |
| 2667 | } |
| 2668 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2669 | if (!tick_isset(ctx->process_exp)) { |
| 2670 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2671 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2672 | ctx->process_exp); |
| 2673 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2674 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2675 | if (!ret) |
| 2676 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2677 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2678 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2679 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2680 | } |
| 2681 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2682 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 63263e5 | 2019-04-10 14:47:18 +0200 | [diff] [blame] | 2683 | if (tv_iszero(&ctx->stats.tv_request)) |
| 2684 | ctx->stats.tv_request = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2685 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2686 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2687 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2688 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2689 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2690 | if (!ret) |
| 2691 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2692 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2693 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2694 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2695 | } |
| 2696 | |
| 2697 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2698 | if (ctx->spoe_appctx) |
| 2699 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2700 | ret = 0; |
| 2701 | goto out; |
| 2702 | } |
| 2703 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2704 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2705 | ret = 0; |
| 2706 | goto out; |
| 2707 | } |
| 2708 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2709 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2710 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2711 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2712 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2713 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2714 | spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2715 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | out: |
| 2719 | return ret; |
| 2720 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2721 | skip: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2722 | tv_zero(&ctx->stats.tv_start); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2723 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2724 | spoe_stop_processing(agent, ctx); |
| 2725 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2726 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2727 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2728 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2729 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2730 | if (ctx->status_code) { |
Olivier Houchard | 9e7ae28 | 2019-03-08 18:50:42 +0100 | [diff] [blame] | 2731 | _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2732 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2733 | ret = 1; |
| 2734 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2735 | return ret; |
| 2736 | } |
| 2737 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2738 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2739 | * See spoe_process_message for details. */ |
| 2740 | static int |
| 2741 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2742 | struct spoe_group *group, int dir) |
| 2743 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2744 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2745 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2746 | int ret; |
| 2747 | |
| 2748 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2749 | " - ctx-state=%s - Process messages for group=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2750 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2751 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2752 | group->id); |
| 2753 | |
| 2754 | if (LIST_ISEMPTY(&group->messages)) |
| 2755 | return 1; |
| 2756 | |
| 2757 | 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] | 2758 | if (ret && ctx->stats.t_process != -1) { |
| 2759 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2760 | " - <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] | 2761 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2762 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2763 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2764 | ctx->stats.t_response, ctx->stats.t_process, |
| 2765 | agent->counters.idles, agent->counters.applets, |
| 2766 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2767 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2768 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2769 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2770 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2771 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2772 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2773 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2774 | ctx->stats.t_response, ctx->stats.t_process, |
| 2775 | agent->counters.idles, agent->counters.applets, |
| 2776 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2777 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2778 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2779 | return ret; |
| 2780 | } |
| 2781 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2782 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2783 | * See spoe_process_message for details. */ |
| 2784 | static int |
| 2785 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2786 | enum spoe_event ev) |
| 2787 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2788 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2789 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2790 | int dir, ret; |
| 2791 | |
| 2792 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2793 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2794 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2795 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2796 | spoe_event_str[ev]); |
| 2797 | |
| 2798 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2799 | |
| 2800 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2801 | return 1; |
| 2802 | |
| 2803 | 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] | 2804 | if (ret && ctx->stats.t_process != -1) { |
| 2805 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2806 | " - <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] | 2807 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2808 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2809 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2810 | ctx->stats.t_response, ctx->stats.t_process, |
| 2811 | agent->counters.idles, agent->counters.applets, |
| 2812 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2813 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2814 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2815 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2816 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2817 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2818 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2819 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2820 | ctx->stats.t_response, ctx->stats.t_process, |
| 2821 | agent->counters.idles, agent->counters.applets, |
| 2822 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2823 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2824 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2825 | return ret; |
| 2826 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2827 | |
| 2828 | /*************************************************************************** |
| 2829 | * Functions that create/destroy SPOE contexts |
| 2830 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2831 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2832 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2833 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2834 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2835 | return 1; |
| 2836 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2837 | if (LIST_ADDED(&buffer_wait->list)) |
| 2838 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2839 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2840 | if (b_alloc_margin(buf, global.tune.reserved_bufs)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2841 | return 1; |
| 2842 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2843 | LIST_ADDQ(&ti->buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2844 | return 0; |
| 2845 | } |
| 2846 | |
| 2847 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2848 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2849 | { |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2850 | if (LIST_ADDED(&buffer_wait->list)) |
| 2851 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2852 | |
| 2853 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2854 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2855 | b_free(buf); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 2856 | offer_buffers(buffer_wait->target, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2860 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2861 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2862 | { |
| 2863 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2864 | return 1; |
| 2865 | } |
| 2866 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2867 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2868 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2869 | { |
| 2870 | struct spoe_config *conf = FLT_CONF(filter); |
| 2871 | struct spoe_context *ctx; |
| 2872 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2873 | ctx = pool_alloc_dirty(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2874 | if (ctx == NULL) { |
| 2875 | return NULL; |
| 2876 | } |
| 2877 | memset(ctx, 0, sizeof(*ctx)); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2878 | ctx->filter = filter; |
| 2879 | ctx->state = SPOE_CTX_ST_NONE; |
| 2880 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2881 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2882 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2883 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2884 | ctx->buffer = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2885 | LIST_INIT(&ctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2886 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2887 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2888 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2889 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2890 | ctx->stream_id = 0; |
| 2891 | ctx->frame_id = 1; |
| 2892 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2893 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2894 | tv_zero(&ctx->stats.tv_start); |
| 2895 | tv_zero(&ctx->stats.tv_request); |
| 2896 | tv_zero(&ctx->stats.tv_queue); |
| 2897 | tv_zero(&ctx->stats.tv_wait); |
| 2898 | tv_zero(&ctx->stats.tv_response); |
| 2899 | ctx->stats.t_request = -1; |
| 2900 | ctx->stats.t_queue = -1; |
| 2901 | ctx->stats.t_waiting = -1; |
| 2902 | ctx->stats.t_response = -1; |
| 2903 | ctx->stats.t_process = -1; |
| 2904 | ctx->stats.t_total = 0; |
| 2905 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2906 | ctx->strm = s; |
| 2907 | ctx->state = SPOE_CTX_ST_READY; |
| 2908 | filter->ctx = ctx; |
| 2909 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2910 | return ctx; |
| 2911 | } |
| 2912 | |
| 2913 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2914 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2915 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2916 | struct spoe_config *conf = FLT_CONF(filter); |
| 2917 | struct spoe_context *ctx = filter->ctx; |
| 2918 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2919 | if (!ctx) |
| 2920 | return; |
| 2921 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2922 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2923 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2924 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2928 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2929 | { |
| 2930 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2931 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2932 | |
| 2933 | tv_zero(&ctx->stats.tv_start); |
| 2934 | tv_zero(&ctx->stats.tv_request); |
| 2935 | tv_zero(&ctx->stats.tv_queue); |
| 2936 | tv_zero(&ctx->stats.tv_wait); |
| 2937 | tv_zero(&ctx->stats.tv_response); |
| 2938 | ctx->stats.t_request = -1; |
| 2939 | ctx->stats.t_queue = -1; |
| 2940 | ctx->stats.t_waiting = -1; |
| 2941 | ctx->stats.t_response = -1; |
| 2942 | ctx->stats.t_process = -1; |
| 2943 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | |
| 2947 | /*************************************************************************** |
| 2948 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2949 | **************************************************************************/ |
| 2950 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2951 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2952 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2953 | { |
| 2954 | struct proxy *p; |
| 2955 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2956 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2957 | while (p) { |
| 2958 | struct flt_conf *fconf; |
| 2959 | |
| 2960 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2961 | struct spoe_config *conf; |
| 2962 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2963 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2964 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2965 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2966 | if (fconf->id != spoe_filter_id) |
| 2967 | continue; |
| 2968 | |
| 2969 | conf = fconf->conf; |
| 2970 | agent = conf->agent; |
| 2971 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2972 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2973 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2974 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 2975 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2976 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2977 | } |
| 2978 | } |
| 2979 | p = p->next; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | |
| 2984 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 2985 | static int |
| 2986 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 2987 | { |
| 2988 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2989 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 2990 | /* conf->agent_fe was already initialized during the config |
| 2991 | * parsing. Finish initialization. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2992 | conf->agent_fe.last_change = now.tv_sec; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2993 | conf->agent_fe.cap = PR_CAP_FE; |
| 2994 | conf->agent_fe.mode = PR_MODE_TCP; |
| 2995 | conf->agent_fe.maxconn = 0; |
| 2996 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 2997 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 2998 | conf->agent_fe.accept = frontend_accept; |
| 2999 | conf->agent_fe.srv = NULL; |
| 3000 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 3001 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 3002 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3003 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3004 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3005 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3006 | sighandler_registered = 1; |
| 3007 | } |
| 3008 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3009 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3010 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3011 | } |
| 3012 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3013 | /* Free resources allocated by the SPOE filter. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3014 | static void |
| 3015 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3016 | { |
| 3017 | struct spoe_config *conf = fconf->conf; |
| 3018 | |
| 3019 | if (conf) { |
| 3020 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3021 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3022 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3023 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3024 | free(conf); |
| 3025 | } |
| 3026 | fconf->conf = NULL; |
| 3027 | } |
| 3028 | |
| 3029 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3030 | * Return 1 on error, else 0. */ |
| 3031 | static int |
| 3032 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3033 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3034 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3035 | struct spoe_config *conf = fconf->conf; |
| 3036 | struct proxy *target; |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3037 | struct logsrv *logsrv; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3038 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3039 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3040 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3041 | * are uniq */ |
| 3042 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3043 | struct spoe_config *c = f->conf; |
| 3044 | |
| 3045 | /* This is not an SPOE filter */ |
| 3046 | if (f->id != spoe_filter_id) |
| 3047 | continue; |
| 3048 | /* This is the current SPOE filter */ |
| 3049 | if (f == fconf) |
| 3050 | continue; |
| 3051 | |
| 3052 | /* Check engine Id. It should be uniq */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3053 | if (strcmp(conf->id, c->id) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3054 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3055 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3056 | return 1; |
| 3057 | } |
| 3058 | } |
| 3059 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3060 | target = proxy_be_by_name(conf->agent->b.name); |
| 3061 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3062 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3063 | " declared at %s:%d.\n", |
| 3064 | px->id, conf->agent->b.name, conf->agent->id, |
| 3065 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3066 | return 1; |
| 3067 | } |
| 3068 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3069 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3070 | " at %s:%d does not support HTTP mode.\n", |
| 3071 | px->id, target->id, conf->agent->id, |
| 3072 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3073 | return 1; |
| 3074 | } |
| 3075 | |
Willy Tarreau | 2bdcfde | 2019-02-05 13:37:19 +0100 | [diff] [blame] | 3076 | if (px->bind_proc & ~target->bind_proc) { |
| 3077 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3078 | " at %s:%d does not cover all of its processes.\n", |
| 3079 | px->id, target->id, conf->agent->id, |
| 3080 | conf->agent->conf.file, conf->agent->conf.line); |
| 3081 | return 1; |
| 3082 | } |
| 3083 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3084 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3085 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3086 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3087 | return 1; |
| 3088 | } |
| 3089 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3090 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3091 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3092 | conf->agent->rt[i].processing = 0; |
| 3093 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3094 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3095 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3096 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3097 | } |
| 3098 | |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3099 | list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) { |
| 3100 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 3101 | struct sink *sink = sink_find(logsrv->ring_name); |
| 3102 | |
| 3103 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 3104 | ha_alert("Proxy %s : log server used by SPOE agent '%s' declared" |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 3105 | " at %s:%d uses unknown ring named '%s'.\n", |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3106 | px->id, conf->agent->id, conf->agent->conf.file, |
| 3107 | conf->agent->conf.line, logsrv->ring_name); |
| 3108 | return 1; |
| 3109 | } |
| 3110 | logsrv->sink = sink; |
| 3111 | } |
| 3112 | } |
| 3113 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3114 | ha_free(&conf->agent->b.name); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3115 | conf->agent->b.be = target; |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3119 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3120 | * Returns a negative value if an error occurs. */ |
| 3121 | static int |
| 3122 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3123 | { |
| 3124 | struct spoe_config *conf = fconf->conf; |
| 3125 | struct spoe_agent *agent = conf->agent; |
| 3126 | |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3127 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3128 | if (agent->rt[tid].engine_id == NULL) |
| 3129 | return -1; |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3130 | return 0; |
| 3131 | } |
| 3132 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3133 | /************************************************************************** |
| 3134 | * Hooks attached to a stream |
| 3135 | *************************************************************************/ |
| 3136 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3137 | * the context that will be used to process this stream. */ |
| 3138 | static int |
| 3139 | spoe_start(struct stream *s, struct filter *filter) |
| 3140 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3141 | struct spoe_config *conf = FLT_CONF(filter); |
| 3142 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3143 | struct spoe_context *ctx; |
| 3144 | |
| 3145 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3146 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3147 | __FUNCTION__, s); |
| 3148 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3149 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3150 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3151 | " - failed to create SPOE context\n", |
| 3152 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3153 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3154 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3155 | "SPOE: [%s] failed to create SPOE context\n", |
| 3156 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3157 | return 0; |
| 3158 | } |
| 3159 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3160 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3161 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3162 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3163 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3164 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3165 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3166 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3167 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3168 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3169 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3170 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3171 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3172 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3173 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3174 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3175 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3176 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3177 | |
| 3178 | return 1; |
| 3179 | } |
| 3180 | |
| 3181 | /* Called when a filter instance is detached from a stream. It release the |
| 3182 | * attached SPOE context. */ |
| 3183 | static void |
| 3184 | spoe_stop(struct stream *s, struct filter *filter) |
| 3185 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3186 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 3187 | (int)now.tv_sec, (int)now.tv_usec, |
| 3188 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3189 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3190 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3191 | } |
| 3192 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3193 | |
| 3194 | /* |
| 3195 | * Called when the stream is woken up because of expired timer. |
| 3196 | */ |
| 3197 | static void |
| 3198 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3199 | { |
| 3200 | struct spoe_context *ctx = filter->ctx; |
| 3201 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3202 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3203 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3204 | } |
| 3205 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3206 | /* Called when we are ready to filter data on a channel */ |
| 3207 | static int |
| 3208 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3209 | { |
| 3210 | struct spoe_context *ctx = filter->ctx; |
| 3211 | int ret = 1; |
| 3212 | |
| 3213 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3214 | " - ctx-flags=0x%08x\n", |
| 3215 | (int)now.tv_sec, (int)now.tv_usec, |
| 3216 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3217 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3218 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3219 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3220 | goto out; |
| 3221 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3222 | if (!(chn->flags & CF_ISRESP)) { |
| 3223 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3224 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3225 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3226 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3227 | |
| 3228 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3229 | goto out; |
| 3230 | |
| 3231 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3232 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3233 | if (!ret) |
| 3234 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3235 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3236 | } |
| 3237 | else { |
Miroslav Zagorac | 8840326 | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3238 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3239 | chn->analysers |= AN_RES_INSPECT; |
| 3240 | |
| 3241 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3242 | goto out; |
| 3243 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3244 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3245 | if (!ret) { |
| 3246 | channel_dont_read(chn); |
| 3247 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3248 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3249 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3250 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3254 | return ret; |
| 3255 | } |
| 3256 | |
| 3257 | /* Called before a processing happens on a given channel */ |
| 3258 | static int |
| 3259 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3260 | struct channel *chn, unsigned an_bit) |
| 3261 | { |
| 3262 | struct spoe_context *ctx = filter->ctx; |
| 3263 | int ret = 1; |
| 3264 | |
| 3265 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3266 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3267 | (int)now.tv_sec, (int)now.tv_usec, |
| 3268 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3269 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3270 | ctx->flags, an_bit); |
| 3271 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3272 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3273 | goto out; |
| 3274 | |
| 3275 | switch (an_bit) { |
| 3276 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3277 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3278 | break; |
| 3279 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3280 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3281 | break; |
| 3282 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3283 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3284 | break; |
| 3285 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3286 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3287 | break; |
| 3288 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3289 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3290 | break; |
| 3291 | case AN_RES_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_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3293 | break; |
| 3294 | } |
| 3295 | |
| 3296 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3297 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3298 | channel_dont_read(chn); |
| 3299 | channel_dont_close(chn); |
| 3300 | } |
| 3301 | return ret; |
| 3302 | } |
| 3303 | |
| 3304 | /* Called when the filtering on the channel ends. */ |
| 3305 | static int |
| 3306 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3307 | { |
| 3308 | struct spoe_context *ctx = filter->ctx; |
| 3309 | |
| 3310 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3311 | " - ctx-flags=0x%08x\n", |
| 3312 | (int)now.tv_sec, (int)now.tv_usec, |
| 3313 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3314 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3315 | |
| 3316 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3317 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | return 1; |
| 3321 | } |
| 3322 | |
| 3323 | /******************************************************************** |
| 3324 | * Functions that manage the filter initialization |
| 3325 | ********************************************************************/ |
| 3326 | struct flt_ops spoe_ops = { |
| 3327 | /* Manage SPOE filter, called for each filter declaration */ |
| 3328 | .init = spoe_init, |
| 3329 | .deinit = spoe_deinit, |
| 3330 | .check = spoe_check, |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3331 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3332 | |
| 3333 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3334 | .attach = spoe_start, |
| 3335 | .detach = spoe_stop, |
| 3336 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3337 | |
| 3338 | /* Handle channels activity */ |
| 3339 | .channel_start_analyze = spoe_start_analyze, |
| 3340 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3341 | .channel_end_analyze = spoe_end_analyze, |
| 3342 | }; |
| 3343 | |
| 3344 | |
| 3345 | static int |
| 3346 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3347 | { |
| 3348 | const char *err; |
| 3349 | int i, err_code = 0; |
| 3350 | |
| 3351 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3352 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3353 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3354 | goto out; |
| 3355 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3356 | if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3357 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3358 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3359 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3360 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3361 | goto out; |
| 3362 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3363 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3364 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3365 | goto out; |
| 3366 | } |
| 3367 | |
| 3368 | err = invalid_char(args[1]); |
| 3369 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3370 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3371 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3372 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3373 | goto out; |
| 3374 | } |
| 3375 | |
| 3376 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3377 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3378 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3379 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3380 | goto out; |
| 3381 | } |
| 3382 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3383 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3384 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3385 | goto out; |
| 3386 | } |
| 3387 | |
| 3388 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3389 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3390 | curagent->conf.file = strdup(file); |
| 3391 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3392 | |
| 3393 | curagent->timeout.hello = TICK_ETERNITY; |
| 3394 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3395 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3396 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3397 | curagent->var_pfx = NULL; |
| 3398 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3399 | curagent->var_t_process = NULL; |
| 3400 | curagent->var_t_total = NULL; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3401 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3402 | curagent->cps_max = 0; |
| 3403 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3404 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3405 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3406 | |
| 3407 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3408 | LIST_INIT(&curagent->events[i]); |
| 3409 | LIST_INIT(&curagent->groups); |
| 3410 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3411 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3412 | else if (strcmp(args[0], "use-backend") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3413 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3414 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3415 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3416 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3417 | goto out; |
| 3418 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3419 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3420 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3421 | free(curagent->b.name); |
| 3422 | curagent->b.name = strdup(args[1]); |
| 3423 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3424 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3425 | int cur_arg = 1; |
| 3426 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3427 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3428 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3429 | list_for_each_entry(ph, &curmphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3430 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3431 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3432 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3433 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3434 | goto out; |
| 3435 | } |
| 3436 | } |
| 3437 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3438 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3439 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3440 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3441 | goto out; |
| 3442 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3443 | ph->id = strdup(args[cur_arg]); |
| 3444 | LIST_ADDQ(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3445 | cur_arg++; |
| 3446 | } |
| 3447 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3448 | else if (strcmp(args[0], "groups") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3449 | int cur_arg = 1; |
| 3450 | while (*args[cur_arg]) { |
| 3451 | struct spoe_placeholder *ph = NULL; |
| 3452 | |
| 3453 | list_for_each_entry(ph, &curgphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3454 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3455 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3456 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3457 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3458 | goto out; |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3463 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3464 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3465 | goto out; |
| 3466 | } |
| 3467 | ph->id = strdup(args[cur_arg]); |
| 3468 | LIST_ADDQ(&curgphs, &ph->list); |
| 3469 | cur_arg++; |
| 3470 | } |
| 3471 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3472 | else if (strcmp(args[0], "timeout") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3473 | unsigned int *tv = NULL; |
| 3474 | const char *res; |
| 3475 | unsigned timeout; |
| 3476 | |
| 3477 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3478 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3479 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3480 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3481 | goto out; |
| 3482 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3483 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3484 | goto out; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3485 | if (strcmp(args[1], "hello") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3486 | tv = &curagent->timeout.hello; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3487 | else if (strcmp(args[1], "idle") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3488 | tv = &curagent->timeout.idle; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3489 | else if (strcmp(args[1], "processing") == 0) |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3490 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3491 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3492 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3493 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3494 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3495 | goto out; |
| 3496 | } |
| 3497 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3498 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\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 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3504 | if (res == PARSE_TIME_OVER) { |
| 3505 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3506 | file, linenum, args[2], args[0], args[1]); |
| 3507 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3508 | goto out; |
| 3509 | } |
| 3510 | else if (res == PARSE_TIME_UNDER) { |
| 3511 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\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) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3517 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3518 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3519 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3520 | goto out; |
| 3521 | } |
| 3522 | *tv = MS_TO_TICKS(timeout); |
| 3523 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3524 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3525 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3526 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3527 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3528 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3529 | goto out; |
| 3530 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3531 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3532 | if (strcmp(args[1], "pipelining") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3533 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3534 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3535 | if (kwm == 1) |
| 3536 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3537 | else |
| 3538 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3539 | goto out; |
| 3540 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3541 | else if (strcmp(args[1], "async") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3542 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3543 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3544 | if (kwm == 1) |
| 3545 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3546 | else |
| 3547 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3548 | goto out; |
| 3549 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3550 | else if (strcmp(args[1], "send-frag-payload") == 0) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3551 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3552 | goto out; |
| 3553 | if (kwm == 1) |
| 3554 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3555 | else |
| 3556 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3557 | goto out; |
| 3558 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3559 | else if (strcmp(args[1], "dontlog-normal") == 0) { |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3560 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3561 | goto out; |
| 3562 | if (kwm == 1) |
| 3563 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3564 | else |
| 3565 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3566 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3567 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3568 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3569 | /* Following options does not support negation */ |
| 3570 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3571 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3572 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3573 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3574 | goto out; |
| 3575 | } |
| 3576 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3577 | if (strcmp(args[1], "var-prefix") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3578 | char *tmp; |
| 3579 | |
| 3580 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3581 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3582 | file, linenum, args[0], |
| 3583 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3584 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3585 | goto out; |
| 3586 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3587 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3588 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3589 | tmp = args[2]; |
| 3590 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3591 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3592 | 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] | 3593 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3594 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3595 | goto out; |
| 3596 | } |
| 3597 | tmp++; |
| 3598 | } |
| 3599 | curagent->var_pfx = strdup(args[2]); |
| 3600 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3601 | else if (strcmp(args[1], "force-set-var") == 0) { |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3602 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3603 | goto out; |
| 3604 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3605 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3606 | else if (strcmp(args[1], "continue-on-error") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3607 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3608 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3609 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3610 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3611 | else if (strcmp(args[1], "set-on-error") == 0) { |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3612 | char *tmp; |
| 3613 | |
| 3614 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3615 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3616 | file, linenum, args[0], |
| 3617 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3618 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3619 | goto out; |
| 3620 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3621 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3622 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3623 | tmp = args[2]; |
| 3624 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3625 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3626 | 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] | 3627 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3628 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3629 | goto out; |
| 3630 | } |
| 3631 | tmp++; |
| 3632 | } |
| 3633 | curagent->var_on_error = strdup(args[2]); |
| 3634 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3635 | else if (strcmp(args[1], "set-process-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3636 | char *tmp; |
| 3637 | |
| 3638 | if (!*args[2]) { |
| 3639 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3640 | file, linenum, args[0], |
| 3641 | args[1]); |
| 3642 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3643 | goto out; |
| 3644 | } |
| 3645 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3646 | goto out; |
| 3647 | tmp = args[2]; |
| 3648 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3649 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3650 | 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] | 3651 | file, linenum, args[0], args[1]); |
| 3652 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3653 | goto out; |
| 3654 | } |
| 3655 | tmp++; |
| 3656 | } |
| 3657 | curagent->var_t_process = strdup(args[2]); |
| 3658 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3659 | else if (strcmp(args[1], "set-total-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3660 | char *tmp; |
| 3661 | |
| 3662 | if (!*args[2]) { |
| 3663 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3664 | file, linenum, args[0], |
| 3665 | args[1]); |
| 3666 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3667 | goto out; |
| 3668 | } |
| 3669 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3670 | goto out; |
| 3671 | tmp = args[2]; |
| 3672 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3673 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3674 | 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] | 3675 | file, linenum, args[0], args[1]); |
| 3676 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3677 | goto out; |
| 3678 | } |
| 3679 | tmp++; |
| 3680 | } |
| 3681 | curagent->var_t_total = strdup(args[2]); |
| 3682 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3683 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3684 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3685 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3686 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3687 | goto out; |
| 3688 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3689 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3690 | else if (strcmp(args[0], "maxconnrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3691 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3692 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3693 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3694 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3695 | goto out; |
| 3696 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3697 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3698 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3699 | curagent->cps_max = atol(args[1]); |
| 3700 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3701 | else if (strcmp(args[0], "maxerrrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3702 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3703 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3704 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3705 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3706 | goto out; |
| 3707 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3708 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3709 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3710 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3711 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3712 | else if (strcmp(args[0], "max-frame-size") == 0) { |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3713 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3714 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3715 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3716 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3717 | goto out; |
| 3718 | } |
| 3719 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3720 | goto out; |
| 3721 | curagent->max_frame_size = atol(args[1]); |
| 3722 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3723 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3724 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3725 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3726 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3727 | goto out; |
| 3728 | } |
| 3729 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3730 | else if (strcmp(args[0], "max-waiting-frames") == 0) { |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3731 | if (!*args[1]) { |
| 3732 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3733 | file, linenum, args[0]); |
| 3734 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3735 | goto out; |
| 3736 | } |
| 3737 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3738 | goto out; |
| 3739 | curagent->max_fpa = atol(args[1]); |
| 3740 | if (curagent->max_fpa < 1) { |
| 3741 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3742 | file, linenum, args[0]); |
| 3743 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3744 | goto out; |
| 3745 | } |
| 3746 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3747 | else if (strcmp(args[0], "register-var-names") == 0) { |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3748 | int cur_arg; |
| 3749 | |
| 3750 | if (!*args[1]) { |
| 3751 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3752 | file, linenum, args[0]); |
| 3753 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3754 | goto out; |
| 3755 | } |
| 3756 | cur_arg = 1; |
| 3757 | while (*args[cur_arg]) { |
| 3758 | struct spoe_var_placeholder *vph; |
| 3759 | |
| 3760 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3761 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3762 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3763 | goto out; |
| 3764 | } |
| 3765 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | b298613 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3766 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3767 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3768 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3769 | goto out; |
| 3770 | } |
| 3771 | LIST_ADDQ(&curvars, &vph->list); |
| 3772 | cur_arg++; |
| 3773 | } |
| 3774 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3775 | else if (strcmp(args[0], "log") == 0) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3776 | char *errmsg = NULL; |
| 3777 | |
| 3778 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) { |
| 3779 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3780 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3781 | goto out; |
| 3782 | } |
| 3783 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3784 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3785 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3786 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3787 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3788 | goto out; |
| 3789 | } |
| 3790 | out: |
| 3791 | return err_code; |
| 3792 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3793 | static int |
| 3794 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3795 | { |
| 3796 | struct spoe_group *grp; |
| 3797 | const char *err; |
| 3798 | int err_code = 0; |
| 3799 | |
| 3800 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3801 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3802 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3803 | goto out; |
| 3804 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3805 | if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3806 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3807 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3808 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3809 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3810 | goto out; |
| 3811 | } |
| 3812 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3813 | err_code |= ERR_ABORT; |
| 3814 | goto out; |
| 3815 | } |
| 3816 | |
| 3817 | err = invalid_char(args[1]); |
| 3818 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3819 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3820 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3821 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3822 | goto out; |
| 3823 | } |
| 3824 | |
| 3825 | list_for_each_entry(grp, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3826 | if (strcmp(grp->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3827 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3828 | " name as another one declared at %s:%d.\n", |
| 3829 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3830 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3831 | goto out; |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3836 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3837 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3838 | goto out; |
| 3839 | } |
| 3840 | |
| 3841 | curgrp->id = strdup(args[1]); |
| 3842 | curgrp->conf.file = strdup(file); |
| 3843 | curgrp->conf.line = linenum; |
| 3844 | LIST_INIT(&curgrp->phs); |
| 3845 | LIST_INIT(&curgrp->messages); |
| 3846 | LIST_ADDQ(&curgrps, &curgrp->list); |
| 3847 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3848 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3849 | int cur_arg = 1; |
| 3850 | while (*args[cur_arg]) { |
| 3851 | struct spoe_placeholder *ph = NULL; |
| 3852 | |
| 3853 | list_for_each_entry(ph, &curgrp->phs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3854 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3855 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3856 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3857 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3858 | goto out; |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3863 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3864 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3865 | goto out; |
| 3866 | } |
| 3867 | ph->id = strdup(args[cur_arg]); |
| 3868 | LIST_ADDQ(&curgrp->phs, &ph->list); |
| 3869 | cur_arg++; |
| 3870 | } |
| 3871 | } |
| 3872 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3873 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3874 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3875 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3876 | goto out; |
| 3877 | } |
| 3878 | out: |
| 3879 | return err_code; |
| 3880 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3881 | |
| 3882 | static int |
| 3883 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3884 | { |
| 3885 | struct spoe_message *msg; |
| 3886 | struct spoe_arg *arg; |
| 3887 | const char *err; |
| 3888 | char *errmsg = NULL; |
| 3889 | int err_code = 0; |
| 3890 | |
| 3891 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3892 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3893 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3894 | goto out; |
| 3895 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3896 | if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3897 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3898 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3899 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3900 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3901 | goto out; |
| 3902 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3903 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3904 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3905 | goto out; |
| 3906 | } |
| 3907 | |
| 3908 | err = invalid_char(args[1]); |
| 3909 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3910 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3911 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3912 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3913 | goto out; |
| 3914 | } |
| 3915 | |
| 3916 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3917 | if (strcmp(msg->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3918 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3919 | " name as another one declared at %s:%d.\n", |
| 3920 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3921 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3922 | goto out; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3927 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3928 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3929 | goto out; |
| 3930 | } |
| 3931 | |
| 3932 | curmsg->id = strdup(args[1]); |
| 3933 | curmsg->id_len = strlen(curmsg->id); |
| 3934 | curmsg->event = SPOE_EV_NONE; |
| 3935 | curmsg->conf.file = strdup(file); |
| 3936 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3937 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3938 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3939 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3940 | LIST_INIT(&curmsg->by_evt); |
| 3941 | LIST_INIT(&curmsg->by_grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3942 | LIST_ADDQ(&curmsgs, &curmsg->list); |
| 3943 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3944 | else if (strcmp(args[0], "args") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3945 | int cur_arg = 1; |
| 3946 | |
| 3947 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3948 | curproxy->conf.args.file = file; |
| 3949 | curproxy->conf.args.line = linenum; |
| 3950 | while (*args[cur_arg]) { |
| 3951 | char *delim = strchr(args[cur_arg], '='); |
| 3952 | int idx = 0; |
| 3953 | |
| 3954 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3955 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3956 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3957 | goto out; |
| 3958 | } |
| 3959 | |
| 3960 | if (!delim) { |
| 3961 | arg->name = NULL; |
| 3962 | arg->name_len = 0; |
| 3963 | delim = args[cur_arg]; |
| 3964 | } |
| 3965 | else { |
| 3966 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3967 | arg->name_len = delim - args[cur_arg]; |
| 3968 | delim++; |
| 3969 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3970 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3971 | &idx, file, linenum, &errmsg, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 3972 | &curproxy->conf.args, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3973 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3974 | 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] | 3975 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3976 | free(arg->name); |
| 3977 | free(arg); |
| 3978 | goto out; |
| 3979 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3980 | curmsg->nargs++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3981 | LIST_ADDQ(&curmsg->args, &arg->list); |
| 3982 | cur_arg++; |
| 3983 | } |
| 3984 | curproxy->conf.args.file = NULL; |
| 3985 | curproxy->conf.args.line = 0; |
| 3986 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3987 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3988 | err = invalid_char(args[1]); |
| 3989 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3990 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 3991 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3992 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3993 | goto out; |
| 3994 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 3995 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 3996 | ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a " |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 3997 | "logical disjunction within a condition.\n", |
| 3998 | file, linenum, args[1]); |
| 3999 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4000 | goto out; |
| 4001 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4002 | 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] | 4003 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4004 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4005 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4006 | goto out; |
| 4007 | } |
| 4008 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4009 | else if (strcmp(args[0], "event") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4010 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4011 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4012 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4013 | goto out; |
| 4014 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4015 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4016 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4017 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4018 | if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4019 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4020 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4021 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4022 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4023 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4024 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4025 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4026 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4027 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4028 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4029 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4030 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4031 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4032 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4033 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4034 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4035 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4036 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4037 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4038 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4039 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4040 | goto out; |
| 4041 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4042 | |
| 4043 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4044 | struct acl_cond *cond; |
| 4045 | |
| 4046 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4047 | curproxy, (const char **)args+2, |
| 4048 | &errmsg); |
| 4049 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4050 | ha_alert("parsing [%s:%d] : error detected while " |
| 4051 | "parsing an 'event %s' condition : %s.\n", |
| 4052 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4053 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4054 | goto out; |
| 4055 | } |
| 4056 | curmsg->cond = cond; |
| 4057 | } |
| 4058 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4059 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4060 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4061 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4062 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4063 | goto out; |
| 4064 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4065 | } |
| 4066 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4067 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4068 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4069 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4070 | goto out; |
| 4071 | } |
| 4072 | out: |
| 4073 | free(errmsg); |
| 4074 | return err_code; |
| 4075 | } |
| 4076 | |
| 4077 | /* Return -1 on error, else 0 */ |
| 4078 | static int |
| 4079 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4080 | struct flt_conf *fconf, char **err, void *private) |
| 4081 | { |
| 4082 | struct list backup_sections; |
| 4083 | struct spoe_config *conf; |
| 4084 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4085 | struct spoe_group *grp, *grpback; |
| 4086 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4087 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4088 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4089 | char *file = NULL, *engine = NULL; |
| 4090 | int ret, pos = *cur_arg + 1; |
| 4091 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4092 | LIST_INIT(&curmsgs); |
| 4093 | LIST_INIT(&curgrps); |
| 4094 | LIST_INIT(&curmphs); |
| 4095 | LIST_INIT(&curgphs); |
| 4096 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4097 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4098 | curpxopts = 0; |
| 4099 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4100 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4101 | conf = calloc(1, sizeof(*conf)); |
| 4102 | if (conf == NULL) { |
| 4103 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4104 | goto error; |
| 4105 | } |
| 4106 | conf->proxy = px; |
| 4107 | |
| 4108 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4109 | if (strcmp(args[pos], "config") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4110 | if (!*args[pos+1]) { |
| 4111 | memprintf(err, "'%s' : '%s' option without value", |
| 4112 | args[*cur_arg], args[pos]); |
| 4113 | goto error; |
| 4114 | } |
| 4115 | file = args[pos+1]; |
| 4116 | pos += 2; |
| 4117 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4118 | else if (strcmp(args[pos], "engine") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4119 | if (!*args[pos+1]) { |
| 4120 | memprintf(err, "'%s' : '%s' option without value", |
| 4121 | args[*cur_arg], args[pos]); |
| 4122 | goto error; |
| 4123 | } |
| 4124 | engine = args[pos+1]; |
| 4125 | pos += 2; |
| 4126 | } |
| 4127 | else { |
| 4128 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4129 | goto error; |
| 4130 | } |
| 4131 | } |
| 4132 | if (file == NULL) { |
| 4133 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4134 | goto error; |
| 4135 | } |
| 4136 | |
| 4137 | /* backup sections and register SPOE sections */ |
| 4138 | LIST_INIT(&backup_sections); |
| 4139 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4140 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4141 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4142 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4143 | |
| 4144 | /* Parse SPOE filter configuration file */ |
| 4145 | curengine = engine; |
| 4146 | curproxy = px; |
| 4147 | curagent = NULL; |
| 4148 | curmsg = NULL; |
| 4149 | ret = readcfgfile(file); |
| 4150 | curproxy = NULL; |
| 4151 | |
| 4152 | /* unregister SPOE sections and restore previous sections */ |
| 4153 | cfg_unregister_sections(); |
| 4154 | cfg_restore_sections(&backup_sections); |
| 4155 | |
| 4156 | if (ret == -1) { |
| 4157 | memprintf(err, "Could not open configuration file %s : %s", |
| 4158 | file, strerror(errno)); |
| 4159 | goto error; |
| 4160 | } |
| 4161 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4162 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4163 | goto error; |
| 4164 | } |
| 4165 | |
| 4166 | /* Check SPOE agent */ |
| 4167 | if (curagent == NULL) { |
| 4168 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4169 | goto error; |
| 4170 | } |
| 4171 | if (curagent->b.name == NULL) { |
| 4172 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4173 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4174 | goto error; |
| 4175 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4176 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4177 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4178 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4179 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4180 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4181 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4182 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4183 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4184 | } |
| 4185 | if (curagent->var_pfx == NULL) { |
| 4186 | char *tmp = curagent->id; |
| 4187 | |
| 4188 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4189 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4190 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4191 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4192 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4193 | goto error; |
| 4194 | } |
| 4195 | tmp++; |
| 4196 | } |
| 4197 | curagent->var_pfx = strdup(curagent->id); |
| 4198 | } |
| 4199 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4200 | if (curagent->var_on_error) { |
| 4201 | struct arg arg; |
| 4202 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4203 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4204 | curagent->var_pfx, curagent->var_on_error); |
| 4205 | |
| 4206 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4207 | arg.data.str.area = trash.area; |
| 4208 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4209 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4210 | if (!vars_check_arg(&arg, err)) { |
| 4211 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4212 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4213 | goto error; |
| 4214 | } |
| 4215 | } |
| 4216 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4217 | if (curagent->var_t_process) { |
| 4218 | struct arg arg; |
| 4219 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4220 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4221 | curagent->var_pfx, curagent->var_t_process); |
| 4222 | |
| 4223 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4224 | arg.data.str.area = trash.area; |
| 4225 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4226 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4227 | if (!vars_check_arg(&arg, err)) { |
| 4228 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4229 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4230 | goto error; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | if (curagent->var_t_total) { |
| 4235 | struct arg arg; |
| 4236 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4237 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4238 | curagent->var_pfx, curagent->var_t_total); |
| 4239 | |
| 4240 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4241 | arg.data.str.area = trash.area; |
| 4242 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4243 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4244 | if (!vars_check_arg(&arg, err)) { |
| 4245 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4246 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4247 | goto error; |
| 4248 | } |
| 4249 | } |
| 4250 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4251 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4252 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4253 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4254 | goto finish; |
| 4255 | } |
| 4256 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4257 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4258 | * agent */ |
| 4259 | list_for_each_entry(ph, &curmphs, list) { |
| 4260 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4261 | struct spoe_arg *arg; |
| 4262 | unsigned int where; |
| 4263 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4264 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4265 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4266 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4267 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4268 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4269 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4270 | } |
| 4271 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4272 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4273 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4274 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4275 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4276 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4277 | } |
| 4278 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4279 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4280 | px->id, msg->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 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4283 | |
| 4284 | where = 0; |
| 4285 | switch (msg->event) { |
| 4286 | case SPOE_EV_ON_CLIENT_SESS: |
| 4287 | where |= SMP_VAL_FE_CON_ACC; |
| 4288 | break; |
| 4289 | |
| 4290 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4291 | where |= SMP_VAL_FE_REQ_CNT; |
| 4292 | break; |
| 4293 | |
| 4294 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4295 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4296 | break; |
| 4297 | |
| 4298 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4299 | if (px->cap & PR_CAP_FE) |
| 4300 | where |= SMP_VAL_FE_REQ_CNT; |
| 4301 | if (px->cap & PR_CAP_BE) |
| 4302 | where |= SMP_VAL_BE_REQ_CNT; |
| 4303 | break; |
| 4304 | |
| 4305 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4306 | if (px->cap & PR_CAP_FE) |
| 4307 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4308 | if (px->cap & PR_CAP_BE) |
| 4309 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4310 | break; |
| 4311 | |
| 4312 | case SPOE_EV_ON_SERVER_SESS: |
| 4313 | where |= SMP_VAL_BE_SRV_CON; |
| 4314 | break; |
| 4315 | |
| 4316 | case SPOE_EV_ON_TCP_RSP: |
| 4317 | if (px->cap & PR_CAP_FE) |
| 4318 | where |= SMP_VAL_FE_RES_CNT; |
| 4319 | if (px->cap & PR_CAP_BE) |
| 4320 | where |= SMP_VAL_BE_RES_CNT; |
| 4321 | break; |
| 4322 | |
| 4323 | case SPOE_EV_ON_HTTP_RSP: |
| 4324 | if (px->cap & PR_CAP_FE) |
| 4325 | where |= SMP_VAL_FE_HRS_HDR; |
| 4326 | if (px->cap & PR_CAP_BE) |
| 4327 | where |= SMP_VAL_BE_HRS_HDR; |
| 4328 | break; |
| 4329 | |
| 4330 | default: |
| 4331 | break; |
| 4332 | } |
| 4333 | |
| 4334 | list_for_each_entry(arg, &msg->args, list) { |
| 4335 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4336 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4337 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4338 | "none of which is available here ('%s')", |
| 4339 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4340 | sample_ckp_names(arg->expr->fetch->use), |
| 4341 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4342 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4343 | } |
| 4344 | } |
| 4345 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4346 | msg->agent = curagent; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4347 | LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt); |
| 4348 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | 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] | 4352 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4353 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4354 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4355 | continue; |
| 4356 | } |
| 4357 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4358 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4359 | * agent */ |
| 4360 | list_for_each_entry(ph, &curgphs, list) { |
| 4361 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4362 | if (strcmp(grp->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4363 | grp->agent = curagent; |
| 4364 | LIST_DEL(&grp->list); |
| 4365 | LIST_ADDQ(&curagent->groups, &grp->list); |
| 4366 | goto next_aph; |
| 4367 | } |
| 4368 | } |
| 4369 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4370 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4371 | goto error; |
| 4372 | next_aph: |
| 4373 | continue; |
| 4374 | } |
| 4375 | |
| 4376 | /* Replace placeholders by the corresponding message for each SPOE |
| 4377 | * group of the SPOE agent */ |
| 4378 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4379 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4380 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4381 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4382 | if (msg->group != NULL) { |
| 4383 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4384 | "the SPOE group '%s' declare at %s:%d", |
| 4385 | msg->id, msg->group->id, |
| 4386 | msg->group->conf.file, |
| 4387 | msg->group->conf.line); |
| 4388 | goto error; |
| 4389 | } |
| 4390 | |
| 4391 | /* Scope for arguments are not checked for now. We will check |
| 4392 | * them only if a rule use the corresponding SPOE group. */ |
| 4393 | msg->agent = curagent; |
| 4394 | msg->group = grp; |
| 4395 | LIST_DEL(&ph->list); |
| 4396 | LIST_ADDQ(&grp->messages, &msg->by_grp); |
| 4397 | goto next_mph_grp; |
| 4398 | } |
| 4399 | } |
| 4400 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4401 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4402 | goto error; |
| 4403 | next_mph_grp: |
| 4404 | continue; |
| 4405 | } |
| 4406 | } |
| 4407 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4408 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4409 | /* move curmsgs to the agent message list */ |
| 4410 | curmsgs.n->p = &curagent->messages; |
| 4411 | curmsgs.p->n = &curagent->messages; |
| 4412 | curagent->messages = curmsgs; |
| 4413 | LIST_INIT(&curmsgs); |
| 4414 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4415 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4416 | conf->agent = curagent; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4417 | |
| 4418 | /* Start agent's proxy initialization here. It will be finished during |
| 4419 | * the filter init. */ |
| 4420 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4421 | init_new_proxy(&conf->agent_fe); |
| 4422 | conf->agent_fe.id = conf->agent->id; |
| 4423 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4424 | conf->agent_fe.options |= curpxopts; |
| 4425 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4426 | |
| 4427 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
| 4428 | LIST_DEL(&logsrv->list); |
| 4429 | LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list); |
| 4430 | } |
| 4431 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4432 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 4433 | LIST_DEL(&ph->list); |
| 4434 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4435 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4436 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 4437 | LIST_DEL(&ph->list); |
| 4438 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4439 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4440 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4441 | struct arg arg; |
| 4442 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4443 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4444 | curagent->var_pfx, vph->name); |
| 4445 | |
| 4446 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4447 | arg.data.str.area = trash.area; |
| 4448 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4449 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4450 | if (!vars_check_arg(&arg, err)) { |
| 4451 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4452 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4453 | goto error; |
| 4454 | } |
| 4455 | |
| 4456 | LIST_DEL(&vph->list); |
| 4457 | free(vph->name); |
| 4458 | free(vph); |
| 4459 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4460 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4461 | LIST_DEL(&grp->list); |
| 4462 | spoe_release_group(grp); |
| 4463 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4464 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4465 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4466 | fconf->ops = &spoe_ops; |
| 4467 | fconf->conf = conf; |
| 4468 | return 0; |
| 4469 | |
| 4470 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4471 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4472 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
| 4473 | LIST_DEL(&ph->list); |
| 4474 | spoe_release_placeholder(ph); |
| 4475 | } |
| 4476 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
| 4477 | LIST_DEL(&ph->list); |
| 4478 | spoe_release_placeholder(ph); |
| 4479 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4480 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4481 | LIST_DEL(&vph->list); |
| 4482 | free(vph->name); |
| 4483 | free(vph); |
| 4484 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4485 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
| 4486 | LIST_DEL(&grp->list); |
| 4487 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4488 | } |
| 4489 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
| 4490 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4491 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4492 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4493 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
| 4494 | LIST_DEL(&logsrv->list); |
| 4495 | free(logsrv); |
| 4496 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4497 | free(conf); |
| 4498 | return -1; |
| 4499 | } |
| 4500 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4501 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4502 | * associated to a "send-spoe-group" action. |
| 4503 | * |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4504 | * It returns ACT_RET_CONT if processing is finished (with error or not), it returns |
| 4505 | * ACT_RET_YIELD if the action is in progress. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4506 | static enum act_return |
| 4507 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4508 | struct session *sess, struct stream *s, int flags) |
| 4509 | { |
| 4510 | struct filter *filter; |
| 4511 | struct spoe_agent *agent = NULL; |
| 4512 | struct spoe_group *group = NULL; |
| 4513 | struct spoe_context *ctx = NULL; |
| 4514 | int ret, dir; |
| 4515 | |
| 4516 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4517 | if (filter->config == rule->arg.act.p[0]) { |
| 4518 | agent = rule->arg.act.p[2]; |
| 4519 | group = rule->arg.act.p[3]; |
| 4520 | ctx = filter->ctx; |
| 4521 | break; |
| 4522 | } |
| 4523 | } |
| 4524 | if (agent == NULL || group == NULL || ctx == NULL) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4525 | return ACT_RET_CONT; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4526 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4527 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4528 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4529 | switch (rule->from) { |
| 4530 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4531 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4532 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4533 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4534 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4535 | default: |
| 4536 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4537 | " - internal error while execute spoe-send-group\n", |
| 4538 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4539 | __FUNCTION__, s); |
| 4540 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4541 | agent->id); |
| 4542 | return ACT_RET_CONT; |
| 4543 | } |
| 4544 | |
| 4545 | ret = spoe_process_group(s, ctx, group, dir); |
| 4546 | if (ret == 1) |
| 4547 | return ACT_RET_CONT; |
| 4548 | else if (ret == 0) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 4549 | if (flags & ACT_OPT_FINAL) { |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4550 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4551 | " - failed to process group '%s': interrupted by caller\n", |
| 4552 | (int)now.tv_sec, (int)now.tv_usec, |
| 4553 | agent->id, __FUNCTION__, s, group->id); |
| 4554 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4555 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4556 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4557 | return ACT_RET_CONT; |
| 4558 | } |
| 4559 | return ACT_RET_YIELD; |
| 4560 | } |
| 4561 | else |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4562 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4566 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4567 | * action should be: |
| 4568 | * |
| 4569 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4570 | * |
| 4571 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4572 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4573 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4574 | * group. |
| 4575 | * |
| 4576 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4577 | * filled. |
| 4578 | */ |
| 4579 | static int |
| 4580 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4581 | { |
| 4582 | struct flt_conf *fconf; |
| 4583 | struct spoe_config *conf; |
| 4584 | struct spoe_agent *agent = NULL; |
| 4585 | struct spoe_group *group; |
| 4586 | struct spoe_message *msg; |
| 4587 | char *engine_id = rule->arg.act.p[0]; |
| 4588 | char *group_id = rule->arg.act.p[1]; |
| 4589 | unsigned int where = 0; |
| 4590 | |
| 4591 | switch (rule->from) { |
| 4592 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4593 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4594 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4595 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4596 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4597 | default: |
| 4598 | memprintf(err, |
| 4599 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4600 | rule->from); |
| 4601 | goto error; |
| 4602 | } |
| 4603 | |
| 4604 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4605 | * <px> */ |
| 4606 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4607 | conf = fconf->conf; |
| 4608 | |
| 4609 | /* This is not an SPOE filter */ |
| 4610 | if (fconf->id != spoe_filter_id) |
| 4611 | continue; |
| 4612 | |
| 4613 | /* This is the good engine */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4614 | if (strcmp(conf->id, engine_id) == 0) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4615 | agent = conf->agent; |
| 4616 | break; |
| 4617 | } |
| 4618 | } |
| 4619 | if (agent == NULL) { |
| 4620 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4621 | engine_id, group_id); |
| 4622 | goto error; |
| 4623 | } |
| 4624 | |
| 4625 | /* Try to find the right group */ |
| 4626 | list_for_each_entry(group, &agent->groups, list) { |
| 4627 | /* This is the good group */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4628 | if (strcmp(group->id, group_id) == 0) |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4629 | break; |
| 4630 | } |
| 4631 | if (&group->list == &agent->groups) { |
| 4632 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4633 | group_id, engine_id); |
| 4634 | goto error; |
| 4635 | } |
| 4636 | |
| 4637 | /* Ok, we found the group, we need to check messages and their |
| 4638 | * arguments */ |
| 4639 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4640 | struct spoe_arg *arg; |
| 4641 | |
| 4642 | list_for_each_entry(arg, &msg->args, list) { |
| 4643 | if (!(arg->expr->fetch->val & where)) { |
| 4644 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4645 | "some args extract information from '%s'," |
| 4646 | "none of which is available here ('%s')", |
| 4647 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4648 | sample_ckp_names(arg->expr->fetch->use), |
| 4649 | sample_ckp_names(where)); |
| 4650 | goto error; |
| 4651 | } |
| 4652 | } |
| 4653 | } |
| 4654 | |
| 4655 | free(engine_id); |
| 4656 | free(group_id); |
| 4657 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4658 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4659 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4660 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4661 | return 1; |
| 4662 | |
| 4663 | error: |
| 4664 | free(engine_id); |
| 4665 | free(group_id); |
| 4666 | return 0; |
| 4667 | } |
| 4668 | |
| 4669 | /* Parse 'send-spoe-group' action following the format: |
| 4670 | * |
| 4671 | * ... send-spoe-group <engine-id> <group-id> |
| 4672 | * |
| 4673 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4674 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4675 | * ids are saved and used later, when the rule will be checked. |
| 4676 | */ |
| 4677 | static enum act_parse_ret |
| 4678 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4679 | struct act_rule *rule, char **err) |
| 4680 | { |
| 4681 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4682 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4683 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4684 | return ACT_RET_PRS_ERR; |
| 4685 | } |
| 4686 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4687 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4688 | |
| 4689 | (*orig_arg) += 2; |
| 4690 | |
| 4691 | rule->action = ACT_CUSTOM; |
| 4692 | rule->action_ptr = spoe_send_group; |
| 4693 | rule->check_ptr = check_send_spoe_group; |
| 4694 | return ACT_RET_PRS_OK; |
| 4695 | } |
| 4696 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4697 | |
| 4698 | /* Declare the filter parser for "spoe" keyword */ |
| 4699 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4700 | { "spoe", parse_spoe_flt, NULL }, |
| 4701 | { NULL, NULL, NULL }, |
| 4702 | } |
| 4703 | }; |
| 4704 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4705 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4706 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4707 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4708 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4709 | { "send-spoe-group", parse_send_spoe_group }, |
| 4710 | { /* END */ }, |
| 4711 | } |
| 4712 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4713 | |
| 4714 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws); |
| 4715 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4716 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4717 | { "send-spoe-group", parse_send_spoe_group }, |
| 4718 | { /* END */ }, |
| 4719 | } |
| 4720 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4721 | |
| 4722 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4723 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4724 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4725 | { "send-spoe-group", parse_send_spoe_group }, |
| 4726 | { /* END */ }, |
| 4727 | } |
| 4728 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4729 | |
| 4730 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4731 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4732 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4733 | { "send-spoe-group", parse_send_spoe_group }, |
| 4734 | { /* END */ }, |
| 4735 | } |
| 4736 | }; |
| 4737 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4738 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |