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; |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 38 | struct stream_interface *si = cs_si(appctx->owner); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 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 | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 51 | if (!b_alloc(&si_ic(si)->buf)) { |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 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; |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 64 | struct stream_interface *si; |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 65 | unsigned int rate; |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 66 | size_t count; |
Christopher Faulet | b4a4d9a | 2017-11-15 22:14:49 +0100 | [diff] [blame] | 67 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 68 | if (app->state & APPLET_WANT_DIE) { |
| 69 | __appctx_free(app); |
| 70 | return NULL; |
Christopher Faulet | 7163056 | 2017-11-14 11:30:47 +0100 | [diff] [blame] | 71 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 72 | |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 73 | si = cs_si(app->owner); |
| 74 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 75 | /* 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 Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 78 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 79 | si_cant_get(si); |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 80 | si_rx_endp_done(si); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 82 | /* 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 Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 89 | si_rx_endp_more(si); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 90 | |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 91 | count = co_data(si_oc(si)); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 92 | app->applet->fct(app); |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 93 | |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 94 | /* 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 Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 102 | /* 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 Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 113 | si_applet_wake_cb(si); |
| 114 | channel_release_buffer(si_ic(si), &app->buffer_wait); |
| 115 | return t; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 116 | } |