blob: eafce3d1f09f7fa76ac7bc5888922b44981a076b [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;
38 struct stream_interface *si = appctx->owner;
39
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 Tarreau8be7cd72018-11-14 15:12:08 +010051 if (!b_alloc_margin(&si_ic(si)->buf, global.tune.reserved_bufs)) {
52 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;
64 struct stream_interface *si = app->owner;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +020065 unsigned int rate;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +010066
Olivier Houchard673867c2018-05-25 16:58:52 +020067 if (app->state & APPLET_WANT_DIE) {
68 __appctx_free(app);
69 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +010070 }
Emeric Brun1138fd02017-06-19 12:38:55 +020071
Olivier Houchard673867c2018-05-25 16:58:52 +020072 /* We always pretend the applet can't get and doesn't want to
73 * put, it's up to it to change this if needed. This ensures
74 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +020075 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +010076 si_cant_get(si);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +010077 si_rx_endp_done(si);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020078
Willy Tarreau8be7cd72018-11-14 15:12:08 +010079 /* Now we'll try to allocate the input buffer. We wake up the applet in
80 * all cases. So this is the applet's responsibility to check if this
81 * buffer was allocated or not. This leaves a chance for applets to do
82 * some other processing if needed. The applet doesn't have anything to
83 * do if it needs the buffer, it will be called again upon readiness.
84 */
85 if (!si_alloc_ibuf(si, &app->buffer_wait))
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +010086 si_rx_endp_more(si);
Willy Tarreau8be7cd72018-11-14 15:12:08 +010087
Olivier Houchard673867c2018-05-25 16:58:52 +020088 app->applet->fct(app);
Willy Tarreau19920d62019-10-11 14:15:47 +020089
90 /* measure the call rate and check for anomalies when too high */
91 rate = update_freq_ctr(&app->call_rate, 1);
92 if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second
93 ((b_size(si_ib(si)) && si->flags & SI_FL_RXBLK_BUFF) || // asks for a buffer which is present
94 (b_size(si_ib(si)) && !b_data(si_ib(si)) && si->flags & SI_FL_RXBLK_ROOM) || // asks for room in an empty buffer
95 (b_data(si_ob(si)) && si_tx_endp_ready(si) && !si_tx_blocked(si)) || // asks for data already present
96 (!b_data(si_ib(si)) && b_data(si_ob(si)) && // didn't return anything ...
97 (si_oc(si)->flags & (CF_WRITE_PARTIAL|CF_SHUTW_NOW)) == CF_SHUTW_NOW))) { // ... and left data pending after a shut
98 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
99 }
100
Olivier Houchard673867c2018-05-25 16:58:52 +0200101 si_applet_wake_cb(si);
102 channel_release_buffer(si_ic(si), &app->buffer_wait);
103 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200104}
Emeric Brun1138fd02017-06-19 12:38:55 +0200105