blob: fedd05c5782aaa48f9a9a93328eb18fafe18f372 [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>
Willy Tarreau81f38d62015-04-13 17:11:11 +020024
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010025unsigned int nb_applets = 0;
Emeric Brun1138fd02017-06-19 12:38:55 +020026
Willy Tarreau8280ea92019-07-18 10:41:36 +020027DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx));
28
Willy Tarreau009e42b2022-05-05 19:55:03 +020029/* Tries to allocate a new appctx and initialize all of its fields. The appctx
Christopher Fauletcb2fa362022-03-23 11:46:56 +010030 * is returned on success, NULL on failure. The appctx must be released using
Willy Tarreaucb854272022-06-15 16:35:51 +020031 * appctx_free(). <applet> is assigned as the applet, but it can be NULL. <thr>
32 * is the thread ID to start the applet on, and a negative value allows the
33 * applet to start anywhere. Backend applets may only be created on the current
34 * thread.
Christopher Fauletcb2fa362022-03-23 11:46:56 +010035 */
Willy Tarreaucb854272022-06-15 16:35:51 +020036struct appctx *appctx_new_on(struct applet *applet, struct sedesc *sedesc, int thr)
Christopher Fauletcb2fa362022-03-23 11:46:56 +010037{
38 struct appctx *appctx;
39
Christopher Fauletd9c1d332022-05-16 17:15:31 +020040 /* Backend appctx cannot be started on another thread than the local one */
Willy Tarreaucb854272022-06-15 16:35:51 +020041 BUG_ON(thr != tid && sedesc);
Christopher Faulet6095d572022-05-16 17:09:48 +020042
Willy Tarreau009e42b2022-05-05 19:55:03 +020043 appctx = pool_zalloc(pool_head_appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010044 if (unlikely(!appctx))
45 goto fail_appctx;
46
Willy Tarreau009e42b2022-05-05 19:55:03 +020047 LIST_INIT(&appctx->wait_entry);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010048 appctx->obj_type = OBJ_TYPE_APPCTX;
49 appctx->applet = applet;
Christopher Fauletac57bb52022-05-09 08:08:26 +020050 appctx->sess = NULL;
Willy Tarreaud869e132022-05-17 18:05:31 +020051 if (!sedesc) {
52 sedesc = sedesc_new();
53 if (!sedesc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010054 goto fail_endp;
Willy Tarreaud869e132022-05-17 18:05:31 +020055 sedesc->se = appctx;
56 se_fl_set(sedesc, SE_FL_T_APPLET | SE_FL_ORPHAN);
Christopher Fauletcb2fa362022-03-23 11:46:56 +010057 }
Willy Tarreaud869e132022-05-17 18:05:31 +020058 appctx->sedesc = sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010059
Willy Tarreau3b7a19c2022-06-15 16:40:58 +020060 appctx->t = task_new_on(thr);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010061 if (unlikely(!appctx->t))
62 goto fail_task;
63 appctx->t->process = task_run_applet;
64 appctx->t->context = appctx;
65
66 LIST_INIT(&appctx->buffer_wait.list);
67 appctx->buffer_wait.target = appctx;
68 appctx->buffer_wait.wakeup_cb = appctx_buf_available;
69
70 _HA_ATOMIC_INC(&nb_applets);
Christopher Fauletcb2fa362022-03-23 11:46:56 +010071 return appctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010072
73 fail_task:
Willy Tarreaud869e132022-05-17 18:05:31 +020074 sedesc_free(appctx->sedesc);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010075 fail_endp:
76 pool_free(pool_head_appctx, appctx);
77 fail_appctx:
78 return NULL;
Christopher Fauletcb2fa362022-03-23 11:46:56 +010079}
80
Christopher Faulet8718c952022-05-12 15:15:53 +020081/* Finalize the frontend appctx startup. It must not be called for a backend
82 * appctx. This function is responsible to create the appctx's session and the
Willy Tarreau4596fe22022-05-17 19:07:51 +020083 * frontend stream connector. By transitivity, the stream is also created.
Christopher Faulet8718c952022-05-12 15:15:53 +020084 *
85 * It returns 0 on success and -1 on error. In this case, it is the caller
86 * responsibility to release the appctx. However, the session is released if it
87 * was created. On success, if an error is encountered in the caller function,
Christopher Fauletd0c4ec02022-05-12 15:18:48 +020088 * the stream must be released instead of the appctx. To be sure,
89 * appctx_free_on_early_error() must be called in this case.
Christopher Faulet8718c952022-05-12 15:15:53 +020090 */
91int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input)
92{
93 struct session *sess;
94
Christopher Fauletd9c1d332022-05-16 17:15:31 +020095 /* async startup is only possible for frontend appctx. Thus for orphan
96 * appctx. Because no backend appctx can be orphan.
97 */
Willy Tarreaud869e132022-05-17 18:05:31 +020098 BUG_ON(!se_fl_test(appctx->sedesc, SE_FL_ORPHAN));
Christopher Faulet8718c952022-05-12 15:15:53 +020099
100 sess = session_new(px, NULL, &appctx->obj_type);
101 if (!sess)
102 return -1;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200103 if (!sc_new_from_endp(appctx->sedesc, sess, input)) {
Christopher Faulet8718c952022-05-12 15:15:53 +0200104 session_free(sess);
105 return -1;
106 }
107 appctx->sess = sess;
108 return 0;
109}
110
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200111/* Release function to call when an error occurred during init stage of a
112 * frontend appctx. For a backend appctx, it just calls appctx_free()
113 */
114void appctx_free_on_early_error(struct appctx *appctx)
115{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200116 /* If a frontend appctx is attached to a stream connector, release the stream
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200117 * instead of the appctx.
118 */
Willy Tarreauc12b3212022-05-27 11:08:15 +0200119 if (!se_fl_test(appctx->sedesc, SE_FL_ORPHAN) && !(appctx_sc(appctx)->flags & SC_FL_ISBACK)) {
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200120 stream_free(appctx_strm(appctx));
121 return;
122 }
123 appctx_free(appctx);
124}
125
Willy Tarreauf12f32a2022-05-02 14:57:03 +0200126/* reserves a command context of at least <size> bytes in the <appctx>, for
127 * use by a CLI command or any regular applet. The pointer to this context is
128 * stored in ctx.svcctx and is returned. The caller doesn't need to release
129 * it as it's allocated from reserved space. If the size is larger than
130 * APPLET_MAX_SVCCTX a crash will occur (hence that will never happen outside
131 * of development).
132 *
133 * Note that the command does *not* initialize the area, so that it can easily
134 * be used upon each entry in a function. It's left to the initialization code
135 * to do it if needed. The CLI will always zero the whole area before calling
136 * a keyword's ->parse() function.
137 */
138void *applet_reserve_svcctx(struct appctx *appctx, size_t size)
139{
140 BUG_ON(size > APPLET_MAX_SVCCTX);
141 appctx->svcctx = &appctx->svc.storage;
142 return appctx->svcctx;
143}
144
Willy Tarreau1cc08a32022-08-18 18:13:34 +0200145/* This is used to reset an svcctx and the svc.storage without releasing the
146 * appctx. In fact this is only used by the CLI applet between commands.
147 */
148void applet_reset_svcctx(struct appctx *appctx)
149{
150 memset(&appctx->svc.storage, 0, APPLET_MAX_SVCCTX);
151 appctx->svcctx = NULL;
152}
153
Willy Tarreaud869e132022-05-17 18:05:31 +0200154/* call the applet's release() function if any, and marks the sedesc as shut.
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200155 * Needs to be called upon close().
156 */
157void appctx_shut(struct appctx *appctx)
158{
Willy Tarreaud869e132022-05-17 18:05:31 +0200159 if (se_fl_test(appctx->sedesc, SE_FL_SHR | SE_FL_SHW))
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200160 return;
161
162 if (appctx->applet->release)
163 appctx->applet->release(appctx);
164
Willy Tarreaud869e132022-05-17 18:05:31 +0200165 se_fl_set(appctx->sedesc, SE_FL_SHRR | SE_FL_SHWN);
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200166}
167
Willy Tarreau21028b52018-11-06 17:32:37 +0100168/* Callback used to wake up an applet when a buffer is available. The applet
169 * <appctx> is woken up if an input buffer was requested for the associated
Willy Tarreau4596fe22022-05-17 19:07:51 +0200170 * stream connector. In this case the buffer is immediately allocated and the
Willy Tarreau21028b52018-11-06 17:32:37 +0100171 * function returns 1. Otherwise it returns 0. Note that this automatically
172 * covers multiple wake-up attempts by ensuring that the same buffer will not
173 * be accounted for multiple times.
174 */
175int appctx_buf_available(void *arg)
176{
177 struct appctx *appctx = arg;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200178 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau21028b52018-11-06 17:32:37 +0100179
180 /* allocation requested ? */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200181 if (!(sc->flags & SC_FL_NEED_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +0100182 return 0;
183
Willy Tarreau07ee0442022-05-27 10:32:34 +0200184 sc_have_buff(sc);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100185
186 /* was already allocated another way ? if so, don't take this one */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200187 if (c_size(sc_ic(sc)) || sc_ic(sc)->pipe)
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100188 return 0;
189
Willy Tarreau21028b52018-11-06 17:32:37 +0100190 /* allocation possible now ? */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200191 if (!b_alloc(&sc_ic(sc)->buf)) {
192 sc_need_buff(sc);
Willy Tarreau21028b52018-11-06 17:32:37 +0100193 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100194 }
Willy Tarreau21028b52018-11-06 17:32:37 +0100195
Willy Tarreau21028b52018-11-06 17:32:37 +0100196 task_wakeup(appctx->t, TASK_WOKEN_RES);
197 return 1;
198}
199
200/* Default applet handler */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100201struct task *task_run_applet(struct task *t, void *context, unsigned int state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200202{
Olivier Houchard673867c2018-05-25 16:58:52 +0200203 struct appctx *app = context;
Willy Tarreau07ee0442022-05-27 10:32:34 +0200204 struct stconn *sc;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +0200205 unsigned int rate;
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200206 size_t count;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +0100207
Olivier Houchard673867c2018-05-25 16:58:52 +0200208 if (app->state & APPLET_WANT_DIE) {
209 __appctx_free(app);
210 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +0100211 }
Emeric Brun1138fd02017-06-19 12:38:55 +0200212
Willy Tarreaud869e132022-05-17 18:05:31 +0200213 if (se_fl_test(app->sedesc, SE_FL_ORPHAN)) {
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200214 /* Finalize init of orphan appctx. .init callback function must
215 * be defined and it must finalize appctx startup.
216 */
217 BUG_ON(!app->applet->init);
218
219 if (appctx_init(app) == -1) {
220 appctx_free_on_early_error(app);
221 return NULL;
222 }
Willy Tarreauc12b3212022-05-27 11:08:15 +0200223 BUG_ON(!app->sess || !appctx_sc(app) || !appctx_strm(app));
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200224 }
225
Willy Tarreauc12b3212022-05-27 11:08:15 +0200226 sc = appctx_sc(app);
Christopher Fauletd9c1d332022-05-16 17:15:31 +0200227
Olivier Houchard673867c2018-05-25 16:58:52 +0200228 /* We always pretend the applet can't get and doesn't want to
229 * put, it's up to it to change this if needed. This ensures
230 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +0200231 */
Willy Tarreau90e8b452022-05-25 18:21:43 +0200232 applet_need_more_data(app);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200233 applet_have_no_more_data(app);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200234
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100235 /* Now we'll try to allocate the input buffer. We wake up the applet in
236 * all cases. So this is the applet's responsibility to check if this
237 * buffer was allocated or not. This leaves a chance for applets to do
238 * some other processing if needed. The applet doesn't have anything to
239 * do if it needs the buffer, it will be called again upon readiness.
240 */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200241 if (!sc_alloc_ibuf(sc, &app->buffer_wait))
Willy Tarreau4164eb92022-05-25 15:42:03 +0200242 applet_have_more_data(app);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100243
Willy Tarreau07ee0442022-05-27 10:32:34 +0200244 count = co_data(sc_oc(sc));
Olivier Houchard673867c2018-05-25 16:58:52 +0200245 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +0200246
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200247 /* now check if the applet has released some room and forgot to
248 * notify the other side about it.
249 */
Willy Tarreau07ee0442022-05-27 10:32:34 +0200250 if (count != co_data(sc_oc(sc))) {
251 sc_oc(sc)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
252 sc_have_room(sc_opposite(sc));
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200253 }
254
Willy Tarreau19920d62019-10-11 14:15:47 +0200255 /* measure the call rate and check for anomalies when too high */
Willy Tarreaudf3cab12022-07-19 20:36:15 +0200256 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 +0200257 (b_size(sc_ib(sc)) && !b_data(sc_ib(sc)) && sc->flags & SC_FL_NEED_ROOM) || // asks for room in an empty buffer
258 (b_data(sc_ob(sc)) && sc_is_send_allowed(sc)) || // asks for data already present
259 (!b_data(sc_ib(sc)) && b_data(sc_ob(sc)) && // didn't return anything ...
260 (sc_oc(sc)->flags & (CF_WRITE_PARTIAL|CF_SHUTW_NOW)) == CF_SHUTW_NOW))) { // ... and left data pending after a shut
Willy Tarreaudf3cab12022-07-19 20:36:15 +0200261 rate = update_freq_ctr(&app->call_rate, 1);
262 if (rate >= 100000 && app->call_rate.prev_ctr) // looped like this more than 100k times over last second
263 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
Willy Tarreau19920d62019-10-11 14:15:47 +0200264 }
265
Willy Tarreau07ee0442022-05-27 10:32:34 +0200266 sc->app_ops->wake(sc);
267 channel_release_buffer(sc_ic(sc), &app->buffer_wait);
Olivier Houchard673867c2018-05-25 16:58:52 +0200268 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200269}