2017-07-14 13:12:32 +02:00
|
|
|
/** Node-type for loopback connections.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-07-14 13:12:32 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2018-10-21 10:36:17 +02:00
|
|
|
|
2018-08-23 17:31:01 +02:00
|
|
|
#include <villas/node/config.h>
|
2021-06-21 16:11:42 -04:00
|
|
|
#include <villas/node.h>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/loopback.hpp>
|
2020-09-10 11:16:23 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2018-02-06 21:26:12 +01:00
|
|
|
#include <villas/memory.h>
|
2021-06-21 16:11:42 -04:00
|
|
|
#include <villas/utils.hpp>
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2020-09-10 11:16:23 +02:00
|
|
|
using namespace villas;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2021-06-21 16:11:42 -04:00
|
|
|
static struct vnode_type p;
|
2020-09-10 11:16:23 +02:00
|
|
|
|
|
|
|
int loopback_init(struct vnode *n)
|
|
|
|
{
|
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
|
|
|
|
|
|
|
l->mode = QueueSignalledMode::AUTO;
|
|
|
|
l->queuelen = DEFAULT_QUEUE_LENGTH;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int loopback_parse(struct vnode *n, json_t *json)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2019-04-22 23:45:38 +02:00
|
|
|
const char *mode_str = nullptr;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
2020-09-10 11:16:23 +02:00
|
|
|
int ret;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: s }",
|
2018-10-17 16:12:16 +02:00
|
|
|
"queuelen", &l->queuelen,
|
|
|
|
"mode", &mode_str
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-loopback");
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2018-10-17 16:12:16 +02:00
|
|
|
if (mode_str) {
|
2018-10-20 18:47:21 +02:00
|
|
|
if (!strcmp(mode_str, "auto"))
|
2019-06-23 16:13:23 +02:00
|
|
|
l->mode = QueueSignalledMode::AUTO;
|
2018-10-21 19:08:19 +01:00
|
|
|
#ifdef HAVE_EVENTFD
|
2018-10-20 18:47:21 +02:00
|
|
|
else if (!strcmp(mode_str, "eventfd"))
|
2019-06-23 16:13:23 +02:00
|
|
|
l->mode = QueueSignalledMode::EVENTFD;
|
2018-10-21 19:08:19 +01:00
|
|
|
#endif
|
2018-10-17 16:12:16 +02:00
|
|
|
else if (!strcmp(mode_str, "pthread"))
|
2019-06-23 16:13:23 +02:00
|
|
|
l->mode = QueueSignalledMode::PTHREAD;
|
2018-10-17 16:12:16 +02:00
|
|
|
else if (!strcmp(mode_str, "polling"))
|
2019-06-23 16:13:23 +02:00
|
|
|
l->mode = QueueSignalledMode::POLLING;
|
2018-10-17 23:57:19 +02:00
|
|
|
#ifdef __APPLE__
|
2018-10-17 16:12:16 +02:00
|
|
|
else if (!strcmp(mode_str, "pipe"))
|
2019-06-23 16:13:23 +02:00
|
|
|
l->mode = QueueSignalledMode::PIPE;
|
2018-10-17 23:57:19 +02:00
|
|
|
#endif /* __APPLE__ */
|
2018-10-17 16:12:16 +02:00
|
|
|
else
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, "node-config-node-loopback-mode", "Unknown mode '{}'", mode_str);
|
2018-10-17 16:12:16 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 13:12:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:16:23 +02:00
|
|
|
int loopback_prepare(struct vnode *n)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2019-10-26 13:34:03 +02:00
|
|
|
return queue_signalled_init(&l->queue, l->queuelen, memory_default, l->mode);
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 11:16:23 +02:00
|
|
|
int loopback_destroy(struct vnode *n)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l= (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
|
|
|
return queue_signalled_destroy(&l->queue);
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int loopback_read(struct vnode *n, struct sample * const smps[], unsigned cnt)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-07-14 16:29:05 +02:00
|
|
|
int avail;
|
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2018-07-11 18:14:29 +02:00
|
|
|
struct sample *cpys[cnt];
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2018-07-11 18:14:29 +02:00
|
|
|
avail = queue_signalled_pull_many(&l->queue, (void **) cpys, cnt);
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2017-07-14 16:29:05 +02:00
|
|
|
for (int i = 0; i < avail; i++) {
|
|
|
|
sample_copy(smps[i], cpys[i]);
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_decref(cpys[i]);
|
2017-07-14 16:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return avail;
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int loopback_write(struct vnode *n, struct sample * const smps[], unsigned cnt)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2021-06-29 10:45:12 -04:00
|
|
|
int pushed_cnt;
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2020-08-28 09:43:12 +02:00
|
|
|
sample_incref_many(smps, cnt);
|
2017-10-18 14:23:50 +02:00
|
|
|
|
2021-06-29 10:45:12 -04:00
|
|
|
pushed_cnt = queue_signalled_push_many(&l->queue, (void **) smps, cnt);
|
|
|
|
if (pushed_cnt < (int) cnt) {
|
|
|
|
sample_decref_many(smps + pushed_cnt, cnt - pushed_cnt);
|
|
|
|
n->logger->warn("Queue overrun in node {}", node_name(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
return pushed_cnt;
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * loopback_print(struct vnode *n)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2019-04-22 23:45:38 +02:00
|
|
|
char *buf = nullptr;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
|
|
|
strcatf(&buf, "queuelen=%d", l->queuelen);
|
|
|
|
|
|
|
|
return buf;
|
2017-10-16 23:08:46 +02:00
|
|
|
}
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int loopback_poll_fds(struct vnode *n, int fds[])
|
2017-08-30 13:30:31 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct loopback *l = (struct loopback *) n->_vd;
|
2017-10-16 23:08:46 +02:00
|
|
|
|
2019-01-21 15:47:34 +01:00
|
|
|
fds[0] = queue_signalled_fd(&l->queue);
|
|
|
|
|
|
|
|
return 1;
|
2017-08-30 13:30:31 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
2021-06-21 16:11:42 -04:00
|
|
|
p.name = "loopback";
|
|
|
|
p.description = "Loopback to connect multiple paths";
|
|
|
|
p.vectorize = 0;
|
|
|
|
p.flags = 0;
|
|
|
|
p.size = sizeof(struct loopback);
|
|
|
|
p.parse = loopback_parse;
|
|
|
|
p.print = loopback_print;
|
|
|
|
p.prepare = loopback_prepare;
|
|
|
|
p.init = loopback_init;
|
|
|
|
p.destroy = loopback_destroy;
|
|
|
|
p.read = loopback_read;
|
|
|
|
p.write = loopback_write;
|
|
|
|
p.poll_fds = loopback_poll_fds;
|
|
|
|
|
|
|
|
if (!node_types)
|
|
|
|
node_types = new NodeTypeList();
|
|
|
|
|
|
|
|
node_types->push_back(&p);
|
2019-06-04 16:55:38 +02:00
|
|
|
}
|