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>
|
2020-10-16 17:31:32 +02:00
|
|
|
#include <villas/path_source.h>
|
|
|
|
#include <villas/path_destination.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>
|
2020-10-15 14:47:43 +02:00
|
|
|
#include <villas/timing.h>
|
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>
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/if.hpp>
|
2017-03-12 17:01:24 -03:00
|
|
|
|
2019-01-21 22:14:51 +01:00
|
|
|
#ifdef WITH_NETEM
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/nl.hpp>
|
2019-01-21 22:14:51 +01:00
|
|
|
#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
|
|
|
|
2020-10-20 22:11:08 +02:00
|
|
|
typedef char uuid_string_t[37];
|
|
|
|
|
2018-07-03 21:51:48 +02:00
|
|
|
SuperNode::SuperNode() :
|
2019-06-23 16:13:23 +02:00
|
|
|
state(State::INITIALIZED),
|
2020-10-15 14:47:43 +02:00
|
|
|
idleStop(-1),
|
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),
|
2020-09-13 08:14:41 +02:00
|
|
|
statsRate(1.0),
|
2020-10-15 14:47:43 +02:00
|
|
|
task(CLOCK_REALTIME),
|
|
|
|
started(time_now())
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-10-15 14:47:43 +02:00
|
|
|
char hname[128];
|
|
|
|
ret = gethostname(hname, sizeof(hname));
|
|
|
|
if (ret)
|
|
|
|
throw SystemError("Failed to determine hostname");
|
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
/* Default UUID is derived from hostname */
|
2020-10-15 14:47:43 +02:00
|
|
|
uuid_generate_from_str(uuid, hname);
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = vlist_init(&nodes);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize list");
|
|
|
|
|
|
|
|
ret = vlist_init(&paths);
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize list");
|
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef WITH_NETEM
|
2020-09-13 11:01:20 +02:00
|
|
|
kernel::nl::init(); /* Fill link cache */
|
2019-01-21 15:50:18 +01:00
|
|
|
#endif /* WITH_NETEM */
|
|
|
|
|
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
|
|
|
{
|
2020-09-10 17:35:35 +02:00
|
|
|
config.root = 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
|
|
|
}
|
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
void SuperNode::parse(json_t *root)
|
2017-03-12 17:01:24 -03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state != State::STARTED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2020-10-15 14:47:43 +02:00
|
|
|
const char *uuid_str = nullptr;
|
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
json_t *json_nodes = nullptr;
|
|
|
|
json_t *json_paths = nullptr;
|
|
|
|
json_t *json_logging = nullptr;
|
2020-09-10 17:35:35 +02:00
|
|
|
json_t *json_http = nullptr;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
|
|
|
json_error_t err;
|
|
|
|
|
2020-10-15 14:47:43 +02:00
|
|
|
idleStop = 1;
|
|
|
|
|
|
|
|
ret = json_unpack_ex(root, &err, 0, "{ s?: F, s?: o, s?: o, s?: o, s?: o, s?: i, s?: i, s?: i, s?: b, s?: s }",
|
2020-09-13 08:14:41 +02:00
|
|
|
"stats", &statsRate,
|
2020-09-10 17:35:35 +02:00
|
|
|
"http", &json_http,
|
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,
|
2020-10-15 14:47:43 +02:00
|
|
|
"idle_stop", &idleStop,
|
|
|
|
"uuid", &uuid_str
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
2020-09-10 17:35:35 +02:00
|
|
|
throw ConfigError(root, err, "node-config", "Unpacking top-level config failed");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2020-10-15 14:47:43 +02:00
|
|
|
if (uuid_str) {
|
|
|
|
ret = uuid_parse(uuid_str, uuid);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(root, "node-config-uuid", "Failed to parse UUID: {}", uuid_str);
|
|
|
|
}
|
|
|
|
|
2017-07-25 11:57:01 +02:00
|
|
|
#ifdef WITH_WEB
|
2020-09-10 17:35:35 +02:00
|
|
|
if (json_http)
|
|
|
|
web.parse(json_http);
|
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) {
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode_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)
|
2020-09-10 17:35:35 +02:00
|
|
|
throw ConfigError(root, err, "node-config-node-type", "Failed to parse type of node '{}'", name);
|
2017-03-13 23:51:38 -03:00
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
json_object_set(json_node, "name", json_string(name));
|
|
|
|
|
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-08-25 21:00:52 +02:00
|
|
|
auto *n = new struct vnode;
|
2020-01-25 17:39:06 +01:00
|
|
|
if (!n)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2020-01-25 17:39:06 +01: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
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
ret = node_parse(n, json_node, uuid);
|
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;
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!p)
|
|
|
|
throw MemoryAllocationError();
|
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
|
|
|
|
2020-10-16 11:08:40 +02:00
|
|
|
ret = path_parse(p, json_path, &nodes, uuid);
|
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++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
auto *n = (struct vnode *) 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
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
path_check(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++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
auto *n = (struct vnode *) 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;
|
|
|
|
|
2020-09-13 11:01:20 +02:00
|
|
|
for (auto *i : interfaces) {
|
|
|
|
ret = i->start();
|
2019-01-21 15:44:39 +01:00
|
|
|
if (ret)
|
2020-09-13 11:01:20 +02:00
|
|
|
throw RuntimeError("Failed to start network interface: {}", i->getName());
|
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++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
auto *n = (struct vnode *) 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()
|
|
|
|
{
|
2020-09-10 13:20:50 +02:00
|
|
|
int ret;
|
2019-02-24 11:13:28 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < vlist_length(&nodes); i++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
auto *n = (struct vnode *) vlist_at(&nodes, i);
|
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;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = path_prepare(p, &nodes);
|
2019-02-24 11:13:28 +01:00
|
|
|
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();
|
|
|
|
|
2020-09-10 17:35:35 +02:00
|
|
|
for (size_t i = 0; i < vlist_length(&nodes); i++) {
|
|
|
|
auto *n = (struct vnode *) vlist_at(&nodes, i);
|
|
|
|
if (vlist_length(&n->sources) == 0 &&
|
|
|
|
vlist_length(&n->destinations) == 0) {
|
2020-09-13 11:01:20 +02:00
|
|
|
logger->info("Node {} is not used by any path. Disabling...", node_name(n));
|
2020-09-10 17:35:35 +02:00
|
|
|
n->enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-13 08:14:41 +02:00
|
|
|
if (statsRate > 0) // A rate <0 will disable the periodic stats
|
|
|
|
task.setRate(statsRate);
|
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++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
auto *n = (struct vnode *) 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;
|
|
|
|
|
2020-09-13 11:01:20 +02:00
|
|
|
for (auto *i : interfaces) {
|
|
|
|
ret = i->stop();
|
2019-01-21 15:44:39 +01:00
|
|
|
if (ret)
|
2020-09-13 11:01:20 +02:00
|
|
|
throw RuntimeError("Failed to stop interface: {}", i->getName());
|
2019-01-21 15:44:39 +01:00
|
|
|
}
|
|
|
|
#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()
|
|
|
|
{
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret __attribute__((unused));
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state != State::STARTED);
|
2018-07-03 21:51:48 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = vlist_destroy(&paths, (dtor_cb_t) path_destroy, true);
|
|
|
|
ret = vlist_destroy(&nodes, (dtor_cb_t) node_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++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
auto *n = (struct vnode *) 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
|
|
|
|
2020-10-15 14:47:43 +02:00
|
|
|
if (idleStop > 0 && 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
|
2020-10-20 22:11:08 +02:00
|
|
|
static void
|
|
|
|
set_attr(void *ptr, const std::string &key, const std::string &value, bool html = false) {
|
|
|
|
Agraph_t *g = agraphof(ptr);
|
|
|
|
|
|
|
|
char *d = (char *) "";
|
|
|
|
char *k = (char *) key.c_str();
|
|
|
|
char *v = (char *) value.c_str();
|
|
|
|
char *vd = html ? agstrdup_html(g, v) : agstrdup(g, v);
|
|
|
|
|
|
|
|
agsafeset(ptr, k, vd, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-30 14:46:55 +02:00
|
|
|
graph_t * SuperNode::getGraph()
|
|
|
|
{
|
|
|
|
Agraph_t *g;
|
2020-10-16 17:31:32 +02:00
|
|
|
Agnode_t *m;
|
2019-06-30 14:46:55 +02:00
|
|
|
|
2020-06-08 02:25:07 +02:00
|
|
|
g = agopen((char *) "g", Agdirected, 0);
|
2020-10-16 17:31:32 +02:00
|
|
|
|
|
|
|
std::map<struct vnode *, Agnode_t *> nodeMap;
|
|
|
|
|
2020-10-20 22:11:08 +02:00
|
|
|
uuid_string_t uuid_str;
|
|
|
|
|
2020-10-16 17:31:32 +02:00
|
|
|
for (size_t i = 0; i < vlist_length(&nodes); i++) {
|
|
|
|
auto *n = (struct vnode *) vlist_at(&nodes, i);
|
|
|
|
|
|
|
|
nodeMap[n] = agnode(g, (char *) node_name_short(n), 1);
|
2020-10-20 22:11:08 +02:00
|
|
|
|
|
|
|
uuid_unparse(n->uuid, uuid_str);
|
|
|
|
|
|
|
|
set_attr(nodeMap[n], "shape", "ellipse");
|
|
|
|
set_attr(nodeMap[n], "tooltip", fmt::format("type={}, uuid={}", plugin_name(node_type(n)), uuid_str));
|
|
|
|
// set_attr(nodeMap[n], "fixedsize", "true");
|
|
|
|
// set_attr(nodeMap[n], "width", "0.15");
|
|
|
|
// set_attr(nodeMap[n], "height", "0.15");
|
2020-10-16 17:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vlist_length(&paths); i++) {
|
|
|
|
auto *p = (struct vpath *) vlist_at(&paths, i);
|
|
|
|
|
|
|
|
auto name = fmt::format("path_{}", i);
|
|
|
|
|
|
|
|
m = agnode(g, (char *) name.c_str(), 1);
|
|
|
|
|
2020-10-20 22:11:08 +02:00
|
|
|
uuid_unparse(p->uuid, uuid_str);
|
|
|
|
|
|
|
|
set_attr(m, "shape", "box");
|
|
|
|
set_attr(m, "tooltip", fmt::format("uuid={}", uuid_str));
|
|
|
|
|
2020-10-16 17:31:32 +02:00
|
|
|
for (size_t j = 0; j < vlist_length(&p->sources); j++) {
|
|
|
|
auto *ps = (struct vpath_source *) vlist_at(&p->sources, j);
|
|
|
|
|
|
|
|
agedge(g, nodeMap[ps->node], m, nullptr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t j = 0; j < vlist_length(&p->destinations); j++) {
|
|
|
|
auto *pd = (struct vpath_destination *) vlist_at(&p->destinations, j);
|
|
|
|
|
|
|
|
agedge(g, m, nodeMap[pd->node], nullptr, 1);
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 14:46:55 +02:00
|
|
|
|
|
|
|
return g;
|
2020-10-16 17:31:32 +02:00
|
|
|
|
2019-06-30 14:46:55 +02:00
|
|
|
}
|
|
|
|
#endif /* WITH_GRAPHVIZ */
|