blob: 65e4337050282a7f66677f15c66b2168e08c6d61 [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 Tarreau5e539c92020-06-04 20:45:39 +020023#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020024#include <haproxy/task.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 Fauletcb2fa362022-03-23 11:46:56 +010030/* Initializes all required fields for a new appctx. Note that it does the
31 * minimum acceptable initialization for an appctx. This means only the
32 * 3 integer states st0, st1, st2 and the chunk used to gather unfinished
33 * commands are zeroed
34 */
35static inline void appctx_init(struct appctx *appctx)
36{
37 appctx->st0 = appctx->st1 = appctx->st2 = 0;
38 appctx->chunk = NULL;
39 appctx->io_release = NULL;
40 appctx->call_rate.curr_tick = 0;
41 appctx->call_rate.curr_ctr = 0;
42 appctx->call_rate.prev_ctr = 0;
43 appctx->state = 0;
44 LIST_INIT(&appctx->wait_entry);
45}
46
47/* Tries to allocate a new appctx and initialize its main fields. The appctx
48 * is returned on success, NULL on failure. The appctx must be released using
49 * appctx_free(). <applet> is assigned as the applet, but it can be NULL. The
50 * applet's task is always created on the current thread.
51 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010052struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp)
Christopher Fauletcb2fa362022-03-23 11:46:56 +010053{
54 struct appctx *appctx;
55
56 appctx = pool_alloc(pool_head_appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010057 if (unlikely(!appctx))
58 goto fail_appctx;
59
60 appctx_init(appctx);
61 appctx->obj_type = OBJ_TYPE_APPCTX;
62 appctx->applet = applet;
63
64 if (!endp) {
65 endp = cs_endpoint_new();
66 if (!endp)
67 goto fail_endp;
68 endp->target = appctx;
69 endp->ctx = appctx;
70 endp->flags |= (CS_EP_T_APPLET|CS_EP_ORPHAN);
Christopher Fauletcb2fa362022-03-23 11:46:56 +010071 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010072 appctx->endp = endp;
73
74 appctx->t = task_new_here();
75 if (unlikely(!appctx->t))
76 goto fail_task;
77 appctx->t->process = task_run_applet;
78 appctx->t->context = appctx;
79
80 LIST_INIT(&appctx->buffer_wait.list);
81 appctx->buffer_wait.target = appctx;
82 appctx->buffer_wait.wakeup_cb = appctx_buf_available;
83
84 _HA_ATOMIC_INC(&nb_applets);
Christopher Fauletcb2fa362022-03-23 11:46:56 +010085 return appctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010086
87 fail_task:
88 cs_endpoint_free(appctx->endp);
89 fail_endp:
90 pool_free(pool_head_appctx, appctx);
91 fail_appctx:
92 return NULL;
Christopher Fauletcb2fa362022-03-23 11:46:56 +010093}
94
Willy Tarreau21028b52018-11-06 17:32:37 +010095/* Callback used to wake up an applet when a buffer is available. The applet
96 * <appctx> is woken up if an input buffer was requested for the associated
97 * stream interface. In this case the buffer is immediately allocated and the
98 * function returns 1. Otherwise it returns 0. Note that this automatically
99 * covers multiple wake-up attempts by ensuring that the same buffer will not
100 * be accounted for multiple times.
101 */
102int appctx_buf_available(void *arg)
103{
104 struct appctx *appctx = arg;
Christopher Faulet908628c2022-03-25 16:43:49 +0100105 struct conn_stream *cs = appctx->owner;
Willy Tarreau21028b52018-11-06 17:32:37 +0100106
107 /* allocation requested ? */
Christopher Faulet908628c2022-03-25 16:43:49 +0100108 if (!(cs->si->flags & SI_FL_RXBLK_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +0100109 return 0;
110
Christopher Faulet908628c2022-03-25 16:43:49 +0100111 si_rx_buff_rdy(cs->si);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100112
113 /* was already allocated another way ? if so, don't take this one */
Christopher Faulet908628c2022-03-25 16:43:49 +0100114 if (c_size(cs_ic(cs)) || cs_ic(cs)->pipe)
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100115 return 0;
116
Willy Tarreau21028b52018-11-06 17:32:37 +0100117 /* allocation possible now ? */
Christopher Faulet908628c2022-03-25 16:43:49 +0100118 if (!b_alloc(&cs_ic(cs)->buf)) {
119 si_rx_buff_blk(cs->si);
Willy Tarreau21028b52018-11-06 17:32:37 +0100120 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100121 }
Willy Tarreau21028b52018-11-06 17:32:37 +0100122
Willy Tarreau21028b52018-11-06 17:32:37 +0100123 task_wakeup(appctx->t, TASK_WOKEN_RES);
124 return 1;
125}
126
127/* Default applet handler */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100128struct task *task_run_applet(struct task *t, void *context, unsigned int state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200129{
Olivier Houchard673867c2018-05-25 16:58:52 +0200130 struct appctx *app = context;
Christopher Faulet908628c2022-03-25 16:43:49 +0100131 struct conn_stream *cs = app->owner;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +0200132 unsigned int rate;
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200133 size_t count;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +0100134
Olivier Houchard673867c2018-05-25 16:58:52 +0200135 if (app->state & APPLET_WANT_DIE) {
136 __appctx_free(app);
137 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +0100138 }
Emeric Brun1138fd02017-06-19 12:38:55 +0200139
Olivier Houchard673867c2018-05-25 16:58:52 +0200140 /* We always pretend the applet can't get and doesn't want to
141 * put, it's up to it to change this if needed. This ensures
142 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +0200143 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100144 si_cant_get(cs->si);
145 si_rx_endp_done(cs->si);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200146
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100147 /* Now we'll try to allocate the input buffer. We wake up the applet in
148 * all cases. So this is the applet's responsibility to check if this
149 * buffer was allocated or not. This leaves a chance for applets to do
150 * some other processing if needed. The applet doesn't have anything to
151 * do if it needs the buffer, it will be called again upon readiness.
152 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100153 if (!si_alloc_ibuf(cs->si, &app->buffer_wait))
154 si_rx_endp_more(cs->si);
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100155
Christopher Faulet908628c2022-03-25 16:43:49 +0100156 count = co_data(cs_oc(cs));
Olivier Houchard673867c2018-05-25 16:58:52 +0200157 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +0200158
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200159 /* now check if the applet has released some room and forgot to
160 * notify the other side about it.
161 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100162 if (count != co_data(cs_oc(cs))) {
163 cs_oc(cs)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
164 si_rx_room_rdy(cs_opposite(cs)->si);
Christopher Faulet1eedf9b2021-04-27 17:08:10 +0200165 }
166
Willy Tarreau19920d62019-10-11 14:15:47 +0200167 /* measure the call rate and check for anomalies when too high */
168 rate = update_freq_ctr(&app->call_rate, 1);
169 if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second
Christopher Faulet908628c2022-03-25 16:43:49 +0100170 ((b_size(cs_ib(cs)) && cs->si->flags & SI_FL_RXBLK_BUFF) || // asks for a buffer which is present
171 (b_size(cs_ib(cs)) && !b_data(cs_ib(cs)) && cs->si->flags & SI_FL_RXBLK_ROOM) || // asks for room in an empty buffer
172 (b_data(cs_ob(cs)) && si_tx_endp_ready(cs->si) && !si_tx_blocked(cs->si)) || // asks for data already present
173 (!b_data(cs_ib(cs)) && b_data(cs_ob(cs)) && // didn't return anything ...
174 (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 +0200175 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
176 }
177
Christopher Faulet908628c2022-03-25 16:43:49 +0100178 si_applet_wake_cb(cs->si);
179 channel_release_buffer(cs_ic(cs), &app->buffer_wait);
Olivier Houchard673867c2018-05-25 16:58:52 +0200180 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200181}