2014-07-14 11:49:44 +00:00
|
|
|
/** Message paths.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2015-06-02 21:53:04 +02:00
|
|
|
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
2015-08-07 01:11:43 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-06-25 17:50:30 +00:00
|
|
|
#include <unistd.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
2015-06-02 21:59:31 +02:00
|
|
|
#include "timing.h"
|
2015-06-02 22:04:03 +02:00
|
|
|
#include "config.h"
|
2015-06-10 15:12:36 +02:00
|
|
|
#include "stats.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-03-21 15:29:00 +01:00
|
|
|
#ifndef sigev_notify_thread_id
|
2015-09-19 12:24:11 +02:00
|
|
|
#define sigev_notify_thread_id _sigev_un._tid
|
2015-03-21 15:29:00 +01:00
|
|
|
#endif
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-06-02 22:04:03 +02:00
|
|
|
extern struct settings settings;
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-05-06 13:22:22 +02:00
|
|
|
static void path_write(struct path *p)
|
|
|
|
{
|
|
|
|
FOREACH(&p->destinations, it) {
|
|
|
|
int sent = node_write(
|
|
|
|
it->node, /* Destination node */
|
|
|
|
p->pool, /* Pool of received messages */
|
|
|
|
p->poolsize, /* Size of the pool */
|
|
|
|
p->received-it->node->combine, /* Index of the first message which should be sent */
|
|
|
|
it->node->combine /* Number of messages which should be sent */
|
|
|
|
);
|
|
|
|
|
|
|
|
debug(1, "Sent %u messages to node '%s'", sent, it->node->name);
|
|
|
|
p->sent += sent;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:05:36 +02:00
|
|
|
clock_gettime(CLOCK_REALTIME, &p->ts_sent);
|
2015-05-06 13:22:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 12:20:17 +02:00
|
|
|
int path_run_hook(struct path *p, enum hook_type t)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
FOREACH(&p->hooks[t], it)
|
|
|
|
ret += ((hook_cb_t) it->ptr)(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:53:01 +01:00
|
|
|
/** Send messages asynchronously */
|
2015-05-06 13:22:22 +02:00
|
|
|
static void * path_run_async(void *arg)
|
2014-06-25 17:50:30 +00:00
|
|
|
{
|
2015-03-21 15:30:42 +01:00
|
|
|
struct path *p = arg;
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-05-06 13:20:02 +02:00
|
|
|
/* Block until 1/p->rate seconds elapsed */
|
2015-08-07 01:11:43 +02:00
|
|
|
while (timerfd_wait(p->tfd))
|
2015-05-06 13:22:22 +02:00
|
|
|
path_write(p);
|
2014-06-25 17:50:30 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Receive messages */
|
2014-06-05 09:34:29 +00:00
|
|
|
static void * path_run(void *arg)
|
|
|
|
{
|
2015-03-17 23:23:13 +01:00
|
|
|
struct path *p = arg;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
/* Allocate memory for message pool */
|
|
|
|
p->pool = alloc(p->poolsize * sizeof(struct msg));
|
2015-05-07 13:01:18 +02:00
|
|
|
p->previous = p->current = p->pool;
|
2015-03-21 18:04:52 +01:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* Main thread loop */
|
2015-06-10 15:10:51 +02:00
|
|
|
for(;;) {
|
2015-04-01 13:26:46 +02:00
|
|
|
/* Receive message */
|
2015-05-06 11:49:13 +02:00
|
|
|
int recv = node_read(p->in, p->pool, p->poolsize, p->received, p->in->combine);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-05 12:40:59 +02:00
|
|
|
/** @todo Replace this timestamp by hardware timestamping */
|
2015-06-10 15:05:36 +02:00
|
|
|
clock_gettime(CLOCK_REALTIME, &p->ts_recv);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
debug(10, "Received %u messages from node '%s'", recv, p->in->name);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
/* Run preprocessing hooks */
|
2015-09-19 12:20:17 +02:00
|
|
|
if (path_run_hook(p, HOOK_PRE)) {
|
2015-06-10 15:10:51 +02:00
|
|
|
p->skipped += recv;
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
/* For each received message... */
|
2015-06-10 15:10:51 +02:00
|
|
|
for (int i = 0; i < recv; i++) {
|
2015-08-07 01:11:43 +02:00
|
|
|
p->previous = p->current;
|
|
|
|
p->current = &p->pool[ p->received % p->poolsize];
|
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
p->received++;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
/* Run hooks for filtering, stats collection and manipulation */
|
2015-09-19 12:20:17 +02:00
|
|
|
if (path_run_hook(p, HOOK_MSG)) {
|
2015-06-10 15:10:51 +02:00
|
|
|
p->skipped++;
|
|
|
|
continue;
|
2015-05-06 11:49:13 +02:00
|
|
|
}
|
2014-09-09 09:03:06 +00:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
/* Run post processing hooks */
|
2015-09-19 12:20:17 +02:00
|
|
|
if (path_run_hook(p, HOOK_POST)) {
|
2015-06-10 15:10:51 +02:00
|
|
|
p->skipped += recv;
|
|
|
|
continue;
|
2014-07-04 15:58:11 +00:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* At fixed rate mode, messages are send by another thread */
|
2015-05-06 13:22:22 +02:00
|
|
|
if (!p->rate)
|
|
|
|
path_write(p);
|
2015-03-21 18:04:52 +01:00
|
|
|
}
|
2014-07-18 16:05:44 +00:00
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int path_start(struct path *p)
|
2014-12-05 12:39:52 +01:00
|
|
|
{ INDENT
|
2015-03-18 15:45:06 +01:00
|
|
|
char buf[33];
|
|
|
|
path_print(p, buf, sizeof(buf));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
info("Starting path: %s (poolsize = %u)", buf, p->poolsize);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-19 12:20:17 +02:00
|
|
|
if (path_run_hook(p, HOOK_PATH_START))
|
2015-06-10 15:10:51 +02:00
|
|
|
return -1;
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* At fixed rate mode, we start another thread for sending */
|
2015-06-10 15:13:02 +02:00
|
|
|
if (p->rate) {
|
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = time_from_double(1 / p->rate),
|
|
|
|
.it_value = { 1, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
p->tfd = timerfd_create(CLOCK_REALTIME, 0);
|
|
|
|
if (p->tfd < 0)
|
|
|
|
serror("Failed to create timer");
|
|
|
|
|
|
|
|
if (timerfd_settime(p->tfd, 0, &its, NULL))
|
|
|
|
serror("Failed to start timer");
|
|
|
|
|
2015-05-06 13:22:22 +02:00
|
|
|
pthread_create(&p->sent_tid, NULL, &path_run_async, p);
|
2015-06-10 15:13:02 +02:00
|
|
|
}
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-03-21 15:31:42 +01:00
|
|
|
return pthread_create(&p->recv_tid, NULL, &path_run, p);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int path_stop(struct path *p)
|
2014-12-05 12:39:52 +01:00
|
|
|
{ INDENT
|
2015-03-18 15:45:06 +01:00
|
|
|
char buf[33];
|
|
|
|
path_print(p, buf, sizeof(buf));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:45:06 +01:00
|
|
|
info("Stopping path: %s", buf);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-09-09 09:03:01 +00:00
|
|
|
pthread_cancel(p->recv_tid);
|
|
|
|
pthread_join(p->recv_tid, NULL);
|
2014-06-25 17:50:30 +00:00
|
|
|
|
|
|
|
if (p->rate) {
|
2014-09-09 09:03:01 +00:00
|
|
|
pthread_cancel(p->sent_tid);
|
|
|
|
pthread_join(p->sent_tid, NULL);
|
2015-03-21 15:30:42 +01:00
|
|
|
|
2015-03-31 16:58:25 +02:00
|
|
|
close(p->tfd);
|
2014-06-25 17:50:30 +00:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-19 12:20:17 +02:00
|
|
|
if (path_run_hook(p, HOOK_PATH_STOP))
|
2015-06-10 15:10:51 +02:00
|
|
|
return -1;
|
2015-04-01 13:25:03 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
return 0;
|
2014-07-04 09:47:28 +00:00
|
|
|
}
|
2015-03-18 15:45:06 +01:00
|
|
|
|
|
|
|
int path_print(struct path *p, char *buf, int len)
|
|
|
|
{
|
|
|
|
*buf = 0;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-31 16:44:33 +02:00
|
|
|
strap(buf, len, "%s " MAG("=>"), p->in->name);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:45:06 +01:00
|
|
|
if (list_length(&p->destinations) > 1) {
|
2015-04-01 13:25:03 +02:00
|
|
|
strap(buf, len, " [");
|
2015-03-18 15:45:06 +01:00
|
|
|
FOREACH(&p->destinations, it)
|
|
|
|
strap(buf, len, " %s", it->node->name);
|
|
|
|
strap(buf, len, " ]");
|
|
|
|
}
|
|
|
|
else
|
2015-03-31 16:44:33 +02:00
|
|
|
strap(buf, len, " %s", p->out->name);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:45:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-18 15:47:18 +01:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
int path_reset(struct path *p)
|
|
|
|
{
|
2015-09-19 12:20:17 +02:00
|
|
|
if (path_run_hook(p, HOOK_PATH_RESTART))
|
2015-06-10 15:10:51 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
p->sent =
|
|
|
|
p->received =
|
|
|
|
p->invalid =
|
|
|
|
p->skipped =
|
|
|
|
p->dropped = 0;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-21 15:23:57 +01:00
|
|
|
struct path * path_create()
|
|
|
|
{
|
|
|
|
struct path *p = alloc(sizeof(struct path));
|
|
|
|
|
|
|
|
list_init(&p->destinations, NULL);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
for (int i = 0; i < HOOK_MAX; i++)
|
|
|
|
list_init(&p->hooks[i], NULL);
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
#define hook_add(type, priority, cb) list_insert(&p->hooks[type], priority, cb)
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
hook_add(HOOK_MSG, 1, hook_verify);
|
|
|
|
hook_add(HOOK_MSG, 2, hook_restart);
|
|
|
|
hook_add(HOOK_MSG, 3, hook_drop);
|
2015-06-10 15:12:36 +02:00
|
|
|
hook_add(HOOK_MSG, 4, stats_collect);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:12:36 +02:00
|
|
|
hook_add(HOOK_PATH_START, 1, stats_start);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:12:36 +02:00
|
|
|
hook_add(HOOK_PATH_STOP, 2, stats_show);
|
|
|
|
hook_add(HOOK_PATH_STOP, 3, stats_stop);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:12:36 +02:00
|
|
|
hook_add(HOOK_PATH_RESTART, 1, stats_line);
|
|
|
|
hook_add(HOOK_PATH_RESTART, 3, stats_reset);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:12:36 +02:00
|
|
|
hook_add(HOOK_PERIODIC, 1, stats_line);
|
2015-03-21 15:23:57 +01:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void path_destroy(struct path *p)
|
2015-03-18 15:47:18 +01:00
|
|
|
{
|
|
|
|
list_destroy(&p->destinations);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-06-10 15:10:51 +02:00
|
|
|
for (int i = 0; i < HOOK_MAX; i++)
|
|
|
|
list_destroy(&p->hooks[i]);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
free(p->pool);
|
2015-03-21 15:23:57 +01:00
|
|
|
free(p);
|
2015-03-18 15:47:18 +01:00
|
|
|
}
|