blob: 4605e8d7529d18466e125c35556f4a5c8841b15c [file] [log] [blame]
Willy Tarreau81f38d62015-04-13 17:11:11 +02001/*
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 Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020018#include <haproxy/channel.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020019#include <haproxy/list.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020020#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020022#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020023#include <haproxy/task.h>
Christopher Faulet26e09352023-03-29 17:42:28 +020024#include <haproxy/trace.h>
Willy Tarreau81f38d62015-04-13 17:11:11 +020025
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010026unsigned int nb_applets = 0;
Emeric Brun1138fd02017-06-19 12:38:55 +020027
Willy Tarreau8280ea92019-07-18 10:41:36 +020028DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx));
29
Christopher Faulet26e09352023-03-29 17:42:28 +020030
31/* trace source and events */
32static 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 */
40static 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
56static const struct name_desc applet_trace_lockon_args[4] = {
57 /* arg1 */ { /* already used by the applet */ },
58 /* arg2 */ { },
59 /* arg3 */ { },
60 /* arg4 */ { }
61};
62
63static 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
77static 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
89INITCALL1(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 */
94static 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) {
Christopher Faulet8eeec382023-04-06 08:48:16 +0200108 s = __sc_strm(sc);
Christopher Faulet26e09352023-03-29 17:42:28 +0200109 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
Christopher Fauletc0878c62023-08-29 10:55:15 +0200141 chunk_appendf(&trace_buf, " sc=(%p,%d,0x%08x,0x%x) sco=(%p,%d,0x%08x,0x%x) sc.exp(r,w)=(%d,%d) sco.exp(r,w)=(%d,%d)",
Christopher Faulet26e09352023-03-29 17:42:28 +0200142 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 Tarreau009e42b2022-05-05 19:55:03 +0200188/* Tries to allocate a new appctx and initialize all of its fields. The appctx
Christopher Fauletcb2fa362022-03-23 11:46:56 +0100189 * is returned on success, NULL on failure. The appctx must be released using
Willy Tarreaucb854272022-06-15 16:35:51 +0200190 * 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 Fauletcb2fa362022-03-23 11:46:56 +0100194 */
Willy Tarreaucb854272022-06-15 16:35:51 +0200195struct appctx *appctx_new_on(struct applet *applet, struct sedesc *sedesc, int thr)
Christopher Fauletcb2fa362022-03-23 11:46:56 +0100196{
197 struct appctx *appctx;
198
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200199 /* Backend appctx cannot be started on another thread than the local one */
Willy Tarreaucb854272022-06-15 16:35:51 +0200200 BUG_ON(thr != tid && sedesc);
Christopher Faulet6095d572022-05-16 17:09:48 +0200201
Christopher Faulet26e09352023-03-29 17:42:28 +0200202 TRACE_ENTER(APPLET_EV_NEW);
203
Willy Tarreau009e42b2022-05-05 19:55:03 +0200204 appctx = pool_zalloc(pool_head_appctx);
Christopher Faulet26e09352023-03-29 17:42:28 +0200205 if (unlikely(!appctx)) {
206 TRACE_ERROR("APPCTX allocation failure", APPLET_EV_NEW|APPLET_EV_ERR);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100207 goto fail_appctx;
Christopher Faulet26e09352023-03-29 17:42:28 +0200208 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100209
Willy Tarreau009e42b2022-05-05 19:55:03 +0200210 LIST_INIT(&appctx->wait_entry);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100211 appctx->obj_type = OBJ_TYPE_APPCTX;
212 appctx->applet = applet;
Christopher Fauletac57bb52022-05-09 08:08:26 +0200213 appctx->sess = NULL;
Aurelien DARRAGON821581c2023-03-24 10:01:19 +0100214
215 appctx->t = task_new_on(thr);
Christopher Faulet26e09352023-03-29 17:42:28 +0200216 if (unlikely(!appctx->t)) {
217 TRACE_ERROR("APPCTX task allocation failure", APPLET_EV_NEW|APPLET_EV_ERR);
Aurelien DARRAGON821581c2023-03-24 10:01:19 +0100218 goto fail_task;
Christopher Faulet26e09352023-03-29 17:42:28 +0200219 }
Aurelien DARRAGON821581c2023-03-24 10:01:19 +0100220
Willy Tarreaud869e132022-05-17 18:05:31 +0200221 if (!sedesc) {
222 sedesc = sedesc_new();
Christopher Faulet26e09352023-03-29 17:42:28 +0200223 if (unlikely(!sedesc)) {
224 TRACE_ERROR("APPCTX sedesc allocation failure", APPLET_EV_NEW|APPLET_EV_ERR);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100225 goto fail_endp;
Christopher Faulet26e09352023-03-29 17:42:28 +0200226 }
Willy Tarreaud869e132022-05-17 18:05:31 +0200227 sedesc->se = appctx;
228 se_fl_set(sedesc, SE_FL_T_APPLET | SE_FL_ORPHAN);
Christopher Fauletcb2fa362022-03-23 11:46:56 +0100229 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100230
Willy Tarreau465a6c82023-03-21 10:50:51 +0100231 appctx->sedesc = sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100232 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 Faulet26e09352023-03-29 17:42:28 +0200240
241 TRACE_LEAVE(APPLET_EV_NEW, appctx);
Christopher Fauletcb2fa362022-03-23 11:46:56 +0100242 return appctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100243
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100244 fail_endp:
Aurelien DARRAGON821581c2023-03-24 10:01:19 +0100245 task_destroy(appctx->t);
246 fail_task:
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100247 pool_free(pool_head_appctx, appctx);
248 fail_appctx:
249 return NULL;
Christopher Fauletcb2fa362022-03-23 11:46:56 +0100250}
251
Christopher Faulet8718c952022-05-12 15:15:53 +0200252/* 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 Tarreau4596fe22022-05-17 19:07:51 +0200254 * frontend stream connector. By transitivity, the stream is also created.
Christopher Faulet8718c952022-05-12 15:15:53 +0200255 *
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 Fauletd0c4ec02022-05-12 15:18:48 +0200259 * 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 Faulet8718c952022-05-12 15:15:53 +0200261 */
262int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input)
263{
264 struct session *sess;
265
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200266 /* async startup is only possible for frontend appctx. Thus for orphan
267 * appctx. Because no backend appctx can be orphan.
268 */
Willy Tarreaud869e132022-05-17 18:05:31 +0200269 BUG_ON(!se_fl_test(appctx->sedesc, SE_FL_ORPHAN));
Christopher Faulet8718c952022-05-12 15:15:53 +0200270
Christopher Faulet26e09352023-03-29 17:42:28 +0200271 TRACE_ENTER(APPLET_EV_START, appctx);
272
Christopher Faulet8718c952022-05-12 15:15:53 +0200273 sess = session_new(px, NULL, &appctx->obj_type);
Christopher Faulet26e09352023-03-29 17:42:28 +0200274 if (!sess) {
275 TRACE_ERROR("APPCTX session allocation failure", APPLET_EV_START|APPLET_EV_ERR, appctx);
Christopher Faulet8718c952022-05-12 15:15:53 +0200276 return -1;
Christopher Faulet26e09352023-03-29 17:42:28 +0200277 }
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200278 if (!sc_new_from_endp(appctx->sedesc, sess, input)) {
Christopher Faulet8718c952022-05-12 15:15:53 +0200279 session_free(sess);
Christopher Faulet26e09352023-03-29 17:42:28 +0200280 TRACE_ERROR("APPCTX sc allocation failure", APPLET_EV_START|APPLET_EV_ERR, appctx);
Christopher Faulet8718c952022-05-12 15:15:53 +0200281 return -1;
282 }
Christopher Faulet26e09352023-03-29 17:42:28 +0200283
Christopher Faulet8718c952022-05-12 15:15:53 +0200284 appctx->sess = sess;
Christopher Faulet26e09352023-03-29 17:42:28 +0200285 TRACE_LEAVE(APPLET_EV_START, appctx);
Christopher Faulet8718c952022-05-12 15:15:53 +0200286 return 0;
287}
288
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200289/* 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 */
292void appctx_free_on_early_error(struct appctx *appctx)
293{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200294 /* If a frontend appctx is attached to a stream connector, release the stream
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200295 * instead of the appctx.
296 */
Willy Tarreauc12b3212022-05-27 11:08:15 +0200297 if (!se_fl_test(appctx->sedesc, SE_FL_ORPHAN) && !(appctx_sc(appctx)->flags & SC_FL_ISBACK)) {
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200298 stream_free(appctx_strm(appctx));
299 return;
300 }
Christopher Fauletae9e0382024-02-16 15:00:07 +0100301 __appctx_free(appctx);
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200302}
303
Christopher Fauleta5915eb2023-03-29 17:37:48 +0200304void 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 Faulet26e09352023-03-29 17:42:28 +0200310 TRACE_POINT(APPLET_EV_FREE, appctx);
Christopher Fauleta5915eb2023-03-29 17:37:48 +0200311 __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 Faulet26e09352023-03-29 17:42:28 +0200319 TRACE_DEVEL("Cannot release APPCTX now, wake it up", APPLET_EV_FREE, appctx);
Christopher Fauleta5915eb2023-03-29 17:37:48 +0200320 }
321}
322
Willy Tarreauf12f32a2022-05-02 14:57:03 +0200323/* 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 */
335void *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 Tarreau1cc08a32022-08-18 18:13:34 +0200342/* 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 */
345void applet_reset_svcctx(struct appctx *appctx)
346{
347 memset(&appctx->svc.storage, 0, APPLET_MAX_SVCCTX);
348 appctx->svcctx = NULL;
349}
350
Willy Tarreaud869e132022-05-17 18:05:31 +0200351/* call the applet's release() function if any, and marks the sedesc as shut.
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200352 * Needs to be called upon close().
353 */
354void appctx_shut(struct appctx *appctx)
355{
Willy Tarreaud869e132022-05-17 18:05:31 +0200356 if (se_fl_test(appctx->sedesc, SE_FL_SHR | SE_FL_SHW))
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200357 return;
358
Christopher Faulet26e09352023-03-29 17:42:28 +0200359 TRACE_ENTER(APPLET_EV_RELEASE, appctx);
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200360 if (appctx->applet->release)
361 appctx->applet->release(appctx);
362
Christopher Fauleta821a6f2023-11-10 17:04:23 +0100363 if (LIST_INLIST(&appctx->buffer_wait.list))
364 LIST_DEL_INIT(&appctx->buffer_wait.list);
365
Willy Tarreaud869e132022-05-17 18:05:31 +0200366 se_fl_set(appctx->sedesc, SE_FL_SHRR | SE_FL_SHWN);
Christopher Faulet26e09352023-03-29 17:42:28 +0200367 TRACE_LEAVE(APPLET_EV_RELEASE, appctx);
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200368}
369
Willy Tarreau21028b52018-11-06 17:32:37 +0100370/* Callback used to wake up an applet when a buffer is available. The applet
371 * <appctx> is woken up if an input buffer was requested for the associated
Willy Tarreau4596fe22022-05-17 19:07:51 +0200372 * stream connector. In this case the buffer is immediately allocated and the
Willy Tarreau21028b52018-11-06 17:32:37 +0100373 * function returns 1. Otherwise it returns 0. Note that this automatically
374 * covers multiple wake-up attempts by ensuring that the same buffer will not
375 * be accounted for multiple times.
376 */
377int appctx_buf_available(void *arg)
378{
379 struct appctx *appctx = arg;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200380 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau21028b52018-11-06 17:32:37 +0100381
382 /* allocation requested ? */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200383 if (!(sc->flags & SC_FL_NEED_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +0100384 return 0;
385
Willy Tarreau07ee0442022-05-27 10:32:34 +0200386 sc_have_buff(sc);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100387
388 /* was already allocated another way ? if so, don't take this one */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200389 if (c_size(sc_ic(sc)) || sc_ic(sc)->pipe)
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100390 return 0;
391
Willy Tarreau21028b52018-11-06 17:32:37 +0100392 /* allocation possible now ? */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200393 if (!b_alloc(&sc_ic(sc)->buf)) {
394 sc_need_buff(sc);
Willy Tarreau21028b52018-11-06 17:32:37 +0100395 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100396 }
Willy Tarreau21028b52018-11-06 17:32:37 +0100397
Willy Tarreau21028b52018-11-06 17:32:37 +0100398 task_wakeup(appctx->t, TASK_WOKEN_RES);
399 return 1;
400}
401
402/* Default applet handler */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100403struct task *task_run_applet(struct task *t, void *context, unsigned int state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200404{
Olivier Houchard673867c2018-05-25 16:58:52 +0200405 struct appctx *app = context;
Christopher Fauletc184b112023-05-05 11:39:03 +0200406 struct stconn *sc, *sco;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +0200407 unsigned int rate;
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200408 size_t count;
Christopher Faulete65daba2023-11-15 17:33:06 +0100409 int did_send = 0;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +0100410
Christopher Faulet26e09352023-03-29 17:42:28 +0200411 TRACE_ENTER(APPLET_EV_PROCESS, app);
412
Olivier Houchard673867c2018-05-25 16:58:52 +0200413 if (app->state & APPLET_WANT_DIE) {
Christopher Faulet26e09352023-03-29 17:42:28 +0200414 TRACE_DEVEL("APPCTX want die, release it", APPLET_EV_FREE, app);
Olivier Houchard673867c2018-05-25 16:58:52 +0200415 __appctx_free(app);
416 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +0100417 }
Emeric Brun1138fd02017-06-19 12:38:55 +0200418
Willy Tarreaud869e132022-05-17 18:05:31 +0200419 if (se_fl_test(app->sedesc, SE_FL_ORPHAN)) {
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200420 /* Finalize init of orphan appctx. .init callback function must
421 * be defined and it must finalize appctx startup.
422 */
423 BUG_ON(!app->applet->init);
424
425 if (appctx_init(app) == -1) {
Christopher Faulet26e09352023-03-29 17:42:28 +0200426 TRACE_DEVEL("APPCTX init failed", APPLET_EV_FREE|APPLET_EV_ERR, app);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200427 appctx_free_on_early_error(app);
428 return NULL;
429 }
Willy Tarreauc12b3212022-05-27 11:08:15 +0200430 BUG_ON(!app->sess || !appctx_sc(app) || !appctx_strm(app));
Christopher Faulet26e09352023-03-29 17:42:28 +0200431 TRACE_DEVEL("APPCTX initialized", APPLET_EV_PROCESS, app);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200432 }
433
Willy Tarreauc12b3212022-05-27 11:08:15 +0200434 sc = appctx_sc(app);
Christopher Fauletc184b112023-05-05 11:39:03 +0200435 sco = sc_opposite(sc);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200436
Olivier Houchard673867c2018-05-25 16:58:52 +0200437 /* We always pretend the applet can't get and doesn't want to
438 * put, it's up to it to change this if needed. This ensures
439 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +0200440 */
Willy Tarreau90e8b452022-05-25 18:21:43 +0200441 applet_need_more_data(app);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200442 applet_have_no_more_data(app);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200443
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100444 /* Now we'll try to allocate the input buffer. We wake up the applet in
445 * all cases. So this is the applet's responsibility to check if this
446 * buffer was allocated or not. This leaves a chance for applets to do
447 * some other processing if needed. The applet doesn't have anything to
448 * do if it needs the buffer, it will be called again upon readiness.
449 */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200450 if (!sc_alloc_ibuf(sc, &app->buffer_wait))
Willy Tarreau4164eb92022-05-25 15:42:03 +0200451 applet_have_more_data(app);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100452
Willy Tarreau07ee0442022-05-27 10:32:34 +0200453 count = co_data(sc_oc(sc));
Olivier Houchard673867c2018-05-25 16:58:52 +0200454 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +0200455
Christopher Faulet26e09352023-03-29 17:42:28 +0200456 TRACE_POINT(APPLET_EV_PROCESS, app);
457
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200458 /* now check if the applet has released some room and forgot to
459 * notify the other side about it.
460 */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200461 if (count != co_data(sc_oc(sc))) {
Christopher Fauletd8988412022-12-20 18:10:04 +0100462 sc_oc(sc)->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Christopher Fauletc184b112023-05-05 11:39:03 +0200463 if (sco->room_needed < 0 || channel_recv_max(sc_oc(sc)) >= sco->room_needed)
464 sc_have_room(sco);
Christopher Faulete65daba2023-11-15 17:33:06 +0100465 did_send = 1;
Christopher Faulet4887e352023-10-10 18:23:05 +0200466 }
467 else {
468 if (!sco->room_needed)
469 sc_have_room(sco);
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200470 }
471
Christopher Faulet4c135682023-02-16 11:09:31 +0100472 if (sc_ic(sc)->flags & CF_READ_EVENT)
473 sc_ep_report_read_activity(sc);
474
Christopher Fauletc4a80ae2023-09-06 08:52:39 +0200475 if (sc_waiting_room(sc) && (sc->flags & SC_FL_ABRT_DONE)) {
476 sc_ep_set(sc, SE_FL_EOS|SE_FL_ERROR);
477 }
Christopher Faulet59b240c2023-02-27 16:38:12 +0100478
Christopher Faulet7fb330b2023-11-17 15:26:57 +0100479 if (!co_data(sc_oc(sc))) {
480 if (did_send)
481 sc_ep_report_send_activity(sc);
482 }
Christopher Faulete65daba2023-11-15 17:33:06 +0100483 else
484 sc_ep_report_blocked_send(sc, did_send);
485
Willy Tarreau19920d62019-10-11 14:15:47 +0200486 /* measure the call rate and check for anomalies when too high */
Willy Tarreaudf3cab12022-07-19 20:36:15 +0200487 if (((b_size(sc_ib(sc)) && sc->flags & SC_FL_NEED_BUFF) || // asks for a buffer which is present
Willy Tarreau07ee0442022-05-27 10:32:34 +0200488 (b_size(sc_ib(sc)) && !b_data(sc_ib(sc)) && sc->flags & SC_FL_NEED_ROOM) || // asks for room in an empty buffer
489 (b_data(sc_ob(sc)) && sc_is_send_allowed(sc)) || // asks for data already present
490 (!b_data(sc_ib(sc)) && b_data(sc_ob(sc)) && // didn't return anything ...
Christopher Faulete38534c2023-04-13 15:45:24 +0200491 (!(sc_oc(sc)->flags & CF_WRITE_EVENT) && (sc->flags & SC_FL_SHUT_WANTED))))) { // ... and left data pending after a shut
Willy Tarreaudf3cab12022-07-19 20:36:15 +0200492 rate = update_freq_ctr(&app->call_rate, 1);
493 if (rate >= 100000 && app->call_rate.prev_ctr) // looped like this more than 100k times over last second
494 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
Willy Tarreau19920d62019-10-11 14:15:47 +0200495 }
496
Willy Tarreau07ee0442022-05-27 10:32:34 +0200497 sc->app_ops->wake(sc);
498 channel_release_buffer(sc_ic(sc), &app->buffer_wait);
Christopher Faulet26e09352023-03-29 17:42:28 +0200499 TRACE_LEAVE(APPLET_EV_PROCESS, app);
Olivier Houchard673867c2018-05-25 16:58:52 +0200500 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200501}