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/nodes/socket.cpp

576 lines
15 KiB
C++
Raw Permalink Normal View History

2020-01-21 14:20:49 +01:00
/** The socket node-type for Layer 2, 3, 4 BSD-style sockets
*
* @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/>.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <unistd.h>
#include <cstring>
#include <cerrno>
2019-04-05 19:30:44 +02:00
#include <arpa/inet.h>
#include <netinet/ip.h>
2021-06-21 16:11:42 -04:00
#include <villas/node.h>
#include <villas/nodes/socket.hpp>
#include <villas/utils.hpp>
2017-12-09 02:19:28 +08:00
#include <villas/sample.h>
#include <villas/queue.h>
2020-03-04 13:07:20 +01:00
#include <villas/compat.hpp>
2019-04-23 13:14:47 +02:00
#include <villas/super_node.hpp>
2018-08-23 17:37:16 +02:00
#ifdef WITH_SOCKET_LAYER_ETH
#include <netinet/ether.h>
#endif /* WITH_SOCKET_LAYER_ETH */
2019-01-21 22:14:41 +01:00
#ifdef WITH_NETEM
#include <villas/kernel/if.hpp>
#include <villas/kernel/nl.hpp>
2019-01-21 22:14:41 +01:00
#endif /* WITH_NETEM */
/* Forward declartions */
2021-06-21 16:11:42 -04:00
static struct vnode_type p;
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
using namespace villas::node;
using namespace villas::kernel;
2019-04-23 13:14:47 +02:00
int socket_type_start(villas::node::SuperNode *sn)
2015-12-11 17:56:14 +01:00
{
#ifdef WITH_NETEM
/* Gather list of used network interfaces */
2021-06-21 16:11:42 -04:00
for (auto *n : p.instances) {
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
2019-06-23 16:13:23 +02:00
if (s->layer == SocketLayer::UNIX)
continue;
/* Determine outgoing interface */
Interface *j = Interface::getEgress((struct sockaddr *) &s->out.saddr, sn);
j->addNode(n);
}
#endif /* WITH_NETEM */
2015-08-07 01:11:43 +02:00
return 0;
}
2020-08-25 21:00:52 +02:00
char * socket_print(struct vnode *n)
{
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
2019-04-22 23:45:38 +02:00
const char *layer = nullptr;
2019-04-22 23:43:46 +02:00
char *buf;
switch (s->layer) {
2019-06-23 16:13:23 +02:00
case SocketLayer::UDP:
layer = "udp";
break;
2019-06-23 16:13:23 +02:00
case SocketLayer::IP:
layer = "ip";
break;
2019-06-23 16:13:23 +02:00
case SocketLayer::ETH:
layer = "eth";
break;
2019-06-23 16:13:23 +02:00
case SocketLayer::UNIX:
layer = "unix";
break;
}
char *local = socket_print_addr((struct sockaddr *) &s->in.saddr);
char *remote = socket_print_addr((struct sockaddr *) &s->out.saddr);
2021-05-10 00:12:30 +02:00
buf = strf("layer=%s, in.address=%s, out.address=%s", layer, local, remote);
2017-07-24 19:33:35 +02:00
if (s->multicast.enabled) {
char group[INET_ADDRSTRLEN];
char interface[INET_ADDRSTRLEN];
2017-07-24 19:33:35 +02:00
inet_ntop(AF_INET, &s->multicast.mreq.imr_multiaddr, group, sizeof(group));
inet_ntop(AF_INET, &s->multicast.mreq.imr_interface, interface, sizeof(interface));
2017-07-24 19:33:35 +02:00
strcatf(&buf, ", in.multicast.enabled=%s", s->multicast.enabled ? "yes" : "no");
strcatf(&buf, ", in.multicast.loop=%s", s->multicast.loop ? "yes" : "no");
strcatf(&buf, ", in.multicast.group=%s", group);
strcatf(&buf, ", in.multicast.interface=%s", s->multicast.mreq.imr_interface.s_addr == INADDR_ANY ? "any" : interface);
strcatf(&buf, ", in.multicast.ttl=%u", s->multicast.ttl);
}
free(local);
free(remote);
return buf;
}
2020-08-25 21:00:52 +02:00
int socket_check(struct vnode *n)
{
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
2017-07-24 19:33:35 +02:00
/* Some checks on the addresses */
2019-06-23 16:13:23 +02:00
if (s->layer != SocketLayer::UNIX) {
if (s->in.saddr.sa.sa_family != s->out.saddr.sa.sa_family)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Address families of local and remote must match!");
}
2017-07-24 19:33:35 +02:00
2019-06-23 16:13:23 +02:00
if (s->layer == SocketLayer::IP) {
if (ntohs(s->in.saddr.sin.sin_port) != ntohs(s->out.saddr.sin.sin_port))
2021-02-16 14:15:14 +01:00
throw RuntimeError("IP protocol numbers of local and remote must match!");
}
2018-08-23 17:37:16 +02:00
#ifdef WITH_SOCKET_LAYER_ETH
2019-06-23 16:13:23 +02:00
else if (s->layer == SocketLayer::ETH) {
if (ntohs(s->in.saddr.sll.sll_protocol) != ntohs(s->out.saddr.sll.sll_protocol))
2021-02-16 14:15:14 +01:00
throw RuntimeError("Ethertypes of local and remote must match!");
2017-07-24 19:33:35 +02:00
if (ntohs(s->in.saddr.sll.sll_protocol) <= 0x5DC)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Ethertype must be large than {} or it is interpreted as an IEEE802.3 length field!", 0x5DC);
}
2018-08-23 17:37:16 +02:00
#endif /* WITH_SOCKET_LAYER_ETH */
2015-08-07 01:11:43 +02:00
if (s->multicast.enabled) {
if (s->in.saddr.sa.sa_family != AF_INET)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Multicast is only supported by IPv4");
uint32_t addr = ntohl(s->multicast.mreq.imr_multiaddr.s_addr);
if ((addr >> 28) != 14)
2021-02-16 14:15:14 +01:00
throw RuntimeError("Multicast group address must be within 224.0.0.0/4");
}
return 0;
}
2020-08-25 21:00:52 +02:00
int socket_start(struct vnode *n)
{
struct socket *s = (struct socket *) n->_vd;
int ret;
2018-05-12 15:25:29 +02:00
/* Initialize IO */
2021-05-10 00:12:30 +02:00
s->formatter->start(&n->in.signals, ~(int) SampleFlags::HAS_OFFSET);
/* Create socket */
switch (s->layer) {
2019-06-23 16:13:23 +02:00
case SocketLayer::UDP:
s->sd = socket(s->in.saddr.sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
break;
2019-06-23 16:13:23 +02:00
case SocketLayer::IP:
s->sd = socket(s->in.saddr.sa.sa_family, SOCK_RAW, ntohs(s->in.saddr.sin.sin_port));
break;
2018-08-23 17:37:16 +02:00
#ifdef WITH_SOCKET_LAYER_ETH
2019-06-23 16:13:23 +02:00
case SocketLayer::ETH:
s->sd = socket(s->in.saddr.sa.sa_family, SOCK_DGRAM, s->in.saddr.sll.sll_protocol);
break;
2018-08-23 17:37:16 +02:00
#endif /* WITH_SOCKET_LAYER_ETH */
2019-06-23 16:13:23 +02:00
case SocketLayer::UNIX:
s->sd = socket(s->in.saddr.sa.sa_family, SOCK_DGRAM, 0);
break;
default:
2021-02-16 14:15:14 +01:00
throw RuntimeError("Invalid socket type!");
}
2015-08-07 01:11:43 +02:00
if (s->sd < 0)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to create socket");
2015-08-07 01:11:43 +02:00
/* Delete Unix domain socket if already existing */
2019-06-23 16:13:23 +02:00
if (s->layer == SocketLayer::UNIX) {
ret = unlink(s->in.saddr.sun.sun_path);
if (ret && errno != ENOENT)
return ret;
}
/* Bind socket for receiving */
socklen_t addrlen = 0;
switch(s->in.saddr.ss.ss_family) {
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
addrlen = sizeof(struct sockaddr_in6);
break;
case AF_UNIX:
addrlen = SUN_LEN(&s->in.saddr.sun);
break;
2018-08-23 17:37:16 +02:00
#ifdef WITH_SOCKET_LAYER_ETH
case AF_PACKET:
addrlen = sizeof(struct sockaddr_ll);
break;
2018-08-23 17:37:16 +02:00
#endif /* WITH_SOCKET_LAYER_ETH */
default:
addrlen = sizeof(s->in.saddr);
}
ret = bind(s->sd, (struct sockaddr *) &s->in.saddr, addrlen);
if (ret < 0)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to bind socket");
2015-08-07 01:11:43 +02:00
if (s->multicast.enabled) {
ret = setsockopt(s->sd, IPPROTO_IP, IP_MULTICAST_LOOP, &s->multicast.loop, sizeof(s->multicast.loop));
if (ret)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to set multicast loop option");
ret = setsockopt(s->sd, IPPROTO_IP, IP_MULTICAST_TTL, &s->multicast.ttl, sizeof(s->multicast.ttl));
if (ret)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to set multicast ttl option");
2017-07-24 19:33:35 +02:00
ret = setsockopt(s->sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &s->multicast.mreq, sizeof(s->multicast.mreq));
if (ret)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to join multicast group");
}
/* Set socket priority, QoS or TOS IP options */
int prio;
switch (s->layer) {
2019-06-23 16:13:23 +02:00
case SocketLayer::UDP:
case SocketLayer::IP:
prio = IPTOS_LOWDELAY;
if (setsockopt(s->sd, IPPROTO_IP, IP_TOS, &prio, sizeof(prio)))
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to set type of service (QoS)");
else
n->logger->debug("Set QoS/TOS IP option to {:#x}", prio);
break;
default:
#ifdef __linux__
prio = SOCKET_PRIO;
if (setsockopt(s->sd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to set socket priority");
else
2021-02-16 14:15:14 +01:00
n->logger->debug("Set socket priority to {}", prio);
break;
#else
{ }
#endif /* __linux__ */
}
2015-08-07 01:11:43 +02:00
s->out.buflen = SOCKET_INITIAL_BUFFER_LEN;
s->out.buf = new char[s->out.buflen];
if (!s->out.buf)
throw MemoryAllocationError();
s->in.buflen = SOCKET_INITIAL_BUFFER_LEN;
s->in.buf = new char[s->in.buflen];
if (!s->in.buf)
throw MemoryAllocationError();
return 0;
}
2020-08-25 21:00:52 +02:00
int socket_reverse(struct vnode *n)
{
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
union sockaddr_union tmp;
tmp = s->in.saddr;
s->in.saddr = s->out.saddr;
s->out.saddr = tmp;
return 0;
}
2020-08-25 21:00:52 +02:00
int socket_stop(struct vnode *n)
{
int ret;
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
2017-07-24 19:33:35 +02:00
if (s->multicast.enabled) {
ret = setsockopt(s->sd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &s->multicast.mreq, sizeof(s->multicast.mreq));
if (ret)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed to leave multicast group");
}
2015-08-07 01:11:43 +02:00
if (s->sd >= 0) {
ret = close(s->sd);
if (ret)
return ret;
}
2021-05-10 00:12:30 +02:00
delete s->formatter;
delete[] s->in.buf;
delete[] s->out.buf;
return 0;
}
2021-05-10 00:12:30 +02:00
int socket_read(struct vnode *n, struct sample * const smps[], unsigned cnt)
{
int ret;
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
char *ptr;
ssize_t bytes;
size_t rbytes;
union sockaddr_union src;
socklen_t srclen = sizeof(src);
/* Receive next sample */
bytes = recvfrom(s->sd, s->in.buf, s->in.buflen, 0, &src.sa, &srclen);
if (bytes < 0)
2021-02-16 14:15:14 +01:00
throw SystemError("Failed recvfrom()");
else if (bytes == 0)
return 0;
2017-07-24 19:33:35 +02:00
ptr = s->in.buf;
/* Strip IP header from packet */
2019-06-23 16:13:23 +02:00
if (s->layer == SocketLayer::IP) {
struct ip *iphdr = (struct ip *) ptr;
2017-07-24 19:33:35 +02:00
bytes -= iphdr->ip_hl * 4;
ptr += iphdr->ip_hl * 4;
}
2017-07-24 19:33:35 +02:00
/* SOCK_RAW IP sockets to not provide the IP protocol number via recvmsg()
* So we simply set it ourself. */
2019-06-23 16:13:23 +02:00
if (s->layer == SocketLayer::IP) {
switch (src.sa.sa_family) {
case AF_INET:
src.sin.sin_port = s->out.saddr.sin.sin_port;
break;
case AF_INET6:
src.sin6.sin6_port = s->out.saddr.sin6.sin6_port;
break;
}
}
if (s->verify_source && socket_compare_addr(&src.sa, &s->out.saddr.sa) != 0) {
char *buf = socket_print_addr((struct sockaddr *) &src);
2021-02-16 14:15:14 +01:00
n->logger->warn("Received packet from unauthorized source: {}", buf);
free(buf);
return 0;
}
2021-05-10 00:12:30 +02:00
ret = s->formatter->sscan(ptr, bytes, &rbytes, smps, cnt);
2019-04-22 23:43:46 +02:00
if (ret < 0 || (size_t) bytes != rbytes)
2021-02-16 14:15:14 +01:00
n->logger->warn("Received invalid packet: ret={}, bytes={}, rbytes={}", ret, bytes, rbytes);
return ret;
}
2021-05-10 00:12:30 +02:00
int socket_write(struct vnode *n, struct sample * const smps[], unsigned cnt)
{
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
int ret;
ssize_t bytes;
size_t wbytes;
2021-05-10 00:12:30 +02:00
retry: ret = s->formatter->sprint(s->out.buf, s->out.buflen, &wbytes, smps, cnt);
if (ret < 0) {
2021-02-16 14:15:14 +01:00
n->logger->warn("Failed to format payload: reason={}", ret);
return ret;
}
if (wbytes == 0) {
2021-02-16 14:15:14 +01:00
n->logger->warn("Failed to format payload: wbytes={}", wbytes);
return -1;
}
if (wbytes > s->out.buflen) {
s->out.buflen = wbytes;
delete[] s->out.buf;
s->out.buf = new char[s->out.buflen];
if (!s->out.buf)
throw MemoryAllocationError();
goto retry;
}
/* Send message */
socklen_t addrlen = 0;
switch(s->in.saddr.ss.ss_family) {
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
addrlen = sizeof(struct sockaddr_in6);
break;
case AF_UNIX:
addrlen = SUN_LEN(&s->out.saddr.sun);
break;
2018-08-23 17:37:16 +02:00
#ifdef WITH_SOCKET_LAYER_ETH
case AF_PACKET:
addrlen = sizeof(struct sockaddr_ll);
break;
2018-08-23 17:37:16 +02:00
#endif /* WITH_SOCKET_LAYER_ETH */
default:
addrlen = sizeof(s->in.saddr);
}
retry2: bytes = sendto(s->sd, s->out.buf, wbytes, 0, (struct sockaddr *) &s->out.saddr, addrlen);
if (bytes < 0) {
if ((errno == EPERM) ||
2019-06-23 16:13:23 +02:00
(errno == ENOENT && s->layer == SocketLayer::UNIX))
2021-02-16 14:15:14 +01:00
n->logger->warn("Failed sendto(): {}", strerror(errno));
else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
2021-02-16 14:15:14 +01:00
n->logger->warn("Blocking sendto()");
goto retry2;
}
else
2021-02-16 14:15:14 +01:00
n->logger->warn("Failed sendto(): {}", strerror(errno));
}
2019-04-22 23:43:46 +02:00
else if ((size_t) bytes < wbytes)
2021-02-16 14:15:14 +01:00
n->logger->warn("Partial sendto()");
return cnt;
}
2021-02-16 14:15:14 +01:00
int socket_parse(struct vnode *n, json_t *json)
{
2021-05-10 00:12:30 +02:00
int ret;
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
const char *local, *remote;
2019-04-22 23:45:38 +02:00
const char *layer = nullptr;
2015-08-07 01:11:43 +02:00
json_error_t err;
2021-05-10 00:12:30 +02:00
json_t *json_multicast = nullptr;
json_t *json_format = nullptr;
/* Default values */
2019-06-23 16:13:23 +02:00
s->layer = SocketLayer::UDP;
s->verify_source = 0;
2021-05-10 00:12:30 +02:00
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: o, s: { s: s }, s: { s: s, s?: b, s?: o } }",
"layer", &layer,
2021-05-10 00:12:30 +02:00
"format", &json_format,
"out",
"address", &remote,
"in",
"address", &local,
"verify_source", &s->verify_source,
"multicast", &json_multicast
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-socket");
/* Format */
2021-05-10 00:12:30 +02:00
s->formatter = json_format
? FormatFactory::make(json_format)
: FormatFactory::make("villas.binary");
if (!s->formatter)
throw ConfigError(json_format, "node-config-node-socket-format", "Invalid format configuration");
/* IP layer */
if (layer) {
if (!strcmp(layer, "ip"))
2019-06-23 16:13:23 +02:00
s->layer = SocketLayer::IP;
2018-08-23 17:37:16 +02:00
#ifdef WITH_SOCKET_LAYER_ETH
else if (!strcmp(layer, "eth"))
2019-06-23 16:13:23 +02:00
s->layer = SocketLayer::ETH;
2018-08-23 17:37:16 +02:00
#endif /* WITH_SOCKET_LAYER_ETH */
else if (!strcmp(layer, "udp"))
2019-06-23 16:13:23 +02:00
s->layer = SocketLayer::UDP;
else if (!strcmp(layer, "unix") || !strcmp(layer, "local"))
2019-06-23 16:13:23 +02:00
s->layer = SocketLayer::UNIX;
else
2021-02-16 14:15:14 +01:00
throw SystemError("Invalid layer '{}'", layer);
}
ret = socket_parse_address(remote, (struct sockaddr *) &s->out.saddr, s->layer, 0);
2021-02-16 14:15:14 +01:00
if (ret)
throw SystemError("Failed to resolve remote address '{}': {}", remote, gai_strerror(ret));
2017-07-24 19:33:35 +02:00
ret = socket_parse_address(local, (struct sockaddr *) &s->in.saddr, s->layer, AI_PASSIVE);
2021-02-16 14:15:14 +01:00
if (ret)
throw SystemError("Failed to resolve local address '{}': {}", local, gai_strerror(ret));
2017-10-16 08:08:35 +02:00
if (json_multicast) {
2019-04-22 23:45:38 +02:00
const char *group, *interface = nullptr;
/* Default values */
s->multicast.enabled = true;
s->multicast.mreq.imr_interface.s_addr = INADDR_ANY;
s->multicast.loop = 0;
s->multicast.ttl = 255;
2017-10-16 08:08:35 +02:00
ret = json_unpack_ex(json_multicast, &err, 0, "{ s?: b, s: s, s?: s, s?: b, s?: i }",
"enabled", &s->multicast.enabled,
"group", &group,
"interface", &interface,
"loop", &s->multicast.loop,
"ttl", &s->multicast.ttl
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json_multicast, err, "node-config-node-socket-multicast", "Failed to parse multicast settings");
2017-07-24 19:33:35 +02:00
ret = inet_aton(group, &s->multicast.mreq.imr_multiaddr);
2021-02-16 14:15:14 +01:00
if (!ret)
throw SystemError("Failed to resolve multicast group address '{}'", group);
2017-07-24 19:33:35 +02:00
if (interface) {
ret = inet_aton(group, &s->multicast.mreq.imr_interface);
2021-02-16 14:15:14 +01:00
if (!ret)
throw SystemError("Failed to resolve multicast interface address '{}'", interface);
}
2017-07-24 19:33:35 +02:00
}
return 0;
}
2020-08-25 21:00:52 +02:00
int socket_fds(struct vnode *n, int fds[])
{
2017-10-18 15:39:53 +02:00
struct socket *s = (struct socket *) n->_vd;
fds[0] = s->sd;
return 1;
}
2019-04-22 23:43:46 +02:00
__attribute__((constructor(110)))
static void register_plugin() {
p.name = "socket";
2017-12-14 13:23:19 +01:00
#ifdef WITH_NETEM
2019-04-22 23:43:46 +02:00
p.description = "BSD network sockets for Ethernet / IP / UDP (libnl3, netem support)";
2017-12-14 13:23:19 +01:00
#else
2019-04-22 23:43:46 +02:00
p.description = "BSD network sockets for Ethernet / IP / UDP";
2017-12-14 13:23:19 +01:00
#endif
2021-06-21 16:11:42 -04:00
p.vectorize = 0;
p.size = sizeof(struct socket);
p.type.start = socket_type_start;
p.reverse = socket_reverse;
p.parse = socket_parse;
p.print = socket_print;
p.check = socket_check;
p.start = socket_start;
p.stop = socket_stop;
p.read = socket_read;
p.write = socket_write;
p.poll_fds = socket_fds;
p.netem_fds = socket_fds;
if (!node_types)
node_types = new NodeTypeList();
node_types->push_back(&p);
2019-04-22 23:43:46 +02:00
}