diff --git a/server/include/cfg.h b/server/include/cfg.h index 66484cd44..016aa49d2 100644 --- a/server/include/cfg.h +++ b/server/include/cfg.h @@ -34,13 +34,12 @@ struct settings { * @param set The global configuration structure * @param nodes A linked list of nodes which should be parsed * @param paths A linked list of paths which should be parsed - * @param interfaces A linked list of interfaces which should be parsed * @return * - 0 on success * - otherwise an error occured */ int config_parse(const char *filename, config_t *cfg, struct settings *set, - struct node **nodes, struct path **paths, struct interface **interfaces); + struct node **nodes, struct path **paths); /** Parse the global section of a configuration file. * @@ -68,13 +67,12 @@ int config_parse_path(config_setting_t *cfg, * * @param cfg A libconfig object pointing to the node * @param nodes Add new nodes to this linked list - * @param interfaces Search this list for existing interfaces * @return * - 0 on success * - otherwise an error occured */ int config_parse_node(config_setting_t *cfg, - struct node **nodes, struct interface **interfaces); + struct node **nodes); /** Parse network emulator (netem) settings * diff --git a/server/src/cfg.c b/server/src/cfg.c index e6faa8bfa..fb2fa5c2f 100644 --- a/server/src/cfg.c +++ b/server/src/cfg.c @@ -19,7 +19,7 @@ #include "utils.h" int config_parse(const char *filename, config_t *cfg, struct settings *set, - struct node **nodes, struct path **paths, struct interface **interfaces) + struct node **nodes, struct path **paths) { config_set_auto_convert(cfg, 1); @@ -50,7 +50,7 @@ int config_parse(const char *filename, config_t *cfg, struct settings *set, /* Parse nodes */ for (int i = 0; i < config_setting_length(cfg_nodes); i++) { config_setting_t *cfg_node = config_setting_get_elem(cfg_nodes, i); - config_parse_node(cfg_node, nodes, interfaces); + config_parse_node(cfg_node, nodes); } /* Parse paths */ @@ -131,8 +131,7 @@ int config_parse_path(config_setting_t *cfg, return 0; } -int config_parse_node(config_setting_t *cfg, - struct node **nodes, struct interface **interfaces) +int config_parse_node(config_setting_t *cfg, struct node **nodes) { const char *remote_str = NULL; const char *local_str = NULL; @@ -170,21 +169,6 @@ int config_parse_node(config_setting_t *cfg, config_parse_netem(cfg_netem, node->netem); } - /* Determine outgoing interface */ - int index = if_getegress(&node->remote); - struct interface *i = if_lookup_index(index, *interfaces); - if (!i) { - i = malloc(sizeof(struct interface)); - memset(i, 0, sizeof(struct interface)); - - i->index = index; - - list_add(*interfaces, i); - } - - node->mark = 1 + i->refcnt++; - node->interface = i; - node->cfg = cfg; list_add(*nodes, node); diff --git a/server/src/if.c b/server/src/if.c index 47e0b3bc4..68a3c3b4e 100644 --- a/server/src/if.c +++ b/server/src/if.c @@ -32,16 +32,14 @@ int if_getegress(struct sockaddr_in *sa) if (!p) return -1; - while (fscanf(p, "%31s", token)) { + while (!feof(p) && fscanf(p, "%31s", token)) { if (!strcmp(token, "dev")) { fscanf(p, "%31s", token); break; } } - fclose(p); - - return if_nametoindex(token); + return (WEXITSTATUS(fclose(p))) ? -1 : if_nametoindex(token); } int if_getirqs(struct interface *i) diff --git a/server/src/server.c b/server/src/server.c index a18c47e95..eca3af8f3 100644 --- a/server/src/server.c +++ b/server/src/server.c @@ -37,32 +37,50 @@ static config_t config; static void start() { - /* Configure interfaces */ - for (struct interface *i = interfaces; i; i = i->next) { - if_indextoname(i->index, i->name); - - debug(3, "Setup interface '%s'", - i->name, i->index, i->refcnt); - - if (settings.affinity) { - if_getirqs(i); - if_setaffinity(i, settings.affinity); - } - - /* Create priority queuing discipline */ - tc_prio(i, TC_HDL(4000, 0), i->refcnt); - } - /* Connect and bind nodes to their sockets, set socket options */ for (struct node *n = nodes; n; n = n->next) { - node_connect(n); + /* Determine outgoing interface */ + int index = if_getegress(&n->remote); + if (index < 0) + error("Failed to get egress interface for node '%s'", n->name); + + n->interface = if_lookup_index(index, interfaces); + + /* 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)); + + n->interface->index = index; + if_indextoname(index, n->interface->name); + + 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++; /* Create queueing discipline */ - if (n->netem) { + if (n->netem && n->mark) { tc_mark(n->interface, TC_HDL(4000, n->mark), n->mark); tc_netem(n->interface, TC_HDL(4000, n->mark), n->netem); } + node_connect(n); + debug(1, " We listen for node '%s' at %s:%u", n->name, inet_ntoa(n->local.sin_addr), ntohs(n->local.sin_port)); @@ -145,7 +163,7 @@ int main(int argc, char *argv[]) /* Parse configuration file */ config_init(&config); - config_parse(argv[1], &config, &settings, &nodes, &paths, &interfaces); + config_parse(argv[1], &config, &settings, &nodes, &paths); /* Check for realtime kernel patch */ struct stat st;