2014-07-14 11:49:44 +00:00
|
|
|
/** Nodes.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @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-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
|
|
|
|
2020-08-25 20:24:46 +02:00
|
|
|
#include <regex>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <cctype>
|
2019-10-30 02:38:56 +01:00
|
|
|
#include <openssl/md5.h>
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2018-08-23 17:31:01 +02:00
|
|
|
#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>
|
2019-04-23 13:09:50 +02:00
|
|
|
#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>
|
2018-06-30 18:20:30 +02:00
|
|
|
#include <villas/memory.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2019-01-21 22:14:41 +01:00
|
|
|
#ifdef WITH_NETEM
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/if.hpp>
|
|
|
|
#include <villas/kernel/nl.hpp>
|
|
|
|
#include <villas/kernel/tc.hpp>
|
|
|
|
#include <villas/kernel/tc_netem.hpp>
|
2019-01-21 22:14:41 +01:00
|
|
|
#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;
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_init(struct vnode *n, struct vnode_type *vt)
|
2018-05-24 09:04:41 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
n->_vt = vt;
|
2020-01-21 16:26:51 +01:00
|
|
|
n->_vd = new char[vt->size];
|
2020-01-25 17:39:06 +01:00
|
|
|
if (!n->_vd)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2018-05-24 09:04:41 +02:00
|
|
|
|
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();
|
|
|
|
|
2020-10-16 09:25:23 +02:00
|
|
|
uuid_clear(n->uuid);
|
|
|
|
|
2020-07-01 17:02:22 +02:00
|
|
|
n->output_path = nullptr;
|
2019-04-08 08:59:08 +02:00
|
|
|
n->name = nullptr;
|
|
|
|
n->_name = nullptr;
|
|
|
|
n->_name_long = nullptr;
|
2020-09-10 11:19:51 +02:00
|
|
|
n->enabled = true;
|
2020-07-04 17:15:54 +02:00
|
|
|
n->affinity = -1; /* all cores */
|
2018-05-24 09:04:41 +02:00
|
|
|
|
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
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef WITH_NETEM
|
2019-04-08 08:59:08 +02:00
|
|
|
n->tc_qdisc = nullptr;
|
|
|
|
n->tc_classifier = nullptr;
|
2019-01-21 15:50:18 +01:00
|
|
|
#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);
|
2018-05-24 09:04:41 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
ret = node_direction_init(&n->out, NodeDir::OUT, n);
|
2018-05-24 09:04:41 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-09-10 11:20:35 +02:00
|
|
|
ret = vlist_init(&n->sources);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = vlist_init(&n->destinations);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-09-25 20:29:05 +02:00
|
|
|
ret = vt->init ? vt->init(n) : 0;
|
2018-09-25 19:18:46 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
n->state = State::INITIALIZED;
|
2018-05-24 09:04:41 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_push(&vt->instances, n);
|
2018-05-24 09:04:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_prepare(struct vnode *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;
|
|
|
|
|
2019-03-09 00:32:22 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
int node_parse(struct vnode *n, json_t *json, const uuid_t sn_uuid)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2020-09-10 11:19:51 +02:00
|
|
|
int ret, enabled = n->enabled;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
2019-04-08 08:59:08 +02:00
|
|
|
json_t *json_netem = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-10-16 09:25:23 +02:00
|
|
|
const char *uuid_str = nullptr;
|
2020-10-16 11:08:40 +02:00
|
|
|
const char *name_str = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: s, s?: b }",
|
|
|
|
"name", &name_str,
|
2020-10-16 09:25:23 +02:00
|
|
|
"uuid", &uuid_str,
|
2020-09-10 11:19:51 +02:00
|
|
|
"enabled", &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 } }",
|
2019-01-21 15:50:18 +01:00
|
|
|
"out",
|
2019-02-15 09:42:53 +01:00
|
|
|
"netem", &json_netem,
|
|
|
|
"fwmark", &n->fwmark
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
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
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
n->enabled = enabled;
|
|
|
|
|
|
|
|
if (name_str)
|
|
|
|
n->name = strdup(name_str);
|
|
|
|
else
|
|
|
|
n->name = strdup("<none>");
|
|
|
|
|
2020-10-16 09:25:23 +02:00
|
|
|
if (uuid_str) {
|
|
|
|
ret = uuid_parse(uuid_str, n->uuid);
|
2019-10-30 02:38:56 +01:00
|
|
|
if (ret)
|
2020-10-16 09:25:23 +02:00
|
|
|
throw ConfigError(json, "node-config-node-uuid", "Failed to parse UUID: {}", uuid_str);
|
2019-10-30 02:38:56 +01:00
|
|
|
}
|
2020-10-15 11:49:42 +02:00
|
|
|
else
|
|
|
|
/* Generate UUID from hashed config including node name */
|
2020-10-16 11:08:40 +02:00
|
|
|
uuid_generate_from_json(n->uuid, json, sn_uuid);
|
2019-10-30 02:38:56 +01:00
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
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;
|
2019-01-21 15:50:18 +01:00
|
|
|
|
|
|
|
if (enabled)
|
2020-09-13 11:01:20 +02:00
|
|
|
kernel::tc::netem_parse(&n->tc_qdisc, json_netem);
|
2019-01-21 15:50:18 +01:00
|
|
|
else
|
2019-04-08 08:59:08 +02:00
|
|
|
n->tc_qdisc = nullptr;
|
2019-01-21 15:50:18 +01:00
|
|
|
#endif /* WITH_NETEM */
|
|
|
|
}
|
|
|
|
|
2018-06-12 20:45:03 +02:00
|
|
|
struct {
|
|
|
|
const char *str;
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode_direction *dir;
|
2018-06-12 20:45:03 +02:00
|
|
|
} dirs[] = {
|
|
|
|
{ "in", &n->in },
|
|
|
|
{ "out", &n->out }
|
|
|
|
};
|
2018-05-26 01:10:33 +02:00
|
|
|
|
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
|
|
|
|
2019-03-26 07:09:55 +01:00
|
|
|
/* Skip if direction is unused */
|
2018-06-15 14:36:00 +02:00
|
|
|
if (!json_dir)
|
|
|
|
json_dir = json_pack("{ s: b }", "enabled", 0);
|
2018-06-12 20:45:03 +02:00
|
|
|
|
2019-03-26 07:09:55 +01: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);
|
|
|
|
}
|
2017-09-02 14:27:58 +02:00
|
|
|
|
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;
|
2017-03-11 23:50:30 -03:00
|
|
|
if (ret)
|
2019-03-26 15:36:24 +01:00
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_check(struct vnode *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
int ret;
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(n->state != State::DESTROYED);
|
2015-12-13 00:45:20 +01:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_check(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-10-09 13:04:52 +02:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_check(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-07-16 08:30:41 +02:00
|
|
|
ret = node_type(n)->check ? node_type(n)->check(n) : 0;
|
2018-07-02 10:59:45 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
n->state = State::CHECKED;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
return 0;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_start(struct vnode *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-11-16 10:51:00 +01:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
info("Starting node %s", node_name_long(n));
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
ret = node_direction_start(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
/* Set fwmark for outgoing packets if netem is enabled for this node */
|
2019-03-15 17:18:35 +01:00
|
|
|
if (n->fwmark >= 0) {
|
2019-01-21 15:50:18 +01:00
|
|
|
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));
|
2019-01-21 15:50:18 +01:00
|
|
|
if (ret)
|
2020-09-11 14:57:05 +02:00
|
|
|
throw RuntimeError("Failed to set FW mark for outgoing packets");
|
2019-01-21 15:50:18 +01:00
|
|
|
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);
|
2019-01-21 15:50:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
return 0;
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_stop(struct vnode *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)
|
2017-03-29 04:25:30 +02:00
|
|
|
return 0;
|
2015-11-29 22:45:46 +01:00
|
|
|
|
|
|
|
info("Stopping node %s", node_name(n));
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2018-11-22 09:56:40 +02:00
|
|
|
ret = node_direction_stop(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2015-03-18 16:18:10 +01:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_pause(struct vnode *n)
|
2018-11-22 09:56:30 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (n->state != State::STARTED)
|
2018-11-22 09:56:30 +02:00
|
|
|
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;
|
2018-11-22 09:56:30 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_resume(struct vnode *n)
|
2018-11-22 09:56:30 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (n->state != State::PAUSED)
|
2018-11-22 09:56:30 +02:00
|
|
|
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;
|
2018-11-22 09:56:30 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_restart(struct vnode *n)
|
2018-11-22 09:56:30 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (n->state != State::STARTED)
|
2018-11-22 09:56:30 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
info("Restarting node %s", node_name(n));
|
|
|
|
|
2019-02-24 11:14:44 +01:00
|
|
|
if (node_type(n)->restart)
|
2018-11-22 09:56:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_destroy(struct vnode *n)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2018-05-24 09:04:41 +02:00
|
|
|
int ret;
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(n->state != State::DESTROYED && n->state != State::STARTED);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-05-24 09:04:41 +02:00
|
|
|
ret = node_direction_destroy(&n->in, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = node_direction_destroy(&n->out, n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2020-09-10 11:20:35 +02:00
|
|
|
ret = vlist_destroy(&n->sources);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = vlist_destroy(&n->destinations);
|
|
|
|
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;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-02-24 09:23:31 +01:00
|
|
|
vlist_remove_all(&node_type(n)->instances, n);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
if (n->_vd)
|
2020-01-21 16:26:51 +01:00
|
|
|
delete[] (char *) n->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
if (n->_name)
|
|
|
|
free(n->_name);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-24 18:59:45 +02:00
|
|
|
if (n->_name_long)
|
|
|
|
free(n->_name_long);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-01 11:56:47 +02:00
|
|
|
if (n->name)
|
|
|
|
free(n->name);
|
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
#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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2020-09-03 15:03:18 +02:00
|
|
|
int toread, readd, nread = 0;
|
|
|
|
unsigned vect;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-07-16 08:30:59 +02:00
|
|
|
assert(node_type(n)->read);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
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;
|
|
|
|
|
2020-08-28 09:43:34 +02:00
|
|
|
vect = node_type(n)->vectorize;
|
|
|
|
if (!vect)
|
|
|
|
vect = cnt;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2020-08-28 09:43:34 +02:00
|
|
|
while (cnt - nread > 0) {
|
2020-09-03 15:03:18 +02:00
|
|
|
toread = MIN(cnt - nread, vect);
|
2020-08-28 09:43:34 +02:00
|
|
|
readd = node_type(n)->read(n, &smps[nread], toread, release);
|
|
|
|
if (readd < 0)
|
|
|
|
return readd;
|
|
|
|
|
|
|
|
nread += readd;
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-08-28 09:43:34 +02:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-09-04 14:28:55 +02:00
|
|
|
/* Run read hooks */
|
2019-03-09 00:32:22 +01:00
|
|
|
int rread = hook_list_process(&n->in.hooks, smps, nread);
|
2021-02-22 23:16:53 +01:00
|
|
|
if (rread < 0)
|
|
|
|
return rread;
|
|
|
|
|
2018-05-23 02:25:27 +02:00
|
|
|
int skipped = nread - rread;
|
2021-02-22 23:16:53 +01:00
|
|
|
if (skipped > 0) {
|
|
|
|
if (n->stats != nullptr)
|
|
|
|
n->stats->update(Stats::Metric::SMPS_SKIPPED, skipped);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2021-02-22 23:16:53 +01:00
|
|
|
debug(LOG_NODE | 5, "Received %u samples from node %s of which %d have been skipped", nread, node_name(n), skipped);
|
2017-09-02 14:27:58 +02:00
|
|
|
}
|
2021-02-22 23:16:53 +01:00
|
|
|
else
|
|
|
|
debug(LOG_NODE | 5, "Received %u samples from node %s", nread, node_name(n));
|
2018-05-23 02:25:27 +02:00
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
return rread;
|
2017-12-09 02:23:29 +08:00
|
|
|
#else
|
2018-07-24 10:42:35 +02:00
|
|
|
debug(LOG_NODE | 5, "Received %u samples from node %s", nread, node_name(n));
|
2018-05-23 02:25:27 +02:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
return nread;
|
|
|
|
#endif /* WITH_HOOKS */
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2020-09-03 15:03:18 +02:00
|
|
|
int tosend, sent, nsent = 0;
|
|
|
|
unsigned vect;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-07-16 08:30:59 +02:00
|
|
|
assert(node_type(n)->write);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
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;
|
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2017-09-16 15:04:59 +02:00
|
|
|
/* Run write hooks */
|
2019-03-09 00:32:22 +01:00
|
|
|
cnt = hook_list_process(&n->out.hooks, smps, cnt);
|
2017-09-02 14:27:58 +02:00
|
|
|
if (cnt <= 0)
|
|
|
|
return cnt;
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif /* WITH_HOOKS */
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2020-08-28 09:43:34 +02:00
|
|
|
vect = node_type(n)->vectorize;
|
|
|
|
if (!vect)
|
|
|
|
vect = cnt;
|
2017-08-22 12:31:12 +02:00
|
|
|
|
2020-08-28 09:43:34 +02:00
|
|
|
while (cnt - nsent > 0) {
|
2020-09-03 15:03:18 +02:00
|
|
|
tosend = MIN(cnt - nsent, vect);
|
2020-08-28 09:43:34 +02:00
|
|
|
sent = node_type(n)->write(n, &smps[nsent], tosend, release);
|
|
|
|
if (sent < 0)
|
|
|
|
return sent;
|
2017-08-22 12:31:12 +02:00
|
|
|
|
2020-08-28 09:43:34 +02:00
|
|
|
nsent += sent;
|
|
|
|
debug(LOG_NODE | 5, "Sent %u samples to node %s", sent, node_name(n));
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return nsent;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * node_name(struct vnode *n)
|
2015-10-17 19:05:15 +02:00
|
|
|
{
|
2015-11-29 22:45:46 +01:00
|
|
|
if (!n->_name)
|
2019-03-09 00:32:22 +01:00
|
|
|
strcatf(&n->_name, CLR_RED("%s") "(" CLR_YEL("%s") ")", n->name, node_type_name(node_type(n)));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
return n->_name;
|
|
|
|
}
|
2015-10-17 19:05:15 +02:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * node_name_long(struct vnode *n)
|
2015-11-29 22:45:46 +01:00
|
|
|
{
|
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
|
|
|
|
);
|
2018-05-24 09:04:41 +02:00
|
|
|
|
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) {
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode_type *vt = node_type(n);
|
2020-07-04 17:15:54 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
return n->_name_long;
|
2015-10-17 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
const char * node_name_short(struct vnode *n)
|
2015-12-13 02:02:29 +01:00
|
|
|
{
|
|
|
|
return n->name;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_reverse(struct vnode *n)
|
2015-03-18 16:18:10 +01:00
|
|
|
{
|
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
|
|
|
}
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_poll_fds(struct vnode *n, int fds[])
|
2019-01-21 15:47:34 +01:00
|
|
|
{
|
|
|
|
return node_type(n)->poll_fds ? node_type(n)->poll_fds(n, fds) : -1;
|
|
|
|
}
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int node_netem_fds(struct vnode *n, int fds[])
|
2017-08-30 00:22:58 +02:00
|
|
|
{
|
2019-01-21 15:50:18 +01:00
|
|
|
return node_type(n)->netem_fds ? node_type(n)->netem_fds(n, fds) : -1;
|
2017-08-30 00:22:58 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
struct memory_type * node_memory_type(struct vnode *n)
|
2018-06-29 17:32:07 +02:00
|
|
|
{
|
2019-10-26 13:34:03 +02:00
|
|
|
return node_type(n)->memory_type ? node_type(n)->memory_type(n, memory_default) : memory_default;
|
2018-06-29 17:32:07 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 09:10:18 +01:00
|
|
|
int node_list_parse(struct vlist *list, json_t *cfg, struct vlist *all)
|
2015-03-21 15:23:57 +01:00
|
|
|
{
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode *node;
|
2017-08-03 00:19:27 +02:00
|
|
|
const char *str;
|
2019-04-08 08:59:08 +02:00
|
|
|
char *allstr = nullptr;
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
size_t index;
|
|
|
|
json_t *elm;
|
|
|
|
|
|
|
|
switch (json_typeof(cfg)) {
|
|
|
|
case JSON_STRING:
|
|
|
|
str = json_string_value(cfg);
|
2020-08-25 21:00:52 +02:00
|
|
|
node = vlist_lookup_name<struct vnode>(all, str);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (!node)
|
|
|
|
goto invalid2;
|
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_push(list, node);
|
2016-11-20 12:59:37 -05:00
|
|
|
break;
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
case JSON_ARRAY:
|
|
|
|
json_array_foreach(cfg, index, elm) {
|
|
|
|
if (!json_is_string(elm))
|
|
|
|
goto invalid;
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
node = vlist_lookup_name<struct vnode>(all, json_string_value(elm));
|
2017-08-03 00:19:27 +02:00
|
|
|
if (!node)
|
2017-09-18 22:56:40 +02:00
|
|
|
goto invalid;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_push(list, node);
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-03 00:19:27 +02:00
|
|
|
goto invalid;
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
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++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode *n = (struct vnode *) vlist_at(all, i);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
strcatf(&allstr, " %s", node_name_short(n));
|
|
|
|
}
|
|
|
|
|
2018-08-09 08:44:45 +02:00
|
|
|
error("Unknown node %s. Choose of one of: %s", str, allstr);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2017-08-28 14:35:50 +02:00
|
|
|
return 0;
|
2017-09-02 14:20:38 +02:00
|
|
|
}
|
2019-02-12 17:54:08 +01:00
|
|
|
|
2019-02-24 11:10:44 +01:00
|
|
|
bool node_is_valid_name(const char *name)
|
2019-02-12 17:54:08 +01:00
|
|
|
{
|
2020-09-03 15:06:50 +02:00
|
|
|
std::regex re(RE_NODE_NAME);
|
2019-02-12 17:54:08 +01:00
|
|
|
|
2020-08-25 20:24:46 +02:00
|
|
|
return std::regex_match(name, re);
|
2019-02-24 11:10:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
bool node_is_enabled(const struct vnode *n)
|
2019-02-24 11:08:15 +01:00
|
|
|
{
|
|
|
|
return n->enabled;
|
|
|
|
}
|
|
|
|
|
2020-10-21 20:56:51 +02:00
|
|
|
struct vlist * node_input_signals(struct vnode *n)
|
2019-03-08 15:21:01 +01:00
|
|
|
{
|
2020-10-21 20:56:51 +02:00
|
|
|
return node_direction_get_signals(&n->in);
|
|
|
|
}
|
2019-03-08 15:21:01 +01:00
|
|
|
|
2020-10-21 20:56:51 +02:00
|
|
|
struct vlist * node_output_signals(struct vnode *n)
|
|
|
|
{
|
|
|
|
if (n->output_path)
|
|
|
|
return path_output_signals(n->output_path);
|
|
|
|
|
|
|
|
return nullptr;
|
2019-02-12 17:54:08 +01:00
|
|
|
}
|
2020-08-25 20:24:18 +02:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
json_t * node_to_json(struct vnode *n)
|
2020-08-25 20:24:18 +02:00
|
|
|
{
|
|
|
|
struct vlist *output_signals;
|
|
|
|
|
|
|
|
json_t *json_node;
|
|
|
|
json_t *json_signals_in = nullptr;
|
|
|
|
json_t *json_signals_out = nullptr;
|
|
|
|
|
|
|
|
char uuid[37];
|
|
|
|
uuid_unparse(n->uuid, uuid);
|
|
|
|
|
|
|
|
json_signals_in = signal_list_to_json(&n->in.signals);
|
|
|
|
|
|
|
|
output_signals = node_output_signals(n);
|
|
|
|
if (output_signals)
|
|
|
|
json_signals_out = signal_list_to_json(output_signals);
|
|
|
|
|
|
|
|
json_node = json_pack("{ s: s, s: s, s: s, s: i, s: { s: i, s: o? }, s: { s: i, s: o? } }",
|
|
|
|
"name", node_name_short(n),
|
|
|
|
"uuid", uuid,
|
|
|
|
"state", state_print(n->state),
|
|
|
|
"affinity", n->affinity,
|
|
|
|
"in",
|
|
|
|
"vectorize", n->in.vectorize,
|
|
|
|
"signals", json_signals_in,
|
|
|
|
"out",
|
|
|
|
"vectorize", n->out.vectorize,
|
|
|
|
"signals", json_signals_out
|
|
|
|
);
|
|
|
|
|
|
|
|
if (n->stats)
|
|
|
|
json_object_set_new(json_node, "stats", n->stats->toJson());
|
|
|
|
|
|
|
|
/* Add all additional fields of node here.
|
|
|
|
* This can be used for metadata */
|
|
|
|
json_object_update(json_node, n->cfg);
|
|
|
|
|
|
|
|
return json_node;
|
2020-08-28 09:43:34 +02:00
|
|
|
}
|