blob: 4ff389391fb10b96ece8b77922c0ca793c0781dd [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
16#include <common/config.h>
17#include <common/mini-clist.h>
18#include <proto/applet.h>
Christopher Fauleta73e59b2016-12-09 17:30:18 +010019#include <proto/channel.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020020#include <proto/stream.h>
21#include <proto/stream_interface.h>
Olivier Houchard673867c2018-05-25 16:58:52 +020022#include <proto/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 Tarreau21028b52018-11-06 17:32:37 +010026/* Callback used to wake up an applet when a buffer is available. The applet
27 * <appctx> is woken up if an input buffer was requested for the associated
28 * stream interface. In this case the buffer is immediately allocated and the
29 * function returns 1. Otherwise it returns 0. Note that this automatically
30 * covers multiple wake-up attempts by ensuring that the same buffer will not
31 * be accounted for multiple times.
32 */
33int appctx_buf_available(void *arg)
34{
35 struct appctx *appctx = arg;
36 struct stream_interface *si = appctx->owner;
37
38 /* allocation requested ? */
Willy Tarreau8be7cd72018-11-14 15:12:08 +010039 if (!(si->flags & SI_FL_RXBLK_BUFF))
Willy Tarreau21028b52018-11-06 17:32:37 +010040 return 0;
41
Willy Tarreau8be7cd72018-11-14 15:12:08 +010042 si_rx_buff_rdy(si);
43
44 /* was already allocated another way ? if so, don't take this one */
45 if (c_size(si_ic(si)) || si_ic(si)->pipe)
46 return 0;
47
Willy Tarreau21028b52018-11-06 17:32:37 +010048 /* allocation possible now ? */
Willy Tarreau8be7cd72018-11-14 15:12:08 +010049 if (!b_alloc_margin(&si_ic(si)->buf, global.tune.reserved_bufs)) {
50 si_rx_buff_blk(si);
Willy Tarreau21028b52018-11-06 17:32:37 +010051 return 0;
Willy Tarreau8be7cd72018-11-14 15:12:08 +010052 }
Willy Tarreau21028b52018-11-06 17:32:37 +010053
Willy Tarreau21028b52018-11-06 17:32:37 +010054 task_wakeup(appctx->t, TASK_WOKEN_RES);
55 return 1;
56}
57
58/* Default applet handler */
Olivier Houchard673867c2018-05-25 16:58:52 +020059struct task *task_run_applet(struct task *t, void *context, unsigned short state)
Willy Tarreau3c595ac2015-04-19 09:59:31 +020060{
Olivier Houchard673867c2018-05-25 16:58:52 +020061 struct appctx *app = context;
62 struct stream_interface *si = app->owner;
Willy Tarreaudcb0e1d2019-04-25 19:12:26 +020063 unsigned int rate;
Christopher Faulet75315682021-04-27 17:08:10 +020064 size_t count;
Christopher Fauletb4a4d9a2017-11-15 22:14:49 +010065
Olivier Houchard673867c2018-05-25 16:58:52 +020066 if (app->state & APPLET_WANT_DIE) {
67 __appctx_free(app);
68 return NULL;
Christopher Faulet71630562017-11-14 11:30:47 +010069 }
Emeric Brun1138fd02017-06-19 12:38:55 +020070
Olivier Houchard673867c2018-05-25 16:58:52 +020071 /* We always pretend the applet can't get and doesn't want to
72 * put, it's up to it to change this if needed. This ensures
73 * that one applet which ignores any event will not spin.
Willy Tarreau99942382015-09-25 17:56:16 +020074 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +010075 si_cant_get(si);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +010076 si_rx_endp_done(si);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020077
Willy Tarreau8be7cd72018-11-14 15:12:08 +010078 /* Now we'll try to allocate the input buffer. We wake up the applet in
79 * all cases. So this is the applet's responsibility to check if this
80 * buffer was allocated or not. This leaves a chance for applets to do
81 * some other processing if needed. The applet doesn't have anything to
82 * do if it needs the buffer, it will be called again upon readiness.
83 */
84 if (!si_alloc_ibuf(si, &app->buffer_wait))
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +010085 si_rx_endp_more(si);
Willy Tarreau8be7cd72018-11-14 15:12:08 +010086
Christopher Faulet75315682021-04-27 17:08:10 +020087 count = co_data(si_oc(si));
Olivier Houchard673867c2018-05-25 16:58:52 +020088 app->applet->fct(app);
Willy Tarreau1cf90c02019-10-11 14:15:47 +020089
Christopher Faulet75315682021-04-27 17:08:10 +020090 /* now check if the applet has released some room and forgot to
91 * notify the other side about it.
92 */
93 if (count != co_data(si_oc(si))) {
94 si_oc(si)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
95 si_rx_room_rdy(si_opposite(si));
96 }
97
Willy Tarreau1cf90c02019-10-11 14:15:47 +020098 /* measure the call rate and check for anomalies when too high */
99 rate = update_freq_ctr(&app->call_rate, 1);
100 if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second
101 ((b_size(si_ib(si)) && si->flags & SI_FL_RXBLK_BUFF) || // asks for a buffer which is present
102 (b_size(si_ib(si)) && !b_data(si_ib(si)) && si->flags & SI_FL_RXBLK_ROOM) || // asks for room in an empty buffer
103 (b_data(si_ob(si)) && si_tx_endp_ready(si) && !si_tx_blocked(si)) || // asks for data already present
104 (!b_data(si_ib(si)) && b_data(si_ob(si)) && // didn't return anything ...
105 (si_oc(si)->flags & (CF_WRITE_PARTIAL|CF_SHUTW_NOW)) == CF_SHUTW_NOW))) { // ... and left data pending after a shut
106 stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate));
107 }
108
Olivier Houchard673867c2018-05-25 16:58:52 +0200109 si_applet_wake_cb(si);
110 channel_release_buffer(si_ic(si), &app->buffer_wait);
111 return t;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200112}
Emeric Brun1138fd02017-06-19 12:38:55 +0200113