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

542 lines
11 KiB
C++
Raw Normal View History

/** The super node object holding the state of the application.
*
* @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/>.
*********************************************************************************/
#include <cstdlib>
#include <cstring>
2018-10-20 14:20:06 +02:00
#include <villas/super_node.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/node.h>
#include <villas/path.h>
#include <villas/utils.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/list.h>
2019-04-23 13:12:04 +02:00
#include <villas/hook_list.hpp>
2020-03-04 13:07:20 +01:00
#include <villas/advio.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/plugin.h>
#include <villas/memory.h>
2019-03-26 07:01:10 +01:00
#include <villas/config_helper.hpp>
2018-10-20 14:20:06 +02:00
#include <villas/log.hpp>
2019-03-26 07:11:27 +01:00
#include <villas/node/exceptions.hpp>
2018-10-20 14:20:06 +02:00
#include <villas/kernel/rt.hpp>
#include <villas/kernel/if.h>
2019-01-21 22:14:51 +01:00
#ifdef WITH_NETEM
#include <villas/kernel/nl.h>
#endif
2018-10-20 14:20:06 +02:00
using namespace villas;
2018-07-03 21:51:48 +02:00
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2018-07-03 21:51:48 +02:00
SuperNode::SuperNode() :
2019-06-23 16:13:23 +02:00
state(State::INITIALIZED),
2019-02-12 15:07:50 +01:00
idleStop(false),
#ifdef WITH_API
2018-10-20 14:20:06 +02:00
api(this),
2019-04-05 02:22:53 +02:00
#endif
#ifdef WITH_WEB
#ifdef WITH_API
web(&api),
#else
web(),
#endif
#endif
priority(0),
affinity(0),
2020-03-04 13:06:28 +01:00
hugepages(DEFAULT_NR_HUGEPAGES),
task(CLOCK_REALTIME)
{
2019-06-23 16:13:23 +02:00
nodes.state = State::DESTROYED;
paths.state = State::DESTROYED;
interfaces.state = State::DESTROYED;
2019-01-07 10:28:55 +01:00
vlist_init(&nodes);
vlist_init(&paths);
vlist_init(&interfaces);
2018-08-17 12:41:10 +02:00
#ifdef WITH_NETEM
nl_init(); /* Fill link cache */
#endif /* WITH_NETEM */
2018-10-20 14:20:06 +02:00
char hname[128];
gethostname(hname, 128);
name = hname;
2019-01-17 20:28:20 +01:00
2019-01-21 10:34:36 +01:00
logger = logging.get("super_node");
}
void SuperNode::parse(const std::string &u)
{
config.load(u);
parse(config.root);
}
void SuperNode::parse(json_t *cfg)
{
int ret;
2018-08-27 11:21:57 +02:00
const char *nme = nullptr;
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2018-08-27 11:21:57 +02:00
json_t *json_nodes = nullptr;
json_t *json_paths = nullptr;
json_t *json_logging = nullptr;
json_t *json_web = nullptr;
2020-01-25 17:39:06 +01:00
json_t *json_ethercat = nullptr;
json_error_t err;
2019-02-12 15:07:50 +01:00
idleStop = true;
2020-01-25 17:39:06 +01:00
ret = json_unpack_ex(cfg, &err, JSON_STRICT, "{ s?: o, s?: o, s?: o, s?: o, s?: o, s?: i, s?: i, s?: i, s?: s, s?: b }",
2017-10-16 08:08:35 +02:00
"http", &json_web,
2020-01-25 17:39:06 +01:00
"ethercat", &json_ethercat,
2017-10-16 08:08:35 +02:00
"logging", &json_logging,
"nodes", &json_nodes,
"paths", &json_paths,
2018-07-03 21:51:48 +02:00
"hugepages", &hugepages,
"affinity", &affinity,
"priority", &priority,
2019-02-12 15:07:50 +01:00
"name", &nme,
"idle_stop", &idleStop
);
if (ret)
2019-03-26 07:11:27 +01:00
throw ConfigError(cfg, err, "node-config");
2018-10-20 14:20:06 +02:00
if (nme)
name = nme;
#ifdef WITH_WEB
2017-10-16 08:08:35 +02:00
if (json_web)
2018-10-20 14:20:06 +02:00
web.parse(json_web);
#endif /* WITH_WEB */
2017-10-16 08:08:35 +02:00
if (json_logging)
2018-10-20 14:20:06 +02:00
logging.parse(json_logging);
/* Parse nodes */
2017-10-16 08:08:35 +02:00
if (json_nodes) {
if (!json_is_object(json_nodes))
throw ConfigError(json_nodes, "node-config-nodes", "Setting 'nodes' must be a group with node name => group mappings.");
const char *name;
2017-10-16 08:08:35 +02:00
json_t *json_node;
json_object_foreach(json_nodes, name, json_node) {
struct node_type *nt;
const char *type;
2019-02-12 17:54:08 +01:00
ret = node_is_valid_name(name);
if (!ret)
2019-02-12 17:54:08 +01:00
throw RuntimeError("Invalid name for node: {}", name);
2017-10-16 08:08:35 +02:00
ret = json_unpack_ex(json_node, &err, 0, "{ s: s }", "type", &type);
if (ret)
2019-03-26 07:11:27 +01:00
throw ConfigError(cfg, err, "node-config-node-type", "Failed to parse type of node '{}'", name);
2017-03-13 23:51:38 -03:00
nt = node_type_lookup(type);
if (!nt)
2019-03-26 07:11:27 +01:00
throw ConfigError(json_node, "node-config-node-type", "Invalid node type: {}", type);
2017-03-13 23:51:38 -03:00
auto *n = new struct node;
2020-01-25 17:39:06 +01:00
if (!n)
throw RuntimeError("Failed to allocate memory");
n->state = State::DESTROYED;
n->in.state = State::DESTROYED;
n->out.state = State::DESTROYED;
ret = node_init(n, nt);
if (ret)
throw RuntimeError("Failed to initialize node");
2017-10-16 08:08:35 +02:00
ret = node_parse(n, json_node, name);
2019-03-26 07:11:27 +01:00
if (ret) {
auto config_id = fmt::format("node-config-node-{}", type);
throw ConfigError(json_node, config_id, "Failed to parse configuration of node '{}'", name);
}
2019-01-07 10:28:55 +01:00
vlist_push(&nodes, n);
}
}
/* Parse paths */
2017-10-16 08:08:35 +02:00
if (json_paths) {
if (!json_is_array(json_paths))
2018-10-20 14:20:06 +02:00
logger->warn("Setting 'paths' must be a list of objects");
2018-08-07 18:40:32 +02:00
size_t i;
2017-10-16 08:08:35 +02:00
json_t *json_path;
2018-08-07 18:40:32 +02:00
json_array_foreach(json_paths, i, json_path) {
2020-06-08 02:25:07 +02:00
parse: auto *p = new vpath;
ret = path_init(p);
if (ret)
throw RuntimeError("Failed to initialize path");
2018-07-03 21:51:48 +02:00
ret = path_parse(p, json_path, &nodes);
if (ret)
throw RuntimeError("Failed to parse path");
2019-01-07 10:28:55 +01:00
vlist_push(&paths, p);
if (p->reverse) {
/* Only simple paths can be reversed */
ret = path_is_simple(p);
if (!ret)
throw RuntimeError("Complex paths can not be reversed!");
/* Parse a second time with in/out reversed */
json_path = json_copy(json_path);
json_t *json_in = json_object_get(json_path, "in");
json_t *json_out = json_object_get(json_path, "out");
if (json_equal(json_in, json_out))
throw RuntimeError("Can not reverse path with identical in/out nodes!");
json_object_set(json_path, "reverse", json_false());
json_object_set(json_path, "in", json_out);
json_object_set(json_path, "out", json_in);
goto parse;
}
}
}
2019-06-23 16:13:23 +02:00
state = State::PARSED;
}
void SuperNode::check()
{
int ret;
2019-06-23 16:13:23 +02:00
assert(state == State::INITIALIZED || state == State::PARSED || state == State::CHECKED);
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&nodes); i++) {
auto *n = (struct node *) vlist_at(&nodes, i);
ret = node_check(n);
if (ret)
throw RuntimeError("Invalid configuration for node {}", node_name(n));
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&paths); i++) {
2020-06-08 02:25:07 +02:00
auto *p = (struct vpath *) vlist_at(&paths, i);
ret = path_check(p);
if (ret)
throw RuntimeError("Invalid configuration for path {}", path_name(p));
}
2019-06-23 16:13:23 +02:00
state = State::CHECKED;
}
void SuperNode::startNodeTypes()
{
int ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&nodes); i++) {
auto *n = (struct node *) vlist_at(&nodes, i);
2019-04-23 13:14:47 +02:00
ret = node_type_start(n->_vt, this);
if (ret)
throw RuntimeError("Failed to start node-type: {}", node_type_name(n->_vt));
}
}
void SuperNode::startInterfaces()
{
#ifdef WITH_NETEM
int ret;
for (size_t i = 0; i < vlist_length(&interfaces); i++) {
2019-01-21 17:28:01 +01:00
auto *j = (struct interface *) vlist_at(&interfaces, i);
2019-01-21 17:28:01 +01:00
ret = if_start(j);
if (ret)
2019-01-21 17:28:01 +01:00
throw RuntimeError("Failed to initialize network interface: {}", if_name(j));
}
#endif /* WITH_NETEM */
}
void SuperNode::startNodes()
{
int ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&nodes); i++) {
auto *n = (struct node *) vlist_at(&nodes, i);
2019-02-24 11:13:28 +01:00
if (!node_is_enabled(n))
continue;
2018-08-20 18:31:27 +02:00
2019-02-24 11:13:28 +01:00
ret = node_start(n);
if (ret)
throw RuntimeError("Failed to start node: {}", node_name(n));
}
}
void SuperNode::startPaths()
{
int ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&paths); i++) {
2020-06-08 02:25:07 +02:00
auto *p = (struct vpath *) vlist_at(&paths, i);
2019-02-24 11:13:28 +01:00
if (!path_is_enabled(p))
continue;
2019-02-24 11:13:28 +01:00
ret = path_start(p);
if (ret)
throw RuntimeError("Failed to start path: {}", path_name(p));
}
}
void SuperNode::prepareNodes()
{
int ret, refs;
for (size_t i = 0; i < vlist_length(&nodes); i++) {
auto *n = (struct node *) vlist_at(&nodes, i);
refs = vlist_count(&paths, (cmp_cb_t) path_uses_node, n);
if (refs <= 0) {
logger->warn("No path is using the node {}. Skipping...", node_name(n));
n->enabled = false;
}
2019-02-24 11:13:28 +01:00
if (!node_is_enabled(n))
continue;
ret = node_prepare(n);
if (ret)
throw RuntimeError("Failed to prepare node: {}", node_name(n));
}
}
void SuperNode::preparePaths()
{
int ret;
for (size_t i = 0; i < vlist_length(&paths); i++) {
2020-06-08 02:25:07 +02:00
auto *p = (struct vpath *) vlist_at(&paths, i);
2019-02-24 11:13:28 +01:00
if (!path_is_enabled(p))
continue;
ret = path_prepare(p);
if (ret)
throw RuntimeError("Failed to prepare path: {}", path_name(p));
}
}
2019-02-24 11:13:28 +01:00
void SuperNode::prepare()
{
2019-02-11 16:32:39 +01:00
int ret;
2019-06-23 16:13:23 +02:00
assert(state == State::CHECKED);
ret = memory_init(hugepages);
if (ret)
throw RuntimeError("Failed to initialize memory system");
kernel::rt::init(priority, affinity);
prepareNodes();
preparePaths();
2019-06-23 16:13:23 +02:00
state = State::PREPARED;
}
void SuperNode::start()
{
2019-06-23 16:13:23 +02:00
assert(state == State::PREPARED);
#ifdef WITH_API
api.start();
#endif
#ifdef WITH_WEB
web.start();
#endif
startNodeTypes();
startInterfaces();
startNodes();
startPaths();
2020-03-04 13:06:28 +01:00
task.setRate(1.0);
2018-10-20 14:20:06 +02:00
2019-06-23 13:35:42 +02:00
Stats::printHeader(Stats::Format::HUMAN);
2018-10-20 14:20:06 +02:00
2019-06-23 16:13:23 +02:00
state = State::STARTED;
}
void SuperNode::stopPaths()
{
int ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&paths); i++) {
2020-06-08 02:25:07 +02:00
auto *p = (struct vpath *) vlist_at(&paths, i);
ret = path_stop(p);
if (ret)
throw RuntimeError("Failed to stop path: {}", path_name(p));
}
}
void SuperNode::stopNodes()
{
int ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&nodes); i++) {
auto *n = (struct node *) vlist_at(&nodes, i);
ret = node_stop(n);
if (ret)
throw RuntimeError("Failed to stop node: {}", node_name(n));
}
}
void SuperNode::stopNodeTypes()
{
int ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&plugins); i++) {
auto *p = (struct plugin *) vlist_at(&plugins, i);
2019-06-23 16:13:23 +02:00
if (p->type == PluginType::NODE) {
ret = node_type_stop(&p->node);
if (ret)
throw RuntimeError("Failed to stop node-type: {}", node_type_name(&p->node));
}
}
}
void SuperNode::stopInterfaces()
{
#ifdef WITH_NETEM
int ret;
for (size_t j = 0; j < vlist_length(&interfaces); j++) {
struct interface *i = (struct interface *) vlist_at(&interfaces, j);
ret = if_stop(i);
if (ret)
throw RuntimeError("Failed to stop interface: {}", if_name(i));
}
#endif /* WITH_NETEM */
}
void SuperNode::stop()
{
stopNodes();
stopPaths();
stopNodeTypes();
stopInterfaces();
#ifdef WITH_API
2018-10-20 14:20:06 +02:00
api.stop();
#endif
#ifdef WITH_WEB
2018-10-20 14:20:06 +02:00
web.stop();
#endif
2019-06-23 16:13:23 +02:00
state = State::STOPPED;
}
2018-07-03 21:51:48 +02:00
void SuperNode::run()
{
int ret;
2019-06-23 16:13:23 +02:00
while (state == State::STARTED) {
2020-03-04 13:06:28 +01:00
task.wait();
ret = periodic();
if (ret)
2019-06-23 16:13:23 +02:00
state = State::STOPPING;
}
2018-07-03 21:51:48 +02:00
}
SuperNode::~SuperNode()
{
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2018-07-03 21:51:48 +02:00
vlist_destroy(&paths, (dtor_cb_t) path_destroy, true);
vlist_destroy(&nodes, (dtor_cb_t) node_destroy, true);
vlist_destroy(&interfaces, (dtor_cb_t) if_destroy, true);
2017-04-06 12:12:56 +02:00
}
2018-07-03 21:51:48 +02:00
int SuperNode::periodic()
{
2019-02-11 16:39:30 +01:00
int started = 0;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&paths); i++) {
2020-06-08 02:25:07 +02:00
auto *p = (struct vpath *) vlist_at(&paths, i);
2019-06-23 16:13:23 +02:00
if (p->state == State::STARTED) {
2019-02-11 16:39:30 +01:00
started++;
2019-02-11 16:39:30 +01:00
#ifdef WITH_HOOKS
hook_list_periodic(&p->hooks);
2019-02-11 16:39:30 +01:00
#endif /* WITH_HOOKS */
}
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&nodes); i++) {
auto *n = (struct node *) vlist_at(&nodes, i);
2019-06-23 16:13:23 +02:00
if (n->state == State::STARTED) {
2019-02-11 16:39:30 +01:00
#ifdef WITH_HOOKS
hook_list_periodic(&n->in.hooks);
hook_list_periodic(&n->out.hooks);
2019-02-11 16:39:30 +01:00
#endif /* WITH_HOOKS */
}
}
2019-02-11 16:39:30 +01:00
2019-06-23 16:13:23 +02:00
if (idleStop && state == State::STARTED && started == 0) {
2019-02-11 16:39:30 +01:00
info("No more active paths. Stopping super-node");
return -1;
}
return 0;
}
#ifdef WITH_GRAPHVIZ
graph_t * SuperNode::getGraph()
{
/* Create a simple digraph */
Agraph_t *g;
Agnode_t *n, *m;
2020-06-08 02:25:07 +02:00
g = agopen((char *) "g", Agdirected, 0);
n = agnode(g, (char *) "n", 1);
m = agnode(g, (char *) "m", 1);
agedge(g, n, m, 0, 1);
return g;
}
#endif /* WITH_GRAPHVIZ */