2019-01-23 14:16:53 +01:00
|
|
|
/** Node type: Real-time Protocol (RTP)
|
2018-11-16 16:07:47 +01:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2019-01-23 14:16:53 +01:00
|
|
|
* @author Marvin Klimke <marvin.klimke@rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2018-11-16 16:07:47 +01: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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cinttypes>
|
2018-12-07 06:37:48 +01:00
|
|
|
#include <pthread.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
2019-01-07 15:49:34 +01:00
|
|
|
#include <signal.h>
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/rtp.hpp>
|
|
|
|
|
2019-03-26 15:34:07 +01:00
|
|
|
extern "C" {
|
2021-02-19 06:38:38 +01:00
|
|
|
#include <re/re_main.h>
|
|
|
|
#include <re/re_types.h>
|
|
|
|
#include <re/re_mbuf.h>
|
|
|
|
#include <re/re_mem.h>
|
|
|
|
#include <re/re_sys.h>
|
|
|
|
#include <re/re_udp.h>
|
|
|
|
#undef ALIGN_MASK
|
2019-03-26 15:34:07 +01:00
|
|
|
}
|
2018-11-21 18:21:29 +01:00
|
|
|
|
2018-11-16 16:07:47 +01:00
|
|
|
#include <villas/plugin.h>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/socket.hpp>
|
2019-04-23 13:15:00 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-06-23 13:35:42 +02:00
|
|
|
#include <villas/stats.hpp>
|
2019-06-23 16:13:23 +02:00
|
|
|
#include <villas/hook.hpp>
|
2018-11-16 16:07:47 +01:00
|
|
|
#include <villas/format_type.h>
|
2019-04-23 13:14:47 +02:00
|
|
|
#include <villas/super_node.hpp>
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-01-21 22:14:41 +01:00
|
|
|
#ifdef WITH_NETEM
|
2020-09-13 11:01:20 +02:00
|
|
|
#include <villas/kernel/if.hpp>
|
2019-01-21 22:14:41 +01:00
|
|
|
#endif /* WITH_NETEM */
|
|
|
|
|
2019-01-16 16:41:02 +01:00
|
|
|
static pthread_t re_pthread;
|
2018-12-07 06:37:48 +01:00
|
|
|
|
2019-06-23 13:35:42 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
2020-09-13 11:01:20 +02:00
|
|
|
using namespace villas::node;
|
|
|
|
using namespace villas::kernel;
|
2019-03-26 15:34:07 +01:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
static struct plugin p;
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
static int rtp_aimd(struct vnode *n, double loss_frac)
|
2019-01-21 12:12:47 +01:00
|
|
|
{
|
2019-01-21 13:10:55 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
|
|
|
|
2019-01-21 12:12:47 +01:00
|
|
|
double rate;
|
|
|
|
|
2019-02-15 10:19:58 +01:00
|
|
|
if (!r->rtcp.enabled)
|
|
|
|
return -1;
|
|
|
|
|
2019-02-17 21:16:51 +01:00
|
|
|
if (loss_frac < 0.01)
|
2019-04-14 19:22:33 +02:00
|
|
|
rate = r->aimd.rate + r->aimd.a;
|
2019-01-21 13:10:55 +01:00
|
|
|
else
|
2019-04-14 19:22:33 +02:00
|
|
|
rate = r->aimd.rate * r->aimd.b;
|
2019-01-21 13:10:55 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
r->aimd.rate = r->aimd.rate_pid.calculate(rate, r->aimd.rate);
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
if (r->aimd.rate_hook) {
|
|
|
|
r->aimd.rate_hook->setRate(r->aimd.rate);
|
|
|
|
r->logger->debug("AIMD: Set rate limit to: {}", r->aimd.rate);
|
|
|
|
}
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-03-29 10:45:23 +01:00
|
|
|
if (r->aimd.log)
|
2019-04-14 19:22:33 +02:00
|
|
|
*(r->aimd.log) << r->rtcp.num_rrs << "\t" << loss_frac << "\t" << r->aimd.rate << std::endl;
|
2019-01-28 12:30:47 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
r->logger->debug("AIMD: {}\t{}\t{}", r->rtcp.num_rrs, loss_frac, r->aimd.rate);
|
2019-01-28 12:30:47 +01:00
|
|
|
|
2019-01-21 12:12:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_init(struct vnode *n)
|
2019-01-23 13:29:23 +01:00
|
|
|
{
|
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
|
|
|
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger = villas::logging.get("node:rtp");
|
|
|
|
|
2019-01-23 13:29:23 +01:00
|
|
|
/* Default values */
|
2019-04-14 19:22:33 +02:00
|
|
|
r->aimd.rate = 1;
|
2019-01-23 13:29:23 +01:00
|
|
|
|
|
|
|
r->aimd.a = 10;
|
|
|
|
r->aimd.b = 0.5;
|
2019-04-14 19:22:33 +02:00
|
|
|
r->aimd.Kp = 1;
|
|
|
|
r->aimd.Ki = 0;
|
|
|
|
r->aimd.Kd = 0;
|
|
|
|
r->aimd.rate_min = 1;
|
|
|
|
r->aimd.rate_source = 2000;
|
2019-03-29 09:50:47 +01:00
|
|
|
r->aimd.log_filename = nullptr;
|
2019-03-29 10:45:23 +01:00
|
|
|
r->aimd.log = nullptr;
|
2019-01-23 13:29:23 +01:00
|
|
|
|
2019-01-28 11:10:23 +01:00
|
|
|
r->rtcp.enabled = false;
|
2019-06-23 16:13:23 +02:00
|
|
|
r->aimd.rate_hook_type = RTPHookType::DISABLED;
|
2019-01-23 13:29:23 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_reverse(struct vnode *n)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2018-11-22 17:53:07 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-03-28 09:23:44 +01:00
|
|
|
SWAP(r->in.saddr_rtp, r->out.saddr_rtp);
|
|
|
|
SWAP(r->in.saddr_rtcp, r->out.saddr_rtcp);
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2018-11-22 17:53:07 +01:00
|
|
|
return 0;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_parse(struct vnode *n, json_t *cfg)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2018-11-21 18:21:29 +01:00
|
|
|
int ret = 0;
|
2018-11-22 17:53:07 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2018-11-22 07:18:27 +01:00
|
|
|
const char *local, *remote;
|
|
|
|
const char *format = "villas.binary";
|
2019-03-29 09:50:47 +01:00
|
|
|
const char *log = nullptr;
|
2019-04-14 19:22:33 +02:00
|
|
|
const char *hook_type = nullptr;
|
2018-12-20 08:25:13 +01:00
|
|
|
uint16_t port;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
|
|
|
json_error_t err;
|
2019-04-14 19:22:33 +02:00
|
|
|
json_t *json_aimd = nullptr;
|
2019-01-21 13:10:55 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: b, s?: o, s: { s: s }, s: { s: s } }",
|
2018-11-22 07:18:27 +01:00
|
|
|
"format", &format,
|
2019-04-14 19:22:33 +02:00
|
|
|
"rtcp", &r->rtcp.enabled,
|
2019-01-21 13:10:55 +01:00
|
|
|
"aimd", &json_aimd,
|
2018-11-22 07:18:27 +01:00
|
|
|
"out",
|
|
|
|
"address", &remote,
|
|
|
|
"in",
|
|
|
|
"address", &local
|
|
|
|
);
|
2018-11-16 16:07:47 +01:00
|
|
|
if (ret)
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->error("Failed to parse configuration of node {}", node_name(n));
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-01-21 13:10:55 +01:00
|
|
|
/* AIMD */
|
|
|
|
if (json_aimd) {
|
2019-04-14 19:22:33 +02:00
|
|
|
ret = json_unpack_ex(json_aimd, &err, 0, "{ s?: F, s?: F, s?: F, s?: F, s?: F, s?: F, s?: F, s?: F, s?: s, s?: s }",
|
2019-01-21 13:10:55 +01:00
|
|
|
"a", &r->aimd.a,
|
2019-01-28 12:31:40 +01:00
|
|
|
"b", &r->aimd.b,
|
2019-04-14 19:22:33 +02:00
|
|
|
"Kp", &r->aimd.Kp,
|
|
|
|
"Ki", &r->aimd.Ki,
|
|
|
|
"Kd", &r->aimd.Kd,
|
|
|
|
"rate_min", &r->aimd.rate_min,
|
|
|
|
"rate_source", &r->aimd.rate_source,
|
|
|
|
"rate_init", &r->aimd.rate,
|
|
|
|
"log", &log,
|
|
|
|
"hook_type", &hook_type
|
2019-01-21 13:10:55 +01:00
|
|
|
);
|
|
|
|
if (ret)
|
2019-03-29 09:50:47 +01:00
|
|
|
r->logger->error("Failed to parse configuration of node {}", node_name(n));
|
2019-04-14 19:22:33 +02:00
|
|
|
|
|
|
|
/* AIMD Hook type */
|
|
|
|
if (!r->rtcp.enabled)
|
2019-06-23 16:13:23 +02:00
|
|
|
r->aimd.rate_hook_type = RTPHookType::DISABLED;
|
2019-04-14 19:22:33 +02:00
|
|
|
else if (hook_type) {
|
|
|
|
if (!strcmp(hook_type, "decimate"))
|
2019-06-23 16:13:23 +02:00
|
|
|
r->aimd.rate_hook_type = RTPHookType::DECIMATE;
|
2019-04-14 19:22:33 +02:00
|
|
|
else if (!strcmp(hook_type, "limit_rate"))
|
2019-06-23 16:13:23 +02:00
|
|
|
r->aimd.rate_hook_type = RTPHookType::LIMIT_RATE;
|
2019-04-14 19:22:33 +02:00
|
|
|
else if (!strcmp(hook_type, "disabled"))
|
2019-06-23 16:13:23 +02:00
|
|
|
r->aimd.rate_hook_type = RTPHookType::DISABLED;
|
2019-04-14 19:22:33 +02:00
|
|
|
else
|
|
|
|
r->logger->error("Unknown RTCP hook_type: {}", hook_type);
|
|
|
|
}
|
2019-01-21 13:10:55 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 09:50:47 +01:00
|
|
|
if (log)
|
|
|
|
r->aimd.log_filename = strdup(log);
|
|
|
|
|
2018-11-22 07:18:27 +01:00
|
|
|
/* Format */
|
2018-11-22 17:53:07 +01:00
|
|
|
r->format = format_type_lookup(format);
|
2019-02-15 10:21:14 +01:00
|
|
|
if (!r->format)
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->error("Invalid format '{}' for node {}", format, node_name(n));
|
2018-11-22 07:18:27 +01:00
|
|
|
|
2018-11-22 17:53:07 +01:00
|
|
|
/* Remote address */
|
2019-01-21 15:50:18 +01:00
|
|
|
ret = sa_decode(&r->out.saddr_rtp, remote, strlen(remote));
|
2018-11-22 07:18:27 +01:00
|
|
|
if (ret) {
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->error("Failed to resolve remote address '{}' of node {}: {}",
|
2018-11-22 07:18:27 +01:00
|
|
|
remote, node_name(n), strerror(ret));
|
|
|
|
}
|
|
|
|
|
2018-12-20 08:25:13 +01:00
|
|
|
/* Assign even port number to RTP socket, next odd number to RTCP socket */
|
2019-01-21 15:50:18 +01:00
|
|
|
port = sa_port(&r->out.saddr_rtp) & ~1;
|
|
|
|
sa_set_sa(&r->out.saddr_rtcp, &r->out.saddr_rtp.u.sa);
|
|
|
|
sa_set_port(&r->out.saddr_rtp, port);
|
|
|
|
sa_set_port(&r->out.saddr_rtcp, port+1);
|
2018-12-20 08:25:13 +01:00
|
|
|
|
2018-11-22 17:53:07 +01:00
|
|
|
/* Local address */
|
2019-01-21 15:50:18 +01:00
|
|
|
ret = sa_decode(&r->in.saddr_rtp, local, strlen(local));
|
2018-11-22 07:18:27 +01:00
|
|
|
if (ret) {
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->error("Failed to resolve local address '{}' of node {}: {}",
|
2018-11-22 07:18:27 +01:00
|
|
|
local, node_name(n), strerror(ret));
|
|
|
|
}
|
|
|
|
|
2018-12-20 08:25:13 +01:00
|
|
|
/* Assign even port number to RTP socket, next odd number to RTCP socket */
|
2019-01-21 15:50:18 +01:00
|
|
|
port = sa_port(&r->in.saddr_rtp) & ~1;
|
|
|
|
sa_set_sa(&r->in.saddr_rtcp, &r->in.saddr_rtp.u.sa);
|
|
|
|
sa_set_port(&r->in.saddr_rtp, port);
|
|
|
|
sa_set_port(&r->in.saddr_rtcp, port+1);
|
2018-12-20 08:25:13 +01:00
|
|
|
|
2018-11-22 17:53:07 +01:00
|
|
|
/** @todo parse * in addresses */
|
2018-11-22 07:18:27 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
char * rtp_print(struct vnode *n)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2018-11-22 17:53:07 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
|
|
|
char *buf;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
char *local = socket_print_addr((struct sockaddr *) &r->in.saddr_rtp.u);
|
|
|
|
char *remote = socket_print_addr((struct sockaddr *) &r->out.saddr_rtp.u);
|
2018-11-22 17:53:07 +01:00
|
|
|
|
2019-01-21 12:12:47 +01:00
|
|
|
buf = strf("format=%s, in.address=%s, out.address=%s, rtcp.enabled=%s",
|
|
|
|
format_type_name(r->format),
|
|
|
|
local, remote,
|
|
|
|
r->rtcp.enabled ? "yes" : "no");
|
|
|
|
|
|
|
|
if (r->rtcp.enabled) {
|
2019-04-14 19:22:33 +02:00
|
|
|
const char *hook_type;
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
switch (r->aimd.rate_hook_type) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case RTPHookType::DECIMATE:
|
2019-04-14 19:22:33 +02:00
|
|
|
hook_type = "decimate";
|
2019-01-28 11:09:53 +01:00
|
|
|
break;
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case RTPHookType::LIMIT_RATE:
|
2019-04-14 19:22:33 +02:00
|
|
|
hook_type = "limit_rate";
|
2019-01-28 11:09:53 +01:00
|
|
|
break;
|
2019-01-23 14:16:53 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case RTPHookType::DISABLED:
|
2019-04-14 19:22:33 +02:00
|
|
|
hook_type = "disabled";
|
2019-01-28 11:09:53 +01:00
|
|
|
break;
|
2019-02-06 17:31:27 +01:00
|
|
|
|
|
|
|
default:
|
2019-04-14 19:22:33 +02:00
|
|
|
hook_type = "unknown";
|
2019-01-21 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
strcatf(&buf, ", aimd.hook_type=%s", hook_type);
|
|
|
|
strcatf(&buf, ", aimd.a=%f, aimd.b=%f, aimd.start_rate=%f", r->aimd.a, r->aimd.b, r->aimd.rate);
|
2019-01-21 12:12:47 +01:00
|
|
|
}
|
2018-11-22 17:53:07 +01:00
|
|
|
|
|
|
|
free(local);
|
|
|
|
free(remote);
|
2018-11-16 16:07:47 +01:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-11-28 06:11:13 +01:00
|
|
|
static void rtp_handler(const struct sa *src, const struct rtp_header *hdr, struct mbuf *mb, void *arg)
|
|
|
|
{
|
2019-01-28 10:53:34 +01:00
|
|
|
int ret;
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode *n = (struct vnode *) arg;
|
2019-01-21 12:12:47 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-12-07 15:15:24 +01:00
|
|
|
|
2019-01-25 17:26:08 +01:00
|
|
|
/* source, header not used */
|
2018-12-16 11:47:33 +01:00
|
|
|
(void) src;
|
|
|
|
(void) hdr;
|
2019-01-25 17:26:08 +01:00
|
|
|
|
2019-01-28 10:53:34 +01:00
|
|
|
void *d = mem_ref((void *) mb);
|
|
|
|
|
|
|
|
ret = queue_signalled_push(&r->recv_queue, d);
|
|
|
|
if (ret != 1) {
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->warn("Failed to push to queue");
|
2019-01-28 10:53:34 +01:00
|
|
|
mem_deref(d);
|
|
|
|
}
|
2018-11-28 06:11:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 08:25:13 +01:00
|
|
|
static void rtcp_handler(const struct sa *src, struct rtcp_msg *msg, void *arg)
|
|
|
|
{
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode *n = (struct vnode *) arg;
|
2019-01-28 12:30:47 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-01-25 17:26:08 +01:00
|
|
|
/* source not used */
|
2019-01-28 10:53:34 +01:00
|
|
|
(void) src;
|
2018-12-20 08:25:13 +01:00
|
|
|
|
2019-03-26 15:34:07 +01:00
|
|
|
debug(5, "RTCP: recv %s", rtcp_type_name((enum rtcp_type) msg->hdr.pt));
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-01-25 17:26:08 +01:00
|
|
|
if (msg->hdr.pt == RTCP_SR) {
|
2019-02-15 10:21:14 +01:00
|
|
|
if (msg->hdr.count > 0) {
|
2019-01-25 17:26:08 +01:00
|
|
|
const struct rtcp_rr *rr = &msg->r.sr.rrv[0];
|
2019-03-29 09:51:00 +01:00
|
|
|
|
2019-04-10 18:10:12 +02:00
|
|
|
double loss_frac = (double) rr->fraction / 256;
|
2019-03-29 09:51:00 +01:00
|
|
|
|
2019-04-10 18:10:12 +02:00
|
|
|
rtp_aimd(n, loss_frac);
|
2019-03-29 09:51:00 +01:00
|
|
|
|
|
|
|
if (n->stats) {
|
2019-06-23 13:35:42 +02:00
|
|
|
n->stats->update(Stats::Metric::RTP_PKTS_LOST, rr->lost);
|
|
|
|
n->stats->update(Stats::Metric::RTP_LOSS_FRACTION, loss_frac);
|
|
|
|
n->stats->update(Stats::Metric::RTP_JITTER, rr->jitter);
|
2019-03-29 09:51:00 +01:00
|
|
|
}
|
2019-04-10 18:10:12 +02:00
|
|
|
|
|
|
|
r->logger->info("RTCP: rr: num_rrs={}, loss_frac={}, pkts_lost={}, jitter={}", r->rtcp.num_rrs, loss_frac, rr->lost, rr->jitter);
|
2019-01-25 17:26:08 +01:00
|
|
|
}
|
2019-01-28 10:53:34 +01:00
|
|
|
else
|
2019-02-17 21:17:43 +01:00
|
|
|
debug(5, "RTCP: Received sender report with zero reception reports");
|
2019-01-25 17:26:08 +01:00
|
|
|
}
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-01-28 12:30:47 +01:00
|
|
|
r->rtcp.num_rrs++;
|
2018-12-20 08:25:13 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_start(struct vnode *n)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2018-11-22 17:53:07 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-12-07 15:15:24 +01:00
|
|
|
|
2019-01-28 10:54:09 +01:00
|
|
|
/* Initialize queue */
|
2019-06-23 16:13:23 +02:00
|
|
|
ret = queue_signalled_init(&r->recv_queue, 1024, &memory_heap);
|
2018-12-07 15:15:24 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-11-22 17:53:07 +01:00
|
|
|
/* Initialize IO */
|
2019-06-23 16:13:23 +02:00
|
|
|
ret = io_init(&r->io, r->format, &n->in.signals, (int) SampleFlags::HAS_ALL & ~(int) SampleFlags::HAS_OFFSET);
|
2018-11-22 17:53:07 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-01-28 09:57:20 +01:00
|
|
|
/* Initialize memory buffer for sending */
|
2019-01-28 10:53:01 +01:00
|
|
|
r->send_mb = mbuf_alloc(RTP_INITIAL_BUFFER_LEN);
|
|
|
|
if (!r->send_mb)
|
2019-01-28 09:57:20 +01:00
|
|
|
return -1;
|
|
|
|
|
2019-01-28 10:53:01 +01:00
|
|
|
ret = mbuf_fill(r->send_mb, 0, RTP_HEADER_SIZE);
|
2019-01-28 09:57:20 +01:00
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
/* Initialize AIMD hook */
|
2019-06-23 16:13:23 +02:00
|
|
|
if (r->aimd.rate_hook_type != RTPHookType::DISABLED) {
|
2019-04-06 14:19:50 +02:00
|
|
|
#ifdef WITH_HOOKS
|
2019-04-14 19:22:33 +02:00
|
|
|
switch (r->aimd.rate_hook_type) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case RTPHookType::DECIMATE:
|
2019-04-14 19:22:33 +02:00
|
|
|
r->aimd.rate_hook = new DecimateHook(nullptr, n, 0, 0);
|
2019-01-23 14:16:53 +01:00
|
|
|
break;
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case RTPHookType::LIMIT_RATE:
|
2019-04-14 19:22:33 +02:00
|
|
|
r->aimd.rate_hook = new LimitRateHook(nullptr, n, 0, 0);
|
2019-01-23 14:16:53 +01:00
|
|
|
break;
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2019-01-23 15:09:38 +01:00
|
|
|
default:
|
2019-03-26 15:34:07 +01:00
|
|
|
return -1;
|
2019-01-23 14:16:53 +01:00
|
|
|
}
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!r->aimd.rate_hook)
|
|
|
|
throw MemoryAllocationError();
|
|
|
|
|
2019-04-15 12:26:51 +02:00
|
|
|
r->aimd.rate_hook->init();
|
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
vlist_push(&n->out.hooks, (void *) r->aimd.rate_hook);
|
|
|
|
|
|
|
|
r->aimd.rate_hook->setRate(r->aimd.rate_last);
|
2019-04-06 14:19:50 +02:00
|
|
|
#else
|
2019-04-14 19:22:33 +02:00
|
|
|
r->logger->error("Rate limiting is not supported");
|
|
|
|
|
2019-04-06 14:19:50 +02:00
|
|
|
return -1;
|
|
|
|
#endif
|
2019-01-23 14:16:53 +01:00
|
|
|
}
|
2019-01-23 13:29:51 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
double dt = 5.0; // TODO
|
|
|
|
|
|
|
|
r->aimd.rate_pid = villas::dsp::PID(dt, r->aimd.rate_source, r->aimd.rate_min, r->aimd.Kp, r->aimd.Ki, r->aimd.Kd);
|
2019-01-21 12:12:47 +01:00
|
|
|
|
2018-11-28 18:12:06 +01:00
|
|
|
/* Initialize RTP socket */
|
2019-01-21 15:50:18 +01:00
|
|
|
uint16_t port = sa_port(&r->in.saddr_rtp) & ~1;
|
|
|
|
ret = rtp_listen(&r->rs, IPPROTO_UDP, &r->in.saddr_rtp, port, port+1, r->rtcp.enabled, rtp_handler, rtcp_handler, n);
|
2018-12-20 08:25:13 +01:00
|
|
|
|
|
|
|
/* Start RTCP session */
|
2019-01-28 12:30:47 +01:00
|
|
|
if (r->rtcp.enabled) {
|
|
|
|
r->rtcp.num_rrs = 0;
|
|
|
|
|
2019-01-23 14:16:53 +01:00
|
|
|
rtcp_start(r->rs, node_name(n), &r->out.saddr_rtcp);
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-04-14 19:22:33 +02:00
|
|
|
if (r->aimd.log_filename) {
|
2019-03-29 09:50:47 +01:00
|
|
|
char fn[128];
|
2019-01-28 12:30:47 +01:00
|
|
|
|
2019-03-29 09:45:12 +01:00
|
|
|
time_t ts = time(nullptr);
|
2019-01-28 12:30:47 +01:00
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
/* Convert time */
|
|
|
|
gmtime_r(&ts, &tm);
|
2019-03-29 09:50:47 +01:00
|
|
|
strftime(fn, sizeof(fn), r->aimd.log_filename, &tm);
|
2019-01-28 12:30:47 +01:00
|
|
|
|
2019-03-29 10:45:23 +01:00
|
|
|
r->aimd.log = new std::ofstream(fn, std::ios::out | std::ios::trunc);
|
2020-07-04 16:22:10 +02:00
|
|
|
if (!r->aimd.log)
|
|
|
|
throw MemoryAllocationError();
|
2019-01-28 12:30:47 +01:00
|
|
|
|
2019-03-29 10:45:23 +01:00
|
|
|
*(r->aimd.log) << "# cnt\tfrac_loss\trate" << std::endl;
|
2019-01-28 12:30:47 +01:00
|
|
|
}
|
2019-04-14 19:22:33 +02:00
|
|
|
else
|
|
|
|
r->aimd.log = nullptr;
|
2019-01-28 12:30:47 +01:00
|
|
|
}
|
|
|
|
|
2018-11-22 17:53:07 +01:00
|
|
|
return ret;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_stop(struct vnode *n)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2018-12-19 18:40:53 +01:00
|
|
|
int ret;
|
2018-11-28 18:12:06 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-01-16 16:41:02 +01:00
|
|
|
mem_deref(r->rs);
|
2018-12-01 12:31:12 +01:00
|
|
|
|
2019-01-07 15:22:38 +01:00
|
|
|
ret = queue_signalled_close(&r->recv_queue);
|
2019-03-29 09:50:27 +01:00
|
|
|
if (ret) {
|
|
|
|
r->logger->error("Problem closing queue");
|
|
|
|
return ret;
|
|
|
|
}
|
2018-12-19 18:40:53 +01:00
|
|
|
|
2019-01-07 15:22:38 +01:00
|
|
|
ret = queue_signalled_destroy(&r->recv_queue);
|
2019-03-29 09:50:27 +01:00
|
|
|
if (ret) {
|
|
|
|
r->logger->error("Problem destroying queue");
|
|
|
|
return ret;
|
|
|
|
}
|
2018-12-19 18:40:53 +01:00
|
|
|
|
2019-01-28 10:53:01 +01:00
|
|
|
mem_deref(r->send_mb);
|
2019-01-28 09:57:20 +01:00
|
|
|
|
2019-03-29 10:45:23 +01:00
|
|
|
if (r->aimd.log)
|
|
|
|
r->aimd.log->close();
|
2019-01-28 12:30:47 +01:00
|
|
|
|
2019-03-26 07:10:37 +01:00
|
|
|
ret = io_destroy(&r->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
return 0;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_destroy(struct vnode *n)
|
2019-03-29 09:50:47 +01:00
|
|
|
{
|
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
|
|
|
|
|
|
|
//r->logger.~Logger();
|
|
|
|
|
2019-03-29 10:45:23 +01:00
|
|
|
if (r->aimd.log)
|
|
|
|
delete r->aimd.log;
|
|
|
|
|
2019-03-29 09:50:47 +01:00
|
|
|
if (r->aimd.log_filename)
|
|
|
|
free(r->aimd.log_filename);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:53:04 +01:00
|
|
|
static void stop_handler(int sig, siginfo_t *si, void *ctx)
|
|
|
|
{
|
|
|
|
re_cancel();
|
|
|
|
}
|
|
|
|
|
2019-03-22 11:27:57 +01:00
|
|
|
typedef void *(*pthread_start_routine)(void *);
|
|
|
|
|
2019-04-23 13:14:47 +02:00
|
|
|
int rtp_type_start(villas::node::SuperNode *sn)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2018-12-07 06:37:48 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-12-01 12:31:12 +01:00
|
|
|
/* Initialize library */
|
2018-12-07 06:37:48 +01:00
|
|
|
ret = libre_init();
|
|
|
|
if (ret) {
|
2019-03-29 09:50:27 +01:00
|
|
|
error("Error initializing libre");
|
2018-12-07 06:37:48 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add worker thread */
|
2019-03-29 09:45:12 +01:00
|
|
|
ret = pthread_create(&re_pthread, nullptr, (pthread_start_routine) re_main, nullptr);
|
2018-12-07 06:37:48 +01:00
|
|
|
if (ret) {
|
2019-03-29 09:50:27 +01:00
|
|
|
error("Error creating rtp node type pthread");
|
2018-12-07 06:37:48 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-07 15:49:34 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = stop_handler;
|
|
|
|
|
2019-03-29 09:45:12 +01:00
|
|
|
ret = sigaction(SIGUSR1, &sa, nullptr);
|
2019-01-07 15:49:34 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef WITH_NETEM
|
|
|
|
/* Gather list of used network interfaces */
|
|
|
|
for (size_t i = 0; i < vlist_length(&p.node.instances); i++) {
|
2020-08-25 21:00:52 +02:00
|
|
|
struct vnode *n = (struct vnode *) vlist_at(&p.node.instances, i);
|
2019-01-21 15:50:18 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2020-09-13 11:01:20 +02:00
|
|
|
Interface *j = Interface::getEgress(&r->out.saddr_rtp.u.sa, sn);
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2019-03-29 09:50:27 +01:00
|
|
|
if (!j) {
|
|
|
|
r->logger->error("Failed to find egress interface for node: {}", node_name(n));
|
|
|
|
return -1;
|
|
|
|
}
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2020-09-13 11:01:20 +02:00
|
|
|
j->addNode(n);
|
2019-01-21 15:50:18 +01:00
|
|
|
}
|
|
|
|
#endif /* WITH_NETEM */
|
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
return 0;
|
2018-12-01 12:31:12 +01:00
|
|
|
}
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2018-12-01 12:31:12 +01:00
|
|
|
int rtp_type_stop()
|
|
|
|
{
|
2018-12-07 06:37:48 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Join worker thread */
|
2019-01-07 15:49:34 +01:00
|
|
|
pthread_kill(re_pthread, SIGUSR1);
|
2019-03-29 09:45:12 +01:00
|
|
|
ret = pthread_join(re_pthread, nullptr);
|
2018-12-07 06:37:48 +01:00
|
|
|
if (ret) {
|
|
|
|
error("Error joining rtp node type pthread");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-01 12:31:12 +01:00
|
|
|
libre_close();
|
2019-01-08 22:53:04 +01:00
|
|
|
|
2018-12-07 06:37:48 +01:00
|
|
|
return ret;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2019-01-28 10:53:34 +01:00
|
|
|
int ret;
|
2018-11-28 18:12:06 +01:00
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-12-13 18:50:18 +01:00
|
|
|
struct mbuf *mb;
|
2018-12-07 15:15:24 +01:00
|
|
|
|
2018-12-13 18:50:18 +01:00
|
|
|
/* Get data from queue */
|
2019-01-28 09:10:45 +01:00
|
|
|
ret = queue_signalled_pull(&r->recv_queue, (void **) &mb);
|
|
|
|
if (ret < 0) {
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->error("Failed to pull from queue");
|
2019-01-28 09:10:45 +01:00
|
|
|
return ret;
|
2018-12-07 15:15:24 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 18:50:18 +01:00
|
|
|
/* Unpack data */
|
2019-03-29 09:45:12 +01:00
|
|
|
ret = io_sscan(&r->io, (char *) mb->buf + mb->pos, mbuf_get_left(mb), nullptr, smps, cnt);
|
2019-01-28 10:53:34 +01:00
|
|
|
|
|
|
|
mem_deref(mb);
|
2018-12-13 18:50:18 +01:00
|
|
|
|
2018-12-07 15:15:24 +01:00
|
|
|
return ret;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
2018-11-16 16:07:47 +01:00
|
|
|
{
|
2018-11-28 18:12:06 +01:00
|
|
|
int ret;
|
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2018-11-28 18:12:06 +01:00
|
|
|
size_t wbytes;
|
2019-01-28 09:57:20 +01:00
|
|
|
size_t avail;
|
2018-11-28 18:12:06 +01:00
|
|
|
|
2019-03-29 09:45:12 +01:00
|
|
|
uint32_t ts = (uint32_t) time(nullptr);
|
2018-11-28 18:12:06 +01:00
|
|
|
|
2019-01-28 10:53:01 +01:00
|
|
|
retry: mbuf_set_pos(r->send_mb, RTP_HEADER_SIZE);
|
|
|
|
avail = mbuf_get_space(r->send_mb);
|
|
|
|
cnt = io_sprint(&r->io, (char *) r->send_mb->buf + r->send_mb->pos, avail, &wbytes, smps, cnt);
|
2019-01-28 09:57:20 +01:00
|
|
|
if (cnt < 0)
|
|
|
|
return -1;
|
2018-11-28 18:12:06 +01:00
|
|
|
|
2019-01-28 09:57:20 +01:00
|
|
|
if (wbytes > avail) {
|
2019-01-28 10:53:01 +01:00
|
|
|
ret = mbuf_resize(r->send_mb, wbytes + RTP_HEADER_SIZE);
|
2019-01-28 09:57:20 +01:00
|
|
|
if (!ret)
|
|
|
|
return -1;
|
2018-11-28 18:12:06 +01:00
|
|
|
|
|
|
|
goto retry;
|
|
|
|
}
|
2019-01-28 09:57:20 +01:00
|
|
|
else
|
2019-01-28 10:53:01 +01:00
|
|
|
mbuf_set_end(r->send_mb, r->send_mb->pos + wbytes);
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2019-01-28 10:53:34 +01:00
|
|
|
mbuf_set_pos(r->send_mb, RTP_HEADER_SIZE);
|
|
|
|
|
2018-11-28 18:12:06 +01:00
|
|
|
/* Send dataset */
|
2019-01-28 10:53:01 +01:00
|
|
|
ret = rtp_send(r->rs, &r->out.saddr_rtp, false, false, RTP_PACKET_TYPE, ts, r->send_mb);
|
2018-12-07 06:37:48 +01:00
|
|
|
if (ret) {
|
2019-03-29 09:50:27 +01:00
|
|
|
r->logger->error("Error from rtp_send, reason: {}", ret);
|
|
|
|
return ret;
|
2018-12-01 12:31:12 +01:00
|
|
|
}
|
2018-11-16 16:07:47 +01:00
|
|
|
|
2018-12-01 12:31:12 +01:00
|
|
|
return cnt;
|
2018-11-16 16:07:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_poll_fds(struct vnode *n, int fds[])
|
2019-01-21 15:50:18 +01:00
|
|
|
{
|
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
|
|
|
|
|
|
|
fds[0] = queue_signalled_fd(&r->recv_queue);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:00:52 +02:00
|
|
|
int rtp_netem_fds(struct vnode *n, int fds[])
|
2019-01-21 15:50:18 +01:00
|
|
|
{
|
|
|
|
struct rtp *r = (struct rtp *) n->_vd;
|
|
|
|
|
|
|
|
int m = 0;
|
|
|
|
struct udp_sock *rtp = (struct udp_sock *) rtp_sock(r->rs);
|
|
|
|
struct udp_sock *rtcp = (struct udp_sock *) rtcp_sock(r->rs);
|
|
|
|
|
|
|
|
fds[m++] = udp_sock_fd(rtp, AF_INET);
|
|
|
|
|
|
|
|
if (r->rtcp.enabled)
|
|
|
|
fds[m++] = udp_sock_fd(rtcp, AF_INET);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:12:34 +01:00
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static void register_plugin() {
|
2019-04-23 00:36:06 +02:00
|
|
|
p.name = "rtp";
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef WITH_NETEM
|
2019-04-23 00:36:06 +02:00
|
|
|
p.description = "real-time transport protocol (libre, libnl3 netem support)";
|
2019-01-21 15:50:18 +01:00
|
|
|
#else
|
2019-04-23 00:36:06 +02:00
|
|
|
p.description = "real-time transport protocol (libre)";
|
2019-01-21 15:50:18 +01:00
|
|
|
#endif
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::NODE;
|
|
|
|
p.node.instances.state = State::DESTROYED;
|
2019-03-27 14:12:34 +01:00
|
|
|
p.node.vectorize = 0;
|
|
|
|
p.node.size = sizeof(struct rtp);
|
|
|
|
p.node.type.start = rtp_type_start;
|
|
|
|
p.node.type.stop = rtp_type_stop;
|
|
|
|
p.node.init = rtp_init;
|
2019-03-29 09:50:47 +01:00
|
|
|
p.node.destroy = rtp_destroy;
|
2019-03-27 14:12:34 +01:00
|
|
|
p.node.parse = rtp_parse;
|
|
|
|
p.node.print = rtp_print;
|
|
|
|
p.node.start = rtp_start;
|
|
|
|
p.node.stop = rtp_stop;
|
|
|
|
p.node.read = rtp_read;
|
|
|
|
p.node.write = rtp_write;
|
|
|
|
p.node.reverse = rtp_reverse;
|
|
|
|
p.node.poll_fds = rtp_poll_fds;
|
|
|
|
p.node.netem_fds = rtp_netem_fds;
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret = vlist_init(&p.node.instances);
|
|
|
|
if (!ret)
|
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-03-27 14:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((destructor(110)))
|
|
|
|
static void deregister_plugin() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-03-27 14:12:34 +01:00
|
|
|
}
|