2015-04-13 11:24:54 +00:00
|
|
|
/*
|
|
|
|
* include/proto/applet.h
|
|
|
|
* This file contains applet function prototypes
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2015 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROTO_APPLET_H
|
|
|
|
#define _PROTO_APPLET_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <common/config.h>
|
2019-07-18 08:41:36 +00:00
|
|
|
#include <common/memory.h>
|
2015-04-13 15:11:11 +00:00
|
|
|
#include <common/mini-clist.h>
|
2015-04-13 11:24:54 +00:00
|
|
|
#include <types/applet.h>
|
2018-05-25 14:58:52 +00:00
|
|
|
#include <proto/task.h>
|
2015-04-13 11:24:54 +00:00
|
|
|
|
2016-12-06 08:13:22 +00:00
|
|
|
extern unsigned int nb_applets;
|
2019-07-18 08:41:36 +00:00
|
|
|
extern struct pool_head *pool_head_appctx;
|
2015-04-13 15:11:11 +00:00
|
|
|
|
2018-05-25 14:58:52 +00:00
|
|
|
struct task *task_run_applet(struct task *t, void *context, unsigned short state);
|
2015-04-19 07:59:31 +00:00
|
|
|
|
2018-11-06 16:32:37 +00:00
|
|
|
int appctx_buf_available(void *arg);
|
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 16:30:18 +00:00
|
|
|
|
|
|
|
|
2015-04-13 11:24:54 +00:00
|
|
|
/* Initializes all required fields for a new appctx. Note that it does the
|
|
|
|
* minimum acceptable initialization for an appctx. This means only the
|
2018-04-18 11:26:46 +00:00
|
|
|
* 3 integer states st0, st1, st2 and the chunk used to gather unfinished
|
|
|
|
* commands are zeroed
|
2015-04-13 11:24:54 +00:00
|
|
|
*/
|
2017-06-19 10:38:55 +00:00
|
|
|
static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask)
|
2015-04-13 11:24:54 +00:00
|
|
|
{
|
|
|
|
appctx->st0 = appctx->st1 = appctx->st2 = 0;
|
2018-04-18 11:26:46 +00:00
|
|
|
appctx->chunk = NULL;
|
2016-11-12 09:51:33 +00:00
|
|
|
appctx->io_release = NULL;
|
2017-10-31 15:06:06 +00:00
|
|
|
appctx->thread_mask = thread_mask;
|
2019-04-24 06:41:29 +00:00
|
|
|
appctx->call_rate.curr_sec = 0;
|
|
|
|
appctx->call_rate.curr_ctr = 0;
|
|
|
|
appctx->call_rate.prev_ctr = 0;
|
2018-05-25 14:58:52 +00:00
|
|
|
appctx->state = 0;
|
2015-04-13 11:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tries to allocate a new appctx and initialize its main fields. The appctx
|
|
|
|
* is returned on success, NULL on failure. The appctx must be released using
|
2019-07-18 08:41:36 +00:00
|
|
|
* appctx_free(). <applet> is assigned as the applet, but it can be NULL.
|
2015-04-13 11:24:54 +00:00
|
|
|
*/
|
2017-06-19 10:38:55 +00:00
|
|
|
static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask)
|
2015-04-13 11:24:54 +00:00
|
|
|
{
|
|
|
|
struct appctx *appctx;
|
|
|
|
|
2019-07-18 08:41:36 +00:00
|
|
|
appctx = pool_alloc(pool_head_appctx);
|
2015-04-13 11:24:54 +00:00
|
|
|
if (likely(appctx != NULL)) {
|
|
|
|
appctx->obj_type = OBJ_TYPE_APPCTX;
|
|
|
|
appctx->applet = applet;
|
2017-06-19 10:38:55 +00:00
|
|
|
appctx_init(appctx, thread_mask);
|
2018-05-25 14:58:52 +00:00
|
|
|
appctx->t = task_new(thread_mask);
|
|
|
|
if (unlikely(appctx->t == NULL)) {
|
2019-07-18 08:41:36 +00:00
|
|
|
pool_free(pool_head_appctx, appctx);
|
2018-05-25 14:58:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
appctx->t->process = task_run_applet;
|
|
|
|
appctx->t->context = appctx;
|
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 16:30:18 +00:00
|
|
|
LIST_INIT(&appctx->buffer_wait.list);
|
|
|
|
appctx->buffer_wait.target = appctx;
|
2018-11-06 16:32:37 +00:00
|
|
|
appctx->buffer_wait.wakeup_cb = appctx_buf_available;
|
2019-03-08 17:46:48 +00:00
|
|
|
_HA_ATOMIC_ADD(&nb_applets, 1);
|
2015-04-13 11:24:54 +00:00
|
|
|
}
|
|
|
|
return appctx;
|
|
|
|
}
|
|
|
|
|
2019-07-18 08:41:36 +00:00
|
|
|
/* Releases an appctx previously allocated by appctx_new(). */
|
2017-06-26 14:36:53 +00:00
|
|
|
static inline void __appctx_free(struct appctx *appctx)
|
2015-04-13 11:24:54 +00:00
|
|
|
{
|
2019-04-17 20:51:06 +00:00
|
|
|
task_destroy(appctx->t);
|
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 16:30:18 +00:00
|
|
|
if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
|
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 16:30:18 +00:00
|
|
|
LIST_DEL(&appctx->buffer_wait.list);
|
|
|
|
LIST_INIT(&appctx->buffer_wait.list);
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
|
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 16:30:18 +00:00
|
|
|
}
|
2017-06-19 10:38:55 +00:00
|
|
|
|
2019-07-18 08:41:36 +00:00
|
|
|
pool_free(pool_head_appctx, appctx);
|
2019-03-08 17:46:48 +00:00
|
|
|
_HA_ATOMIC_SUB(&nb_applets, 1);
|
2015-04-13 11:24:54 +00:00
|
|
|
}
|
2018-05-25 14:58:52 +00:00
|
|
|
|
2017-06-26 14:36:53 +00:00
|
|
|
static inline void appctx_free(struct appctx *appctx)
|
|
|
|
{
|
2018-05-25 14:58:52 +00:00
|
|
|
/* The task is supposed to be run on this thread, so we can just
|
|
|
|
* check if it's running already (or about to run) or not
|
|
|
|
*/
|
2019-04-17 19:58:23 +00:00
|
|
|
if (!(appctx->t->state & (TASK_QUEUED | TASK_RUNNING)))
|
2018-05-25 14:58:52 +00:00
|
|
|
__appctx_free(appctx);
|
|
|
|
else {
|
|
|
|
/* if it's running, or about to run, defer the freeing
|
|
|
|
* until the callback is called.
|
|
|
|
*/
|
2017-06-26 14:36:53 +00:00
|
|
|
appctx->state |= APPLET_WANT_DIE;
|
2018-05-25 14:58:52 +00:00
|
|
|
task_wakeup(appctx->t, TASK_WOKEN_OTHER);
|
2017-06-26 14:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-13 11:24:54 +00:00
|
|
|
|
2015-04-13 15:11:11 +00:00
|
|
|
/* wakes up an applet when conditions have changed */
|
2017-06-26 14:36:53 +00:00
|
|
|
static inline void appctx_wakeup(struct appctx *appctx)
|
2015-04-19 07:59:31 +00:00
|
|
|
{
|
2018-05-25 14:58:52 +00:00
|
|
|
task_wakeup(appctx->t, TASK_WOKEN_OTHER);
|
2015-04-19 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 11:24:54 +00:00
|
|
|
#endif /* _PROTO_APPLET_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|