blob: ff7381da2d3973aef19703aa6885d520df4ffd5c [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 Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020021#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020022#include <haproxy/task.h>
Willy Tarreau81f38d62015-04-13 17:11:11 +020023
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010024unsigned int nb_applets = 0;
Emeric Brun1138fd02017-06-19 12:38:55 +020025
Willy Tarreau8280ea92019-07-18 10:41:36 +020026DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx));
27
Willy Tarreau21028b52018-11-06 17:32:37 +010028/* Callback used to wake up an applet when a buffer is available. The applet
29 * <appctx> is woken up if an input buffer was requested for the associated
30 * stream interface. In this case the buffer is immediately allocated and the
31 * function returns 1. Otherwise it returns 0. Note that this automatically
32 * covers multiple wake-up attempts by ensuring that the same buffer will not
33 * be accounted for multiple times.
34 */
35int appctx_buf_available(void *arg)
36{
37 struct appctx *appctx = arg;
Christopher Faulet86e1c332021-12-20 17:09:39 +010038 struct stream_interface *si = cs_si(appctx->owner);
Willy Tarreau21028b52018-11-06 17:32:37 +010039
40 /* allocation requested ? */
Willy Tarreau8be7cd72018-11-14 15:12:08 +010041 if (!(si->flags & SI_FL_RXBLK_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +010042 return 0;
43
Willy Tarreau8be7cd72018-11-14 15:12:08 +010044 si_rx_buff_rdy(si);
45
46 /* was already allocated another way ? if so, don't take this one */
47 if (c_size(si_ic(si)) || si_ic(si)->pipe)
48 return 0;
49
Willy Tarreau21028b52018-11-06 17:32:37 +010050 /* allocation possible now ? */
Willy Tarreaud68d4f12021-03-22 14:44:31 +010051 if (!b_alloc(&si_ic(si)->buf)) {
Willy Tarreau8be7cd72018-11-14 15:12:08 +010052 si_rx_buff_blk(si);
Willy Tarreau21028b52018-11-06 17:32:37 +010053 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +010054 }
Willy Tarreau21028b52018-11-06 17:32:37 +010055
Willy Tarreau21028b52018-11-06 17:32:37 +010056 task_wakeup(appctx->t, TASK_WOKEN_RES);
57 return 1;
58}
59
60/* Default applet handler */
Willy Tarreau144f84a2021-03-02 16:09:26 +010061struct task *task_run_applet(struct task *t, void *context, unsigned int state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +020062{
Olivier Houchard673867c2018-05-25 16:58:52 +020063 struct appctx *app = context;
Christopher Faulet86e1c332021-12-20 17:09:39 +010064 struct stream_interface *si;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +020065 unsigned int rate;
Christopher Faulet1eedf9b2021-04-27 17:08:10 +020066 size_t count;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +010067
Olivier Houchard673867c2018-05-25 16:58:52 +020068 if (app->state & APPLET_WANT_DIE) {
69 __appctx_free(app);
70 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +010071 }
Emeric Brun1138fd02017-06-19 12:38:55 +020072
Christopher Faulet86e1c332021-12-20 17:09:39 +010073 si = cs_si(app->owner);
74
Olivier Houchard673867c2018-05-25 16:58:52 +020075 /* We always pretend the applet can't get and doesn't want to
76 * put, it's up to it to change this if needed. This ensures
77 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +020078 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +010079 si_cant_get(si);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +010080 si_rx_endp_done(si);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020081
Willy Tarreau8be7cd72018-11-14 15:12:08 +010082 /* Now we'll try to allocate the input buffer. We wake up the applet in
83 * all cases. So this is the applet's responsibility to check if this
84 * buffer was allocated or not. This leaves a chance for applets to do
85 * some other processing if needed. The applet doesn't have anything to
86 * do if it needs the buffer, it will be called again upon readiness.
87 */
88 if (!si_alloc_ibuf(si, &app->buffer_wait))
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +010089 si_rx_endp_more(si);
Willy Tarreau8be7cd72018-11-14 15:12:08 +010090
Christopher Faulet1eedf9b2021-04-27 17:08:10 +020091 count = co_data(si_oc(si));
Olivier Houchard673867c2018-05-25 16:58:52 +020092 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +020093
Christopher Faulet1eedf9b2021-04-27 17:08:10 +020094 /* now check if the applet has released some room and forgot to
95 * notify the other side about it.
96 */
97 if (count != co_data(si_oc(si))) {
98 si_oc(si)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
99 si_rx_room_rdy(si_opposite(si));
100 }
101
Willy Tarreau19920d62019-10-11 14:15:47 +0200102 /* measure the call rate and check for anomalies when too high */
103 rate = update_freq_ctr(&app->call_rate, 1);
104 if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second
105 ((b_size(si_ib(si)) && si->flags & SI_FL_RXBLK_BUFF) || // asks for a buffer which is present
106 (b_size(si_ib(si)) && !b_data(si_ib(si)) && si->flags & SI_FL_RXBLK_ROOM) || // asks for room in an empty buffer
107 (b_data(si_ob(si)) && si_tx_endp_ready(si) && !si_tx_blocked(si)) || // asks for data already present
108 (!b_data(si_ib(si)) && b_data(si_ob(si)) && // didn't return anything ...
109 (si_oc(si)->flags & (CF_WRITE_PARTIAL|CF_SHUTW_NOW)) == CF_SHUTW_NOW))) { // ... and left data pending after a shut
110 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
111 }
112
Olivier Houchard673867c2018-05-25 16:58:52 +0200113 si_applet_wake_cb(si);
114 channel_release_buffer(si_ic(si), &app->buffer_wait);
115 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200116}