2014-07-14 11:49:44 +00:00
|
|
|
/** Message paths.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program 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 General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2014-06-25 17:50:30 +00:00
|
|
|
#include <unistd.h>
|
2016-06-08 22:38:21 +02:00
|
|
|
#include <string.h>
|
2015-10-11 14:50:55 +02:00
|
|
|
#include <inttypes.h>
|
2017-08-30 23:53:35 +02:00
|
|
|
#include <poll.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/config.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/path.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/queue.h>
|
|
|
|
#include <villas/hook.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/memory.h>
|
|
|
|
#include <villas/stats.h>
|
|
|
|
#include <villas/node.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
/* Forward declaration */
|
|
|
|
static void path_destination_enqueue(struct path *p, struct sample *smps[], unsigned cnt);
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
static int path_source_init(struct path_source *ps)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
ret = pool_init(&ps->pool, MAX(DEFAULT_QUEUELEN, ps->node->in.vectorize), SAMPLE_LEN(ps->node->samplelen), &memory_hugepage);
|
2017-09-02 14:27:58 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int path_source_destroy(struct path_source *ps)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pool_destroy(&ps->pool);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-09-05 01:07:11 +02:00
|
|
|
ret = list_destroy(&ps->mappings, NULL, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
static void path_source_read(struct path_source *ps, struct path *p, int i)
|
|
|
|
{
|
2018-07-11 18:14:29 +02:00
|
|
|
int recv, tomux, allocated, cnt;
|
|
|
|
unsigned release;
|
2018-05-08 08:16:14 +02:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
cnt = ps->node->in.vectorize;
|
2018-05-08 08:16:14 +02:00
|
|
|
|
|
|
|
struct sample *read_smps[cnt];
|
|
|
|
struct sample *muxed_smps[cnt];
|
|
|
|
struct sample **tomux_smps;
|
|
|
|
|
|
|
|
/* Fill smps[] free sample blocks from the pool */
|
2018-07-11 18:14:29 +02:00
|
|
|
allocated = sample_alloc_many(&ps->pool, read_smps, cnt);
|
|
|
|
if (allocated != cnt)
|
2018-05-08 08:16:14 +02:00
|
|
|
warn("Pool underrun for path source %s", node_name(ps->node));
|
|
|
|
|
|
|
|
/* Read ready samples and store them to blocks pointed by smps[] */
|
2018-07-11 18:14:29 +02:00
|
|
|
release = allocated;
|
|
|
|
|
|
|
|
recv = node_read(ps->node, read_smps, allocated, &release);
|
2018-05-08 08:16:14 +02:00
|
|
|
if (recv == 0)
|
|
|
|
goto out2;
|
|
|
|
else if (recv < 0)
|
|
|
|
error("Failed to read samples from node %s", node_name(ps->node));
|
2018-07-11 18:14:29 +02:00
|
|
|
else if (recv < allocated)
|
|
|
|
warn("Partial read for path %s: read=%u, expected=%u", path_name(p), recv, allocated);
|
2018-05-08 08:16:14 +02:00
|
|
|
|
|
|
|
bitset_set(&p->received, i);
|
|
|
|
|
|
|
|
if (p->mode == PATH_MODE_ANY) { /* Mux all samples */
|
|
|
|
tomux_smps = read_smps;
|
|
|
|
tomux = recv;
|
|
|
|
}
|
|
|
|
else { /* Mux only last sample and discard others */
|
|
|
|
tomux_smps = read_smps + recv - 1;
|
|
|
|
tomux = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tomux; i++) {
|
|
|
|
muxed_smps[i] = i == 0
|
|
|
|
? sample_clone(p->last_sample)
|
|
|
|
: sample_clone(muxed_smps[i-1]);
|
|
|
|
|
|
|
|
muxed_smps[i]->sequence = p->last_sequence + 1;
|
|
|
|
|
|
|
|
mapping_remap(&ps->mappings, muxed_smps[i], tomux_smps[i], NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_copy(p->last_sample, muxed_smps[tomux-1]);
|
|
|
|
|
|
|
|
debug(15, "Path %s received = %s", path_name(p), bitset_dump(&p->received));
|
|
|
|
|
2018-05-23 14:59:38 +02:00
|
|
|
#ifdef WITH_HOOKS
|
|
|
|
int toenqueue = hook_process_list(&p->hooks, muxed_smps, tomux);
|
|
|
|
if (toenqueue != tomux) {
|
|
|
|
int skipped = tomux - toenqueue;
|
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
debug(LOG_NODES | 10, "Hooks skipped %u out of %u samples for path %s", skipped, tomux, path_name(p));
|
2018-05-23 14:59:38 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
int toenqueue = tomux;
|
|
|
|
#endif
|
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
if (bitset_test(&p->mask, i)) {
|
|
|
|
/* Check if we received an update from all nodes/ */
|
|
|
|
if ((p->mode == PATH_MODE_ANY) ||
|
|
|
|
(p->mode == PATH_MODE_ALL && !bitset_cmp(&p->mask, &p->received)))
|
|
|
|
{
|
2018-05-23 14:59:38 +02:00
|
|
|
path_destination_enqueue(p, muxed_smps, toenqueue);
|
2018-05-08 08:16:14 +02:00
|
|
|
|
|
|
|
/* Reset bitset of updated nodes */
|
|
|
|
bitset_clear_all(&p->received);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_put_many(muxed_smps, tomux);
|
2018-07-11 18:14:29 +02:00
|
|
|
out2: sample_put_many(read_smps, release);
|
2018-05-08 08:16:14 +02:00
|
|
|
}
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
static int path_destination_init(struct path_destination *pd, int queuelen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
ret = queue_init(&pd->queue, queuelen, &memory_hugepage);
|
2017-09-02 14:27:58 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int path_destination_destroy(struct path_destination *pd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = queue_destroy(&pd->queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2017-08-30 23:53:35 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
static void path_destination_enqueue(struct path *p, struct sample *smps[], unsigned cnt)
|
2017-08-30 23:53:35 +02:00
|
|
|
{
|
2017-10-16 23:07:42 +02:00
|
|
|
unsigned enqueued, cloned;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
struct sample *clones[cnt];
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
cloned = sample_clone_many(clones, smps, cnt);
|
|
|
|
if (cloned < cnt)
|
|
|
|
warn("Pool underrun in path %s", path_name(p));
|
2017-08-30 23:53:35 +02:00
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
enqueued = queue_push_many(&pd->queue, (void **) clones, cloned);
|
|
|
|
if (enqueued != cnt)
|
|
|
|
warn("Queue overrun for path %s", path_name(p));
|
2016-10-20 18:05:56 -04:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
/* Increase reference counter of these samples as they are now also owned by the queue. */
|
|
|
|
sample_get_many(clones, cloned);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
debug(LOG_PATH | 15, "Enqueued %u samples to destination %s of path %s", enqueued, node_name(pd->node), path_name(p));
|
2015-03-21 18:04:52 +01:00
|
|
|
}
|
2017-10-16 23:07:42 +02:00
|
|
|
|
|
|
|
sample_put_many(clones, cloned);
|
2016-11-07 22:17:45 -05:00
|
|
|
}
|
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
static void path_destination_write(struct path_destination *pd, struct path *p)
|
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
int cnt = pd->node->out.vectorize;
|
2018-05-08 08:16:14 +02:00
|
|
|
int sent;
|
|
|
|
int released;
|
2018-07-11 18:14:29 +02:00
|
|
|
int allocated;
|
|
|
|
unsigned release;
|
2018-05-08 08:16:14 +02:00
|
|
|
|
|
|
|
struct sample *smps[cnt];
|
|
|
|
|
|
|
|
/* As long as there are still samples in the queue */
|
|
|
|
while (1) {
|
2018-07-11 18:14:29 +02:00
|
|
|
allocated = queue_pull_many(&pd->queue, (void **) smps, cnt);
|
|
|
|
if (allocated == 0)
|
2018-05-08 08:16:14 +02:00
|
|
|
break;
|
2018-07-11 18:14:29 +02:00
|
|
|
else if (allocated < cnt)
|
|
|
|
debug(LOG_PATH | 5, "Queue underrun for path %s: allocated=%u expected=%u", path_name(p), allocated, cnt);
|
|
|
|
|
|
|
|
debug(LOG_PATH | 15, "Dequeued %u samples from queue of node %s which is part of path %s", allocated, node_name(pd->node), path_name(p));
|
2018-05-08 08:16:14 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
release = allocated;
|
2018-05-08 08:16:14 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
sent = node_write(pd->node, smps, allocated, &release);
|
2018-05-08 08:16:14 +02:00
|
|
|
if (sent < 0)
|
|
|
|
error("Failed to sent %u samples to node %s", cnt, node_name(pd->node));
|
2018-07-11 18:14:29 +02:00
|
|
|
else if (sent < allocated)
|
|
|
|
warn("Partial write to node %s: written=%d, expected=%d", node_name(pd->node), sent, allocated);
|
2018-05-08 08:16:14 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
released = sample_put_many(smps, release);
|
2018-05-08 08:16:14 +02:00
|
|
|
|
|
|
|
debug(LOG_PATH | 15, "Released %d samples back to memory pool", released);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * path_run_single(void *arg)
|
|
|
|
{
|
|
|
|
struct path *p = arg;
|
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, 0);
|
|
|
|
|
|
|
|
debug(1, "Started path %s in single mode", path_name(p));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
path_source_read(ps, p, 0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
|
|
|
|
|
|
|
path_destination_write(pd, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-18 14:24:16 +02:00
|
|
|
/** Main thread function per path: read samples -> write samples */
|
2018-05-08 08:16:14 +02:00
|
|
|
static void * path_run_poll(void *arg)
|
2016-11-07 22:17:45 -05:00
|
|
|
{
|
2018-05-08 08:16:14 +02:00
|
|
|
int ret;
|
2016-11-07 22:17:45 -05:00
|
|
|
struct path *p = arg;
|
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
debug(1, "Started path %s in polling mode", path_name(p));
|
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
for (;;) {
|
2017-10-16 23:07:42 +02:00
|
|
|
ret = poll(p->reader.pfds, p->reader.nfds, -1);
|
|
|
|
if (ret < 0)
|
|
|
|
serror("Failed to poll");
|
|
|
|
|
2018-05-23 02:24:55 +02:00
|
|
|
debug(10, "Path %s returned from poll(2)", path_name(p));
|
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
for (int i = 0; i < p->reader.nfds; i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-10-16 23:07:42 +02:00
|
|
|
|
|
|
|
if (p->reader.pfds[i].revents & POLLIN) {
|
|
|
|
/* Timeout: re-enqueue the last sample */
|
|
|
|
if (p->reader.pfds[i].fd == task_fd(&p->timeout)) {
|
|
|
|
task_wait(&p->timeout);
|
|
|
|
|
|
|
|
p->last_sample->sequence = p->last_sequence++;
|
|
|
|
|
|
|
|
path_destination_enqueue(p, &p->last_sample, 1);
|
|
|
|
}
|
|
|
|
/* A source is ready to receive samples */
|
|
|
|
else {
|
2018-05-08 08:16:14 +02:00
|
|
|
path_source_read(ps, p, i);
|
2017-10-16 23:07:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
2017-10-16 23:07:42 +02:00
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
path_destination_write(pd, p);
|
2017-10-16 23:07:42 +02:00
|
|
|
}
|
2016-11-07 22:17:45 -05:00
|
|
|
}
|
2014-07-18 16:05:44 +00:00
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
int path_init(struct path *p)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2017-03-12 17:01:24 -03:00
|
|
|
assert(p->state == STATE_DESTROYED);
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
list_init(&p->destinations);
|
2017-08-28 14:38:30 +02:00
|
|
|
list_init(&p->sources);
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-27 18:01:12 +02:00
|
|
|
p->_name = NULL;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:54:24 +02:00
|
|
|
/* Default values */
|
2017-10-16 23:07:42 +02:00
|
|
|
p->mode = PATH_MODE_ANY;
|
|
|
|
p->rate = 0; /* Disabled */
|
|
|
|
|
2018-04-19 14:08:35 +02:00
|
|
|
p->builtin = 1;
|
2017-03-27 12:54:24 +02:00
|
|
|
p->reverse = 0;
|
|
|
|
p->enabled = 1;
|
2018-05-23 02:24:55 +02:00
|
|
|
p->poll = -1;
|
2017-03-27 12:54:24 +02:00
|
|
|
p->queuelen = DEFAULT_QUEUELEN;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-09-02 14:27:58 +02:00
|
|
|
/* Add internal hooks if they are not already in the list */
|
2017-12-09 02:23:29 +08:00
|
|
|
list_init(&p->hooks);
|
2018-04-19 14:08:35 +02:00
|
|
|
if (p->builtin) {
|
2018-02-17 09:39:45 +01:00
|
|
|
int ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-12-20 14:56:37 +01:00
|
|
|
for (size_t i = 0; i < list_length(&plugins); i++) {
|
|
|
|
struct plugin *q = (struct plugin *) list_at(&plugins, i);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-12-20 14:56:37 +01:00
|
|
|
if (q->type != PLUGIN_TYPE_HOOK)
|
|
|
|
continue;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-12-20 14:56:37 +01:00
|
|
|
struct hook_type *vt = &q->hook;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-02-17 09:39:45 +01:00
|
|
|
if (!(vt->flags & HOOK_PATH) || !(vt->flags & HOOK_BUILTIN))
|
|
|
|
continue;
|
2017-12-09 02:23:29 +08:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) alloc(sizeof(struct hook));
|
2017-09-02 14:27:58 +02:00
|
|
|
|
|
|
|
ret = hook_init(h, vt, p, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
list_push(&p->hooks, h);
|
|
|
|
}
|
|
|
|
}
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_INITIALIZED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
return 0;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 02:24:55 +02:00
|
|
|
int path_init_poll(struct path *p)
|
|
|
|
{
|
|
|
|
int ret, nfds;
|
|
|
|
|
|
|
|
nfds = list_length(&p->sources);
|
|
|
|
if (p->rate > 0)
|
|
|
|
nfds++;
|
|
|
|
|
|
|
|
p->reader.nfds = nfds;
|
|
|
|
p->reader.pfds = alloc(sizeof(struct pollfd) * p->reader.nfds);
|
|
|
|
|
|
|
|
for (int i = 0; i < list_length(&p->sources); i++) {
|
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
|
|
|
|
|
|
|
/* This slot is only used if it is not masked */
|
|
|
|
p->reader.pfds[i].events = POLLIN;
|
|
|
|
p->reader.pfds[i].fd = node_fd(ps->node);
|
|
|
|
|
2018-05-23 09:05:49 +02:00
|
|
|
if (p->reader.pfds[i].fd < 0)
|
|
|
|
error("Failed to get file descriptor for node %s", node_name(ps->node));
|
2018-05-23 02:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We use the last slot for the timeout timer. */
|
|
|
|
if (p->rate > 0) {
|
|
|
|
ret = task_init(&p->timeout, p->rate, CLOCK_MONOTONIC);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
p->reader.pfds[nfds-1].events = POLLIN;
|
2018-05-23 09:05:49 +02:00
|
|
|
p->reader.pfds[nfds-1].fd = task_fd(&p->timeout);
|
|
|
|
if (p->reader.pfds[nfds-1].fd < 0)
|
|
|
|
error("Failed to get file descriptor for timer of path %s", path_name(p));
|
2018-05-23 02:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
int path_init2(struct path *p)
|
|
|
|
{
|
2017-08-31 11:29:49 +02:00
|
|
|
int ret;
|
2017-08-30 23:53:35 +02:00
|
|
|
|
|
|
|
assert(p->state == STATE_CHECKED);
|
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-08-30 23:53:35 +02:00
|
|
|
/* We sort the hooks according to their priority before starting the path */
|
|
|
|
list_sort(&p->hooks, hook_cmp_priority);
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-08-30 23:53:35 +02:00
|
|
|
|
|
|
|
/* Initialize destinations */
|
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
2017-08-30 23:53:35 +02:00
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
ret = path_destination_init(pd, p->queuelen);
|
2017-08-30 23:53:35 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize sources */
|
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-08-30 23:53:35 +02:00
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
ret = path_source_init(ps);
|
2017-08-30 23:53:35 +02:00
|
|
|
if (ret)
|
2017-09-02 14:27:58 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
bitset_init(&p->received, list_length(&p->sources));
|
|
|
|
bitset_init(&p->mask, list_length(&p->sources));
|
|
|
|
|
|
|
|
/* Calc sample length of path and initialize bitset */
|
2017-09-02 14:27:58 +02:00
|
|
|
p->samplelen = 0;
|
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
if (ps->masked)
|
|
|
|
bitset_set(&p->mask, i);
|
|
|
|
|
2017-08-31 11:29:49 +02:00
|
|
|
for (size_t i = 0; i < list_length(&ps->mappings); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct mapping_entry *me = (struct mapping_entry *) list_at(&ps->mappings, i);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-09-23 23:57:19 -06:00
|
|
|
int len = me->length;
|
|
|
|
int off = me->offset;
|
|
|
|
|
|
|
|
if (off + len > p->samplelen)
|
|
|
|
p->samplelen = off + len;
|
2017-08-31 11:29:49 +02:00
|
|
|
}
|
2017-08-30 23:53:35 +02:00
|
|
|
}
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-09-04 23:03:00 +02:00
|
|
|
if (!p->samplelen)
|
|
|
|
p->samplelen = DEFAULT_SAMPLELEN;
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
ret = pool_init(&p->pool, MAX(1, list_length(&p->destinations)) * p->queuelen, SAMPLE_LEN(p->samplelen), &memory_hugepage);
|
2017-08-30 23:53:35 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
p->last_sample = sample_alloc(&p->pool);
|
|
|
|
if (!p->last_sample)
|
2017-09-23 23:57:19 -06:00
|
|
|
return -1;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
/* Prepare poll() */
|
2018-05-23 02:24:55 +02:00
|
|
|
if (p->poll) {
|
|
|
|
ret = path_init_poll(p);
|
2017-10-16 23:07:42 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-08-30 23:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int path_parse(struct path *p, json_t *cfg, struct list *nodes)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2017-03-27 12:54:24 +02:00
|
|
|
int ret;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
json_error_t err;
|
2017-08-28 14:38:30 +02:00
|
|
|
json_t *json_in;
|
|
|
|
json_t *json_out = NULL;
|
|
|
|
json_t *json_hooks = NULL;
|
2017-10-16 23:07:42 +02:00
|
|
|
json_t *json_mask = NULL;
|
|
|
|
|
|
|
|
const char *mode = NULL;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
struct list sources = { .state = STATE_DESTROYED };
|
2017-03-12 17:01:24 -03:00
|
|
|
struct list destinations = { .state = STATE_DESTROYED };
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
list_init(&sources);
|
2017-03-12 17:01:24 -03:00
|
|
|
list_init(&destinations);
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2018-05-23 02:24:55 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s: o, s?: o, s?: o, s?: b, s?: b, s?: b, s?: i, s?: s, s?: b, s?: F, s?: o }",
|
2017-08-28 14:38:30 +02:00
|
|
|
"in", &json_in,
|
|
|
|
"out", &json_out,
|
|
|
|
"hooks", &json_hooks,
|
2017-08-03 00:19:27 +02:00
|
|
|
"reverse", &p->reverse,
|
|
|
|
"enabled", &p->enabled,
|
2018-04-19 14:08:35 +02:00
|
|
|
"builtin", &p->builtin,
|
2017-10-16 23:07:42 +02:00
|
|
|
"queuelen", &p->queuelen,
|
|
|
|
"mode", &mode,
|
2018-05-23 02:24:55 +02:00
|
|
|
"poll", &p->poll,
|
2017-10-16 23:07:42 +02:00
|
|
|
"rate", &p->rate,
|
|
|
|
"mask", &json_mask
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse path configuration");
|
2015-04-01 13:25:03 +02:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
/* Input node(s) */
|
2017-08-31 09:42:36 +02:00
|
|
|
ret = mapping_parse_list(&sources, json_in, nodes);
|
2017-08-28 14:38:30 +02:00
|
|
|
if (ret)
|
|
|
|
error("Failed to parse input mapping of path %s", path_name(p));
|
|
|
|
|
|
|
|
/* Optional settings */
|
2017-10-16 23:07:42 +02:00
|
|
|
if (mode) {
|
|
|
|
if (!strcmp(mode, "any"))
|
|
|
|
p->mode = PATH_MODE_ANY;
|
|
|
|
else if (!strcmp(mode, "all"))
|
|
|
|
p->mode = PATH_MODE_ALL;
|
|
|
|
else
|
|
|
|
error("Invalid path mode '%s'", mode);
|
|
|
|
}
|
2016-10-20 18:04:18 -04:00
|
|
|
|
2016-11-20 12:59:37 -05:00
|
|
|
/* Output node(s) */
|
2017-08-28 14:38:30 +02:00
|
|
|
if (json_out) {
|
|
|
|
ret = node_parse_list(&destinations, json_out, nodes);
|
2017-07-13 22:25:26 +02:00
|
|
|
if (ret)
|
2017-08-03 00:19:27 +02:00
|
|
|
jerror(&err, "Failed to parse output nodes");
|
2017-07-13 22:25:26 +02:00
|
|
|
}
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
for (size_t i = 0; i < list_length(&sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct mapping_entry *me = (struct mapping_entry *) list_at(&sources, i);
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
struct path_source *ps = NULL;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
/* Check if there is already a path_source for this source */
|
|
|
|
for (size_t j = 0; j < list_length(&p->sources); j++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *pt = (struct path_source *) list_at(&p->sources, j);
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
if (pt->node == me->node) {
|
|
|
|
ps = pt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
if (!ps) {
|
|
|
|
ps = alloc(sizeof(struct path_source));
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
ps->node = me->node;
|
2017-10-18 09:31:31 +02:00
|
|
|
ps->masked = false;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
|
|
|
ps->mappings.state = STATE_DESTROYED;
|
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
list_init(&ps->mappings);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
list_push(&p->sources, ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_push(&ps->mappings, me);
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct node *n = (struct node *) list_at(&destinations, i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) alloc(sizeof(struct path_destination));
|
2017-07-06 23:48:19 +02:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
pd->node = n;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
list_push(&p->destinations, pd);
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
if (json_mask) {
|
|
|
|
json_t *json_entry;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!json_is_array(json_mask))
|
|
|
|
error("The 'mask' setting must be a list of node names");
|
|
|
|
|
|
|
|
json_array_foreach(json_mask, i, json_entry) {
|
|
|
|
const char *name;
|
|
|
|
struct node *node;
|
2017-10-18 09:31:31 +02:00
|
|
|
struct path_source *ps = NULL;
|
2017-10-16 23:07:42 +02:00
|
|
|
|
|
|
|
name = json_string_value(json_entry);
|
|
|
|
if (!name)
|
|
|
|
error("The 'mask' setting must be a list of node names");
|
|
|
|
|
|
|
|
node = list_lookup(nodes, name);
|
|
|
|
if (!node)
|
|
|
|
error("The 'mask' entry '%s' is not a valid node name", name);
|
|
|
|
|
|
|
|
/* Search correspondending path_source to node */
|
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *pt = (struct path_source *) list_at(&p->sources, i);
|
2017-10-16 23:07:42 +02:00
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
if (pt->node == node) {
|
|
|
|
ps = pt;
|
2017-10-16 23:07:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
if (!ps)
|
2017-10-16 23:07:42 +02:00
|
|
|
error("Node %s is not a source of the path %s", node_name(node), path_name(p));
|
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
ps->masked = true;
|
2017-10-16 23:07:42 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 09:31:31 +02:00
|
|
|
else {/* Enable all by default */
|
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-10-18 09:31:31 +02:00
|
|
|
|
|
|
|
ps->masked = true;
|
|
|
|
}
|
2017-10-16 23:07:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 09:37:01 +02:00
|
|
|
#ifdef WITH_HOOKS
|
2017-08-30 23:53:35 +02:00
|
|
|
if (json_hooks) {
|
2017-09-02 14:27:58 +02:00
|
|
|
ret = hook_parse_list(&p->hooks, json_hooks, p, NULL);
|
2017-08-30 23:53:35 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-08-30 23:53:35 +02:00
|
|
|
|
2018-05-23 02:24:55 +02:00
|
|
|
/* Autodetect whether to use poll() for this path or not */
|
|
|
|
if (p->poll == -1) {
|
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, 0);
|
|
|
|
|
|
|
|
p->poll = (
|
|
|
|
p->rate > 0 ||
|
|
|
|
list_length(&p->sources) > 1
|
|
|
|
)
|
|
|
|
&& node_fd(ps->node) != 1;
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = list_destroy(&sources, NULL, false);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = list_destroy(&destinations, NULL, false);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
p->cfg = cfg;
|
|
|
|
p->state = STATE_PARSED;
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
2015-03-18 15:45:06 +01:00
|
|
|
}
|
2015-03-18 15:47:18 +01:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int path_check(struct path *p)
|
2016-11-20 12:59:37 -05:00
|
|
|
{
|
2017-03-13 23:51:38 -03:00
|
|
|
assert(p->state != STATE_DESTROYED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
if (p->rate < 0)
|
|
|
|
error("Setting 'rate' of path %s must be a positive number.", path_name(p));
|
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-08-28 14:38:30 +02:00
|
|
|
|
|
|
|
if (!ps->node->_vt->read)
|
|
|
|
error("Source node '%s' is not supported as a source for path '%s'", node_name(ps->node), path_name(p));
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:54:24 +02:00
|
|
|
if (!pd->node->_vt->write)
|
|
|
|
error("Destiation node '%s' is not supported as a sink for path '%s'", node_name(pd->node), path_name(p));
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
if (!IS_POW2(p->queuelen)) {
|
|
|
|
p->queuelen = LOG2_CEIL(p->queuelen);
|
|
|
|
warn("Queue length should always be a power of 2. Adjusting to %d", p->queuelen);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:54:24 +02:00
|
|
|
p->state = STATE_CHECKED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int path_start(struct path *p)
|
2016-11-07 22:17:45 -05:00
|
|
|
{
|
2017-03-11 23:50:30 -03:00
|
|
|
int ret;
|
2017-10-18 09:31:31 +02:00
|
|
|
char *mode, *mask;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
assert(p->state == STATE_CHECKED);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
switch (p->mode) {
|
2017-10-18 09:31:31 +02:00
|
|
|
case PATH_MODE_ANY: mode = "any"; break;
|
|
|
|
case PATH_MODE_ALL: mode = "all"; break;
|
|
|
|
default: mode = "unknown"; break;
|
2017-10-16 23:07:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
mask = bitset_dump(&p->mask);
|
|
|
|
|
2018-05-23 02:24:55 +02:00
|
|
|
info("Starting path %s: mode=%s, poll=%s, mask=%s, rate=%.2f, enabled=%s, reversed=%s, queuelen=%d, samplelen=%d, #hooks=%zu, #sources=%zu, #destinations=%zu",
|
2017-08-31 11:31:43 +02:00
|
|
|
path_name(p),
|
2017-10-16 23:07:42 +02:00
|
|
|
mode,
|
2018-05-23 02:24:55 +02:00
|
|
|
p->poll ? "yes" : "no",
|
2017-10-18 09:31:31 +02:00
|
|
|
mask,
|
2017-10-16 23:07:42 +02:00
|
|
|
p->rate,
|
2018-05-23 02:24:55 +02:00
|
|
|
p->enabled ? "yes" : "no",
|
|
|
|
p->reverse ? "yes" : "no",
|
2017-09-03 10:52:46 +02:00
|
|
|
p->queuelen, p->samplelen,
|
2017-08-31 11:31:43 +02:00
|
|
|
list_length(&p->hooks),
|
|
|
|
list_length(&p->sources),
|
|
|
|
list_length(&p->destinations)
|
|
|
|
);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-10-18 09:31:31 +02:00
|
|
|
free(mask);
|
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&p->hooks); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) list_at(&p->hooks, i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
ret = hook_start(h);
|
2017-03-17 01:08:48 -03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
p->last_sequence = 0;
|
2017-10-18 09:31:31 +02:00
|
|
|
|
|
|
|
bitset_clear_all(&p->received);
|
2017-03-13 23:51:38 -03:00
|
|
|
|
2017-09-23 23:57:19 -06:00
|
|
|
/* We initialize the intial sample with zeros */
|
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-09-23 23:57:19 -06:00
|
|
|
|
|
|
|
for (size_t j = 0; j < list_length(&ps->mappings); j++) {
|
2017-10-18 17:15:56 +02:00
|
|
|
struct mapping_entry *me = (struct mapping_entry *) list_at(&ps->mappings, j);
|
2017-09-23 23:57:19 -06:00
|
|
|
|
|
|
|
int len = me->length;
|
|
|
|
int off = me->offset;
|
|
|
|
|
|
|
|
if (len + off > p->last_sample->length)
|
|
|
|
p->last_sample->length = len + off;
|
|
|
|
|
|
|
|
for (int k = off; k < off + len; k++) {
|
|
|
|
p->last_sample->data[k].f = 0;
|
|
|
|
|
|
|
|
sample_set_data_format(p->last_sample, k, SAMPLE_DATA_FORMAT_FLOAT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:16:14 +02:00
|
|
|
/* Start one thread per path for sending to destinations
|
|
|
|
*
|
|
|
|
* Special case: If the path only has a single source and this source
|
|
|
|
* does not offer a file descriptor for polling, we will use a special
|
|
|
|
* thread function.
|
|
|
|
*/
|
2018-05-23 02:24:55 +02:00
|
|
|
ret = pthread_create(&p->tid, NULL, p->poll ? path_run_poll : path_run_single, p);
|
2017-03-11 23:50:30 -03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
p->state = STATE_STARTED;
|
|
|
|
|
|
|
|
return 0;
|
2016-11-07 22:17:45 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int path_stop(struct path *p)
|
2016-11-07 22:17:45 -05:00
|
|
|
{
|
2017-03-11 23:50:30 -03:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-07 17:44:20 +02:00
|
|
|
if (p->state != STATE_STARTED)
|
|
|
|
return 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
info("Stopping path: %s", path_name(p));
|
|
|
|
|
2017-09-03 10:52:46 +02:00
|
|
|
ret = pthread_cancel(p->tid);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = pthread_join(p->tid, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&p->hooks); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) list_at(&p->hooks, i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
ret = hook_stop(h);
|
2017-03-17 01:08:48 -03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
p->state = STATE_STOPPED;
|
|
|
|
|
|
|
|
return 0;
|
2016-11-07 22:17:45 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int path_destroy(struct path *p)
|
2016-06-08 22:38:21 +02:00
|
|
|
{
|
2017-04-02 04:56:08 +02:00
|
|
|
if (p->state == STATE_DESTROYED)
|
|
|
|
return 0;
|
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2016-11-07 22:17:45 -05:00
|
|
|
list_destroy(&p->hooks, (dtor_cb_t) hook_destroy, true);
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif
|
2017-08-28 14:38:30 +02:00
|
|
|
list_destroy(&p->sources, (dtor_cb_t) path_source_destroy, true);
|
2016-11-07 22:17:45 -05:00
|
|
|
list_destroy(&p->destinations, (dtor_cb_t) path_destination_destroy, true);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
if (p->reader.pfds)
|
|
|
|
free(p->reader.pfds);
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
if (p->_name)
|
|
|
|
free(p->_name);
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-10-16 23:07:42 +02:00
|
|
|
if (p->rate > 0)
|
|
|
|
task_destroy(&p->timeout);
|
|
|
|
|
2017-08-30 23:53:35 +02:00
|
|
|
pool_destroy(&p->pool);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
p->state = STATE_DESTROYED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * path_name(struct path *p)
|
|
|
|
{
|
|
|
|
if (!p->_name) {
|
2017-08-28 14:38:30 +02:00
|
|
|
strcatf(&p->_name, "[");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
strcatf(&p->_name, " %s", node_name_short(ps->node));
|
|
|
|
}
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
strcatf(&p->_name, " ] " CLR_MAG("=>") " [");
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
strcatf(&p->_name, " %s", node_name_short(pd->node));
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
2017-08-28 14:38:30 +02:00
|
|
|
|
|
|
|
strcatf(&p->_name, " ]");
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return p->_name;
|
2015-03-18 15:47:18 +01:00
|
|
|
}
|
2016-01-14 22:52:08 +01:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
int path_uses_node(struct path *p, struct node *n)
|
|
|
|
{
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&p->destinations); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *pd = (struct path_destination *) list_at(&p->destinations, i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
if (pd->node == n)
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
for (size_t i = 0; i < list_length(&p->sources); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_source *ps = (struct path_source *) list_at(&p->sources, i);
|
2017-08-28 14:38:30 +02:00
|
|
|
|
|
|
|
if (ps->node == n)
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-07 22:17:45 -05:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
return -1;
|
2016-01-14 22:52:08 +01:00
|
|
|
}
|
2016-11-07 22:17:45 -05:00
|
|
|
|
|
|
|
int path_reverse(struct path *p, struct path *r)
|
|
|
|
{
|
2017-08-28 14:38:30 +02:00
|
|
|
if (list_length(&p->destinations) != 1 || list_length(&p->sources) != 1)
|
2016-11-07 22:17:45 -05:00
|
|
|
return -1;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2016-11-07 22:17:45 -05:00
|
|
|
/* General */
|
|
|
|
r->enabled = p->enabled;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
/* Source / Destinations */
|
|
|
|
struct path_destination *orig_pd = list_first(&p->destinations);
|
|
|
|
struct path_source *orig_ps = list_first(&p->sources);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct path_destination *new_pd = (struct path_destination *) alloc(sizeof(struct path_destination));
|
|
|
|
struct path_source *new_ps = (struct path_source *) alloc(sizeof(struct path_source));
|
2017-12-20 11:47:15 +01:00
|
|
|
struct mapping_entry *new_me = alloc(sizeof(struct mapping_entry));
|
|
|
|
new_pd->node = orig_ps->node;
|
|
|
|
new_ps->node = orig_pd->node;
|
|
|
|
new_ps->masked = true;
|
|
|
|
|
|
|
|
new_me->node = new_ps->node;
|
|
|
|
new_me->type = MAPPING_TYPE_DATA;
|
|
|
|
new_me->data.offset = 0;
|
|
|
|
new_me->length = 0;
|
|
|
|
|
|
|
|
list_init(&new_ps->mappings);
|
|
|
|
list_push(&new_ps->mappings, new_me);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-28 14:38:30 +02:00
|
|
|
list_push(&r->destinations, new_pd);
|
|
|
|
list_push(&r->sources, new_ps);
|
2016-11-07 22:17:45 -05:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&p->hooks); i++) {
|
2017-12-09 02:23:29 +08:00
|
|
|
int ret;
|
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) list_at(&p->hooks, i);
|
|
|
|
struct hook *g = (struct hook *) alloc(sizeof(struct hook));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
ret = hook_init(g, h->_vt, r, NULL);
|
2016-11-07 22:17:45 -05:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
list_push(&r->hooks, g);
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2016-11-07 22:17:45 -05:00
|
|
|
return 0;
|
2017-04-05 12:40:21 +02:00
|
|
|
}
|