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