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