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