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 | |
| 27 | #include <common/config.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 28 | #include <common/mini-clist.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 29 | #include <types/applet.h> |
| 30 | #include <proto/connection.h> |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 31 | #include <proto/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 | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 34 | |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 35 | 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] | 36 | |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 37 | int appctx_buf_available(void *arg); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 38 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 39 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | /* Initializes all required fields for a new appctx. Note that it does the |
| 41 | * minimum acceptable initialization for an appctx. This means only the |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 42 | * 3 integer states st0, st1, st2 and the chunk used to gather unfinished |
| 43 | * commands are zeroed |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 44 | */ |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 45 | static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 46 | { |
| 47 | appctx->st0 = appctx->st1 = appctx->st2 = 0; |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 48 | appctx->chunk = NULL; |
Thierry FOURNIER / OZON.IO | 6a22dcb | 2016-11-12 10:51:33 +0100 | [diff] [blame] | 49 | appctx->io_release = NULL; |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 50 | appctx->thread_mask = thread_mask; |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 51 | appctx->state = 0; |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* Tries to allocate a new appctx and initialize its main fields. The appctx |
| 55 | * is returned on success, NULL on failure. The appctx must be released using |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 56 | * pool_free(connection) or appctx_free(), since it's allocated from the |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 57 | * connection pool. <applet> is assigned as the applet, but it can be NULL. |
| 58 | */ |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 59 | 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] | 60 | { |
| 61 | struct appctx *appctx; |
| 62 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 63 | appctx = pool_alloc(pool_head_connection); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 64 | if (likely(appctx != NULL)) { |
| 65 | appctx->obj_type = OBJ_TYPE_APPCTX; |
| 66 | appctx->applet = applet; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 67 | appctx_init(appctx, thread_mask); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 68 | appctx->t = task_new(thread_mask); |
| 69 | if (unlikely(appctx->t == NULL)) { |
| 70 | pool_free(pool_head_connection, appctx); |
| 71 | return NULL; |
| 72 | } |
| 73 | appctx->t->process = task_run_applet; |
| 74 | appctx->t->context = appctx; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 75 | LIST_INIT(&appctx->buffer_wait.list); |
| 76 | appctx->buffer_wait.target = appctx; |
Willy Tarreau | 21028b5 | 2018-11-06 17:32:37 +0100 | [diff] [blame] | 77 | appctx->buffer_wait.wakeup_cb = appctx_buf_available; |
Olivier Houchard | a273534 | 2019-03-08 18:46:48 +0100 | [diff] [blame] | 78 | _HA_ATOMIC_ADD(&nb_applets, 1); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 79 | } |
| 80 | return appctx; |
| 81 | } |
| 82 | |
| 83 | /* Releases an appctx previously allocated by appctx_new(). Note that |
| 84 | * we share the connection pool. |
| 85 | */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 86 | static inline void __appctx_free(struct appctx *appctx) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 87 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 88 | task_delete(appctx->t); |
| 89 | task_free(appctx->t); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 90 | if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 91 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 92 | LIST_DEL(&appctx->buffer_wait.list); |
| 93 | LIST_INIT(&appctx->buffer_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 94 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 95 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 97 | pool_free(pool_head_connection, appctx); |
Olivier Houchard | a273534 | 2019-03-08 18:46:48 +0100 | [diff] [blame] | 98 | _HA_ATOMIC_SUB(&nb_applets, 1); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 99 | } |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 100 | |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 101 | static inline void appctx_free(struct appctx *appctx) |
| 102 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 103 | /* The task is supposed to be run on this thread, so we can just |
| 104 | * check if it's running already (or about to run) or not |
| 105 | */ |
| 106 | if (!(appctx->t->state & TASK_RUNNING)) |
| 107 | __appctx_free(appctx); |
| 108 | else { |
| 109 | /* if it's running, or about to run, defer the freeing |
| 110 | * until the callback is called. |
| 111 | */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 112 | appctx->state |= APPLET_WANT_DIE; |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 113 | task_wakeup(appctx->t, TASK_WOKEN_OTHER); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 114 | } |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 115 | } |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 117 | /* wakes up an applet when conditions have changed */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 118 | static inline void appctx_wakeup(struct appctx *appctx) |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 119 | { |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 120 | task_wakeup(appctx->t, TASK_WOKEN_OTHER); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 123 | #endif /* _PROTO_APPLET_H */ |
| 124 | |
| 125 | /* |
| 126 | * Local variables: |
| 127 | * c-indent-level: 8 |
| 128 | * c-basic-offset: 8 |
| 129 | * End: |
| 130 | */ |