2017-08-03 00:19:27 +02:00
|
|
|
/** The super node object holding the state of the application.
|
2017-03-12 17:01:24 -03: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/>.
|
2017-03-12 17:01:24 -03:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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>
|
2019-04-23 13:09:50 +02:00
|
|
|
#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>
|
2019-01-21 15:44:39 +01:00
|
|
|
#include <villas/kernel/if.h>
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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),
|
2019-01-30 12:00:53 +01:00
|
|
|
#ifdef WITH_API
|
2018-10-20 14:20:06 +02:00
|
|
|
api(this),
|
2019-04-05 02:22:53 +02:00
|
|
|
#endif
|
2019-01-30 12:00:53 +01:00
|
|
|
#ifdef WITH_WEB
|
2019-04-05 19:28:42 +02:00
|
|
|
#ifdef WITH_API
|
2018-10-25 17:02:59 +01:00
|
|
|
web(&api),
|
2019-04-05 19:28:42 +02:00
|
|
|
#else
|
|
|
|
web(),
|
|
|
|
#endif
|
2019-01-30 12:00:53 +01:00
|
|
|
#endif
|
2019-03-26 07:03:57 +01:00
|
|
|
priority(0),
|
|
|
|
affinity(0),
|
2020-03-04 13:06:28 +01:00
|
|
|
hugepages(DEFAULT_NR_HUGEPAGES),
|
|
|
|
task(CLOCK_REALTIME)
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
nodes.state = State::DESTROYED;
|
|
|
|
paths.state = State::DESTROYED;
|
|
|
|
interfaces.state = State::DESTROYED;
|
2018-12-02 03:03:03 +01:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_init(&nodes);
|
|
|
|
vlist_init(&paths);
|
2019-01-21 15:44:39 +01:00
|
|
|
vlist_init(&interfaces);
|
2018-08-17 12:41:10 +02:00
|
|
|
|
2019-01-21 15:50:18 +01: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");
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
void SuperNode::parse(const std::string &u)
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
2019-03-26 07:03:57 +01:00
|
|
|
config.load(u);
|
2017-03-29 04:26:57 +02:00
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
parse(config.root);
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
|
2019-03-26 07:03:57 +01:00
|
|
|
void SuperNode::parse(json_t *cfg)
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
|
|
|
int ret;
|
2018-08-27 11:21:57 +02:00
|
|
|
const char *nme = nullptr;
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state != State::STARTED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
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
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2019-03-26 07:11:27 +01:00
|
|
|
throw ConfigError(cfg, err, "node-config");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2018-10-20 14:20:06 +02:00
|
|
|
if (nme)
|
|
|
|
name = nme;
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2017-07-25 11:57:01 +02:00
|
|
|
#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);
|
2017-07-25 11:57:01 +02:00
|
|
|
#endif /* WITH_WEB */
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
if (json_logging)
|
2018-10-20 14:20:06 +02:00
|
|
|
logging.parse(json_logging);
|
2017-03-12 17:01:24 -03:00
|
|
|
|
|
|
|
/* Parse nodes */
|
2017-10-16 08:08:35 +02:00
|
|
|
if (json_nodes) {
|
|
|
|
if (!json_is_object(json_nodes))
|
2018-12-02 03:01:13 +01:00
|
|
|
throw ConfigError(json_nodes, "node-config-nodes", "Setting 'nodes' must be a group with node name => group mappings.");
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
const char *name;
|
2017-10-16 08:08:35 +02:00
|
|
|
json_t *json_node;
|
|
|
|
json_object_foreach(json_nodes, name, json_node) {
|
2018-05-13 13:52:02 +02:00
|
|
|
struct node_type *nt;
|
2017-03-12 17:01:24 -03:00
|
|
|
const char *type;
|
|
|
|
|
2019-02-12 17:54:08 +01:00
|
|
|
ret = node_is_valid_name(name);
|
2019-02-24 11:10:44 +01:00
|
|
|
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);
|
2017-08-03 00:19:27 +02:00
|
|
|
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
|
|
|
|
2018-05-13 13:52:02 +02: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
|
|
|
|
2020-01-21 16:26:51 +01: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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-13 13:52:02 +02:00
|
|
|
ret = node_init(n, nt);
|
2017-03-12 17:01:24 -03:00
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to initialize node");
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_push(&nodes, n);
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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");
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
ret = path_init(p);
|
2017-03-12 17:01:24 -03:00
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to initialize path");
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
ret = path_parse(p, json_path, &nodes);
|
2017-03-12 17:01:24 -03:00
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to parse path");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
vlist_push(&paths, p);
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
if (p->reverse) {
|
2019-02-12 17:54:35 +01:00
|
|
|
/* Only simple paths can be reversed */
|
|
|
|
ret = path_is_simple(p);
|
2019-02-24 11:10:44 +01:00
|
|
|
if (!ret)
|
2019-02-12 17:54:35 +01:00
|
|
|
throw RuntimeError("Complex paths can not be reversed!");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-02-12 17:54:35 +01:00
|
|
|
/* 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);
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2019-02-12 17:54:35 +01:00
|
|
|
goto parse;
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::PARSED;
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
|
2019-03-22 13:44:42 +01:00
|
|
|
void SuperNode::check()
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::INITIALIZED || state == State::PARSED || state == State::CHECKED);
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
ret = node_check(n);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Invalid configuration for node {}", node_name(n));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
ret = path_check(p);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Invalid configuration for path {}", path_name(p));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::CHECKED;
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:11:30 +01:00
|
|
|
void SuperNode::startNodeTypes()
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
2017-09-19 04:18:05 +02:00
|
|
|
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);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-23 13:14:47 +02:00
|
|
|
ret = node_type_start(n->_vt, this);
|
2017-09-19 04:18:05 +02:00
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to start node-type: {}", node_type_name(n->_vt));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-01-21 22:11:30 +01:00
|
|
|
void SuperNode::startInterfaces()
|
|
|
|
{
|
2019-01-21 15:44:39 +01:00
|
|
|
#ifdef WITH_NETEM
|
2019-01-21 22:11:30 +01:00
|
|
|
int ret;
|
|
|
|
|
2019-01-21 15:44:39 +01:00
|
|
|
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 15:44:39 +01:00
|
|
|
|
2019-01-21 17:28:01 +01:00
|
|
|
ret = if_start(j);
|
2019-01-21 15:44:39 +01:00
|
|
|
if (ret)
|
2019-01-21 17:28:01 +01:00
|
|
|
throw RuntimeError("Failed to initialize network interface: {}", if_name(j));
|
2019-01-21 15:44:39 +01:00
|
|
|
}
|
|
|
|
#endif /* WITH_NETEM */
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuperNode::startNodes()
|
|
|
|
{
|
|
|
|
int ret;
|
2019-01-21 15:44:39 +01:00
|
|
|
|
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);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
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));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuperNode::startPaths()
|
|
|
|
{
|
|
|
|
int ret;
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-02-24 11:13:28 +01:00
|
|
|
if (!path_is_enabled(p))
|
|
|
|
continue;
|
2017-09-19 04:18:05 +02:00
|
|
|
|
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;
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
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));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 11:13:28 +01:00
|
|
|
void SuperNode::prepare()
|
2019-01-21 22:11:30 +01:00
|
|
|
{
|
2019-02-11 16:32:39 +01:00
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::CHECKED);
|
2019-01-21 22:11:30 +01:00
|
|
|
|
2019-02-12 17:06:09 +01:00
|
|
|
ret = memory_init(hugepages);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize memory system");
|
|
|
|
|
2019-01-21 22:11:30 +01:00
|
|
|
kernel::rt::init(priority, affinity);
|
|
|
|
|
2019-03-09 00:32:22 +01:00
|
|
|
prepareNodes();
|
|
|
|
preparePaths();
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::PREPARED;
|
2019-03-09 00:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuperNode::start()
|
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::PREPARED);
|
2019-03-09 00:32:22 +01:00
|
|
|
|
2019-01-21 22:11:30 +01:00
|
|
|
#ifdef WITH_API
|
|
|
|
api.start();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_WEB
|
|
|
|
web.start();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
startNodeTypes();
|
|
|
|
startInterfaces();
|
|
|
|
startNodes();
|
|
|
|
startPaths();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
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;
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:11:30 +01:00
|
|
|
void SuperNode::stopPaths()
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
2017-04-07 17:44:20 +02:00
|
|
|
int ret;
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-04-07 17:44:20 +02:00
|
|
|
ret = path_stop(p);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to stop path: {}", path_name(p));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuperNode::stopNodes()
|
|
|
|
{
|
|
|
|
int ret;
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-04-07 17:44:20 +02:00
|
|
|
ret = node_stop(n);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to stop node: {}", node_name(n));
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuperNode::stopNodeTypes()
|
|
|
|
{
|
|
|
|
int ret;
|
2017-03-12 17:01:24 -03:00
|
|
|
|
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);
|
2017-04-07 17:44:20 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (p->type == PluginType::NODE) {
|
2017-04-07 17:44:20 +02:00
|
|
|
ret = node_type_stop(&p->node);
|
|
|
|
if (ret)
|
2018-12-02 03:01:13 +01:00
|
|
|
throw RuntimeError("Failed to stop node-type: {}", node_type_name(&p->node));
|
2017-04-07 17:44:20 +02:00
|
|
|
}
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2019-01-21 22:11:30 +01:00
|
|
|
void SuperNode::stopInterfaces()
|
|
|
|
{
|
2019-01-21 15:44:39 +01:00
|
|
|
#ifdef WITH_NETEM
|
2019-01-21 22:11:30 +01:00
|
|
|
int ret;
|
|
|
|
|
2019-01-21 15:44:39 +01:00
|
|
|
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 */
|
2019-01-21 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SuperNode::stop()
|
|
|
|
{
|
|
|
|
stopNodes();
|
2019-03-26 17:01:55 +01:00
|
|
|
stopPaths();
|
2019-01-21 22:11:30 +01:00
|
|
|
stopNodeTypes();
|
|
|
|
stopInterfaces();
|
2019-01-21 15:44:39 +01:00
|
|
|
|
2017-07-25 11:57:01 +02:00
|
|
|
#ifdef WITH_API
|
2018-10-20 14:20:06 +02:00
|
|
|
api.stop();
|
2017-08-10 13:35:08 +02:00
|
|
|
#endif
|
2019-04-05 20:57:34 +02:00
|
|
|
|
2017-08-10 13:35:08 +02:00
|
|
|
#ifdef WITH_WEB
|
2018-10-20 14:20:06 +02:00
|
|
|
web.stop();
|
2017-07-25 11:57:01 +02:00
|
|
|
#endif
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STOPPED;
|
2017-03-12 17:01:24 -03:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
void SuperNode::run()
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
2019-02-11 16:33:14 +01:00
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
while (state == State::STARTED) {
|
2020-03-04 13:06:28 +01:00
|
|
|
task.wait();
|
2019-02-11 16:33:14 +01:00
|
|
|
|
|
|
|
ret = periodic();
|
|
|
|
if (ret)
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STOPPING;
|
2019-02-11 16:33:14 +01:00
|
|
|
}
|
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
|
|
|
|
2019-01-21 15:44:39 +01: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-08-20 19:06:24 +02:00
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
int SuperNode::periodic()
|
2018-08-20 19:06:24 +02:00
|
|
|
{
|
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);
|
2018-08-20 19:06:24 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (p->state == State::STARTED) {
|
2019-02-11 16:39:30 +01:00
|
|
|
started++;
|
2018-08-20 19:06:24 +02:00
|
|
|
|
2019-02-11 16:39:30 +01:00
|
|
|
#ifdef WITH_HOOKS
|
2019-03-26 17:01:55 +01:00
|
|
|
hook_list_periodic(&p->hooks);
|
2019-02-11 16:39:30 +01:00
|
|
|
#endif /* WITH_HOOKS */
|
2018-08-20 19:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2018-08-20 19:06:24 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (n->state == State::STARTED) {
|
2019-02-11 16:39:30 +01:00
|
|
|
#ifdef WITH_HOOKS
|
2019-03-26 17:01:55 +01:00
|
|
|
hook_list_periodic(&n->in.hooks);
|
|
|
|
hook_list_periodic(&n->out.hooks);
|
2019-02-11 16:39:30 +01:00
|
|
|
#endif /* WITH_HOOKS */
|
2019-03-26 17:01:55 +01:00
|
|
|
}
|
2018-08-20 19:06:24 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-20 19:06:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-06-30 14:46:55 +02:00
|
|
|
|
|
|
|
#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);
|
2019-06-30 14:46:55 +02:00
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
#endif /* WITH_GRAPHVIZ */
|