2014-07-14 11:49:44 +00:00
|
|
|
/** Configuration parser.
|
2014-06-05 09:34:32 +00:00
|
|
|
*
|
2014-06-05 09:35:41 +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:32 +00:00
|
|
|
|
2014-06-10 19:44:21 +00:00
|
|
|
#include <stdlib.h>
|
2014-12-05 12:34:45 +01:00
|
|
|
#include <string.h>
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
#include "utils.h"
|
|
|
|
#include "list.h"
|
2014-06-25 01:53:35 +00:00
|
|
|
#include "if.h"
|
2014-06-05 09:34:50 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:35:23 +00:00
|
|
|
#include "node.h"
|
|
|
|
#include "path.h"
|
2014-08-31 14:43:28 +00:00
|
|
|
#include "hooks.h"
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2015-06-03 10:13:35 +02:00
|
|
|
#include "file.h"
|
2015-09-14 20:04:04 +02:00
|
|
|
#ifdef ENABLE_SOCKET
|
|
|
|
#include "socket.h"
|
|
|
|
#endif
|
2015-03-31 14:28:00 +02:00
|
|
|
#ifdef ENABLE_GTFPGA
|
2015-08-21 10:19:32 +02:00
|
|
|
#include "gtfpga.h"
|
2015-03-31 14:28:00 +02:00
|
|
|
#endif
|
2015-03-21 11:47:08 +01:00
|
|
|
#ifdef ENABLE_OPAL_ASYNC
|
2015-08-21 10:19:32 +02:00
|
|
|
#include "opal.h"
|
2015-03-21 11:47:08 +01:00
|
|
|
#endif
|
2014-12-05 12:34:45 +01:00
|
|
|
|
2014-06-25 01:53:46 +00:00
|
|
|
int config_parse(const char *filename, config_t *cfg, struct settings *set,
|
2015-03-21 15:19:41 +01:00
|
|
|
struct list *nodes, struct list *paths)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2014-07-04 09:44:09 +00:00
|
|
|
config_set_auto_convert(cfg, 1);
|
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
int ret = strcmp("-", filename) ? config_read_file(cfg, filename)
|
|
|
|
: config_read(cfg, stdin);
|
2015-01-16 15:42:09 +00:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
if (ret != CONFIG_TRUE)
|
2014-06-05 09:34:50 +00:00
|
|
|
error("Failed to parse configuration: %s in %s:%d",
|
2015-03-20 18:55:14 +01:00
|
|
|
config_error_text(cfg),
|
|
|
|
config_error_file(cfg) ? config_error_file(cfg) : filename,
|
2014-06-05 09:34:50 +00:00
|
|
|
config_error_line(cfg)
|
|
|
|
);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
config_setting_t *cfg_root = config_root_setting(cfg);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/* Parse global settings */
|
2014-12-05 12:39:52 +01:00
|
|
|
if (set) {
|
|
|
|
if (!cfg_root || !config_setting_is_group(cfg_root))
|
|
|
|
error("Missing global section in config file: %s", filename);
|
2014-06-10 16:57:40 +00:00
|
|
|
|
2014-12-05 12:39:52 +01:00
|
|
|
config_parse_global(cfg_root, set);
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
/* Parse nodes */
|
2014-12-05 12:39:52 +01:00
|
|
|
if (nodes) {
|
|
|
|
config_setting_t *cfg_nodes = config_setting_get_member(cfg_root, "nodes");
|
|
|
|
if (!cfg_nodes || !config_setting_is_group(cfg_nodes))
|
|
|
|
error("Missing node section in config file: %s", filename);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-12-05 12:39:52 +01:00
|
|
|
for (int i = 0; i < config_setting_length(cfg_nodes); i++) {
|
|
|
|
config_setting_t *cfg_node = config_setting_get_elem(cfg_nodes, i);
|
2015-09-17 01:33:19 +02:00
|
|
|
config_parse_node(cfg_node, nodes, set);
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
/* Parse paths */
|
2014-12-05 12:39:52 +01:00
|
|
|
if (paths) {
|
|
|
|
config_setting_t *cfg_paths = config_setting_get_member(cfg_root, "paths");
|
|
|
|
if (!cfg_paths || !config_setting_is_list(cfg_paths))
|
|
|
|
error("Missing path section in config file: %s", filename);
|
|
|
|
|
|
|
|
for (int i = 0; i < config_setting_length(cfg_paths); i++) {
|
|
|
|
config_setting_t *cfg_path = config_setting_get_elem(cfg_paths, i);
|
2015-09-17 01:33:19 +02:00
|
|
|
config_parse_path(cfg_path, paths, nodes, set);
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
return 0;
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
int config_parse_global(config_setting_t *cfg, struct settings *set)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2014-06-10 18:47:25 +00:00
|
|
|
config_setting_lookup_int(cfg, "affinity", &set->affinity);
|
|
|
|
config_setting_lookup_int(cfg, "priority", &set->priority);
|
2014-09-07 16:28:50 +00:00
|
|
|
config_setting_lookup_int(cfg, "debug", &set->debug);
|
2014-09-07 16:28:54 +00:00
|
|
|
config_setting_lookup_float(cfg, "stats", &set->stats);
|
2014-09-07 16:28:50 +00:00
|
|
|
|
2015-05-06 11:43:05 +02:00
|
|
|
log_setlevel(set->debug);
|
2014-06-05 09:35:41 +00:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
return 0;
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
int config_parse_path(config_setting_t *cfg,
|
2015-09-17 01:33:19 +02:00
|
|
|
struct list *paths, struct list *nodes, struct settings *set)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2015-03-18 15:50:02 +01:00
|
|
|
const char *in;
|
2015-05-06 11:49:13 +02:00
|
|
|
int enabled;
|
|
|
|
int reverse;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
struct path *p = path_create();
|
2014-06-25 01:53:35 +00:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
/* Input node */
|
2015-10-06 09:02:00 +02:00
|
|
|
config_setting_t *cfg_in = config_setting_get_member(cfg, "in");
|
2015-03-18 15:50:02 +01:00
|
|
|
if (!cfg_in || config_setting_type(cfg_in) != CONFIG_TYPE_STRING)
|
|
|
|
cerror(cfg, "Invalid input node for path");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
in = config_setting_get_string(cfg_in);
|
2015-09-19 15:26:30 +02:00
|
|
|
p->in = list_lookup(nodes, in);
|
2014-09-11 14:40:43 +00:00
|
|
|
if (!p->in)
|
2015-03-20 18:55:14 +01:00
|
|
|
cerror(cfg_in, "Invalid input node '%s'", in);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
/* Output node(s) */
|
2015-10-06 09:02:00 +02:00
|
|
|
config_setting_t *cfg_out = config_setting_get_member(cfg, "out");
|
2015-03-18 15:50:02 +01:00
|
|
|
if (cfg_out)
|
|
|
|
config_parse_nodelist(cfg_out, &p->destinations, nodes);
|
|
|
|
|
|
|
|
if (list_length(&p->destinations) >= 1)
|
2015-10-09 12:50:35 +02:00
|
|
|
p->out = (struct node *) list_first(&p->destinations);
|
2015-03-18 15:50:02 +01:00
|
|
|
else
|
|
|
|
cerror(cfg, "Missing output node for path");
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-06-25 17:50:30 +00:00
|
|
|
/* Optional settings */
|
2015-10-06 09:02:00 +02:00
|
|
|
config_setting_t *cfg_hook = config_setting_get_member(cfg, "hook");
|
2015-03-18 16:13:18 +01:00
|
|
|
if (cfg_hook)
|
2015-06-10 14:58:10 +02:00
|
|
|
config_parse_hooklist(cfg_hook, p->hooks);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
if (!config_setting_lookup_bool(cfg, "enabled", &enabled))
|
|
|
|
enabled = 1;
|
|
|
|
if (!config_setting_lookup_bool(cfg, "reverse", &reverse))
|
|
|
|
reverse = 0;
|
|
|
|
if (!config_setting_lookup_float(cfg, "rate", &p->rate))
|
|
|
|
p->rate = 0; /* disabled */
|
|
|
|
if (!config_setting_lookup_int(cfg, "poolsize", &p->poolsize))
|
|
|
|
p->poolsize = DEFAULT_POOLSIZE;
|
2015-10-09 13:06:59 +02:00
|
|
|
if (!config_setting_lookup_int(cfg, "msgsize", &p->msgsize))
|
|
|
|
p->msgsize = MAX_VALUES;
|
2014-08-31 14:43:28 +00:00
|
|
|
|
2014-09-11 14:40:43 +00:00
|
|
|
p->cfg = cfg;
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-06-25 01:53:35 +00:00
|
|
|
if (enabled) {
|
2014-09-11 14:40:48 +00:00
|
|
|
p->in->refcnt++;
|
2015-03-31 13:54:04 +02:00
|
|
|
p->in->vt->refcnt++;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-09 12:50:35 +02:00
|
|
|
list_foreach(struct node *node, &p->destinations) {
|
|
|
|
node->refcnt++;
|
|
|
|
node->vt->refcnt++;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2014-06-25 01:53:32 +00:00
|
|
|
if (reverse) {
|
2015-03-18 15:50:02 +01:00
|
|
|
if (list_length(&p->destinations) > 1)
|
2015-03-31 14:28:00 +02:00
|
|
|
error("Can't reverse path with multiple destination nodes");
|
2014-09-11 14:40:48 +00:00
|
|
|
|
2015-03-21 15:23:57 +01:00
|
|
|
struct path *r = path_create();
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
r->in = p->out; /* Swap in/out */
|
|
|
|
r->out = p->in;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
list_push(&r->destinations, r->out);
|
2014-06-25 01:53:48 +00:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
r->in->refcnt++;
|
|
|
|
r->out->refcnt++;
|
2015-03-31 13:54:04 +02:00
|
|
|
r->in->vt->refcnt++;
|
|
|
|
r->out->vt->refcnt++;
|
2015-03-18 15:50:02 +01:00
|
|
|
|
2015-03-21 15:19:41 +01:00
|
|
|
list_push(paths, r);
|
2014-06-25 01:53:32 +00:00
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-21 15:19:41 +01:00
|
|
|
list_push(paths, p);
|
2014-06-05 09:34:50 +00:00
|
|
|
}
|
2014-06-25 01:53:35 +00:00
|
|
|
else {
|
2015-09-22 12:58:37 +02:00
|
|
|
char *buf = path_print(p);
|
2015-03-18 15:45:06 +01:00
|
|
|
warn("Path %s is not enabled", buf);
|
2015-09-22 12:58:37 +02:00
|
|
|
free(buf);
|
|
|
|
|
2015-03-18 15:47:18 +01:00
|
|
|
path_destroy(p);
|
2014-06-25 01:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-06-05 09:34:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
int config_parse_nodelist(config_setting_t *cfg, struct list *list, struct list *all) {
|
2015-03-18 15:50:02 +01:00
|
|
|
const char *str;
|
|
|
|
struct node *node;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
switch (config_setting_type(cfg)) {
|
|
|
|
case CONFIG_TYPE_STRING:
|
|
|
|
str = config_setting_get_string(cfg);
|
2015-09-19 15:28:28 +02:00
|
|
|
if (str) {
|
2015-09-19 18:46:04 +02:00
|
|
|
node = list_lookup(all, str);
|
2015-09-19 15:28:28 +02:00
|
|
|
if (node)
|
|
|
|
list_push(list, node);
|
|
|
|
else
|
|
|
|
cerror(cfg, "Unknown outgoing node '%s'", str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cerror(cfg, "Invalid outgoing node");
|
2015-03-18 15:50:02 +01:00
|
|
|
break;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
case CONFIG_TYPE_ARRAY:
|
2015-09-19 15:28:28 +02:00
|
|
|
for (int i = 0; i < config_setting_length(cfg); i++) {
|
|
|
|
config_setting_t *elm = config_setting_get_elem(cfg, i);
|
|
|
|
|
|
|
|
str = config_setting_get_string(elm);
|
|
|
|
if (str) {
|
|
|
|
node = list_lookup(all, str);
|
|
|
|
if (node)
|
|
|
|
list_push(list, node);
|
|
|
|
else
|
|
|
|
cerror(elm, "Unknown outgoing node '%s'", str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cerror(cfg, "Invalid outgoing node");
|
2015-03-18 15:50:02 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
default:
|
|
|
|
cerror(cfg, "Invalid output node(s)");
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 15:50:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
int config_parse_hooklist(config_setting_t *cfg, struct list *list) {
|
2015-03-18 16:13:18 +01:00
|
|
|
const char *str;
|
2015-06-10 15:10:51 +02:00
|
|
|
const struct hook *hook;
|
|
|
|
|
2015-03-18 16:13:18 +01:00
|
|
|
switch (config_setting_type(cfg)) {
|
|
|
|
case CONFIG_TYPE_STRING:
|
|
|
|
str = config_setting_get_string(cfg);
|
2015-09-19 15:28:28 +02:00
|
|
|
if (str) {
|
|
|
|
hook = list_lookup(&hooks, str);
|
|
|
|
if (hook)
|
|
|
|
list_insert(&list[hook->type], hook->priority, hook->callback);
|
|
|
|
else
|
|
|
|
cerror(cfg, "Unknown hook function '%s'", str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cerror(cfg, "Invalid hook function");
|
2015-03-18 16:13:18 +01:00
|
|
|
break;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 16:13:18 +01:00
|
|
|
case CONFIG_TYPE_ARRAY:
|
2015-09-19 15:28:28 +02:00
|
|
|
for (int i = 0; i<config_setting_length(cfg); i++) {
|
|
|
|
config_setting_t *elm = config_setting_get_elem(cfg, i);
|
|
|
|
|
|
|
|
str = config_setting_get_string(elm);
|
|
|
|
if (str) {
|
|
|
|
hook = list_lookup(&hooks, str);
|
|
|
|
if (hook)
|
|
|
|
list_insert(&list[hook->type], hook->priority, hook->callback);
|
|
|
|
else
|
|
|
|
cerror(elm, "Invalid hook function '%s'", str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cerror(cfg, "Invalid hook function");
|
2015-03-18 16:13:18 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 16:13:18 +01:00
|
|
|
default:
|
|
|
|
cerror(cfg, "Invalid hook functions");
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-18 16:13:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-17 01:33:19 +02:00
|
|
|
int config_parse_node(config_setting_t *cfg, struct list *nodes, struct settings *set)
|
2014-06-05 09:34:50 +00:00
|
|
|
{
|
2014-12-05 12:39:52 +01:00
|
|
|
const char *type;
|
2014-09-10 12:22:21 +00:00
|
|
|
int ret;
|
2014-06-25 01:53:35 +00:00
|
|
|
|
2015-03-21 15:23:57 +01:00
|
|
|
struct node *n = node_create();
|
2014-06-05 09:34:50 +00:00
|
|
|
|
|
|
|
/* Required settings */
|
2014-12-05 12:39:52 +01:00
|
|
|
n->cfg = cfg;
|
2014-09-11 14:40:43 +00:00
|
|
|
n->name = config_setting_name(cfg);
|
|
|
|
if (!n->name)
|
2014-06-10 18:47:25 +00:00
|
|
|
cerror(cfg, "Missing node name");
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2015-03-21 15:29:00 +01:00
|
|
|
if (!config_setting_lookup_string(cfg, "type", &type))
|
2015-05-06 11:49:13 +02:00
|
|
|
cerror(cfg, "Missing node type");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
if (!config_setting_lookup_int(cfg, "combine", &n->combine))
|
|
|
|
n->combine = 1;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-17 01:33:19 +02:00
|
|
|
if (!config_setting_lookup_int(cfg, "affinity", &n->combine))
|
|
|
|
n->affinity = set->affinity;
|
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
n->vt = list_lookup(&node_types, type);
|
2015-03-21 15:29:00 +01:00
|
|
|
if (!n->vt)
|
|
|
|
cerror(cfg, "Invalid type for node '%s'", n->name);
|
2014-12-05 12:39:52 +01:00
|
|
|
|
|
|
|
ret = n->vt->parse(cfg, n);
|
2015-03-20 18:55:14 +01:00
|
|
|
if (!ret)
|
2015-03-21 15:19:41 +01:00
|
|
|
list_push(nodes, n);
|
2014-12-05 12:39:52 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-05 12:34:45 +01:00
|
|
|
|