Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing applets |
| 3 | * |
| 4 | * Copyright 2000-2015 Willy Tarreau <w@1wt.eu> |
| 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 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/applet.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 18 | #include <haproxy/channel.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 19 | #include <haproxy/list.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 20 | #include <haproxy/sc_strm.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 21 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 22 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 23 | #include <haproxy/task.h> |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 24 | #include <haproxy/trace.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 25 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 26 | unsigned int nb_applets = 0; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 28 | DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx)); |
| 29 | |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 30 | |
| 31 | /* trace source and events */ |
| 32 | static void applet_trace(enum trace_level level, uint64_t mask, |
| 33 | const struct trace_source *src, |
| 34 | const struct ist where, const struct ist func, |
| 35 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 36 | |
| 37 | /* The event representation is split like this : |
| 38 | * app - applet |
| 39 | */ |
| 40 | static const struct trace_event applet_trace_events[] = { |
| 41 | #define APPLET_EV_NEW (1ULL << 0) |
| 42 | { .mask = APPLET_EV_NEW, .name = "app_new", .desc = "new appctx" }, |
| 43 | #define APPLET_EV_FREE (1ULL << 1) |
| 44 | { .mask = APPLET_EV_FREE, .name = "app_free", .desc = "free appctx" }, |
| 45 | #define APPLET_EV_RELEASE (1ULL << 2) |
| 46 | { .mask = APPLET_EV_RELEASE, .name = "app_release", .desc = "release appctx" }, |
| 47 | #define APPLET_EV_PROCESS (1ULL << 3) |
| 48 | { .mask = APPLET_EV_PROCESS, .name = "app_proc", .desc = "process appctx" }, |
| 49 | #define APPLET_EV_ERR (1ULL << 4) |
| 50 | { .mask = APPLET_EV_ERR, .name = "app_err", .desc = "error on appctx" }, |
| 51 | #define APPLET_EV_START (1ULL << 5) |
| 52 | { .mask = APPLET_EV_START, .name = "app_start", .desc = "start appctx" }, |
| 53 | {} |
| 54 | }; |
| 55 | |
| 56 | static const struct name_desc applet_trace_lockon_args[4] = { |
| 57 | /* arg1 */ { /* already used by the applet */ }, |
| 58 | /* arg2 */ { }, |
| 59 | /* arg3 */ { }, |
| 60 | /* arg4 */ { } |
| 61 | }; |
| 62 | |
| 63 | static const struct name_desc applet_trace_decoding[] = { |
| 64 | #define STRM_VERB_CLEAN 1 |
| 65 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 66 | #define STRM_VERB_MINIMAL 2 |
| 67 | { .name="minimal", .desc="report info on streams and connectors" }, |
| 68 | #define STRM_VERB_SIMPLE 3 |
| 69 | { .name="simple", .desc="add info on request and response channels" }, |
| 70 | #define STRM_VERB_ADVANCED 4 |
| 71 | { .name="advanced", .desc="add info on channel's buffer for data and developer levels only" }, |
| 72 | #define STRM_VERB_COMPLETE 5 |
| 73 | { .name="complete", .desc="add info on channel's buffer" }, |
| 74 | { /* end */ } |
| 75 | }; |
| 76 | |
| 77 | static struct trace_source trace_applet = { |
| 78 | .name = IST("applet"), |
| 79 | .desc = "Applet endpoint", |
| 80 | .arg_def = TRC_ARG1_APPCTX, // TRACE()'s first argument is always an appctx |
| 81 | .default_cb = applet_trace, |
| 82 | .known_events = applet_trace_events, |
| 83 | .lockon_args = applet_trace_lockon_args, |
| 84 | .decoding = applet_trace_decoding, |
| 85 | .report_events = ~0, // report everything by default |
| 86 | }; |
| 87 | |
| 88 | #define TRACE_SOURCE &trace_applet |
| 89 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 90 | |
| 91 | /* the applet traces always expect that arg1, if non-null, is of a appctx (from |
| 92 | * which we can derive everything). |
| 93 | */ |
| 94 | static void applet_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 95 | const struct ist where, const struct ist func, |
| 96 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 97 | { |
| 98 | const struct appctx *appctx = a1; |
| 99 | const struct stconn *sc = NULL, *sco = NULL; |
| 100 | const struct stream *s = NULL; |
| 101 | const struct channel *ic = NULL, *oc = NULL; |
| 102 | |
| 103 | if (!appctx || src->verbosity < STRM_VERB_CLEAN) |
| 104 | return; |
| 105 | |
| 106 | sc = appctx_sc(appctx); |
| 107 | if (sc) { |
| 108 | s = sc_strm(sc); |
| 109 | sco = sc_opposite(sc); |
| 110 | ic = sc_ic(sc); |
| 111 | oc = sc_oc(sc); |
| 112 | } |
| 113 | |
| 114 | /* General info about the stream (htx/tcp, id...) */ |
| 115 | if (s) |
| 116 | chunk_appendf(&trace_buf, " : [%s,%s]", |
| 117 | appctx->applet->name, ((s->flags & SF_HTX) ? "HTX" : "TCP")); |
| 118 | else |
| 119 | chunk_appendf(&trace_buf, " : [%s]", appctx->applet->name); |
| 120 | |
| 121 | if (sc) |
| 122 | /* local and opposite stream connector state */ |
| 123 | chunk_appendf(&trace_buf, " SC=(%s,%s)", |
| 124 | sc_state_str(sc->state), sc_state_str(sco->state)); |
| 125 | else |
| 126 | /* local and opposite stream connector state */ |
| 127 | chunk_appendf(&trace_buf, " SC=(none,none)"); |
| 128 | |
| 129 | if (src->verbosity == STRM_VERB_CLEAN) |
| 130 | return; |
| 131 | |
| 132 | chunk_appendf(&trace_buf, " appctx=%p .t=%p .t.exp=%d .state=%d .st0=%d .st1=%d", |
| 133 | appctx, appctx->t, tick_isset(appctx->t->expire) ? TICKS_TO_MS(appctx->t->expire - now_ms) : TICK_ETERNITY, |
| 134 | appctx->state, appctx->st0, appctx->st1); |
| 135 | |
| 136 | if (!sc || src->verbosity == STRM_VERB_MINIMAL) |
| 137 | return; |
| 138 | |
| 139 | chunk_appendf(&trace_buf, " - s=(%p,0x%08x,0x%x)", s, s->flags, s->conn_err_type); |
| 140 | |
| 141 | chunk_appendf(&trace_buf, " sc=(%p,%d,0x%08x,0x%x) sco=(%p,%d,0x%08x,0x%x) sc.exp(r,w)=(%d,%d) sc.exp(r,w)=(%d,%d)", |
| 142 | sc, sc->state, sc->flags, sc->sedesc->flags, |
| 143 | sco, sco->state, sco->flags, sco->sedesc->flags, |
| 144 | tick_isset(sc_ep_rcv_ex(sc)) ? TICKS_TO_MS(sc_ep_rcv_ex(sc) - now_ms) : TICK_ETERNITY, |
| 145 | tick_isset(sc_ep_snd_ex(sc)) ? TICKS_TO_MS(sc_ep_snd_ex(sc) - now_ms) : TICK_ETERNITY, |
| 146 | tick_isset(sc_ep_rcv_ex(sco)) ? TICKS_TO_MS(sc_ep_rcv_ex(sco) - now_ms) : TICK_ETERNITY, |
| 147 | tick_isset(sc_ep_snd_ex(sco)) ? TICKS_TO_MS(sc_ep_snd_ex(sco) - now_ms) : TICK_ETERNITY); |
| 148 | |
| 149 | |
| 150 | /* If txn defined, don't display all channel info */ |
| 151 | if (src->verbosity == STRM_VERB_SIMPLE) { |
| 152 | chunk_appendf(&trace_buf, " ic=(%p .fl=0x%08x .exp=%d)", |
| 153 | ic, ic->flags, tick_isset(ic->analyse_exp) ? TICKS_TO_MS(ic->analyse_exp - now_ms) : TICK_ETERNITY); |
| 154 | chunk_appendf(&trace_buf, " oc=(%p .fl=0x%08x .exp=%d)", |
| 155 | oc, oc->flags, tick_isset(oc->analyse_exp) ? TICKS_TO_MS(oc->analyse_exp - now_ms) : TICK_ETERNITY); |
| 156 | } |
| 157 | else { |
| 158 | chunk_appendf(&trace_buf, " ic=(%p .fl=0x%08x .ana=0x%08x .exp=%u .o=%lu .tot=%llu .to_fwd=%u)", |
| 159 | ic, ic->flags, ic->analysers, ic->analyse_exp, |
| 160 | (long)ic->output, ic->total, ic->to_forward); |
| 161 | chunk_appendf(&trace_buf, " oc=(%p .fl=0x%08x .ana=0x%08x .exp=%u .o=%lu .tot=%llu .to_fwd=%u)", |
| 162 | oc, oc->flags, oc->analysers, oc->analyse_exp, |
| 163 | (long)oc->output, oc->total, oc->to_forward); |
| 164 | } |
| 165 | |
| 166 | if (src->verbosity == STRM_VERB_SIMPLE || |
| 167 | (src->verbosity == STRM_VERB_ADVANCED && src->level < TRACE_LEVEL_DATA)) |
| 168 | return; |
| 169 | |
| 170 | /* channels' buffer info */ |
| 171 | if (s->flags & SF_HTX) { |
| 172 | struct htx *ichtx = htxbuf(&ic->buf); |
| 173 | struct htx *ochtx = htxbuf(&oc->buf); |
| 174 | |
| 175 | chunk_appendf(&trace_buf, " htx=(%u/%u#%u, %u/%u#%u)", |
| 176 | ichtx->data, ichtx->size, htx_nbblks(ichtx), |
| 177 | ochtx->data, ochtx->size, htx_nbblks(ochtx)); |
| 178 | } |
| 179 | else { |
| 180 | chunk_appendf(&trace_buf, " buf=(%u@%p+%u/%u, %u@%p+%u/%u)", |
| 181 | (unsigned int)b_data(&ic->buf), b_orig(&ic->buf), |
| 182 | (unsigned int)b_head_ofs(&ic->buf), (unsigned int)b_size(&ic->buf), |
| 183 | (unsigned int)b_data(&oc->buf), b_orig(&oc->buf), |
| 184 | (unsigned int)b_head_ofs(&oc->buf), (unsigned int)b_size(&oc->buf)); |
| 185 | } |
| 186 | } |
| 187 | |
Willy Tarreau | 009e42b | 2022-05-05 19:55:03 +0200 | [diff] [blame] | 188 | /* Tries to allocate a new appctx and initialize all of its fields. The appctx |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 189 | * is returned on success, NULL on failure. The appctx must be released using |
Willy Tarreau | cb85427 | 2022-06-15 16:35:51 +0200 | [diff] [blame] | 190 | * appctx_free(). <applet> is assigned as the applet, but it can be NULL. <thr> |
| 191 | * is the thread ID to start the applet on, and a negative value allows the |
| 192 | * applet to start anywhere. Backend applets may only be created on the current |
| 193 | * thread. |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 194 | */ |
Willy Tarreau | cb85427 | 2022-06-15 16:35:51 +0200 | [diff] [blame] | 195 | struct appctx *appctx_new_on(struct applet *applet, struct sedesc *sedesc, int thr) |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 196 | { |
| 197 | struct appctx *appctx; |
| 198 | |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 199 | /* Backend appctx cannot be started on another thread than the local one */ |
Willy Tarreau | cb85427 | 2022-06-15 16:35:51 +0200 | [diff] [blame] | 200 | BUG_ON(thr != tid && sedesc); |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 201 | |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 202 | TRACE_ENTER(APPLET_EV_NEW); |
| 203 | |
Willy Tarreau | 009e42b | 2022-05-05 19:55:03 +0200 | [diff] [blame] | 204 | appctx = pool_zalloc(pool_head_appctx); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 205 | if (unlikely(!appctx)) { |
| 206 | TRACE_ERROR("APPCTX allocation failure", APPLET_EV_NEW|APPLET_EV_ERR); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 207 | goto fail_appctx; |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 208 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 209 | |
Willy Tarreau | 009e42b | 2022-05-05 19:55:03 +0200 | [diff] [blame] | 210 | LIST_INIT(&appctx->wait_entry); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 211 | appctx->obj_type = OBJ_TYPE_APPCTX; |
| 212 | appctx->applet = applet; |
Christopher Faulet | ac57bb5 | 2022-05-09 08:08:26 +0200 | [diff] [blame] | 213 | appctx->sess = NULL; |
Aurelien DARRAGON | 821581c | 2023-03-24 10:01:19 +0100 | [diff] [blame] | 214 | |
| 215 | appctx->t = task_new_on(thr); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 216 | if (unlikely(!appctx->t)) { |
| 217 | TRACE_ERROR("APPCTX task allocation failure", APPLET_EV_NEW|APPLET_EV_ERR); |
Aurelien DARRAGON | 821581c | 2023-03-24 10:01:19 +0100 | [diff] [blame] | 218 | goto fail_task; |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 219 | } |
Aurelien DARRAGON | 821581c | 2023-03-24 10:01:19 +0100 | [diff] [blame] | 220 | |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 221 | if (!sedesc) { |
| 222 | sedesc = sedesc_new(); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 223 | if (unlikely(!sedesc)) { |
| 224 | TRACE_ERROR("APPCTX sedesc allocation failure", APPLET_EV_NEW|APPLET_EV_ERR); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 225 | goto fail_endp; |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 226 | } |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 227 | sedesc->se = appctx; |
| 228 | se_fl_set(sedesc, SE_FL_T_APPLET | SE_FL_ORPHAN); |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 229 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 230 | |
Willy Tarreau | 465a6c8 | 2023-03-21 10:50:51 +0100 | [diff] [blame] | 231 | appctx->sedesc = sedesc; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 232 | appctx->t->process = task_run_applet; |
| 233 | appctx->t->context = appctx; |
| 234 | |
| 235 | LIST_INIT(&appctx->buffer_wait.list); |
| 236 | appctx->buffer_wait.target = appctx; |
| 237 | appctx->buffer_wait.wakeup_cb = appctx_buf_available; |
| 238 | |
| 239 | _HA_ATOMIC_INC(&nb_applets); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 240 | |
| 241 | TRACE_LEAVE(APPLET_EV_NEW, appctx); |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 242 | return appctx; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 243 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 244 | fail_endp: |
Aurelien DARRAGON | 821581c | 2023-03-24 10:01:19 +0100 | [diff] [blame] | 245 | task_destroy(appctx->t); |
| 246 | fail_task: |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 247 | pool_free(pool_head_appctx, appctx); |
| 248 | fail_appctx: |
| 249 | return NULL; |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 252 | /* Finalize the frontend appctx startup. It must not be called for a backend |
| 253 | * appctx. This function is responsible to create the appctx's session and the |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 254 | * frontend stream connector. By transitivity, the stream is also created. |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 255 | * |
| 256 | * It returns 0 on success and -1 on error. In this case, it is the caller |
| 257 | * responsibility to release the appctx. However, the session is released if it |
| 258 | * was created. On success, if an error is encountered in the caller function, |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 259 | * the stream must be released instead of the appctx. To be sure, |
| 260 | * appctx_free_on_early_error() must be called in this case. |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 261 | */ |
| 262 | int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input) |
| 263 | { |
| 264 | struct session *sess; |
| 265 | |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 266 | /* async startup is only possible for frontend appctx. Thus for orphan |
| 267 | * appctx. Because no backend appctx can be orphan. |
| 268 | */ |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 269 | BUG_ON(!se_fl_test(appctx->sedesc, SE_FL_ORPHAN)); |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 270 | |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 271 | TRACE_ENTER(APPLET_EV_START, appctx); |
| 272 | |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 273 | sess = session_new(px, NULL, &appctx->obj_type); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 274 | if (!sess) { |
| 275 | TRACE_ERROR("APPCTX session allocation failure", APPLET_EV_START|APPLET_EV_ERR, appctx); |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 276 | return -1; |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 277 | } |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 278 | if (!sc_new_from_endp(appctx->sedesc, sess, input)) { |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 279 | session_free(sess); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 280 | TRACE_ERROR("APPCTX sc allocation failure", APPLET_EV_START|APPLET_EV_ERR, appctx); |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 281 | return -1; |
| 282 | } |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 283 | |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 284 | appctx->sess = sess; |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 285 | TRACE_LEAVE(APPLET_EV_START, appctx); |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 289 | /* Release function to call when an error occurred during init stage of a |
| 290 | * frontend appctx. For a backend appctx, it just calls appctx_free() |
| 291 | */ |
| 292 | void appctx_free_on_early_error(struct appctx *appctx) |
| 293 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 294 | /* If a frontend appctx is attached to a stream connector, release the stream |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 295 | * instead of the appctx. |
| 296 | */ |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 297 | if (!se_fl_test(appctx->sedesc, SE_FL_ORPHAN) && !(appctx_sc(appctx)->flags & SC_FL_ISBACK)) { |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 298 | stream_free(appctx_strm(appctx)); |
| 299 | return; |
| 300 | } |
| 301 | appctx_free(appctx); |
| 302 | } |
| 303 | |
Christopher Faulet | a5915eb | 2023-03-29 17:37:48 +0200 | [diff] [blame] | 304 | void appctx_free(struct appctx *appctx) |
| 305 | { |
| 306 | /* The task is supposed to be run on this thread, so we can just |
| 307 | * check if it's running already (or about to run) or not |
| 308 | */ |
| 309 | if (!(appctx->t->state & (TASK_QUEUED | TASK_RUNNING))) { |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 310 | TRACE_POINT(APPLET_EV_FREE, appctx); |
Christopher Faulet | a5915eb | 2023-03-29 17:37:48 +0200 | [diff] [blame] | 311 | __appctx_free(appctx); |
| 312 | } |
| 313 | else { |
| 314 | /* if it's running, or about to run, defer the freeing |
| 315 | * until the callback is called. |
| 316 | */ |
| 317 | appctx->state |= APPLET_WANT_DIE; |
| 318 | task_wakeup(appctx->t, TASK_WOKEN_OTHER); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 319 | TRACE_DEVEL("Cannot release APPCTX now, wake it up", APPLET_EV_FREE, appctx); |
Christopher Faulet | a5915eb | 2023-03-29 17:37:48 +0200 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Willy Tarreau | f12f32a | 2022-05-02 14:57:03 +0200 | [diff] [blame] | 323 | /* reserves a command context of at least <size> bytes in the <appctx>, for |
| 324 | * use by a CLI command or any regular applet. The pointer to this context is |
| 325 | * stored in ctx.svcctx and is returned. The caller doesn't need to release |
| 326 | * it as it's allocated from reserved space. If the size is larger than |
| 327 | * APPLET_MAX_SVCCTX a crash will occur (hence that will never happen outside |
| 328 | * of development). |
| 329 | * |
| 330 | * Note that the command does *not* initialize the area, so that it can easily |
| 331 | * be used upon each entry in a function. It's left to the initialization code |
| 332 | * to do it if needed. The CLI will always zero the whole area before calling |
| 333 | * a keyword's ->parse() function. |
| 334 | */ |
| 335 | void *applet_reserve_svcctx(struct appctx *appctx, size_t size) |
| 336 | { |
| 337 | BUG_ON(size > APPLET_MAX_SVCCTX); |
| 338 | appctx->svcctx = &appctx->svc.storage; |
| 339 | return appctx->svcctx; |
| 340 | } |
| 341 | |
Willy Tarreau | 1cc08a3 | 2022-08-18 18:13:34 +0200 | [diff] [blame] | 342 | /* This is used to reset an svcctx and the svc.storage without releasing the |
| 343 | * appctx. In fact this is only used by the CLI applet between commands. |
| 344 | */ |
| 345 | void applet_reset_svcctx(struct appctx *appctx) |
| 346 | { |
| 347 | memset(&appctx->svc.storage, 0, APPLET_MAX_SVCCTX); |
| 348 | appctx->svcctx = NULL; |
| 349 | } |
| 350 | |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 351 | /* call the applet's release() function if any, and marks the sedesc as shut. |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 352 | * Needs to be called upon close(). |
| 353 | */ |
| 354 | void appctx_shut(struct appctx *appctx) |
| 355 | { |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 356 | if (se_fl_test(appctx->sedesc, SE_FL_SHR | SE_FL_SHW)) |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 357 | return; |
| 358 | |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 359 | TRACE_ENTER(APPLET_EV_RELEASE, appctx); |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 360 | if (appctx->applet->release) |
| 361 | appctx->applet->release(appctx); |
| 362 | |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 363 | se_fl_set(appctx->sedesc, SE_FL_SHRR | SE_FL_SHWN); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 364 | TRACE_LEAVE(APPLET_EV_RELEASE, appctx); |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 367 | /* Callback used to wake up an applet when a buffer is available. The applet |
| 368 | * <appctx> is woken up if an input buffer was requested for the associated |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 369 | * stream connector. In this case the buffer is immediately allocated and the |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 370 | * function returns 1. Otherwise it returns 0. Note that this automatically |
| 371 | * covers multiple wake-up attempts by ensuring that the same buffer will not |
| 372 | * be accounted for multiple times. |
| 373 | */ |
| 374 | int appctx_buf_available(void *arg) |
| 375 | { |
| 376 | struct appctx *appctx = arg; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 377 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 378 | |
| 379 | /* allocation requested ? */ |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 380 | if (!(sc->flags & SC_FL_NEED_BUFF)) |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 381 | return 0; |
| 382 | |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 383 | sc_have_buff(sc); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 384 | |
| 385 | /* was already allocated another way ? if so, don't take this one */ |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 386 | if (c_size(sc_ic(sc)) || sc_ic(sc)->pipe) |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 387 | return 0; |
| 388 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 389 | /* allocation possible now ? */ |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 390 | if (!b_alloc(&sc_ic(sc)->buf)) { |
| 391 | sc_need_buff(sc); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 392 | return 0; |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 393 | } |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 394 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 395 | task_wakeup(appctx->t, TASK_WOKEN_RES); |
| 396 | return 1; |
| 397 | } |
| 398 | |
| 399 | /* Default applet handler */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 400 | struct task *task_run_applet(struct task *t, void *context, unsigned int state) |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 401 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 402 | struct appctx *app = context; |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 403 | struct stconn *sc; |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 404 | unsigned int rate; |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 405 | size_t count; |
Christopher Faulet | b4a4d9a | 2017-11-15 22:14:49 +0100 | [diff] [blame] | 406 | |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 407 | TRACE_ENTER(APPLET_EV_PROCESS, app); |
| 408 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 409 | if (app->state & APPLET_WANT_DIE) { |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 410 | TRACE_DEVEL("APPCTX want die, release it", APPLET_EV_FREE, app); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 411 | __appctx_free(app); |
| 412 | return NULL; |
Christopher Faulet | 7163056 | 2017-11-14 11:30:47 +0100 | [diff] [blame] | 413 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 414 | |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 415 | if (se_fl_test(app->sedesc, SE_FL_ORPHAN)) { |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 416 | /* Finalize init of orphan appctx. .init callback function must |
| 417 | * be defined and it must finalize appctx startup. |
| 418 | */ |
| 419 | BUG_ON(!app->applet->init); |
| 420 | |
| 421 | if (appctx_init(app) == -1) { |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 422 | TRACE_DEVEL("APPCTX init failed", APPLET_EV_FREE|APPLET_EV_ERR, app); |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 423 | appctx_free_on_early_error(app); |
| 424 | return NULL; |
| 425 | } |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 426 | BUG_ON(!app->sess || !appctx_sc(app) || !appctx_strm(app)); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 427 | TRACE_DEVEL("APPCTX initialized", APPLET_EV_PROCESS, app); |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 430 | sc = appctx_sc(app); |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 431 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 432 | /* We always pretend the applet can't get and doesn't want to |
| 433 | * put, it's up to it to change this if needed. This ensures |
| 434 | * that one applet which ignores any event will not spin. |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 435 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 436 | applet_need_more_data(app); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 437 | applet_have_no_more_data(app); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 438 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 439 | /* Now we'll try to allocate the input buffer. We wake up the applet in |
| 440 | * all cases. So this is the applet's responsibility to check if this |
| 441 | * buffer was allocated or not. This leaves a chance for applets to do |
| 442 | * some other processing if needed. The applet doesn't have anything to |
| 443 | * do if it needs the buffer, it will be called again upon readiness. |
| 444 | */ |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 445 | if (!sc_alloc_ibuf(sc, &app->buffer_wait)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 446 | applet_have_more_data(app); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 447 | |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 448 | count = co_data(sc_oc(sc)); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 449 | app->applet->fct(app); |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 450 | |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 451 | TRACE_POINT(APPLET_EV_PROCESS, app); |
| 452 | |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 453 | /* now check if the applet has released some room and forgot to |
| 454 | * notify the other side about it. |
| 455 | */ |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 456 | if (count != co_data(sc_oc(sc))) { |
Christopher Faulet | d898841 | 2022-12-20 18:10:04 +0100 | [diff] [blame] | 457 | sc_oc(sc)->flags |= CF_WRITE_EVENT | CF_WROTE_DATA; |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 458 | sc_have_room(sc_opposite(sc)); |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Christopher Faulet | 4c13568 | 2023-02-16 11:09:31 +0100 | [diff] [blame] | 461 | if (sc_ic(sc)->flags & CF_READ_EVENT) |
| 462 | sc_ep_report_read_activity(sc); |
| 463 | |
Christopher Faulet | 59b240c | 2023-02-27 16:38:12 +0100 | [diff] [blame] | 464 | if (channel_is_empty(sc_oc(sc))) |
| 465 | sc_ep_report_send_activity(sc); |
| 466 | else |
| 467 | sc_ep_report_blocked_send(sc); |
| 468 | |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 469 | /* measure the call rate and check for anomalies when too high */ |
Willy Tarreau | df3cab1 | 2022-07-19 20:36:15 +0200 | [diff] [blame] | 470 | if (((b_size(sc_ib(sc)) && sc->flags & SC_FL_NEED_BUFF) || // asks for a buffer which is present |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 471 | (b_size(sc_ib(sc)) && !b_data(sc_ib(sc)) && sc->flags & SC_FL_NEED_ROOM) || // asks for room in an empty buffer |
| 472 | (b_data(sc_ob(sc)) && sc_is_send_allowed(sc)) || // asks for data already present |
| 473 | (!b_data(sc_ib(sc)) && b_data(sc_ob(sc)) && // didn't return anything ... |
Christopher Faulet | 7faac7c | 2023-04-04 10:05:27 +0200 | [diff] [blame^] | 474 | (!(sc_oc(sc)->flags & CF_WRITE_EVENT) && (sc->flags & SC_FL_SHUTW_NOW))))) { // ... and left data pending after a shut |
Willy Tarreau | df3cab1 | 2022-07-19 20:36:15 +0200 | [diff] [blame] | 475 | rate = update_freq_ctr(&app->call_rate, 1); |
| 476 | if (rate >= 100000 && app->call_rate.prev_ctr) // looped like this more than 100k times over last second |
| 477 | stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate)); |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Willy Tarreau | 07ee044 | 2022-05-27 10:32:34 +0200 | [diff] [blame] | 480 | sc->app_ops->wake(sc); |
| 481 | channel_release_buffer(sc_ic(sc), &app->buffer_wait); |
Christopher Faulet | 26e0935 | 2023-03-29 17:42:28 +0200 | [diff] [blame] | 482 | TRACE_LEAVE(APPLET_EV_PROCESS, app); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 483 | return t; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 484 | } |