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> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 19 | #include <haproxy/conn_stream.h> |
| 20 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 21 | #include <haproxy/list.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 22 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 23 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 24 | #include <haproxy/task.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 25 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 26 | unsigned int nb_applets = 0; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 28 | DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx)); |
| 29 | |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 30 | /* Initializes all required fields for a new appctx. Note that it does the |
| 31 | * minimum acceptable initialization for an appctx. This means only the |
| 32 | * 3 integer states st0, st1, st2 and the chunk used to gather unfinished |
| 33 | * commands are zeroed |
| 34 | */ |
| 35 | static inline void appctx_init(struct appctx *appctx) |
| 36 | { |
| 37 | appctx->st0 = appctx->st1 = appctx->st2 = 0; |
| 38 | appctx->chunk = NULL; |
| 39 | appctx->io_release = NULL; |
| 40 | appctx->call_rate.curr_tick = 0; |
| 41 | appctx->call_rate.curr_ctr = 0; |
| 42 | appctx->call_rate.prev_ctr = 0; |
| 43 | appctx->state = 0; |
| 44 | LIST_INIT(&appctx->wait_entry); |
| 45 | } |
| 46 | |
| 47 | /* Tries to allocate a new appctx and initialize its main fields. The appctx |
| 48 | * is returned on success, NULL on failure. The appctx must be released using |
| 49 | * appctx_free(). <applet> is assigned as the applet, but it can be NULL. The |
| 50 | * applet's task is always created on the current thread. |
| 51 | */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 52 | struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp) |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 53 | { |
| 54 | struct appctx *appctx; |
| 55 | |
| 56 | appctx = pool_alloc(pool_head_appctx); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 57 | if (unlikely(!appctx)) |
| 58 | goto fail_appctx; |
| 59 | |
| 60 | appctx_init(appctx); |
| 61 | appctx->obj_type = OBJ_TYPE_APPCTX; |
| 62 | appctx->applet = applet; |
| 63 | |
| 64 | if (!endp) { |
| 65 | endp = cs_endpoint_new(); |
| 66 | if (!endp) |
| 67 | goto fail_endp; |
| 68 | endp->target = appctx; |
| 69 | endp->ctx = appctx; |
| 70 | endp->flags |= (CS_EP_T_APPLET|CS_EP_ORPHAN); |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 71 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 72 | appctx->endp = endp; |
| 73 | |
| 74 | appctx->t = task_new_here(); |
| 75 | if (unlikely(!appctx->t)) |
| 76 | goto fail_task; |
| 77 | appctx->t->process = task_run_applet; |
| 78 | appctx->t->context = appctx; |
| 79 | |
| 80 | LIST_INIT(&appctx->buffer_wait.list); |
| 81 | appctx->buffer_wait.target = appctx; |
| 82 | appctx->buffer_wait.wakeup_cb = appctx_buf_available; |
| 83 | |
| 84 | _HA_ATOMIC_INC(&nb_applets); |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 85 | return appctx; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 86 | |
| 87 | fail_task: |
| 88 | cs_endpoint_free(appctx->endp); |
| 89 | fail_endp: |
| 90 | pool_free(pool_head_appctx, appctx); |
| 91 | fail_appctx: |
| 92 | return NULL; |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 95 | /* Callback used to wake up an applet when a buffer is available. The applet |
| 96 | * <appctx> is woken up if an input buffer was requested for the associated |
| 97 | * stream interface. In this case the buffer is immediately allocated and the |
| 98 | * function returns 1. Otherwise it returns 0. Note that this automatically |
| 99 | * covers multiple wake-up attempts by ensuring that the same buffer will not |
| 100 | * be accounted for multiple times. |
| 101 | */ |
| 102 | int appctx_buf_available(void *arg) |
| 103 | { |
| 104 | struct appctx *appctx = arg; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 105 | struct conn_stream *cs = appctx->owner; |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 106 | |
| 107 | /* allocation requested ? */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 108 | if (!(cs->si->flags & SI_FL_RXBLK_BUFF)) |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 109 | return 0; |
| 110 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 111 | si_rx_buff_rdy(cs->si); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 112 | |
| 113 | /* was already allocated another way ? if so, don't take this one */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 114 | if (c_size(cs_ic(cs)) || cs_ic(cs)->pipe) |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 115 | return 0; |
| 116 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 117 | /* allocation possible now ? */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 118 | if (!b_alloc(&cs_ic(cs)->buf)) { |
| 119 | si_rx_buff_blk(cs->si); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 120 | return 0; |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 121 | } |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 122 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 123 | task_wakeup(appctx->t, TASK_WOKEN_RES); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | /* Default applet handler */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 128 | 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] | 129 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 130 | struct appctx *app = context; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 131 | struct conn_stream *cs = app->owner; |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 132 | unsigned int rate; |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 133 | size_t count; |
Christopher Faulet | b4a4d9a | 2017-11-15 22:14:49 +0100 | [diff] [blame] | 134 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 135 | if (app->state & APPLET_WANT_DIE) { |
| 136 | __appctx_free(app); |
| 137 | return NULL; |
Christopher Faulet | 7163056 | 2017-11-14 11:30:47 +0100 | [diff] [blame] | 138 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 139 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 140 | /* We always pretend the applet can't get and doesn't want to |
| 141 | * put, it's up to it to change this if needed. This ensures |
| 142 | * that one applet which ignores any event will not spin. |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 143 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 144 | si_cant_get(cs->si); |
| 145 | si_rx_endp_done(cs->si); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 146 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 147 | /* Now we'll try to allocate the input buffer. We wake up the applet in |
| 148 | * all cases. So this is the applet's responsibility to check if this |
| 149 | * buffer was allocated or not. This leaves a chance for applets to do |
| 150 | * some other processing if needed. The applet doesn't have anything to |
| 151 | * do if it needs the buffer, it will be called again upon readiness. |
| 152 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 153 | if (!si_alloc_ibuf(cs->si, &app->buffer_wait)) |
| 154 | si_rx_endp_more(cs->si); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 155 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 156 | count = co_data(cs_oc(cs)); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 157 | app->applet->fct(app); |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 158 | |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 159 | /* now check if the applet has released some room and forgot to |
| 160 | * notify the other side about it. |
| 161 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 162 | if (count != co_data(cs_oc(cs))) { |
| 163 | cs_oc(cs)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA; |
| 164 | si_rx_room_rdy(cs_opposite(cs)->si); |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 167 | /* measure the call rate and check for anomalies when too high */ |
| 168 | rate = update_freq_ctr(&app->call_rate, 1); |
| 169 | if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 170 | ((b_size(cs_ib(cs)) && cs->si->flags & SI_FL_RXBLK_BUFF) || // asks for a buffer which is present |
| 171 | (b_size(cs_ib(cs)) && !b_data(cs_ib(cs)) && cs->si->flags & SI_FL_RXBLK_ROOM) || // asks for room in an empty buffer |
| 172 | (b_data(cs_ob(cs)) && si_tx_endp_ready(cs->si) && !si_tx_blocked(cs->si)) || // asks for data already present |
| 173 | (!b_data(cs_ib(cs)) && b_data(cs_ob(cs)) && // didn't return anything ... |
| 174 | (cs_oc(cs)->flags & (CF_WRITE_PARTIAL|CF_SHUTW_NOW)) == CF_SHUTW_NOW))) { // ... and left data pending after a shut |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 175 | stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate)); |
| 176 | } |
| 177 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 178 | si_applet_wake_cb(cs->si); |
| 179 | channel_release_buffer(cs_ic(cs), &app->buffer_wait); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 180 | return t; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 181 | } |