blob: e8b56c144f9c8353c8d721b1c8a1276b18596e15 [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>
Christopher Faulet908628c2022-03-25 16:43:49 +010019#include <haproxy/conn_stream.h>
20#include <haproxy/cs_utils.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020021#include <haproxy/list.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
31 * appctx_free(). <applet> is assigned as the applet, but it can be NULL. The
32 * applet's task is always created on the current thread.
33 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010034struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp)
Christopher Fauletcb2fa362022-03-23 11:46:56 +010035{
36 struct appctx *appctx;
37
Willy Tarreau009e42b2022-05-05 19:55:03 +020038 appctx = pool_zalloc(pool_head_appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010039 if (unlikely(!appctx))
40 goto fail_appctx;
41
Willy Tarreau009e42b2022-05-05 19:55:03 +020042 LIST_INIT(&appctx->wait_entry);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010043 appctx->obj_type = OBJ_TYPE_APPCTX;
44 appctx->applet = applet;
Christopher Fauletac57bb52022-05-09 08:08:26 +020045 appctx->sess = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010046 if (!endp) {
47 endp = cs_endpoint_new();
48 if (!endp)
49 goto fail_endp;
50 endp->target = appctx;
51 endp->ctx = appctx;
52 endp->flags |= (CS_EP_T_APPLET|CS_EP_ORPHAN);
Christopher Fauletcb2fa362022-03-23 11:46:56 +010053 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010054 appctx->endp = endp;
55
56 appctx->t = task_new_here();
57 if (unlikely(!appctx->t))
58 goto fail_task;
59 appctx->t->process = task_run_applet;
60 appctx->t->context = appctx;
61
62 LIST_INIT(&appctx->buffer_wait.list);
63 appctx->buffer_wait.target = appctx;
64 appctx->buffer_wait.wakeup_cb = appctx_buf_available;
65
66 _HA_ATOMIC_INC(&nb_applets);
Christopher Fauletcb2fa362022-03-23 11:46:56 +010067 return appctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010068
69 fail_task:
70 cs_endpoint_free(appctx->endp);
71 fail_endp:
72 pool_free(pool_head_appctx, appctx);
73 fail_appctx:
74 return NULL;
Christopher Fauletcb2fa362022-03-23 11:46:56 +010075}
76
Christopher Faulet8718c952022-05-12 15:15:53 +020077/* Finalize the frontend appctx startup. It must not be called for a backend
78 * appctx. This function is responsible to create the appctx's session and the
79 * frontend conn-stream. By transitivity, the stream is also created.
80 *
81 * It returns 0 on success and -1 on error. In this case, it is the caller
82 * responsibility to release the appctx. However, the session is released if it
83 * was created. On success, if an error is encountered in the caller function,
Christopher Fauletd0c4ec02022-05-12 15:18:48 +020084 * the stream must be released instead of the appctx. To be sure,
85 * appctx_free_on_early_error() must be called in this case.
Christopher Faulet8718c952022-05-12 15:15:53 +020086 */
87int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input)
88{
89 struct session *sess;
90
91 BUG_ON(appctx->sess || !(appctx->endp->flags & CS_EP_ORPHAN));
92
93 sess = session_new(px, NULL, &appctx->obj_type);
94 if (!sess)
95 return -1;
96 if (!cs_new_from_endp(appctx->endp, sess, input)) {
97 session_free(sess);
98 return -1;
99 }
100 appctx->sess = sess;
101 return 0;
102}
103
Christopher Fauletd0c4ec02022-05-12 15:18:48 +0200104/* Release function to call when an error occurred during init stage of a
105 * frontend appctx. For a backend appctx, it just calls appctx_free()
106 */
107void appctx_free_on_early_error(struct appctx *appctx)
108{
109 /* If a frontend apctx is attached to a conn-stream, release the stream
110 * instead of the appctx.
111 */
112 if (!(appctx->endp->flags & CS_EP_ORPHAN) && !(appctx_cs(appctx)->flags & CS_FL_ISBACK)) {
113 stream_free(appctx_strm(appctx));
114 return;
115 }
116 appctx_free(appctx);
117}
118
Willy Tarreauf12f32a2022-05-02 14:57:03 +0200119/* reserves a command context of at least <size> bytes in the <appctx>, for
120 * use by a CLI command or any regular applet. The pointer to this context is
121 * stored in ctx.svcctx and is returned. The caller doesn't need to release
122 * it as it's allocated from reserved space. If the size is larger than
123 * APPLET_MAX_SVCCTX a crash will occur (hence that will never happen outside
124 * of development).
125 *
126 * Note that the command does *not* initialize the area, so that it can easily
127 * be used upon each entry in a function. It's left to the initialization code
128 * to do it if needed. The CLI will always zero the whole area before calling
129 * a keyword's ->parse() function.
130 */
131void *applet_reserve_svcctx(struct appctx *appctx, size_t size)
132{
133 BUG_ON(size > APPLET_MAX_SVCCTX);
134 appctx->svcctx = &appctx->svc.storage;
135 return appctx->svcctx;
136}
137
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200138/* call the applet's release() function if any, and marks the endp as shut.
139 * Needs to be called upon close().
140 */
141void appctx_shut(struct appctx *appctx)
142{
143 if (appctx->endp->flags & (CS_EP_SHR|CS_EP_SHW))
144 return;
145
146 if (appctx->applet->release)
147 appctx->applet->release(appctx);
148
149 appctx->endp->flags |= CS_EP_SHRR | CS_EP_SHWN;
150}
151
Willy Tarreau21028b52018-11-06 17:32:37 +0100152/* Callback used to wake up an applet when a buffer is available. The applet
153 * <appctx> is woken up if an input buffer was requested for the associated
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200154 * conn-stream. In this case the buffer is immediately allocated and the
Willy Tarreau21028b52018-11-06 17:32:37 +0100155 * function returns 1. Otherwise it returns 0. Note that this automatically
156 * covers multiple wake-up attempts by ensuring that the same buffer will not
157 * be accounted for multiple times.
158 */
159int appctx_buf_available(void *arg)
160{
161 struct appctx *appctx = arg;
Willy Tarreau0698c802022-05-11 14:09:57 +0200162 struct conn_stream *cs = appctx_cs(appctx);
Willy Tarreau21028b52018-11-06 17:32:37 +0100163
164 /* allocation requested ? */
Willy Tarreau66435e52022-05-10 11:32:31 +0200165 if (!(appctx->endp->flags & CS_EP_RXBLK_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +0100166 return 0;
167
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200168 cs_rx_buff_rdy(cs);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100169
170 /* was already allocated another way ? if so, don't take this one */
Christopher Faulet908628c2022-03-25 16:43:49 +0100171 if (c_size(cs_ic(cs)) || cs_ic(cs)->pipe)
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100172 return 0;
173
Willy Tarreau21028b52018-11-06 17:32:37 +0100174 /* allocation possible now ? */
Christopher Faulet908628c2022-03-25 16:43:49 +0100175 if (!b_alloc(&cs_ic(cs)->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200176 cs_rx_buff_blk(cs);
Willy Tarreau21028b52018-11-06 17:32:37 +0100177 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100178 }
Willy Tarreau21028b52018-11-06 17:32:37 +0100179
Willy Tarreau21028b52018-11-06 17:32:37 +0100180 task_wakeup(appctx->t, TASK_WOKEN_RES);
181 return 1;
182}
183
184/* Default applet handler */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100185struct task *task_run_applet(struct task *t, void *context, unsigned int state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200186{
Olivier Houchard673867c2018-05-25 16:58:52 +0200187 struct appctx *app = context;
Willy Tarreau0698c802022-05-11 14:09:57 +0200188 struct conn_stream *cs = appctx_cs(app);
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +0200189 unsigned int rate;
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200190 size_t count;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +0100191
Olivier Houchard673867c2018-05-25 16:58:52 +0200192 if (app->state & APPLET_WANT_DIE) {
193 __appctx_free(app);
194 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +0100195 }
Emeric Brun1138fd02017-06-19 12:38:55 +0200196
Olivier Houchard673867c2018-05-25 16:58:52 +0200197 /* We always pretend the applet can't get and doesn't want to
198 * put, it's up to it to change this if needed. This ensures
199 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +0200200 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200201 cs_cant_get(cs);
202 cs_rx_endp_done(cs);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200203
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100204 /* Now we'll try to allocate the input buffer. We wake up the applet in
205 * all cases. So this is the applet's responsibility to check if this
206 * buffer was allocated or not. This leaves a chance for applets to do
207 * some other processing if needed. The applet doesn't have anything to
208 * do if it needs the buffer, it will be called again upon readiness.
209 */
Christopher Faulet8f45eec2022-04-01 17:19:36 +0200210 if (!cs_alloc_ibuf(cs, &app->buffer_wait))
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200211 cs_rx_endp_more(cs);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100212
Christopher Faulet908628c2022-03-25 16:43:49 +0100213 count = co_data(cs_oc(cs));
Olivier Houchard673867c2018-05-25 16:58:52 +0200214 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +0200215
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200216 /* now check if the applet has released some room and forgot to
217 * notify the other side about it.
218 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100219 if (count != co_data(cs_oc(cs))) {
220 cs_oc(cs)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200221 cs_rx_room_rdy(cs_opposite(cs));
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200222 }
223
Willy Tarreau19920d62019-10-11 14:15:47 +0200224 /* measure the call rate and check for anomalies when too high */
225 rate = update_freq_ctr(&app->call_rate, 1);
226 if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second
Willy Tarreau66435e52022-05-10 11:32:31 +0200227 ((b_size(cs_ib(cs)) && app->endp->flags & CS_EP_RXBLK_BUFF) || // asks for a buffer which is present
228 (b_size(cs_ib(cs)) && !b_data(cs_ib(cs)) && app->endp->flags & CS_EP_RXBLK_ROOM) || // asks for room in an empty buffer
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200229 (b_data(cs_ob(cs)) && cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)) || // asks for data already present
Christopher Faulet908628c2022-03-25 16:43:49 +0100230 (!b_data(cs_ib(cs)) && b_data(cs_ob(cs)) && // didn't return anything ...
231 (cs_oc(cs)->flags & (CF_WRITE_PARTIAL|CF_SHUTW_NOW)) == CF_SHUTW_NOW))) { // ... and left data pending after a shut
Willy Tarreau19920d62019-10-11 14:15:47 +0200232 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
233 }
234
Christopher Faulet6059ba42022-04-01 16:34:53 +0200235 cs->data_cb->wake(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +0100236 channel_release_buffer(cs_ic(cs), &app->buffer_wait);
Olivier Houchard673867c2018-05-25 16:58:52 +0200237 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200238}