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