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 | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 23 | #include <haproxy/task.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 24 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 25 | unsigned int nb_applets = 0; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 27 | DECLARE_POOL(pool_head_appctx, "appctx", sizeof(struct appctx)); |
| 28 | |
Willy Tarreau | 009e42b | 2022-05-05 19:55:03 +0200 | [diff] [blame] | 29 | /* Tries to allocate a new appctx and initialize all of its fields. The appctx |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 30 | * is returned on success, NULL on failure. The appctx must be released using |
| 31 | * appctx_free(). <applet> is assigned as the applet, but it can be NULL. The |
| 32 | * applet's task is always created on the current thread. |
| 33 | */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 34 | struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp, unsigned long thread_mask) |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 35 | { |
| 36 | struct appctx *appctx; |
| 37 | |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 38 | /* Backend appctx cannot be started on another thread than the local one */ |
| 39 | BUG_ON(thread_mask != tid_bit && endp); |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 009e42b | 2022-05-05 19:55:03 +0200 | [diff] [blame] | 41 | appctx = pool_zalloc(pool_head_appctx); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 42 | if (unlikely(!appctx)) |
| 43 | goto fail_appctx; |
| 44 | |
Willy Tarreau | 009e42b | 2022-05-05 19:55:03 +0200 | [diff] [blame] | 45 | LIST_INIT(&appctx->wait_entry); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 46 | appctx->obj_type = OBJ_TYPE_APPCTX; |
| 47 | appctx->applet = applet; |
Christopher Faulet | ac57bb5 | 2022-05-09 08:08:26 +0200 | [diff] [blame] | 48 | appctx->sess = NULL; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 49 | if (!endp) { |
| 50 | endp = cs_endpoint_new(); |
| 51 | if (!endp) |
| 52 | goto fail_endp; |
| 53 | endp->target = appctx; |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 54 | se_fl_set(endp, CS_EP_T_APPLET | CS_EP_ORPHAN); |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 55 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 56 | appctx->endp = endp; |
| 57 | |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 58 | appctx->t = task_new(thread_mask); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 59 | if (unlikely(!appctx->t)) |
| 60 | goto fail_task; |
| 61 | appctx->t->process = task_run_applet; |
| 62 | appctx->t->context = appctx; |
| 63 | |
| 64 | LIST_INIT(&appctx->buffer_wait.list); |
| 65 | appctx->buffer_wait.target = appctx; |
| 66 | appctx->buffer_wait.wakeup_cb = appctx_buf_available; |
| 67 | |
| 68 | _HA_ATOMIC_INC(&nb_applets); |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 69 | return appctx; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 70 | |
| 71 | fail_task: |
| 72 | cs_endpoint_free(appctx->endp); |
| 73 | fail_endp: |
| 74 | pool_free(pool_head_appctx, appctx); |
| 75 | fail_appctx: |
| 76 | return NULL; |
Christopher Faulet | cb2fa36 | 2022-03-23 11:46:56 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 79 | /* Finalize the frontend appctx startup. It must not be called for a backend |
| 80 | * appctx. This function is responsible to create the appctx's session and the |
| 81 | * frontend conn-stream. By transitivity, the stream is also created. |
| 82 | * |
| 83 | * It returns 0 on success and -1 on error. In this case, it is the caller |
| 84 | * responsibility to release the appctx. However, the session is released if it |
| 85 | * was created. On success, if an error is encountered in the caller function, |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 86 | * the stream must be released instead of the appctx. To be sure, |
| 87 | * appctx_free_on_early_error() must be called in this case. |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 88 | */ |
| 89 | int appctx_finalize_startup(struct appctx *appctx, struct proxy *px, struct buffer *input) |
| 90 | { |
| 91 | struct session *sess; |
| 92 | |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 93 | /* async startup is only possible for frontend appctx. Thus for orphan |
| 94 | * appctx. Because no backend appctx can be orphan. |
| 95 | */ |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 96 | BUG_ON(!se_fl_test(appctx->endp, CS_EP_ORPHAN)); |
Christopher Faulet | 8718c95 | 2022-05-12 15:15:53 +0200 | [diff] [blame] | 97 | |
| 98 | sess = session_new(px, NULL, &appctx->obj_type); |
| 99 | if (!sess) |
| 100 | return -1; |
| 101 | if (!cs_new_from_endp(appctx->endp, sess, input)) { |
| 102 | session_free(sess); |
| 103 | return -1; |
| 104 | } |
| 105 | appctx->sess = sess; |
| 106 | return 0; |
| 107 | } |
| 108 | |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 109 | /* Release function to call when an error occurred during init stage of a |
| 110 | * frontend appctx. For a backend appctx, it just calls appctx_free() |
| 111 | */ |
| 112 | void appctx_free_on_early_error(struct appctx *appctx) |
| 113 | { |
| 114 | /* If a frontend apctx is attached to a conn-stream, release the stream |
| 115 | * instead of the appctx. |
| 116 | */ |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 117 | if (!se_fl_test(appctx->endp, CS_EP_ORPHAN) && !(appctx_cs(appctx)->flags & CS_FL_ISBACK)) { |
Christopher Faulet | d0c4ec0 | 2022-05-12 15:18:48 +0200 | [diff] [blame] | 118 | stream_free(appctx_strm(appctx)); |
| 119 | return; |
| 120 | } |
| 121 | appctx_free(appctx); |
| 122 | } |
| 123 | |
Willy Tarreau | f12f32a | 2022-05-02 14:57:03 +0200 | [diff] [blame] | 124 | /* reserves a command context of at least <size> bytes in the <appctx>, for |
| 125 | * use by a CLI command or any regular applet. The pointer to this context is |
| 126 | * stored in ctx.svcctx and is returned. The caller doesn't need to release |
| 127 | * it as it's allocated from reserved space. If the size is larger than |
| 128 | * APPLET_MAX_SVCCTX a crash will occur (hence that will never happen outside |
| 129 | * of development). |
| 130 | * |
| 131 | * Note that the command does *not* initialize the area, so that it can easily |
| 132 | * be used upon each entry in a function. It's left to the initialization code |
| 133 | * to do it if needed. The CLI will always zero the whole area before calling |
| 134 | * a keyword's ->parse() function. |
| 135 | */ |
| 136 | void *applet_reserve_svcctx(struct appctx *appctx, size_t size) |
| 137 | { |
| 138 | BUG_ON(size > APPLET_MAX_SVCCTX); |
| 139 | appctx->svcctx = &appctx->svc.storage; |
| 140 | return appctx->svcctx; |
| 141 | } |
| 142 | |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 143 | /* call the applet's release() function if any, and marks the endp as shut. |
| 144 | * Needs to be called upon close(). |
| 145 | */ |
| 146 | void appctx_shut(struct appctx *appctx) |
| 147 | { |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 148 | if (se_fl_test(appctx->endp, CS_EP_SHR | CS_EP_SHW)) |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 149 | return; |
| 150 | |
| 151 | if (appctx->applet->release) |
| 152 | appctx->applet->release(appctx); |
| 153 | |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 154 | se_fl_set(appctx->endp, CS_EP_SHRR | CS_EP_SHWN); |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 157 | /* Callback used to wake up an applet when a buffer is available. The applet |
| 158 | * <appctx> is woken up if an input buffer was requested for the associated |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 159 | * conn-stream. In this case the buffer is immediately allocated and the |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 160 | * function returns 1. Otherwise it returns 0. Note that this automatically |
| 161 | * covers multiple wake-up attempts by ensuring that the same buffer will not |
| 162 | * be accounted for multiple times. |
| 163 | */ |
| 164 | int appctx_buf_available(void *arg) |
| 165 | { |
| 166 | struct appctx *appctx = arg; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 167 | struct conn_stream *cs = appctx_cs(appctx); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 168 | |
| 169 | /* allocation requested ? */ |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 170 | if (!se_fl_test(appctx->endp, CS_EP_RXBLK_BUFF)) |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 173 | cs_rx_buff_rdy(cs); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 174 | |
| 175 | /* was already allocated another way ? if so, don't take this one */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 176 | if (c_size(cs_ic(cs)) || cs_ic(cs)->pipe) |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 179 | /* allocation possible now ? */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 180 | if (!b_alloc(&cs_ic(cs)->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 181 | cs_rx_buff_blk(cs); |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 182 | return 0; |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 183 | } |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 184 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 185 | task_wakeup(appctx->t, TASK_WOKEN_RES); |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | /* Default applet handler */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 190 | 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] | 191 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 192 | struct appctx *app = context; |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 193 | struct conn_stream *cs; |
Willy Tarreau | dcb0e1d | 2019-04-25 19:12:26 +0200 | [diff] [blame] | 194 | unsigned int rate; |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 195 | size_t count; |
Christopher Faulet | b4a4d9a | 2017-11-15 22:14:49 +0100 | [diff] [blame] | 196 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 197 | if (app->state & APPLET_WANT_DIE) { |
| 198 | __appctx_free(app); |
| 199 | return NULL; |
Christopher Faulet | 7163056 | 2017-11-14 11:30:47 +0100 | [diff] [blame] | 200 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 202 | if (se_fl_test(app->endp, CS_EP_ORPHAN)) { |
Christopher Faulet | d9c1d33 | 2022-05-16 17:15:31 +0200 | [diff] [blame] | 203 | /* Finalize init of orphan appctx. .init callback function must |
| 204 | * be defined and it must finalize appctx startup. |
| 205 | */ |
| 206 | BUG_ON(!app->applet->init); |
| 207 | |
| 208 | if (appctx_init(app) == -1) { |
| 209 | appctx_free_on_early_error(app); |
| 210 | return NULL; |
| 211 | } |
| 212 | BUG_ON(!app->sess || !appctx_cs(app) || !appctx_strm(app)); |
| 213 | } |
| 214 | |
| 215 | cs = appctx_cs(app); |
| 216 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 217 | /* We always pretend the applet can't get and doesn't want to |
| 218 | * put, it's up to it to change this if needed. This ensures |
| 219 | * that one applet which ignores any event will not spin. |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 220 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 221 | cs_cant_get(cs); |
| 222 | cs_rx_endp_done(cs); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 223 | |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 224 | /* Now we'll try to allocate the input buffer. We wake up the applet in |
| 225 | * all cases. So this is the applet's responsibility to check if this |
| 226 | * buffer was allocated or not. This leaves a chance for applets to do |
| 227 | * some other processing if needed. The applet doesn't have anything to |
| 228 | * do if it needs the buffer, it will be called again upon readiness. |
| 229 | */ |
Christopher Faulet | 8f45eec | 2022-04-01 17:19:36 +0200 | [diff] [blame] | 230 | if (!cs_alloc_ibuf(cs, &app->buffer_wait)) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 231 | cs_rx_endp_more(cs); |
Willy Tarreau | 8be7cd7 | 2018-11-14 15:12:08 +0100 | [diff] [blame] | 232 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 233 | count = co_data(cs_oc(cs)); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 234 | app->applet->fct(app); |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 235 | |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 236 | /* now check if the applet has released some room and forgot to |
| 237 | * notify the other side about it. |
| 238 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 239 | if (count != co_data(cs_oc(cs))) { |
| 240 | cs_oc(cs)->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 241 | cs_rx_room_rdy(cs_opposite(cs)); |
Christopher Faulet | 1eedf9b | 2021-04-27 17:08:10 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Willy Tarreau | 19920d6 | 2019-10-11 14:15:47 +0200 | [diff] [blame] | 244 | /* measure the call rate and check for anomalies when too high */ |
| 245 | rate = update_freq_ctr(&app->call_rate, 1); |
| 246 | if (rate >= 100000 && app->call_rate.prev_ctr && // looped more than 100k times over last second |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame^] | 247 | ((b_size(cs_ib(cs)) && se_fl_test(app->endp, CS_EP_RXBLK_BUFF)) || // asks for a buffer which is present |
| 248 | (b_size(cs_ib(cs)) && !b_data(cs_ib(cs)) && se_fl_test(app->endp, CS_EP_RXBLK_ROOM)) || // asks for room in an empty buffer |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 249 | (b_data(cs_ob(cs)) && cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)) || // asks for data already present |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 250 | (!b_data(cs_ib(cs)) && b_data(cs_ob(cs)) && // didn't return anything ... |
| 251 | (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] | 252 | stream_dump_and_crash(&app->obj_type, read_freq_ctr(&app->call_rate)); |
| 253 | } |
| 254 | |
Christopher Faulet | 6059ba4 | 2022-04-01 16:34:53 +0200 | [diff] [blame] | 255 | cs->data_cb->wake(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 256 | channel_release_buffer(cs_ic(cs), &app->buffer_wait); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 257 | return t; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 258 | } |