Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/applet.h |
| 3 | * This file contains applet function prototypes |
| 4 | * |
| 5 | * Copyright (C) 2000-2015 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_APPLET_H |
| 23 | #define _PROTO_APPLET_H |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 28 | #include <haproxy/pool.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 29 | #include <haproxy/list.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 30 | #include <types/applet.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 31 | #include <haproxy/task.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 32 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 33 | extern unsigned int nb_applets; |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 34 | extern struct pool_head *pool_head_appctx; |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 35 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 36 | struct task *task_run_applet(struct task *t, void *context, unsigned short state); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 38 | int appctx_buf_available(void *arg); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 39 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 40 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 41 | /* Initializes all required fields for a new appctx. Note that it does the |
| 42 | * minimum acceptable initialization for an appctx. This means only the |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 43 | * 3 integer states st0, st1, st2 and the chunk used to gather unfinished |
| 44 | * commands are zeroed |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 45 | */ |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 46 | static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 47 | { |
| 48 | appctx->st0 = appctx->st1 = appctx->st2 = 0; |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 49 | appctx->chunk = NULL; |
Thierry FOURNIER / OZON.IO | 6a22dcb | 2016-11-12 10:51:33 +0100 | [diff] [blame] | 50 | appctx->io_release = NULL; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 51 | appctx->thread_mask = thread_mask; |
Willy Tarreau | 22d63a2 | 2019-04-24 08:41:29 +0200 | [diff] [blame] | 52 | appctx->call_rate.curr_sec = 0; |
| 53 | appctx->call_rate.curr_ctr = 0; |
| 54 | appctx->call_rate.prev_ctr = 0; |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 55 | appctx->state = 0; |
Willy Tarreau | 9597cbd | 2020-05-19 17:07:30 +0200 | [diff] [blame] | 56 | LIST_INIT(&appctx->wait_entry); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* Tries to allocate a new appctx and initialize its main fields. The appctx |
| 60 | * is returned on success, NULL on failure. The appctx must be released using |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 61 | * appctx_free(). <applet> is assigned as the applet, but it can be NULL. |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 62 | */ |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 63 | static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 64 | { |
| 65 | struct appctx *appctx; |
| 66 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 67 | appctx = pool_alloc(pool_head_appctx); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 68 | if (likely(appctx != NULL)) { |
| 69 | appctx->obj_type = OBJ_TYPE_APPCTX; |
| 70 | appctx->applet = applet; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 71 | appctx_init(appctx, thread_mask); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 72 | appctx->t = task_new(thread_mask); |
| 73 | if (unlikely(appctx->t == NULL)) { |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 74 | pool_free(pool_head_appctx, appctx); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | appctx->t->process = task_run_applet; |
| 78 | appctx->t->context = appctx; |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 79 | MT_LIST_INIT(&appctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 80 | appctx->buffer_wait.target = appctx; |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 81 | appctx->buffer_wait.wakeup_cb = appctx_buf_available; |
Olivier Houchard | a273534 | 2019-03-08 18:46:48 +0100 | [diff] [blame] | 82 | _HA_ATOMIC_ADD(&nb_applets, 1); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 83 | } |
| 84 | return appctx; |
| 85 | } |
| 86 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 87 | /* Releases an appctx previously allocated by appctx_new(). */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 88 | static inline void __appctx_free(struct appctx *appctx) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 89 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 90 | task_destroy(appctx->t); |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 91 | if (MT_LIST_ADDED(&appctx->buffer_wait.list)) |
| 92 | MT_LIST_DEL(&appctx->buffer_wait.list); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 93 | |
Willy Tarreau | 8280ea9 | 2019-07-18 10:41:36 +0200 | [diff] [blame] | 94 | pool_free(pool_head_appctx, appctx); |
Olivier Houchard | a273534 | 2019-03-08 18:46:48 +0100 | [diff] [blame] | 95 | _HA_ATOMIC_SUB(&nb_applets, 1); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 96 | } |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 97 | |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 98 | static inline void appctx_free(struct appctx *appctx) |
| 99 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 100 | /* The task is supposed to be run on this thread, so we can just |
| 101 | * check if it's running already (or about to run) or not |
| 102 | */ |
Willy Tarreau | a70bfaa | 2019-04-17 21:58:23 +0200 | [diff] [blame] | 103 | if (!(appctx->t->state & (TASK_QUEUED | TASK_RUNNING))) |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 104 | __appctx_free(appctx); |
| 105 | else { |
| 106 | /* if it's running, or about to run, defer the freeing |
| 107 | * until the callback is called. |
| 108 | */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 109 | appctx->state |= APPLET_WANT_DIE; |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 110 | task_wakeup(appctx->t, TASK_WOKEN_OTHER); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 111 | } |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 112 | } |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 113 | |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 114 | /* wakes up an applet when conditions have changed */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 115 | static inline void appctx_wakeup(struct appctx *appctx) |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 116 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 117 | task_wakeup(appctx->t, TASK_WOKEN_OTHER); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 120 | #endif /* _PROTO_APPLET_H */ |
| 121 | |
| 122 | /* |
| 123 | * Local variables: |
| 124 | * c-indent-level: 8 |
| 125 | * c-basic-offset: 8 |
| 126 | * End: |
| 127 | */ |