1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/node.cpp

669 lines
14 KiB
C++
Raw Permalink Normal View History

/** Nodes.
*
* @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-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-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-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
*********************************************************************************/
#include <cstring>
#include <cctype>
#include <openssl/md5.h>
#include <villas/node/config.h>
2019-06-23 16:13:23 +02:00
#include <villas/hook.hpp>
2019-04-23 13:12:04 +02:00
#include <villas/hook_list.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/sample.h>
#include <villas/node.h>
2020-07-01 17:02:22 +02:00
#include <villas/path.h>
#include <villas/utils.hpp>
2019-04-23 13:11:08 +02:00
#include <villas/colors.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/plugin.h>
#include <villas/mapping.h>
#include <villas/timing.h>
2018-03-21 16:59:19 +01:00
#include <villas/signal.h>
#include <villas/memory.h>
2019-01-21 22:14:41 +01:00
#ifdef WITH_NETEM
#include <villas/kernel/if.h>
#include <villas/kernel/nl.h>
#include <villas/kernel/tc.h>
#include <villas/kernel/tc_netem.h>
#endif /* WITH_NETEM */
2019-06-23 13:35:42 +02:00
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
int node_init(struct node *n, struct node_type *vt)
{
int ret;
n->_vt = vt;
n->_vd = new char[vt->size];
2020-01-25 17:39:06 +01:00
if (!n->_vd)
throw MemoryAllocationError();
2020-07-05 13:23:14 +01:00
memset(n->_vd, 0, vt->size);
2020-07-01 17:02:22 +02:00
using stats_ptr = std::shared_ptr<Stats>;
new (&n->stats) stats_ptr();
n->output_path = nullptr;
2019-04-08 08:59:08 +02:00
n->name = nullptr;
n->_name = nullptr;
n->_name_long = nullptr;
2019-02-24 11:08:15 +01:00
n->enabled = 1;
2020-07-04 17:15:54 +02:00
n->affinity = -1; /* all cores */
2019-02-15 09:46:26 +01:00
#ifdef __linux__
2019-02-15 09:42:33 +01:00
n->fwmark = -1;
2019-02-15 11:18:32 +01:00
#endif /* __linux__ */
2019-02-15 09:42:33 +01:00
#ifdef WITH_NETEM
2019-04-08 08:59:08 +02:00
n->tc_qdisc = nullptr;
n->tc_classifier = nullptr;
#endif /* WITH_NETEM */
2018-08-20 18:31:27 +02:00
/* Default values */
2019-06-23 16:13:23 +02:00
ret = node_direction_init(&n->in, NodeDir::IN, n);
if (ret)
return ret;
2019-06-23 16:13:23 +02:00
ret = node_direction_init(&n->out, NodeDir::OUT, n);
if (ret)
return ret;
2018-09-25 20:29:05 +02:00
ret = vt->init ? vt->init(n) : 0;
if (ret)
return ret;
2019-06-23 16:13:23 +02:00
n->state = State::INITIALIZED;
2019-01-07 10:28:55 +01:00
vlist_push(&vt->instances, n);
return 0;
}
2019-02-24 09:06:48 +01:00
int node_prepare(struct node *n)
2018-08-20 18:31:27 +02:00
{
int ret;
2019-06-23 16:13:23 +02:00
assert(n->state == State::CHECKED);
2018-08-20 18:31:27 +02:00
2019-03-15 17:19:28 +01:00
ret = node_type(n)->prepare ? node_type(n)->prepare(n) : 0;
if (ret)
return ret;
2019-02-24 09:06:48 +01:00
ret = node_direction_prepare(&n->in, n);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
ret = node_direction_prepare(&n->out, n);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
2019-06-23 16:13:23 +02:00
n->state = State::PREPARED;
2019-02-24 11:13:28 +01:00
2018-08-20 18:31:27 +02:00
return 0;
}
2018-06-12 20:45:03 +02:00
int node_parse(struct node *n, json_t *json, const char *name)
2015-12-11 17:56:14 +01:00
{
struct node_type *nt;
int ret;
json_error_t err;
2019-04-08 08:59:08 +02:00
json_t *json_netem = nullptr;
const char *type, *uuid = nullptr;
n->name = strdup(name);
ret = json_unpack_ex(json, &err, 0, "{ s: s, s?: s, s?: b }",
"type", &type,
"uuid", &uuid,
2019-03-23 20:49:35 +01:00
"enabled", &n->enabled
2019-02-15 09:46:26 +01:00
);
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
2019-02-15 09:46:26 +01:00
#ifdef __linux__
2019-02-15 11:18:32 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s?: { s?: o, s?: i } }",
"out",
2019-02-15 09:42:53 +01:00
"netem", &json_netem,
"fwmark", &n->fwmark
);
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
2019-02-15 09:46:26 +01:00
#endif /* __linux__ */
2016-06-08 22:38:21 +02:00
nt = node_type_lookup(type);
2018-07-16 08:30:41 +02:00
assert(nt == node_type(n));
2015-08-07 01:11:43 +02:00
2019-02-06 17:36:10 +01:00
n->_vt = nt;
if (uuid) {
ret = uuid_parse(uuid, n->uuid);
if (ret)
throw ConfigError(json, "node-config-node-uuid", "Failed to parse UUID: {}", uuid);
}
else {
/* Generate UUID from hashed config */
char *json_str = json_dumps(json, JSON_COMPACT | JSON_SORT_KEYS);
MD5((unsigned char*) json_str, strlen(json_str), (unsigned char*) &n->uuid);
free(json_str);
}
if (json_netem) {
#ifdef WITH_NETEM
int enabled = 1;
ret = json_unpack_ex(json_netem, &err, 0, "{ s?: b }", "enabled", &enabled);
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
if (enabled)
2019-01-21 22:14:51 +01:00
tc_netem_parse(&n->tc_qdisc, json_netem);
else
2019-04-08 08:59:08 +02:00
n->tc_qdisc = nullptr;
#endif /* WITH_NETEM */
}
2018-06-12 20:45:03 +02:00
struct {
const char *str;
struct node_direction *dir;
} dirs[] = {
{ "in", &n->in },
{ "out", &n->out }
};
2019-02-06 13:11:57 +01:00
const char *fields[] = { "signals", "builtin", "vectorize", "hooks" };
2018-06-12 20:45:03 +02:00
2019-04-07 15:13:40 +02:00
for (unsigned j = 0; j < ARRAY_LEN(dirs); j++) {
2018-08-20 18:31:27 +02:00
json_t *json_dir = json_object_get(json, dirs[j].str);
2018-06-12 20:45:03 +02:00
/* Skip if direction is unused */
if (!json_dir)
json_dir = json_pack("{ s: b }", "enabled", 0);
2018-06-12 20:45:03 +02:00
/* Copy missing fields from main node config to direction config */
2019-04-07 15:13:40 +02:00
for (unsigned i = 0; i < ARRAY_LEN(fields); i++) {
2018-06-12 20:45:03 +02:00
json_t *json_field_dir = json_object_get(json_dir, fields[i]);
json_t *json_field_node = json_object_get(json, fields[i]);
if (json_field_node && !json_field_dir)
json_object_set(json_dir, fields[i], json_field_node);
}
2018-06-12 20:45:03 +02:00
ret = node_direction_parse(dirs[j].dir, n, json_dir);
2018-03-21 16:59:19 +01:00
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
2018-03-21 16:59:19 +01:00
}
2018-07-16 08:30:41 +02:00
ret = node_type(n)->parse ? node_type(n)->parse(n, json) : 0;
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
2018-06-12 20:45:03 +02:00
n->cfg = json;
2019-06-23 16:13:23 +02:00
n->state = State::PARSED;
2016-06-08 22:38:21 +02:00
2019-03-26 15:36:24 +01:00
return 0;
}
int node_check(struct node *n)
2015-12-11 17:56:14 +01:00
{
int ret;
2019-06-23 16:13:23 +02:00
assert(n->state != State::DESTROYED);
ret = node_direction_check(&n->in, n);
if (ret)
return ret;
2015-10-09 13:04:52 +02:00
ret = node_direction_check(&n->out, n);
if (ret)
return ret;
2018-07-16 08:30:41 +02:00
ret = node_type(n)->check ? node_type(n)->check(n) : 0;
if (ret)
return ret;
2019-06-23 16:13:23 +02:00
n->state = State::CHECKED;
return 0;
}
2014-12-05 12:39:52 +01:00
int node_start(struct node *n)
2015-12-11 17:56:14 +01:00
{
2015-11-16 10:51:00 +01:00
int ret;
2019-06-23 16:13:23 +02:00
assert(n->state == State::PREPARED);
assert(node_type(n)->state == State::STARTED);
2014-12-05 12:39:52 +01:00
info("Starting node %s", node_name_long(n));
2018-08-20 18:31:27 +02:00
ret = node_direction_start(&n->in, n);
if (ret)
return ret;
2018-08-20 18:31:27 +02:00
ret = node_direction_start(&n->out, n);
if (ret)
return ret;
2017-03-13 23:51:38 -03:00
2018-08-20 18:31:27 +02:00
ret = node_type(n)->start ? node_type(n)->start(n) : 0;
if (ret)
return ret;
#ifdef __linux__
/* Set fwmark for outgoing packets if netem is enabled for this node */
if (n->fwmark >= 0) {
int fds[16];
int num_sds = node_netem_fds(n, fds);
for (int i = 0; i < num_sds; i++) {
int fd = fds[i];
2019-02-15 09:42:33 +01:00
ret = setsockopt(fd, SOL_SOCKET, SO_MARK, &n->fwmark, sizeof(n->fwmark));
if (ret)
serror("Failed to set FW mark for outgoing packets");
else
2019-02-15 09:42:33 +01:00
debug(LOG_SOCKET | 4, "Set FW mark for socket (sd=%u) to %u", fd, n->fwmark);
}
}
#endif /* __linux__ */
2019-06-23 16:13:23 +02:00
n->state = State::STARTED;
2016-10-19 01:56:00 -04:00
n->sequence = 0;
2015-11-16 10:51:00 +01:00
return ret;
2014-12-05 12:39:52 +01:00
}
2014-12-05 12:39:52 +01:00
int node_stop(struct node *n)
2015-12-11 17:56:14 +01:00
{
2015-11-16 10:51:00 +01:00
int ret;
2019-06-23 16:13:23 +02:00
if (n->state != State::STOPPING && n->state != State::STARTED && n->state != State::CONNECTED && n->state != State::PENDING_CONNECT)
return 0;
info("Stopping node %s", node_name(n));
2018-11-22 09:56:40 +02:00
ret = node_direction_stop(&n->in, n);
if (ret)
return ret;
2018-11-22 09:56:40 +02:00
ret = node_direction_stop(&n->out, n);
if (ret)
return ret;
ret = node_type(n)->stop ? node_type(n)->stop(n) : 0;
2015-11-16 10:51:00 +01:00
if (ret == 0)
2019-06-23 16:13:23 +02:00
n->state = State::STOPPED;
2015-11-16 10:51:00 +01:00
return ret;
}
int node_pause(struct node *n)
{
int ret;
2019-06-23 16:13:23 +02:00
if (n->state != State::STARTED)
return -1;
info("Pausing node %s", node_name(n));
ret = node_type(n)->pause ? node_type(n)->pause(n) : 0;
if (ret == 0)
2019-06-23 16:13:23 +02:00
n->state = State::PAUSED;
return ret;
}
int node_resume(struct node *n)
{
int ret;
2019-06-23 16:13:23 +02:00
if (n->state != State::PAUSED)
return -1;
info("Resuming node %s", node_name(n));
ret = node_type(n)->resume ? node_type(n)->resume(n) : 0;
if (ret == 0)
2019-06-23 16:13:23 +02:00
n->state = State::STARTED;
return ret;
}
int node_restart(struct node *n)
{
int ret;
2019-06-23 16:13:23 +02:00
if (n->state != State::STARTED)
return -1;
info("Restarting node %s", node_name(n));
2019-02-24 11:14:44 +01:00
if (node_type(n)->restart)
ret = node_type(n)->restart(n);
else {
ret = node_type(n)->stop ? node_type(n)->stop(n) : 0;
if (ret)
return ret;
ret = node_type(n)->start ? node_type(n)->start(n) : 0;
if (ret)
return ret;
}
return 0;
}
int node_destroy(struct node *n)
{
int ret;
2019-06-23 16:13:23 +02:00
assert(n->state != State::DESTROYED && n->state != State::STARTED);
ret = node_direction_destroy(&n->in, n);
if (ret)
return ret;
ret = node_direction_destroy(&n->out, n);
if (ret)
return ret;
2018-08-20 18:31:27 +02:00
if (node_type(n)->destroy) {
2018-07-16 08:30:41 +02:00
ret = (int) node_type(n)->destroy(n);
2018-08-20 18:31:27 +02:00
if (ret)
2018-07-09 12:55:36 +02:00
return ret;
}
vlist_remove_all(&node_type(n)->instances, n);
if (n->_vd)
delete[] (char *) n->_vd;
if (n->_name)
free(n->_name);
2017-04-24 18:59:45 +02:00
if (n->_name_long)
free(n->_name_long);
if (n->name)
free(n->name);
#ifdef WITH_NETEM
rtnl_qdisc_put(n->tc_qdisc);
rtnl_cls_put(n->tc_classifier);
#endif /* WITH_NETEM */
2020-07-01 17:02:22 +02:00
using stats_ptr = std::shared_ptr<Stats>;
n->stats.~stats_ptr();
2019-06-23 16:13:23 +02:00
n->state = State::DESTROYED;
return 0;
}
int node_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
{
int readd, nread = 0;
assert(node_type(n)->read);
2019-06-23 16:13:23 +02:00
if (n->state == State::PAUSED || n->state == State::PENDING_CONNECT)
2019-02-11 16:39:30 +01:00
return 0;
2019-06-23 16:13:23 +02:00
else if (n->state != State::STARTED && n->state != State::CONNECTED)
2019-02-11 16:39:30 +01:00
return -1;
/* Send in parts if vector not supported */
2018-07-16 08:30:41 +02:00
if (node_type(n)->vectorize > 0 && node_type(n)->vectorize < cnt) {
while (cnt - nread > 0) {
2018-07-16 08:30:41 +02:00
readd = node_type(n)->read(n, &smps[nread], MIN(cnt - nread, node_type(n)->vectorize), release);
if (readd < 0)
return readd;
2017-09-02 14:20:38 +02:00
2017-07-02 18:58:58 +02:00
nread += readd;
}
}
else {
2018-07-16 08:30:41 +02:00
nread = node_type(n)->read(n, smps, cnt, release);
if (nread < 0)
return nread;
}
#ifdef WITH_HOOKS
/* Run read hooks */
int rread = hook_list_process(&n->in.hooks, smps, nread);
2018-05-23 02:25:27 +02:00
int skipped = nread - rread;
2019-04-08 08:59:08 +02:00
if (skipped > 0 && n->stats != nullptr) {
2019-06-23 13:35:42 +02:00
n->stats->update(Stats::Metric::SMPS_SKIPPED, skipped);
}
debug(LOG_NODE | 5, "Received %u samples from node %s of which %d have been skipped", nread, node_name(n), skipped);
2018-05-23 02:25:27 +02:00
return rread;
#else
debug(LOG_NODE | 5, "Received %u samples from node %s", nread, node_name(n));
2018-05-23 02:25:27 +02:00
return nread;
#endif /* WITH_HOOKS */
}
int node_write(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release)
{
int tosend, sent, nsent = 0;
assert(node_type(n)->write);
2019-06-23 16:13:23 +02:00
if (n->state == State::PAUSED || n->state == State::PENDING_CONNECT)
2019-02-11 16:39:30 +01:00
return 0;
2019-06-23 16:13:23 +02:00
else if (n->state != State::STARTED && n->state != State::CONNECTED)
2019-02-11 16:39:30 +01:00
return -1;
#ifdef WITH_HOOKS
/* Run write hooks */
cnt = hook_list_process(&n->out.hooks, smps, cnt);
if (cnt <= 0)
return cnt;
#endif /* WITH_HOOKS */
/* Send in parts if vector not supported */
2018-07-16 08:30:41 +02:00
if (node_type(n)->vectorize > 0 && node_type(n)->vectorize < cnt) {
2017-07-02 18:58:58 +02:00
while (cnt - nsent > 0) {
tosend = MIN(cnt - nsent, node_type(n)->vectorize);
sent = node_type(n)->write(n, &smps[nsent], tosend, release);
2019-02-12 19:57:59 +01:00
if (sent < 0)
return sent;
2017-07-02 18:58:58 +02:00
nsent += sent;
debug(LOG_NODE | 5, "Sent %u samples to node %s", sent, node_name(n));
2017-07-02 18:58:58 +02:00
}
}
else {
2018-07-16 08:30:41 +02:00
nsent = node_type(n)->write(n, smps, cnt, release);
2019-02-12 19:57:59 +01:00
if (nsent < 0)
return nsent;
debug(LOG_NODE | 5, "Sent %u samples to node %s", nsent, node_name(n));
}
return nsent;
}
char * node_name(struct node *n)
{
if (!n->_name)
strcatf(&n->_name, CLR_RED("%s") "(" CLR_YEL("%s") ")", n->name, node_type_name(node_type(n)));
return n->_name;
}
char * node_name_long(struct node *n)
{
2015-12-11 18:19:35 +01:00
if (!n->_name_long) {
2020-07-04 17:15:54 +02:00
char uuid[37];
uuid_unparse(n->uuid, uuid);
2019-01-21 23:00:16 +01:00
2020-07-04 17:15:54 +02:00
strcatf(&n->_name_long, "%s: uuid=%s, #in.signals=%zu, #out.signals=%zu, #in.hooks=%zu, #out.hooks=%zu, in.vectorize=%d, out.vectorize=%d",
node_name(n), uuid,
vlist_length(&n->in.signals), vlist_length(&n->out.signals),
vlist_length(&n->in.hooks), vlist_length(&n->out.hooks),
n->in.vectorize, n->out.vectorize
);
2019-01-30 00:42:35 +01:00
#ifdef WITH_NETEM
2020-07-04 17:15:54 +02:00
strcatf(&n->_name_long, ", out.netem=%s", n->tc_qdisc ? "yes" : "no");
2019-01-30 00:42:35 +01:00
2020-07-04 17:15:54 +02:00
if (n->tc_qdisc)
strcatf(&n->_name_long, ", fwmark=%d", n->fwmark);
2019-01-30 00:42:35 +01:00
#endif /* WITH_NETEM */
2019-01-21 23:00:16 +01:00
2020-07-04 17:15:54 +02:00
if (n->output_path)
strcatf(&n->_name_long, ", output_path=%s", path_name(n->output_path));
if (node_type(n)->print) {
struct node_type *vt = node_type(n);
2019-01-21 23:00:16 +01:00
/* Append node-type specific details */
char *name_long = vt->print(n);
strcatf(&n->_name_long, ", %s", name_long);
2015-12-11 18:19:35 +01:00
free(name_long);
}
}
return n->_name_long;
}
const char * node_name_short(struct node *n)
{
return n->name;
}
2020-07-01 17:02:22 +02:00
struct vlist * node_output_signals(struct node *n)
{
if (n->output_path)
return path_output_signals(n->output_path);
return nullptr;
}
int node_reverse(struct node *n)
{
2018-07-16 08:30:41 +02:00
return node_type(n)->reverse ? node_type(n)->reverse(n) : -1;
2015-03-21 15:29:00 +01:00
}
int node_poll_fds(struct node *n, int fds[])
{
return node_type(n)->poll_fds ? node_type(n)->poll_fds(n, fds) : -1;
}
int node_netem_fds(struct node *n, int fds[])
{
return node_type(n)->netem_fds ? node_type(n)->netem_fds(n, fds) : -1;
}
struct memory_type * node_memory_type(struct node *n)
{
return node_type(n)->memory_type ? node_type(n)->memory_type(n, memory_default) : memory_default;
}
int node_list_parse(struct vlist *list, json_t *cfg, struct vlist *all)
{
struct node *node;
const char *str;
2019-04-08 08:59:08 +02:00
char *allstr = nullptr;
size_t index;
json_t *elm;
switch (json_typeof(cfg)) {
case JSON_STRING:
str = json_string_value(cfg);
2019-04-07 15:13:40 +02:00
node = (struct node *) vlist_lookup(all, str);
if (!node)
goto invalid2;
2019-01-07 10:28:55 +01:00
vlist_push(list, node);
break;
case JSON_ARRAY:
json_array_foreach(cfg, index, elm) {
if (!json_is_string(elm))
goto invalid;
2019-04-07 15:13:40 +02:00
node = (struct node *) vlist_lookup(all, json_string_value(elm));
if (!node)
goto invalid;
2019-01-07 10:28:55 +01:00
vlist_push(list, node);
}
break;
default:
goto invalid;
}
return 0;
invalid:
error("The node list must be an a single or an array of strings referring to the keys of the 'nodes' section");
return -1;
invalid2:
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(all); i++) {
struct node *n = (struct node *) vlist_at(all, i);
strcatf(&allstr, " %s", node_name_short(n));
}
error("Unknown node %s. Choose of one of: %s", str, allstr);
return 0;
2017-09-02 14:20:38 +02:00
}
2019-02-12 17:54:08 +01:00
bool node_is_valid_name(const char *name)
2019-02-12 17:54:08 +01:00
{
for (const char *p = name; *p; p++) {
if (isalnum(*p) || (*p == '_') || (*p == '-'))
continue;
return false;
2019-02-12 17:54:08 +01:00
}
return true;
}
2019-02-24 11:08:15 +01:00
bool node_is_enabled(const struct node *n)
{
return n->enabled;
}
2019-06-23 16:13:23 +02:00
struct vlist * node_get_signals(struct node *n, enum NodeDir dir)
2019-03-08 15:21:01 +01:00
{
2019-06-23 16:13:23 +02:00
struct node_direction *nd = dir == NodeDir::IN ? &n->in : &n->out;
2019-03-08 15:21:01 +01:00
return node_direction_get_signals(nd);
2019-02-12 17:54:08 +01:00
}