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>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-25 17:50:30 +00:00
|
|
|
#include <unistd.h>
|
2014-06-05 09:34:36 +00:00
|
|
|
#include <errno.h>
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-03-31 16:58:25 +02:00
|
|
|
#include <sys/timerfd.h>
|
2014-06-25 17:50:30 +00:00
|
|
|
#include <sys/syscall.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
2015-05-06 11:49:13 +02:00
|
|
|
#include "socket.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-03-21 15:29:00 +01:00
|
|
|
#ifndef sigev_notify_thread_id
|
|
|
|
#define sigev_notify_thread_id _sigev_un._tid
|
|
|
|
#endif
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-03-21 15:19:41 +01:00
|
|
|
/** Linked list of paths. */
|
|
|
|
struct list paths;
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-03-18 15:53:01 +01:00
|
|
|
/** Send messages asynchronously */
|
2014-06-25 17:50:30 +00:00
|
|
|
static void * path_send(void *arg)
|
|
|
|
{
|
2015-03-21 15:30:42 +01:00
|
|
|
struct path *p = arg;
|
2014-06-25 17:50:30 +00:00
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = timespec_rate(p->rate),
|
|
|
|
.it_value = { 1, 0 }
|
|
|
|
};
|
|
|
|
|
2015-03-31 16:58:25 +02:00
|
|
|
p->tfd = timerfd_create(CLOCK_REALTIME, 0);
|
|
|
|
if (p->tfd < 0)
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to create timer");
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-05-06 13:20:02 +02:00
|
|
|
if (timerfd_settime(p->tfd, 0, &its, NULL))
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to start timer");
|
2014-06-25 17:50:30 +00:00
|
|
|
|
2015-03-18 15:53:01 +01:00
|
|
|
|
2015-03-21 18:04:52 +01:00
|
|
|
FOREACH(&p->destinations, it)
|
2015-05-06 11:49:13 +02:00
|
|
|
p->sent += node_write(p->in, p->pool, p->poolsize, p->received, p->in->combine);
|
|
|
|
|
|
|
|
debug(10, "Sent %u messages to %u destination nodes", p->in->combine, p->destinations.length);
|
2014-06-25 17:50:30 +00:00
|
|
|
}
|
2015-05-06 13:20:02 +02:00
|
|
|
/* Block until 1/p->rate seconds elapsed */
|
|
|
|
while (timerfd_wait(p->tfd))
|
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-05-06 11:49:13 +02:00
|
|
|
|
|
|
|
/* Allocate memory for message pool */
|
|
|
|
p->pool = alloc(p->poolsize * sizeof(struct msg));
|
2015-03-21 18:04:52 +01:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
/* Open deferred TCP connection
|
2014-12-05 12:39:52 +01:00
|
|
|
node_start_defer(p->in);
|
2015-03-31 18:29:07 +02:00
|
|
|
|
|
|
|
FOREACH(&p->destinations, it)
|
2015-05-06 11:49:13 +02:00
|
|
|
node_start_defer(it->node); */
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* Main thread loop */
|
2015-05-06 11:49:13 +02:00
|
|
|
skip: while (1) {
|
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-04-01 13:26:46 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
debug(10, "Received %u messages from node '%s'", recv, p->in->name);
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
/* For each received message... */
|
|
|
|
for (int i=0; i<recv; i++) {
|
|
|
|
p->previous = &p->pool[(p->received-1) % p->poolsize];
|
|
|
|
p->current = &p->pool[ p->received % p->poolsize];
|
|
|
|
|
|
|
|
p->received++;
|
|
|
|
|
|
|
|
/* Check header fields */
|
|
|
|
if (msg_verify(p->current)) {
|
|
|
|
p->invalid++;
|
|
|
|
goto skip; /* Drop message */
|
|
|
|
}
|
2014-09-11 14:40:58 +00:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
/* Update histogram and handle wrap-around of sequence number */
|
|
|
|
int dist = (UINT16_MAX + p->current->sequence - p->previous->sequence) % UINT16_MAX;
|
|
|
|
if (dist > UINT16_MAX / 2)
|
|
|
|
dist -= UINT16_MAX;
|
2014-09-09 09:03:06 +00:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
hist_put(&p->histogram, dist);
|
2014-09-11 14:40:58 +00:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
/* Handle simulation restart */
|
|
|
|
if (p->current->sequence == 0 && abs(dist) >= 1) {
|
|
|
|
char buf[33];
|
|
|
|
path_print(p, buf, sizeof(buf));
|
|
|
|
warn("Simulation for path %s restarted (prev->seq=%u, current->seq=%u, dist=%d)",
|
|
|
|
buf, p->previous->sequence, p->current->sequence, dist);
|
2014-09-09 13:42:36 +00:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
path_reset(p);
|
|
|
|
}
|
|
|
|
else if (dist <= 0 && p->received > 1) {
|
|
|
|
p->dropped++;
|
|
|
|
goto skip;
|
|
|
|
}
|
2014-09-09 09:03:06 +00:00
|
|
|
}
|
2015-05-06 11:49:13 +02:00
|
|
|
|
2014-09-11 14:40:58 +00:00
|
|
|
/* Call hook callbacks */
|
2015-03-18 16:13:18 +01:00
|
|
|
FOREACH(&p->hooks, it) {
|
2015-03-21 18:04:52 +01:00
|
|
|
if (it->hook(p->current, p)) {
|
2015-03-18 16:13:18 +01:00
|
|
|
p->skipped++;
|
2015-05-06 11:49:13 +02:00
|
|
|
goto skip;
|
2015-03-18 16:13:18 +01:00
|
|
|
}
|
2014-07-04 15:58:11 +00:00
|
|
|
}
|
2015-05-06 11:49:13 +02:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* At fixed rate mode, messages are send by another thread */
|
2014-09-09 09:03:06 +00:00
|
|
|
if (!p->rate) {
|
2015-03-21 18:04:52 +01:00
|
|
|
FOREACH(&p->destinations, it)
|
2015-05-06 11:49:13 +02:00
|
|
|
node_write(p->in, p->pool, p->poolsize, p->received, p->in->combine);
|
2015-03-21 18:04:52 +01:00
|
|
|
|
2014-09-04 13:30:38 +00:00
|
|
|
p->sent++;
|
2014-09-09 09:03:06 +00:00
|
|
|
}
|
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-05-06 11:49:13 +02:00
|
|
|
info("Starting path: %s (poolsize = %u)", buf, p->poolsize);
|
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 */
|
|
|
|
if (p->rate)
|
2015-03-21 15:31:42 +01:00
|
|
|
pthread_create(&p->sent_tid, NULL, &path_send, p);
|
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));
|
|
|
|
|
|
|
|
info("Stopping path: %s", buf);
|
2014-12-05 12:39:52 +01: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
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-03-21 18:04:52 +01:00
|
|
|
if (p->received)
|
2015-01-20 10:52:00 +00:00
|
|
|
hist_print(&p->histogram);
|
2014-09-09 09:03:11 +00:00
|
|
|
|
2014-06-05 09:35:04 +00:00
|
|
|
return 0;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2014-07-04 09:47:28 +00:00
|
|
|
|
2015-04-01 13:25:03 +02:00
|
|
|
int path_reset(struct path *p)
|
|
|
|
{
|
|
|
|
p->sent = 0;
|
|
|
|
p->received = 1;
|
|
|
|
p->invalid = 0;
|
|
|
|
p->skipped = 0;
|
|
|
|
p->dropped = 0;
|
|
|
|
|
|
|
|
hist_reset(&p->histogram);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-21 15:31:42 +01:00
|
|
|
void path_print_stats(struct path *p)
|
2014-07-04 09:47:28 +00:00
|
|
|
{
|
2015-03-18 15:45:06 +01:00
|
|
|
char buf[33];
|
|
|
|
path_print(p, buf, sizeof(buf));
|
|
|
|
|
2015-03-21 15:31:42 +01:00
|
|
|
info("%-32s : %-8u %-8u %-8u %-8u %-8u", buf,
|
|
|
|
p->sent, p->received, p->dropped, p->skipped, p->invalid);
|
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-03-31 16:44:33 +02:00
|
|
|
strap(buf, len, "%s " MAG("=>"), p->in->name);
|
|
|
|
|
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-03-18 15:45:06 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-18 15:47:18 +01:00
|
|
|
|
2015-03-21 15:23:57 +01:00
|
|
|
struct path * path_create()
|
|
|
|
{
|
|
|
|
struct path *p = alloc(sizeof(struct path));
|
|
|
|
|
|
|
|
list_init(&p->destinations, NULL);
|
|
|
|
list_init(&p->hooks, NULL);
|
|
|
|
|
|
|
|
hist_create(&p->histogram, -HIST_SEQ, +HIST_SEQ, 1);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void path_destroy(struct path *p)
|
2015-03-18 15:47:18 +01:00
|
|
|
{
|
|
|
|
list_destroy(&p->destinations);
|
|
|
|
list_destroy(&p->hooks);
|
2015-03-21 15:23:57 +01:00
|
|
|
hist_destroy(&p->histogram);
|
2015-03-18 15:47:18 +01: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
|
|
|
}
|