2023-09-04 12:21:37 +02:00
|
|
|
/* Nodes.
|
2021-08-10 10:12:48 -04:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2021-08-10 10:12:48 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-02-08 14:55:05 +00:00
|
|
|
#include <fmt/ostream.h>
|
2023-09-25 12:40:06 +02:00
|
|
|
#include <iostream>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <jansson.h>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <uuid/uuid.h>
|
2023-06-30 10:51:01 +02:00
|
|
|
#include <villas/colors.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/common.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/list.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/log.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/node/memory.hpp>
|
|
|
|
#include <villas/node_direction.hpp>
|
|
|
|
#include <villas/node_list.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/path_destination.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/path_source.hpp>
|
|
|
|
#include <villas/plugin.hpp>
|
|
|
|
#include <villas/queue.h>
|
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/stats.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
#if defined(LIBNL3_ROUTE_FOUND) && defined(__linux__)
|
2023-09-07 11:46:39 +02:00
|
|
|
#define WITH_NETEM
|
2021-08-10 10:12:48 -04:00
|
|
|
#endif // LIBNL3_ROUTE_FOUND
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
struct rtnl_qdisc;
|
|
|
|
struct rtnl_cls;
|
2021-08-10 10:12:48 -04:00
|
|
|
#endif // WITH_NETEM
|
|
|
|
|
|
|
|
#define RE_NODE_NAME "[a-z0-9_-]{2,32}"
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
class NodeFactory;
|
|
|
|
class SuperNode;
|
|
|
|
|
|
|
|
/* The class for a node.
|
|
|
|
*
|
|
|
|
* Every entity which exchanges messages is represented by a node.
|
|
|
|
* Nodes can be remote machines and simulators or locally running processes.
|
|
|
|
*/
|
|
|
|
class Node {
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
friend NodeFactory;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
Logger logger;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
uint64_t sequence_init;
|
|
|
|
uint64_t
|
|
|
|
sequence; // This is a counter of received samples, in case the node-type does not generate sequence numbers itself.
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
NodeDirection in, out;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
PathSourceList sources; // A list of path sources which reference this node.
|
|
|
|
PathDestinationList
|
|
|
|
destinations; // A list of path destinations which reference this node.
|
2024-02-26 18:42:02 +01:00
|
|
|
std::string configPath;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
#ifdef __linux__
|
2023-09-07 11:46:39 +02:00
|
|
|
int fwmark; // Socket mark for netem, routing and filtering
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
struct rtnl_qdisc *tc_qdisc; // libnl3: Network emulator queuing discipline
|
|
|
|
struct rtnl_cls *tc_classifier; // libnl3: Firewall mark classifier
|
|
|
|
#endif // WITH_NETEM
|
|
|
|
#endif // __linux__
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
enum State state;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
uuid_t uuid;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
bool enabled;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Stats::Ptr
|
|
|
|
stats; // Statistic counters. This is a pointer to the statistic hooks private data.
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
json_t *config; // A JSON object containing the configuration of the node.
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string
|
|
|
|
name_short; // A short identifier of the node, only used for configuration and logging
|
|
|
|
std::string name_long; // Singleton: A string used to print to screen.
|
|
|
|
std::string name_full; // Singleton: A string used to print to screen.
|
|
|
|
std::string details;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int affinity; // CPU Affinity of this node
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
NodeFactory *factory; // The factory which created this instance
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual int _read(struct Sample *smps[], unsigned cnt) { return -1; }
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual int _write(struct Sample *smps[], unsigned cnt) { return -1; }
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual json_t *_readStatus() const { return nullptr; }
|
2023-06-30 11:52:14 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
// Initialize node with default values
|
|
|
|
Node(const uuid_t &id = {}, const std::string &name = "");
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Destroy node by freeing dynamically allocated memory.
|
|
|
|
virtual ~Node();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Do initialization after parsing the configuration
|
|
|
|
virtual int prepare();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Parse settings of a node.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* @param json A JSON object containing the configuration of the node.
|
|
|
|
* @retval 0 Success. Everything went well.
|
|
|
|
* @retval <0 Error. Something went wrong.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual int parse(json_t *json);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Validate node configuration.
|
|
|
|
virtual int check();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Start operation of a node.
|
|
|
|
virtual int start();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Stops operation of a node.
|
|
|
|
virtual int stop();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Pauses operation of a node.
|
|
|
|
virtual int pause() {
|
|
|
|
if (state != State::STARTED)
|
|
|
|
return -1;
|
2022-02-25 09:56:15 -05:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->info("Pausing node");
|
2022-02-25 09:56:15 -05:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Resumes operation of a node.
|
|
|
|
virtual int resume() { return 0; }
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Restarts operation of a node.
|
|
|
|
virtual int restart();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Receive multiple messages at once.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* This callback is optional. It will only be called if non-null.
|
|
|
|
*
|
|
|
|
* Messages are received with a single recvmsg() syscall by
|
|
|
|
* using gathering techniques (struct iovec).
|
|
|
|
* The messages will be stored in a circular buffer / array @p m.
|
|
|
|
* Indexes used to address @p m will wrap around after len messages.
|
|
|
|
* Some node-types might only support to receive one message at a time.
|
|
|
|
*
|
|
|
|
* @param smps An array of pointers to memory blocks where the function should store received samples.
|
|
|
|
* @param cnt The number of samples that are allocated by the calling function.
|
|
|
|
* @return The number of messages actually received.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
int read(struct Sample *smps[], unsigned cnt);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Send multiple messages in a single datagram / packet.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* This callback is optional. It will only be called if non-null.
|
|
|
|
*
|
|
|
|
* Messages are sent with a single sendmsg() syscall by
|
|
|
|
* using gathering techniques (struct iovec).
|
|
|
|
* The messages have to be stored in a circular buffer / array m.
|
|
|
|
* So the indexes will wrap around after len.
|
|
|
|
*
|
|
|
|
* @param smps An array of pointers to memory blocks where samples read from.
|
|
|
|
* @param cnt The number of samples that are allocated by the calling function.
|
|
|
|
* @return The number of messages actually sent.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
int write(struct Sample *smps[], unsigned cnt);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Reverse local and remote socket address.
|
|
|
|
virtual int reverse() { return -1; }
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Get a list of file descriptors on which the path should poll
|
2024-02-29 21:47:13 +01:00
|
|
|
* to detect the availability of new samples which can be read.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual std::vector<int> getPollFDs() { return {}; }
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Get a list of socket file descriptors which are used by the node
|
2024-02-29 21:47:13 +01:00
|
|
|
* To perform network IO. We use those to selectively apply network emulation
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual std::vector<int> getNetemFDs() { return {}; }
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Get the memory type which this node-type expects.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* This is useful for special node-types like Infiniband, GPUs & FPGAs
|
|
|
|
* which require DMA-backed memory.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual struct villas::node::memory::Type *getMemoryType() {
|
|
|
|
return villas::node::memory::default_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the factory which was used to construct this node.
|
|
|
|
villas::node::NodeFactory *getFactory() const { return factory; }
|
|
|
|
|
|
|
|
/* Return a pointer to a string which should be used to print this node.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* @param n A pointer to the node structure.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getNameShort() const { return name_short; }
|
|
|
|
|
|
|
|
// Return a pointer to a string which should be used to print this node.
|
|
|
|
const std::string &getName() const { return name_long; }
|
|
|
|
|
|
|
|
// Get the full name including type and details of the node.
|
|
|
|
const std::string &getNameFull();
|
|
|
|
|
|
|
|
// Just get the config details of this node as a string
|
|
|
|
virtual const std::string &getDetails() {
|
|
|
|
static std::string empty;
|
|
|
|
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a pointer to a string which should be used to print this node.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* @param n A pointer to the node structure.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
const std::string &getNameLong();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Return a list of signals which are sent to this node.
|
2024-02-29 21:47:13 +01:00
|
|
|
*
|
|
|
|
* This list is derived from the path which uses the node as destination.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
SignalList::Ptr getOutputSignals(bool after_hooks = true) const;
|
|
|
|
SignalList::Ptr getInputSignals(bool after_hooks = true) const;
|
|
|
|
|
|
|
|
// Get the number of input signals (received by this node)
|
|
|
|
unsigned getInputSignalsMaxCount() const;
|
|
|
|
|
|
|
|
// Get the number of output signals (send out via this node)
|
|
|
|
unsigned getOutputSignalsMaxCount() const;
|
|
|
|
|
|
|
|
void swapSignals();
|
|
|
|
|
|
|
|
// Get the node configuration as JSON.
|
|
|
|
json_t *getConfig() { return config; }
|
|
|
|
|
|
|
|
// Get the state of this node.
|
|
|
|
enum State getState() const { return state; }
|
|
|
|
|
|
|
|
// Set the state of this node.
|
|
|
|
void setState(enum State s) { state = s; }
|
|
|
|
|
|
|
|
// Get the UUID of this node.
|
|
|
|
const uuid_t &getUuid() const { return uuid; }
|
|
|
|
|
|
|
|
std::shared_ptr<Stats> getStats() { return stats; }
|
|
|
|
|
|
|
|
void setStats(std::shared_ptr<Stats> sts) { stats = sts; }
|
|
|
|
|
|
|
|
void setEnabled(bool en) { enabled = en; }
|
|
|
|
|
2023-09-25 12:40:06 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &os, const Node &n) {
|
2023-09-07 11:46:39 +02:00
|
|
|
os << n.getName();
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2024-02-08 14:55:05 +00:00
|
|
|
virtual json_t *toJson() const;
|
2023-09-07 11:46:39 +02:00
|
|
|
|
|
|
|
static bool isValidName(const std::string &name);
|
|
|
|
|
|
|
|
bool isEnabled() const { return enabled; }
|
2021-08-10 10:12:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class NodeFactory : public villas::plugin::Plugin {
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
friend Node;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void init(Node *n) {
|
|
|
|
n->logger = getLogger();
|
|
|
|
n->factory = this;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
n->name_long = fmt::format(CLR_RED("{}") "(" CLR_YEL("{}") ")",
|
|
|
|
n->name_short, getName());
|
2023-06-30 10:51:01 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
instances.push_back(n);
|
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
State state;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
enum class Flags {
|
|
|
|
SUPPORTS_POLL = (1 << 0),
|
|
|
|
SUPPORTS_READ = (1 << 1),
|
|
|
|
SUPPORTS_WRITE = (1 << 2),
|
|
|
|
REQUIRES_WEB = (1 << 3),
|
|
|
|
PROVIDES_SIGNALS = (1 << 4),
|
|
|
|
INTERNAL = (1 << 5),
|
|
|
|
HIDDEN = (1 << 6)
|
|
|
|
};
|
|
|
|
|
|
|
|
NodeList instances;
|
|
|
|
|
|
|
|
NodeFactory() : Plugin() { state = State::INITIALIZED; }
|
|
|
|
|
|
|
|
virtual Node *make(const uuid_t &id = {}, const std::string &nme = "") = 0;
|
|
|
|
|
|
|
|
static Node *make(json_t *json, const uuid_t &id,
|
|
|
|
const std::string &name = "");
|
|
|
|
|
|
|
|
static Node *make(const std::string &type, const uuid_t &id = {},
|
|
|
|
const std::string &name = "");
|
|
|
|
|
|
|
|
virtual std::string getType() const { return "node"; }
|
|
|
|
|
2023-09-25 12:40:06 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &os, const NodeFactory &f) {
|
2023-09-07 11:46:39 +02:00
|
|
|
os << f.getName();
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int getFlags() const { return 0; }
|
|
|
|
|
|
|
|
virtual int getVectorize() const { return 0; }
|
|
|
|
|
|
|
|
bool isInternal() const { return getFlags() & (int)Flags::INTERNAL; }
|
|
|
|
|
|
|
|
bool isHidden() const {
|
|
|
|
return isInternal() || getFlags() & (int)Flags::HIDDEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int start(SuperNode *sn);
|
|
|
|
|
|
|
|
virtual int stop();
|
|
|
|
|
|
|
|
State getState() const { return state; }
|
2021-08-10 10:12:48 -04:00
|
|
|
};
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
template <typename T, const char *name, const char *desc, int flags = 0,
|
|
|
|
int vectorize = 0>
|
2021-08-10 10:12:48 -04:00
|
|
|
class NodePlugin : public NodeFactory {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual Node *make(const uuid_t &id = {}, const std::string &nme = "") {
|
|
|
|
T *n = new T(id, nme);
|
|
|
|
|
|
|
|
init(n);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int getFlags() const { return flags; }
|
|
|
|
|
|
|
|
virtual int getVectorize() const { return vectorize; }
|
|
|
|
|
|
|
|
virtual std::string getName() const { return name; }
|
|
|
|
|
|
|
|
virtual std::string getDescription() const { return desc; }
|
2021-08-10 10:12:48 -04:00
|
|
|
};
|
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|
2023-09-25 12:40:06 +02:00
|
|
|
|
|
|
|
#ifndef FMT_LEGACY_OSTREAM_FORMATTER
|
|
|
|
template <>
|
|
|
|
class fmt::formatter<villas::node::Node> : public fmt::ostream_formatter {};
|
|
|
|
template <>
|
|
|
|
class fmt::formatter<villas::node::NodeFactory>
|
|
|
|
: public fmt::ostream_formatter {};
|
|
|
|
#endif
|