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

527 lines
10 KiB
C++
Raw Permalink Normal View History

/** Nodes.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2015-06-02 21:53:04 +02:00
*********************************************************************************/
2020-08-25 20:24:46 +02:00
#include <regex>
#include <cstring>
#include <cctype>
#include <openssl/md5.h>
#ifdef __linux__
#include <sys/types.h>
#include <sys/socket.h>
#endif
#include <villas/node/config.hpp>
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>
#include <villas/sample.hpp>
#include <villas/node.hpp>
#include <villas/node_list.hpp>
#include <villas/path.hpp>
#include <villas/utils.hpp>
2021-09-19 19:26:03 +02:00
#include <villas/uuid.hpp>
2019-04-23 13:11:08 +02:00
#include <villas/colors.hpp>
#include <villas/mapping.hpp>
#include <villas/timing.hpp>
#include <villas/signal.hpp>
#include <villas/node/memory.hpp>
2019-01-21 22:14:41 +01:00
#ifdef WITH_NETEM
#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;
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
Node::Node(const uuid_t &id, const std::string &name) :
logger(logging.get("node")),
sequence_init(0),
sequence(0),
in(NodeDirection::Direction::IN, this),
out(NodeDirection::Direction::OUT, this),
2019-02-15 09:46:26 +01:00
#ifdef __linux__
fwmark(-1),
2019-02-15 11:18:32 +01:00
#endif /* __linux__ */
#ifdef WITH_NETEM
tc_qdisc(nullptr),
tc_classifier(nullptr),
#endif /* WITH_NETEM */
state(State::INITIALIZED),
enabled(true),
config(nullptr),
name_short(name),
affinity(-1), /* all cores */
factory(nullptr)
{
if (uuid_is_null(id)) {
uuid_generate_random(uuid);
} else {
uuid_copy(uuid, id);
}
if (!name_short.empty()) {
name_long = fmt::format(CLR_RED("{}"), name_short);
}
else if (name_short.empty()) {
name_short = "<none>";
name_long = CLR_RED("<none>");
}
}
Node::~Node()
{
#ifdef WITH_NETEM
rtnl_qdisc_put(tc_qdisc);
rtnl_cls_put(tc_classifier);
#endif /* WITH_NETEM */
factory->instances.remove(this);
}
int Node::prepare()
2018-08-20 18:31:27 +02:00
{
int ret;
ret = in.prepare();
2019-03-15 17:19:28 +01:00
if (ret)
return ret;
ret = out.prepare();
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
state = State::PREPARED;
2019-02-24 11:13:28 +01:00
2018-08-20 18:31:27 +02:00
return 0;
}
int Node::parse(json_t *json)
2015-12-11 17:56:14 +01:00
{
assert(state == State::INITIALIZED ||
state == State::PARSED ||
state == State::CHECKED);
int ret, en = enabled, init_seq = -1;
json_error_t err;
2019-04-08 08:59:08 +02:00
json_t *json_netem = nullptr;
ret = json_unpack_ex(json, &err, 0, "{ s?: b, s?: i }",
"enabled", &en,
"initial_sequenceno", &init_seq
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
if (init_seq >= 0)
sequence_init = init_seq;
2019-02-15 09:46:26 +01:00
#ifdef __linux__
2019-02-15 11:18:32 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s?: { s?: o, s?: i } }",
"out",
2019-02-15 09:42:53 +01:00
"netem", &json_netem,
"fwmark", &fwmark
);
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
2019-02-15 09:46:26 +01:00
#endif /* __linux__ */
2016-06-08 22:38:21 +02:00
enabled = en;
if (json_netem) {
#ifdef WITH_NETEM
int enabled = 1;
ret = json_unpack_ex(json_netem, &err, 0, "{ s?: b }", "enabled", &enabled);
if (ret)
2019-03-26 15:36:24 +01:00
return ret;
if (enabled)
kernel::tc::netem_parse(&tc_qdisc, json_netem);
else
tc_qdisc = nullptr;
#endif /* WITH_NETEM */
}
2018-06-12 20:45:03 +02:00
struct {
const char *str;
struct NodeDirection *dir;
2018-06-12 20:45:03 +02:00
} dirs[] = {
{ "in", &in },
{ "out", &out }
2018-06-12 20:45:03 +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
/* Skip if direction is unused */
if (!json_dir) {
json_dir = json_pack("{ s: b }", "enabled", 0);
}
2018-06-12 20:45:03 +02:00
/* Copy missing fields from main node config to direction config */
2019-04-07 15:13:40 +02:00
for (unsigned i = 0; i < ARRAY_LEN(fields); i++) {
2018-06-12 20:45:03 +02:00
json_t *json_field_dir = json_object_get(json_dir, fields[i]);
json_t *json_field_node = json_object_get(json, fields[i]);
if (json_field_node && !json_field_dir)
json_object_set(json_dir, fields[i], json_field_node);
}
ret = dirs[j].dir->parse(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
}
config = json;
2016-06-08 22:38:21 +02:00
2019-03-26 15:36:24 +01:00
return 0;
}
int Node::check()
2015-12-11 17:56:14 +01:00
{
assert(state == State::CHECKED ||
state == State::PARSED ||
state == State::INITIALIZED);
in.check();
out.check();
state = State::CHECKED;
return 0;
}
int Node::start()
2015-12-11 17:56:14 +01:00
{
2015-11-16 10:51:00 +01:00
int ret;
assert(state == State::PREPARED);
logger->info("Starting node {}", getNameFull());
ret = in.start();
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
2017-03-13 23:51:38 -03:00
ret = out.start();
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
#ifdef __linux__
/* Set fwmark for outgoing packets if netem is enabled for this node */
if (fwmark >= 0) {
for (int fd : getNetemFDs()) {
ret = setsockopt(fd, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark));
if (ret)
throw RuntimeError("Failed to set FW mark for outgoing packets");
else
logger->debug("Set FW mark for socket (sd={}) to {}", fd, fwmark);
}
}
#endif /* __linux__ */
state = State::STARTED;
sequence = sequence_init;
return 0;
2014-12-05 12:39:52 +01:00
}
int Node::stop()
2015-12-11 17:56:14 +01:00
{
2015-11-16 10:51:00 +01:00
int ret;
2022-03-14 15:32:20 -04:00
if (state != State::STOPPING &&
state != State::STARTED &&
state != State::CONNECTED &&
state != State::PENDING_CONNECT)
return 0;
logger->info("Stopping node");
2022-03-14 15:32:20 -04:00
setState(State::STOPPING);
ret = in.stop();
2018-11-22 09:56:40 +02:00
if (ret)
return ret;
ret = out.stop();
2018-11-22 09:56:40 +02:00
if (ret)
return ret;
return 0;
}
int Node::restart()
{
int ret;
assert(state == State::STARTED);
logger->info("Restarting node");
ret = stop();
if (ret)
return ret;
ret = start();
if (ret)
return ret;
return 0;
}
int Node::read(struct Sample * smps[], unsigned cnt)
{
int toread, readd, nread = 0;
unsigned vect;
if (state == State::PAUSED || state == State::PENDING_CONNECT)
2019-02-11 16:39:30 +01:00
return 0;
else if (state != State::STARTED && state != State::CONNECTED)
2019-02-11 16:39:30 +01:00
return -1;
vect = factory->getVectorize();
if (!vect)
vect = cnt;
2017-09-02 14:20:38 +02:00
while (cnt - nread > 0) {
toread = MIN(cnt - nread, vect);
readd = _read(&smps[nread], toread);
if (readd < 0)
return readd;
nread += readd;
}
#ifdef WITH_HOOKS
/* Run read hooks */
int rread = in.hooks.process(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 (stats != nullptr)
stats->update(Stats::Metric::SMPS_SKIPPED, skipped);
logger->debug("Received {} samples of which {} have been skipped", nread, skipped);
}
2021-02-22 23:16:53 +01:00
else
2022-02-25 09:56:15 -05:00
logger->debug("Received {} samples", nread);
2018-05-23 02:25:27 +02:00
return rread;
#else
logger->debug("Received {} samples", nread);
2018-05-23 02:25:27 +02:00
return nread;
#endif /* WITH_HOOKS */
}
int Node::write(struct Sample * smps[], unsigned cnt)
{
int tosend, sent, nsent = 0;
unsigned vect;
if (state == State::PAUSED || state == State::PENDING_CONNECT)
2019-02-11 16:39:30 +01:00
return 0;
else if (state != State::STARTED && state != State::CONNECTED)
2019-02-11 16:39:30 +01:00
return -1;
#ifdef WITH_HOOKS
/* Run write hooks */
cnt = out.hooks.process(smps, cnt);
if (cnt <= 0)
return cnt;
#endif /* WITH_HOOKS */
vect = getFactory()->getVectorize();
if (!vect)
vect = cnt;
while (cnt - nsent > 0) {
tosend = MIN(cnt - nsent, vect);
sent = _write(&smps[nsent], tosend);
if (sent < 0)
return sent;
nsent += sent;
logger->debug("Sent {} samples", sent);
}
return nsent;
}
const std::string & Node::getNameFull()
{
if (name_full.empty()) {
name_full = fmt::format("{}: uuid={}, #in.signals={}/{}, #in.hooks={}, #out.hooks={}, in.vectorize={}, out.vectorize={}",
getName(), uuid::toString(uuid).c_str(),
getInputSignals(false)->size(),
getInputSignals(true)->size(),
in.hooks.size(), out.hooks.size(),
in.vectorize, out.vectorize
2020-07-04 17:15:54 +02:00
);
2019-01-30 00:42:35 +01:00
#ifdef WITH_NETEM
name_full += fmt::format(", out.netem={}", tc_qdisc ? "yes" : "no");
2019-01-30 00:42:35 +01:00
if (tc_qdisc)
name_full += fmt::format(", fwmark={}", fwmark);
2019-01-30 00:42:35 +01:00
#endif /* WITH_NETEM */
2019-01-21 23:00:16 +01:00
if (out.path) {
name_full += fmt::format(", #out.signals={}/{}",
getOutputSignals(false) ? getOutputSignals(false)->size() : 0,
getOutputSignals() ? getOutputSignals()->size() : 0);
2020-07-04 17:15:54 +02:00
name_full += fmt::format(", out.path={}", out.path->toString());
2015-12-11 18:19:35 +01:00
}
/* Append node-type specific details */
auto details = getDetails();
if (!details.empty())
name_full += fmt::format(", {}", details);
2015-12-11 18:19:35 +01:00
}
return name_full;
}
SignalList::Ptr Node::getInputSignals(bool after_hooks) const
{
return in.getSignals(after_hooks);
}
SignalList::Ptr Node::getOutputSignals(bool after_hooks) const
{
if (out.path)
return out.path->getOutputSignals();
return nullptr;
}
unsigned Node::getInputSignalsMaxCount() const
{
return in.getSignalsMaxCount();
}
unsigned Node::getOutputSignalsMaxCount() const
{
if (out.path)
return out.path->getOutputSignalsMaxCount();
return 0;
}
bool Node::isValidName(const std::string &name)
{
std::regex re(RE_NODE_NAME);
return std::regex_match(name, re);
}
2021-02-16 14:15:14 +01:00
json_t * Node::toJson() const
{
json_t *json_node;
json_t *json_signals_in = nullptr;
json_t *json_signals_out = nullptr;
json_signals_in = getInputSignals()->toJson();
auto output_signals = getOutputSignals();
if (output_signals)
json_signals_out = output_signals->toJson();
json_node = json_pack("{ s: s, s: s, s: s, s: i, s: { s: i, s: o? }, s: { s: i, s: o? } }",
"name", getNameShort().c_str(),
"uuid", uuid::toString(uuid).c_str(),
"state", stateToString(state).c_str(),
"affinity", affinity,
"in",
"vectorize", in.vectorize,
"signals", json_signals_in,
"out",
"vectorize", out.vectorize,
"signals", json_signals_out
);
if (stats)
json_object_set_new(json_node, "stats", stats->toJson());
auto *status = _readStatus();
if (status)
json_object_set_new(json_node, "status", status);
/* Add all additional fields of node here.
* This can be used for metadata */
json_object_update(json_node, config);
return json_node;
}
void Node::swapSignals() {
SWAP(in.signals, out.signals);
}
Node * NodeFactory::make(json_t *json, const uuid_t &id, const std::string &name)
{
int ret;
std::string type;
Node *n;
if (json_is_object(json))
throw ConfigError(json, "node-config-node", "Node configuration must be an object");
json_t *json_type = json_object_get(json, "type");
2019-02-12 17:54:08 +01:00
type = json_string_value(json_type);
n = NodeFactory::make(type, id, name);
if (!n)
return nullptr;
2019-02-24 11:08:15 +01:00
ret = n->parse(json);
if (ret) {
delete n;
return nullptr;
}
return n;
2020-10-21 20:56:51 +02:00
}
2019-03-08 15:21:01 +01:00
Node * NodeFactory::make(const std::string &type, const uuid_t &id, const std::string &name)
2020-10-21 20:56:51 +02:00
{
2022-02-25 09:55:43 -05:00
NodeFactory *nf = plugin::registry->lookup<NodeFactory>(type);
if (!nf)
throw RuntimeError("Unknown node-type: {}", type);
2020-10-21 20:56:51 +02:00
return nf->make(id, name);
2019-02-12 17:54:08 +01:00
}
2020-08-25 20:24:18 +02:00
int NodeFactory::start(SuperNode *sn)
{
getLogger()->info("Initialized node type which is used by {} nodes", instances.size());
state = State::STARTED;
return 0;
}
int NodeFactory::stop()
2020-08-25 20:24:18 +02:00
{
getLogger()->info("De-initialized node type");
2020-08-25 20:24:18 +02:00
state = State::STOPPED;
2020-08-25 20:24:18 +02:00
return 0;
}