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>
|
2014-12-05 12:41:06 +01:00
|
|
|
#include <string.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
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-10 19:44:22 +00:00
|
|
|
#include <sys/stat.h>
|
2014-09-11 14:40:43 +00:00
|
|
|
#include <netinet/ip.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 */
|
2014-12-05 12:41:06 +01:00
|
|
|
extern struct node *nodes;
|
2014-06-10 18:47:25 +00:00
|
|
|
/** Linked list of paths */
|
2014-12-05 12:41:06 +01:00
|
|
|
extern struct path *paths;
|
2014-06-25 01:53:46 +00:00
|
|
|
/** Linked list of interfaces */
|
2014-12-05 12:41:06 +01:00
|
|
|
extern struct interface *interfaces;
|
2014-06-10 18:47:25 +00:00
|
|
|
|
2014-07-04 09:44:12 +00:00
|
|
|
/** The global configuration */
|
2014-12-05 12:41:06 +01:00
|
|
|
struct settings settings;
|
|
|
|
config_t config;
|
2014-06-10 16:57:40 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
static void quit()
|
|
|
|
{ _indent = 0;
|
|
|
|
info("Stopping paths:");
|
|
|
|
for (struct path *p = paths; p; p = p->next) { INDENT
|
2014-06-05 09:34:50 +00:00
|
|
|
path_stop(p);
|
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
info("Stopping nodes:");
|
|
|
|
for (struct node *n = nodes; n; n = n->next) { INDENT
|
|
|
|
node_stop(n);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2014-06-25 01:53:49 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
info("Stopping interfaces:");
|
|
|
|
for (struct interface *i = interfaces; i; i = i->next) { INDENT
|
|
|
|
if_stop(i);
|
2014-06-25 01:53:49 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
void realtime_init()
|
|
|
|
{ INDENT
|
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))
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to set realtime priority");
|
2014-07-04 09:44:12 +00:00
|
|
|
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))
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to set CPU affinity to '%#x'", settings.affinity);
|
2014-07-04 09:44:12 +00:00
|
|
|
else
|
|
|
|
debug(3, "Set affinity to %#x", settings.affinity);
|
|
|
|
}
|
2014-12-05 12:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup exit handler */
|
|
|
|
void signals_init()
|
|
|
|
{ INDENT
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage(const char *name)
|
|
|
|
{
|
|
|
|
printf("Usage: %s CONFIG\n", name);
|
|
|
|
printf(" CONFIG is a required path to a configuration file\n\n");
|
|
|
|
printf("Simulator2Simulator Server %s (built on %s, %s)\n",
|
|
|
|
BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
/* Check arguments */
|
|
|
|
if (argc != 2)
|
|
|
|
usage(argv[0]);
|
2015-03-17 23:11:28 +01:00
|
|
|
|
|
|
|
epoch_reset();
|
|
|
|
info("This is Simulator2Simulator Server (S2SS) %s (built on %s, %s)",
|
|
|
|
BLD(YEL(VERSION)), BLD(MAG(__DATE__)), BLD(MAG(__TIME__)));
|
2014-12-05 12:41:06 +01:00
|
|
|
|
|
|
|
/* Check priviledges */
|
|
|
|
if (getuid() != 0)
|
|
|
|
error("The server requires superuser privileges!");
|
|
|
|
|
|
|
|
/* Start initialization */
|
|
|
|
info("Initialize realtime system:");
|
|
|
|
realtime_init();
|
|
|
|
info("Setup signals:");
|
|
|
|
signals_init();
|
|
|
|
info("Parsing configuration:");
|
|
|
|
config_init(&config);
|
|
|
|
|
|
|
|
/* Parse configuration and create nodes/paths */
|
|
|
|
config_parse(argv[1], &config, &settings, &nodes, &paths);
|
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/* Connect all nodes and start one thread per path */
|
2014-12-05 12:41:06 +01:00
|
|
|
info("Starting nodes:");
|
|
|
|
for (struct node *n = nodes; n; n = n->next) { INDENT
|
|
|
|
node_start(n);
|
|
|
|
}
|
2014-06-05 09:35:10 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
info("Starting interfaces:");
|
|
|
|
for (struct interface *i = interfaces; i; i = i->next) { INDENT
|
|
|
|
if_start(i, settings.affinity);
|
|
|
|
}
|
|
|
|
|
|
|
|
info("Starting pathes:");
|
|
|
|
for (struct path *p = paths; p; p = p->next) { INDENT
|
|
|
|
path_start(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run! */
|
2014-09-07 16:28:54 +00:00
|
|
|
if (settings.stats > 0) {
|
2014-07-04 09:47:28 +00:00
|
|
|
struct path *p = paths;
|
|
|
|
|
|
|
|
info("Runtime Statistics:");
|
2014-07-04 15:58:10 +00:00
|
|
|
info("%12s " MAG("=>") " %-12s: %-8s %-8s %-8s %-8s %-8s",
|
2014-09-09 09:03:06 +00:00
|
|
|
"Source", "Destination", "#Sent", "#Recv", "#Drop", "#Skip", "#Inval");
|
2014-07-04 15:58:10 +00:00
|
|
|
info("---------------------------------------------------------------------------");
|
2014-07-04 09:47:28 +00:00
|
|
|
|
|
|
|
while (1) {
|
2014-09-07 16:28:54 +00:00
|
|
|
usleep(settings.stats * 1e6);
|
2014-07-04 09:47:28 +00:00
|
|
|
path_stats(p);
|
|
|
|
|
2014-09-07 16:28:54 +00:00
|
|
|
p = (p->next) ? p->next : paths;
|
2014-07-04 09:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pause();
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
/* Note: quit() is called by exit handler! */
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|