blob: a4702d48a35de3da1cf1fd482cf66fa2807ff7c7 [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
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 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 }
301 appctx_free(appctx);
302}
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
Willy Tarreaud869e132022-05-17 18:05:31 +0200363 se_fl_set(appctx->sedesc, SE_FL_SHRR | SE_FL_SHWN);
Christopher Faulet26e09352023-03-29 17:42:28 +0200364 TRACE_LEAVE(APPLET_EV_RELEASE, appctx);
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200365}
366
Willy Tarreau21028b52018-11-06 17:32:37 +0100367/* 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 Tarreau4596fe22022-05-17 19:07:51 +0200369 * stream connector. In this case the buffer is immediately allocated and the
Willy Tarreau21028b52018-11-06 17:32:37 +0100370 * 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 */
374int appctx_buf_available(void *arg)
375{
376 struct appctx *appctx = arg;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200377 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau21028b52018-11-06 17:32:37 +0100378
379 /* allocation requested ? */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200380 if (!(sc->flags & SC_FL_NEED_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +0100381 return 0;
382
Willy Tarreau07ee0442022-05-27 10:32:34 +0200383 sc_have_buff(sc);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100384
385 /* was already allocated another way ? if so, don't take this one */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200386 if (c_size(sc_ic(sc)) || sc_ic(sc)->pipe)
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100387 return 0;
388
Willy Tarreau21028b52018-11-06 17:32:37 +0100389 /* allocation possible now ? */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200390 if (!b_alloc(&sc_ic(sc)->buf)) {
391 sc_need_buff(sc);
Willy Tarreau21028b52018-11-06 17:32:37 +0100392 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100393 }
Willy Tarreau21028b52018-11-06 17:32:37 +0100394
Willy Tarreau21028b52018-11-06 17:32:37 +0100395 task_wakeup(appctx->t, TASK_WOKEN_RES);
396 return 1;
397}
398
399/* Default applet handler */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100400struct task *task_run_applet(struct task *t, void *context, unsigned int state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200401{
Olivier Houchard673867c2018-05-25 16:58:52 +0200402 struct appctx *app = context;
Willy Tarreau07ee0442022-05-27 10:32:34 +0200403 struct stconn *sc;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +0200404 unsigned int rate;
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200405 size_t count;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +0100406
Christopher Faulet26e09352023-03-29 17:42:28 +0200407 TRACE_ENTER(APPLET_EV_PROCESS, app);
408
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 if (app->state & APPLET_WANT_DIE) {
Christopher Faulet26e09352023-03-29 17:42:28 +0200410 TRACE_DEVEL("APPCTX want die, release it", APPLET_EV_FREE, app);
Olivier Houchard673867c2018-05-25 16:58:52 +0200411 __appctx_free(app);
412 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +0100413 }
Emeric Brun1138fd02017-06-19 12:38:55 +0200414
Willy Tarreaud869e132022-05-17 18:05:31 +0200415 if (se_fl_test(app->sedesc, SE_FL_ORPHAN)) {
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200416 /* 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 Faulet26e09352023-03-29 17:42:28 +0200422 TRACE_DEVEL("APPCTX init failed", APPLET_EV_FREE|APPLET_EV_ERR, app);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200423 appctx_free_on_early_error(app);
424 return NULL;
425 }
Willy Tarreauc12b3212022-05-27 11:08:15 +0200426 BUG_ON(!app->sess || !appctx_sc(app) || !appctx_strm(app));
Christopher Faulet26e09352023-03-29 17:42:28 +0200427 TRACE_DEVEL("APPCTX initialized", APPLET_EV_PROCESS, app);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200428 }
429
Willy Tarreauc12b3212022-05-27 11:08:15 +0200430 sc = appctx_sc(app);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200431
Olivier Houchard673867c2018-05-25 16:58:52 +0200432 /* 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 Tarreau99942382015-09-25 17:56:16 +0200435 */
Willy Tarreau90e8b452022-05-25 18:21:43 +0200436 applet_need_more_data(app);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200437 applet_have_no_more_data(app);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200438
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100439 /* 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 Tarreau07ee0442022-05-27 10:32:34 +0200445 if (!sc_alloc_ibuf(sc, &app->buffer_wait))
Willy Tarreau4164eb92022-05-25 15:42:03 +0200446 applet_have_more_data(app);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100447
Willy Tarreau07ee0442022-05-27 10:32:34 +0200448 count = co_data(sc_oc(sc));
Olivier Houchard673867c2018-05-25 16:58:52 +0200449 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +0200450
Christopher Faulet26e09352023-03-29 17:42:28 +0200451 TRACE_POINT(APPLET_EV_PROCESS, app);
452
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200453 /* now check if the applet has released some room and forgot to
454 * notify the other side about it.
455 */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200456 if (count != co_data(sc_oc(sc))) {
Christopher Fauletd8988412022-12-20 18:10:04 +0100457 sc_oc(sc)->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau07ee0442022-05-27 10:32:34 +0200458 sc_have_room(sc_opposite(sc));
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200459 }
460
Christopher Faulet4c135682023-02-16 11:09:31 +0100461 if (sc_ic(sc)->flags & CF_READ_EVENT)
462 sc_ep_report_read_activity(sc);
463
Christopher Faulet59b240c2023-02-27 16:38:12 +0100464 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 Tarreau19920d62019-10-11 14:15:47 +0200469 /* measure the call rate and check for anomalies when too high */
Willy Tarreaudf3cab12022-07-19 20:36:15 +0200470 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 +0200471 (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 Faulet7faac7c2023-04-04 10:05:27 +0200474 (!(sc_oc(sc)->flags & CF_WRITE_EVENT) && (sc->flags & SC_FL_SHUTW_NOW))))) { // ... and left data pending after a shut
Willy Tarreaudf3cab12022-07-19 20:36:15 +0200475 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 Tarreau19920d62019-10-11 14:15:47 +0200478 }
479
Christopher Faulet8019f782023-03-23 17:30:29 +0100480 BUG_ON_HOT((sc_ep_get(sc) & (SE_FL_EOI|SE_FL_EOS|SE_FL_ERROR)) == SE_FL_EOS);
481
Willy Tarreau07ee0442022-05-27 10:32:34 +0200482 sc->app_ops->wake(sc);
483 channel_release_buffer(sc_ic(sc), &app->buffer_wait);
Christopher Faulet26e09352023-03-29 17:42:28 +0200484 TRACE_LEAVE(APPLET_EV_PROCESS, app);
Olivier Houchard673867c2018-05-25 16:58:52 +0200485 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200486}