Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/applet.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 18 | #include <haproxy/channel.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 19 | #include <haproxy/list.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 20 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 21 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 22 | #include <haproxy/task.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 23 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 24 | unsigned int nb_applets = 0; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 26 | DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx)); |
| 27 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 28 | /* 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 | */ |
| 35 | int appctx_buf_available(void *arg) |
| 36 | { |
| 37 | struct appctx *appctx = arg; |
| 38 | struct stream_interface *si = appctx->owner; |
| 39 | |
| 40 | /* allocation requested ? */ |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 41 | if (!(si->flags & SI_FL_RXBLK_BUFF)) |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 42 | return 0; |
| 43 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 44 | 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 Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 50 | /* allocation possible now ? */ |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 51 | if (!b_alloc_margin(&si_ic(si)->buf, global.tune.reserved_bufs)) { |
| 52 | si_rx_buff_blk(si); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 53 | return 0; |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 54 | } |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 56 | task_wakeup(appctx->t, TASK_WOKEN_RES); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | /* Default applet handler */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 61 | struct task *task_run_applet(struct task *t, void *context, unsigned int state) |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 62 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 63 | struct appctx *app = context; |
| 64 | struct stream_interface *si = app->owner; |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 65 | unsigned int rate; |
Christopher Faulet | b4a4d9a | 2017-11-15 22:14:49 +0100 | [diff] [blame] | 66 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 67 | if (app->state & APPLET_WANT_DIE) { |
| 68 | __appctx_free(app); |
| 69 | return NULL; |
Christopher Faulet | 7163056 | 2017-11-14 11:30:47 +0100 | [diff] [blame] | 70 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 71 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 72 | /* 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 Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 75 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 76 | si_cant_get(si); |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 77 | si_rx_endp_done(si); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 79 | /* 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 Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 86 | si_rx_endp_more(si); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 87 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 88 | app->applet->fct(app); |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 89 | |
| 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 Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 101 | si_applet_wake_cb(si); |
| 102 | channel_release_buffer(si_ic(si), &app->buffer_wait); |
| 103 | return t; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 104 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 105 | |