2014-07-14 11:49:44 +00:00
|
|
|
/** Main routine.
|
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 <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <error.h>
|
|
|
|
|
2014-06-10 19:44:22 +00:00
|
|
|
#include <sched.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2014-06-05 09:35:18 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-06-10 19:44:22 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "cfg.h"
|
2014-06-25 01:53:46 +00:00
|
|
|
#include "if.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "msg.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "node.h"
|
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/** Linked list of nodes */
|
|
|
|
static struct node *nodes;
|
|
|
|
/** Linked list of paths */
|
|
|
|
static struct path *paths;
|
2014-06-25 01:53:46 +00:00
|
|
|
/** Linked list of interfaces */
|
|
|
|
static struct interface *interfaces;
|
2014-06-10 18:47:25 +00:00
|
|
|
|
2014-07-04 09:44:12 +00:00
|
|
|
/** The global configuration */
|
|
|
|
static struct settings settings;
|
2014-06-10 16:57:40 +00:00
|
|
|
static config_t config;
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
static void start()
|
2014-06-05 09:35:12 +00:00
|
|
|
{
|
2014-07-04 15:58:10 +00:00
|
|
|
/* Connect and bind nodes to their sockets, set socket options */
|
|
|
|
for (struct node *n = nodes; n; n = n->next) {
|
|
|
|
/* Determine outgoing interface */
|
|
|
|
int index = if_getegress(&n->remote);
|
|
|
|
if (index < 0)
|
|
|
|
error("Failed to get egress interface for node '%s'", n->name);
|
2014-06-25 01:53:49 +00:00
|
|
|
|
2014-07-04 15:58:10 +00:00
|
|
|
n->interface = if_lookup_index(index, interfaces);
|
2014-06-25 01:53:49 +00:00
|
|
|
|
2014-07-04 15:58:10 +00:00
|
|
|
/* Create new interface */
|
|
|
|
if (!n->interface) {
|
|
|
|
n->interface = malloc(sizeof(struct interface));
|
|
|
|
if (!n->interface)
|
|
|
|
error("Failed to allocate memory for interface");
|
|
|
|
else
|
|
|
|
memset(n->interface, 0, sizeof(struct interface));
|
2014-06-25 01:53:49 +00:00
|
|
|
|
2014-07-04 15:58:10 +00:00
|
|
|
n->interface->index = index;
|
|
|
|
if_indextoname(index, n->interface->name);
|
2014-06-25 01:53:49 +00:00
|
|
|
|
2014-07-04 15:58:10 +00:00
|
|
|
debug(3, "Setup interface '%s'", n->interface->name,
|
|
|
|
n->interface->index, n->interface->refcnt);
|
|
|
|
|
|
|
|
if (settings.affinity) {
|
|
|
|
if_getirqs(n->interface);
|
|
|
|
if_setaffinity(n->interface, settings.affinity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create priority queuing discipline */
|
|
|
|
tc_prio(n->interface, TC_HDL(4000, 0), n->interface->refcnt);
|
|
|
|
|
|
|
|
list_add(interfaces, n->interface);
|
|
|
|
}
|
|
|
|
|
|
|
|
n->mark = 1 + n->interface->refcnt++;
|
2014-06-05 09:35:18 +00:00
|
|
|
|
2014-06-25 01:53:44 +00:00
|
|
|
/* Create queueing discipline */
|
2014-07-04 15:58:10 +00:00
|
|
|
if (n->netem && n->mark) {
|
2014-06-25 01:53:44 +00:00
|
|
|
tc_mark(n->interface, TC_HDL(4000, n->mark), n->mark);
|
|
|
|
tc_netem(n->interface, TC_HDL(4000, n->mark), n->netem);
|
|
|
|
}
|
|
|
|
|
2014-07-04 15:58:10 +00:00
|
|
|
node_connect(n);
|
|
|
|
|
2014-06-25 01:53:44 +00:00
|
|
|
debug(1, " We listen for node '%s' at %s:%u",
|
|
|
|
n->name, inet_ntoa(n->local.sin_addr),
|
|
|
|
ntohs(n->local.sin_port));
|
|
|
|
debug(1, " We sent to node '%s' at %s:%u",
|
|
|
|
n->name, inet_ntoa(n->remote.sin_addr),
|
|
|
|
ntohs(n->remote.sin_port));
|
2014-06-05 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Start on thread per path for asynchronous processing */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct path *p = paths; p; p = p->next) {
|
2014-06-05 09:35:12 +00:00
|
|
|
path_start(p);
|
|
|
|
|
2014-07-04 09:47:28 +00:00
|
|
|
info("Starting path: %12s " GRN("=>") " %-12s",
|
|
|
|
p->in->name, p->out->name);
|
2014-06-05 09:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
static void stop()
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Join all threads and print statistics */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct path *p = paths; p; p = p->next) {
|
2014-06-05 09:34:50 +00:00
|
|
|
path_stop(p);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-07-04 09:47:28 +00:00
|
|
|
info("Stopping path: %12s " RED("=>") " %-12s",
|
|
|
|
p->in->name, p->out->name);
|
2014-06-05 09:34:50 +00:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-25 01:53:44 +00:00
|
|
|
/* Close all sockets we listen on */
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct node *n = nodes; n; n = n->next) {
|
2014-06-05 09:35:12 +00:00
|
|
|
node_disconnect(n);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2014-06-25 01:53:49 +00:00
|
|
|
|
|
|
|
/* Reset interface queues and affinity */
|
|
|
|
for (struct interface *i = interfaces; i; i = i->next) {
|
2014-07-04 09:44:10 +00:00
|
|
|
if_setaffinity(i, -1);
|
2014-06-25 01:53:49 +00:00
|
|
|
tc_reset(i);
|
|
|
|
}
|
2014-06-05 09:35:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
static void quit()
|
2014-06-05 09:35:12 +00:00
|
|
|
{
|
|
|
|
stop();
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-07-04 09:44:11 +00:00
|
|
|
/** @todo Free nodes and paths */
|
2014-06-10 18:47:25 +00:00
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
config_destroy(&config);
|
2014-06-05 09:34:40 +00:00
|
|
|
|
2014-06-05 09:34:45 +00:00
|
|
|
_exit(EXIT_SUCCESS);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Check arguments */
|
2014-06-05 09:34:50 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
printf("Usage: %s CONFIG\n", argv[0]);
|
|
|
|
printf(" CONFIG is a required path to a configuration file\n\n");
|
2014-07-04 09:44:11 +00:00
|
|
|
printf("Simulator2Simulator Server %s (built on %s, %s)\n", BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
|
2014-06-05 09:34:45 +00:00
|
|
|
printf(" Copyright 2014, Institute for Automation of Complex Power Systems, EONERC\n");
|
2014-06-25 01:53:37 +00:00
|
|
|
printf(" Steffen Vogel <stvogel@eonerc.rwth-aachen.de>\n");
|
2014-06-05 09:34:29 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:44:11 +00:00
|
|
|
info("This is %s %s", BLU("s2ss"), BLU(VERSION));
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-09-07 16:28:50 +00:00
|
|
|
/* Setup exit handler */
|
2014-07-04 09:47:29 +00:00
|
|
|
struct sigaction sa_quit = {
|
|
|
|
.sa_flags = SA_SIGINFO,
|
|
|
|
.sa_sigaction = quit
|
|
|
|
};
|
|
|
|
|
|
|
|
sigemptyset(&sa_quit.sa_mask);
|
|
|
|
sigaction(SIGTERM, &sa_quit, NULL);
|
|
|
|
sigaction(SIGINT, &sa_quit, NULL);
|
|
|
|
atexit(&quit);
|
|
|
|
|
2014-06-05 09:35:35 +00:00
|
|
|
/* Parse configuration file */
|
2014-06-10 16:57:40 +00:00
|
|
|
config_init(&config);
|
2014-07-04 15:58:10 +00:00
|
|
|
config_parse(argv[1], &config, &settings, &nodes, &paths);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-09-07 16:28:50 +00:00
|
|
|
debug(1, "Running with debug level: %u", settings.debug);
|
|
|
|
|
|
|
|
/* Check priviledges */
|
|
|
|
if (getuid() != 0)
|
|
|
|
error("The server requires superuser privileges!");
|
|
|
|
|
2014-06-10 19:44:22 +00:00
|
|
|
/* Check for realtime kernel patch */
|
|
|
|
struct stat st;
|
|
|
|
if (stat("/sys/kernel/realtime", &st))
|
2014-06-25 01:53:37 +00:00
|
|
|
warn("Use a RT-preempt patched Linux for lower latencies!");
|
2014-06-10 19:44:22 +00:00
|
|
|
else
|
2014-08-31 14:43:28 +00:00
|
|
|
info("Server is running on a realtime patched kernel");
|
2014-06-10 19:44:22 +00:00
|
|
|
|
|
|
|
/* Use FIFO scheduler with realtime priority */
|
2014-07-04 09:44:12 +00:00
|
|
|
if (settings.priority) {
|
|
|
|
struct sched_param param = { .sched_priority = settings.priority };
|
|
|
|
if (sched_setscheduler(0, SCHED_FIFO, ¶m))
|
|
|
|
perror("Failed to set realtime priority");
|
|
|
|
else
|
|
|
|
debug(3, "Set task priority to %u", settings.priority);
|
|
|
|
}
|
2014-06-10 19:44:22 +00:00
|
|
|
|
|
|
|
/* Pin threads to CPUs by setting the affinity */
|
2014-07-04 09:44:12 +00:00
|
|
|
if (settings.affinity) {
|
|
|
|
cpu_set_t cset = to_cpu_set(settings.affinity);
|
|
|
|
if (sched_setaffinity(0, sizeof(cset), &cset))
|
|
|
|
perror("Failed to set CPU affinity to '%#x'", settings.affinity);
|
|
|
|
else
|
|
|
|
debug(3, "Set affinity to %#x", settings.affinity);
|
|
|
|
}
|
2014-06-05 09:35:12 +00:00
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/* Connect all nodes and start one thread per path */
|
2014-06-05 09:35:35 +00:00
|
|
|
start();
|
2014-06-05 09:35:10 +00:00
|
|
|
|
2014-07-04 09:47:28 +00:00
|
|
|
if (V >= 5) {
|
|
|
|
struct path *p = paths;
|
|
|
|
|
|
|
|
info("");
|
|
|
|
info("Runtime Statistics:");
|
2014-07-04 15:58:10 +00:00
|
|
|
info("%12s " MAG("=>") " %-12s: %-8s %-8s %-8s %-8s %-8s",
|
|
|
|
"Source", "Destination", "#Sent", "#Recv", "#Delay", "#Dupl", "#Inval");
|
|
|
|
info("---------------------------------------------------------------------------");
|
2014-07-04 09:47:28 +00:00
|
|
|
|
|
|
|
while (1) {
|
2014-09-04 13:30:36 +00:00
|
|
|
sleep(1);
|
2014-07-04 09:47:28 +00:00
|
|
|
path_stats(p);
|
|
|
|
|
|
|
|
p = (p->next) ? p->next : paths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pause();
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|