From 7714673b65daf1fd0266d2855d9bfc91d735b51a Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Tue, 14 Mar 2017 16:06:33 +0100 Subject: ipcpd: normal: Add graph structure This adds a graph structure which will be updated by routing when it is notified about a new RIB event. The routing can then use this graph as input for calculating the shortest path to a destination. --- src/ipcpd/normal/CMakeLists.txt | 1 + src/ipcpd/normal/graph.c | 277 ++++++++++++++++++++++++++++++++++++++++ src/ipcpd/normal/graph.h | 67 ++++++++++ src/ipcpd/normal/routing.c | 57 +++------ src/ipcpd/normal/routing.h | 1 - 5 files changed, 365 insertions(+), 38 deletions(-) create mode 100644 src/ipcpd/normal/graph.c create mode 100644 src/ipcpd/normal/graph.h (limited to 'src') diff --git a/src/ipcpd/normal/CMakeLists.txt b/src/ipcpd/normal/CMakeLists.txt index af99b3f9..39ced7bc 100644 --- a/src/ipcpd/normal/CMakeLists.txt +++ b/src/ipcpd/normal/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCE_FILES fmgr.c frct.c gam.c + graph.c main.c neighbors.c pff.c diff --git a/src/ipcpd/normal/graph.c b/src/ipcpd/normal/graph.c new file mode 100644 index 00000000..85bb3fe2 --- /dev/null +++ b/src/ipcpd/normal/graph.c @@ -0,0 +1,277 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2017 + * + * Graph structure + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#define OUROBOROS_PREFIX "graph" + +#include +#include +#include +#include + +#include "graph.h" + +#include +#include +#include + +static struct edge * find_edge_by_addr(struct vertex * vertex, + uint64_t dst_addr) +{ + struct list_head * p = NULL; + + list_for_each(p, &vertex->edges) { + struct edge * e = list_entry(p, struct edge, next); + if (e->dst_addr == dst_addr) + return e; + } + + return NULL; +} + +static struct vertex * find_vertex_by_addr(struct graph * graph, + uint64_t addr) +{ + struct list_head * p = NULL; + + list_for_each(p, &graph->vertices) { + struct vertex * e = list_entry(p, struct vertex, next); + if (e->addr == addr) + return e; + } + + return NULL; +} + +static int add_edge(struct vertex * vertex, + uint64_t dst_addr, + qosspec_t qs) +{ + struct edge * edge; + + edge = malloc(sizeof(*edge)); + if (edge == NULL) + return -ENOMEM; + + list_head_init(&edge->next); + edge->dst_addr = dst_addr; + edge->qs = qs; + + list_add(&edge->next, &vertex->edges); + + return 0; +} + +static void del_edge(struct edge * edge) +{ + list_del(&edge->next); + free(edge); +} + +static int add_vertex(struct graph * graph, + uint64_t addr) +{ + struct vertex * vertex; + struct list_head * p; + + vertex = malloc(sizeof(*vertex)); + if (vertex == NULL) + return -1; + + list_head_init(&vertex->next); + list_head_init(&vertex->edges); + vertex->addr = addr; + + list_for_each(p, &graph->vertices) { + struct vertex * v = list_entry(p, struct vertex, next); + if (v->addr > addr) + break; + } + + list_add_tail(&vertex->next, p); + + graph->nr_vertices++; + + return 0; +} + +static void del_vertex(struct graph * graph, + struct vertex * vertex) +{ + struct list_head * p = NULL; + struct list_head * n = NULL; + + list_del(&vertex->next); + + list_for_each_safe(p, n, &vertex->edges) { + struct edge * e = list_entry(p, struct edge, next); + del_edge(e); + } + + free(vertex); + + graph->nr_vertices--; +} + +struct graph * graph_create(void) +{ + struct graph * graph; + + graph = malloc(sizeof(*graph)); + if (graph == NULL) + return NULL; + + if (pthread_mutex_init(&graph->lock, NULL)) { + free(graph); + return NULL; + } + + graph->nr_vertices = 0; + list_head_init(&graph->vertices); + + return graph; +} + +void graph_destroy(struct graph * graph) +{ + struct list_head * p = NULL; + struct list_head * n = NULL; + + assert(graph); + + pthread_mutex_lock(&graph->lock); + + list_for_each_safe(p, n, &graph->vertices) { + struct vertex * e = list_entry(p, struct vertex, next); + del_vertex(graph, e); + } + + pthread_mutex_unlock(&graph->lock); + + pthread_mutex_destroy(&graph->lock); +} + +int graph_add_edge(struct graph * graph, + uint64_t s_addr, + uint64_t d_addr, + qosspec_t qs) +{ + struct vertex * v; + struct edge * e; + + assert(graph); + + pthread_mutex_lock(&graph->lock); + + v = find_vertex_by_addr(graph, s_addr); + if (v == NULL) { + if (add_vertex(graph, s_addr)) { + pthread_mutex_unlock(&graph->lock); + return -ENOMEM; + } + } + + e = find_edge_by_addr(v, d_addr); + if (e != NULL) { + pthread_mutex_unlock(&graph->lock); + log_err("Edge already exists."); + return -1; + } + + if (add_edge(v, d_addr, qs)) { + pthread_mutex_unlock(&graph->lock); + log_err("Failed to add edge."); + return -1; + } + + pthread_mutex_unlock(&graph->lock); + + return 0; +} + +int graph_update_edge(struct graph * graph, + uint64_t s_addr, + uint64_t d_addr, + qosspec_t qs) +{ + struct vertex * v; + struct edge * e; + + assert(graph); + + pthread_mutex_lock(&graph->lock); + + v = find_vertex_by_addr(graph, s_addr); + if (v == NULL) { + pthread_mutex_unlock(&graph->lock); + log_err("No such vertex."); + return -1; + } + + e = find_edge_by_addr(v, d_addr); + if (e == NULL) { + pthread_mutex_unlock(&graph->lock); + log_err("No such edge."); + return -1; + } + + e->qs = qs; + + pthread_mutex_unlock(&graph->lock); + + return 0; +} + +int graph_del_edge(struct graph * graph, + uint64_t s_addr, + uint64_t d_addr) +{ + struct vertex * v; + struct edge * e; + + assert(graph); + + pthread_mutex_lock(&graph->lock); + + v = find_vertex_by_addr(graph, s_addr); + if (v == NULL) { + pthread_mutex_unlock(&graph->lock); + log_err("No such vertex."); + return -1; + } + + e = find_edge_by_addr(v, d_addr); + if (e == NULL) { + pthread_mutex_unlock(&graph->lock); + log_err("No such edge."); + return -1; + } + + del_edge(e); + + /* Removing vertex if it was the last edge */ + if (list_is_empty(&v->edges)) + del_vertex(graph, v); + + pthread_mutex_unlock(&graph->lock); + + return 0; +} diff --git a/src/ipcpd/normal/graph.h b/src/ipcpd/normal/graph.h new file mode 100644 index 00000000..9653efd7 --- /dev/null +++ b/src/ipcpd/normal/graph.h @@ -0,0 +1,67 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2017 + * + * Graph structure + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef OUROBOROS_IPCPD_NORMAL_GRAPH_H +#define OUROBOROS_IPCPD_NORMAL_GRAPH_H + +#include +#include + +#include + +struct edge { + struct list_head next; + uint64_t dst_addr; + qosspec_t qs; +}; + +struct vertex { + struct list_head next; + uint64_t addr; + struct list_head edges; +}; + +struct graph { + size_t nr_vertices; + struct list_head vertices; + pthread_mutex_t lock; +}; + +struct graph * graph_create(void); + +void graph_destroy(struct graph * graph); + +int graph_add_edge(struct graph * graph, + uint64_t s_addr, + uint64_t d_addr, + qosspec_t qs); + +int graph_update_edge(struct graph * graph, + uint64_t s_addr, + uint64_t d_addr, + qosspec_t qs); + +int graph_del_edge(struct graph * graph, + uint64_t s_addr, + uint64_t d_addr); + +#endif /* OUROBOROS_IPCPD_NORMAL_GRAPH_H */ diff --git a/src/ipcpd/normal/routing.c b/src/ipcpd/normal/routing.c index e699fd77..46c2be08 100644 --- a/src/ipcpd/normal/routing.c +++ b/src/ipcpd/normal/routing.c @@ -32,6 +32,7 @@ #include "ribmgr.h" #include "ribconfig.h" #include "ipcp.h" +#include "graph.h" #include #include @@ -40,53 +41,25 @@ #define ADDR_SIZE 30 -struct edge { +struct routing_table_entry { struct list_head next; - - uint64_t addr; - - qosspec_t qs; -}; - -struct vertex { - struct list_head next; - - uint64_t addr; - - struct list_head edges; + uint64_t dst; + uint64_t nhop; }; struct routing_i { struct pff * pff; - struct list_head vertices; + struct list_head routing_table; }; struct { struct nbs * nbs; struct nb_notifier nb_notifier; - char fso_path[RIB_MAX_PATH_LEN + 1]; -} routing; - -#if 0 -/* FIXME: If zeroed since it is not used currently */ -static int add_vertex(struct routing * instance, - uint64_t addr) -{ - struct vertex * vertex; - vertex = malloc(sizeof(*vertex)); - if (vertex == NULL) - return -1; - - list_head_init(&vertex->next); - list_head_init(&vertex->edges); - vertex->addr = addr; - - list_add(&vertex->next, &instance->vertices); + char fso_path[RIB_MAX_PATH_LEN + 1]; - return 0; -} -#endif + struct graph * graph; +} routing; struct routing_i * routing_i_create(struct pff * pff) { @@ -100,7 +73,7 @@ struct routing_i * routing_i_create(struct pff * pff) tmp->pff = pff; - list_head_init(&tmp->vertices); + list_head_init(&tmp->routing_table); return tmp; } @@ -164,14 +137,21 @@ int routing_init(struct nbs * nbs) { char addr[ADDR_SIZE]; - if (rib_add(RIB_ROOT, ROUTING_NAME)) + routing.graph = graph_create(); + if (routing.graph == NULL) return -1; + if (rib_add(RIB_ROOT, ROUTING_NAME)) { + graph_destroy(routing.graph); + return -1; + } + rib_path_append(routing.fso_path, ROUTING_NAME); snprintf(addr, ADDR_SIZE, "%" PRIx64, ipcpi.dt_addr); if (rib_add(routing.fso_path, addr)) { + graph_destroy(routing.graph); rib_del(ROUTING_PATH); return -1; } @@ -182,6 +162,7 @@ int routing_init(struct nbs * nbs) routing.nb_notifier.notify_call = routing_neighbor_event; if (nbs_reg_notifier(routing.nbs, &routing.nb_notifier)) { + graph_destroy(routing.graph); rib_del(ROUTING_PATH); return -1; } @@ -191,6 +172,8 @@ int routing_init(struct nbs * nbs) void routing_fini(void) { + graph_destroy(routing.graph); + rib_del(ROUTING_PATH); nbs_unreg_notifier(routing.nbs, &routing.nb_notifier); diff --git a/src/ipcpd/normal/routing.h b/src/ipcpd/normal/routing.h index 20dc72f9..0794ef28 100644 --- a/src/ipcpd/normal/routing.h +++ b/src/ipcpd/normal/routing.h @@ -34,7 +34,6 @@ * Routing will take a type in the future, * to allow different policies. */ - int routing_init(struct nbs * nbs); void routing_fini(void); -- cgit v1.2.3 From 5fde9be8b30f284b4a249fbf13b08af290b868b1 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Wed, 15 Mar 2017 17:15:19 +0100 Subject: ipcpd: normal: routing: Listen to RIB events This lets the routing component listen to RIB events. It listens to /fsdb which is populated with FSOs. The graph that is kept within the routing component is updated depending on the event that was received. --- src/ipcpd/normal/CMakeLists.txt | 3 +- src/ipcpd/normal/fso.proto | 29 +++++++ src/ipcpd/normal/routing.c | 184 +++++++++++++++++++++++++++++++++------- 3 files changed, 182 insertions(+), 34 deletions(-) create mode 100644 src/ipcpd/normal/fso.proto (limited to 'src') diff --git a/src/ipcpd/normal/CMakeLists.txt b/src/ipcpd/normal/CMakeLists.txt index 39ced7bc..06292c50 100644 --- a/src/ipcpd/normal/CMakeLists.txt +++ b/src/ipcpd/normal/CMakeLists.txt @@ -15,6 +15,7 @@ include_directories(${CMAKE_BINARY_DIR}/include) set(IPCP_NORMAL_TARGET ipcpd-normal CACHE STRING "IPCP_NORMAL_TARGET") protobuf_generate_c(FLOW_ALLOC_SRCS FLOW_ALLOC_HDRS flow_alloc.proto) +protobuf_generate_c(FSO_SRCS FSO_HDRS fso.proto) set(SOURCE_FILES # Add source files here @@ -38,7 +39,7 @@ set(SOURCE_FILES ) add_executable(ipcpd-normal ${SOURCE_FILES} ${IPCP_SOURCES} - ${FLOW_ALLOC_SRCS}) + ${FLOW_ALLOC_SRCS} ${FSO_SRCS}) target_link_libraries(ipcpd-normal LINK_PUBLIC ouroboros) include(AddCompileFlags) diff --git a/src/ipcpd/normal/fso.proto b/src/ipcpd/normal/fso.proto new file mode 100644 index 00000000..32b281d6 --- /dev/null +++ b/src/ipcpd/normal/fso.proto @@ -0,0 +1,29 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2017 + * + * Flow State Object message + * + * Dimitri Staessens + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +syntax = "proto2"; + +message fso { + required uint64 s_addr = 1; + required uint64 d_addr = 2; + /* Add QoS parameters of link here */ +}; diff --git a/src/ipcpd/normal/routing.c b/src/ipcpd/normal/routing.c index 46c2be08..745d1812 100644 --- a/src/ipcpd/normal/routing.c +++ b/src/ipcpd/normal/routing.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "routing.h" #include "ribmgr.h" @@ -39,7 +40,10 @@ #include #include -#define ADDR_SIZE 30 +#include "fso.pb-c.h" +typedef Fso fso_t; + +#define BUF_SIZE 256 struct routing_table_entry { struct list_head next; @@ -48,17 +52,18 @@ struct routing_table_entry { }; struct routing_i { - struct pff * pff; - struct list_head routing_table; + struct pff * pff; }; struct { struct nbs * nbs; struct nb_notifier nb_notifier; - char fso_path[RIB_MAX_PATH_LEN + 1]; - struct graph * graph; + + ro_set_t * set; + rqueue_t * queue; + pthread_t rib_listener; } routing; struct routing_i * routing_i_create(struct pff * pff) @@ -73,8 +78,6 @@ struct routing_i * routing_i_create(struct pff * pff) tmp->pff = pff; - list_head_init(&tmp->routing_table); - return tmp; } @@ -88,24 +91,41 @@ void routing_i_destroy(struct routing_i * instance) static int routing_neighbor_event(enum nb_event event, struct conn conn) { - char addr[ADDR_SIZE]; - char path[RIB_MAX_PATH_LEN + 1]; + char path[RIB_MAX_PATH_LEN + 1]; + char fso_name[RIB_MAX_PATH_LEN + 1]; + fso_t fso = FSO__INIT; + size_t len; + uint8_t * data; - strcpy(path, routing.fso_path); - snprintf(addr, ADDR_SIZE, "%" PRIx64, conn.conn_info.addr); - rib_path_append(path, addr); + sprintf(fso_name, "%" PRIx64 "-%" PRIx64, + ipcpi.dt_addr, conn.conn_info.addr); + rib_path_append(rib_path_append(path, ROUTING_PATH), fso_name); switch (event) { case NEIGHBOR_ADDED: - if (rib_add(routing.fso_path, addr)) { + fso.s_addr = ipcpi.dt_addr; + fso.d_addr = conn.conn_info.addr; + + len = fso__get_packed_size(&fso); + if (len == 0) + return -1; + + data = malloc(len); + if (data == NULL) + return -1; + + fso__pack(&fso, data); + + if (rib_add(ROUTING_PATH, fso_name)) { log_err("Failed to add FSO."); + free(data); return -1; } - if (rib_write(path, &conn.flow_info.qs, - sizeof(conn.flow_info.qs))) { - log_err("Failed to write qosspec to FSO."); + if (rib_put(path, data, len)) { + log_err("Failed to put FSO in RIB."); rib_del(path); + free(data); return -1; } @@ -118,12 +138,7 @@ static int routing_neighbor_event(enum nb_event event, break; case NEIGHBOR_QOS_CHANGE: - if (rib_write(path, &conn.flow_info.qs, - sizeof(conn.flow_info.qs))) { - log_err("Failed to write qosspec to FSO."); - return -1; - } - + log_info("Not currently supported."); break; default: log_info("Unsupported event for routing."); @@ -133,10 +148,98 @@ static int routing_neighbor_event(enum nb_event event, return 0; } -int routing_init(struct nbs * nbs) +static int read_fso(char * path, + int32_t flag) +{ + ssize_t len; + uint8_t ro[BUF_SIZE]; + fso_t * fso; + qosspec_t qs; + + len = rib_read(path, ro, BUF_SIZE); + if (len < 0) { + log_err("Failed to read FSO."); + return -1; + } + + fso = fso__unpack(NULL, len, ro); + if (fso == NULL) { + log_err("Failed to unpack."); + return -1; + } + + if (flag & RO_CREATE) { + if (graph_add_edge(routing.graph, + fso->s_addr, fso->d_addr, qs)) { + log_err("Failed to add edge to graph."); + fso__free_unpacked(fso, NULL); + return -1; + } + } else if (flag & RO_MODIFY) { + if (graph_update_edge(routing.graph, + fso->s_addr, fso->d_addr, qs)) { + log_err("Failed to update edge of graph."); + fso__free_unpacked(fso, NULL); + return -1; + } + } else if (flag & RO_DELETE) { + if (graph_del_edge(routing.graph, fso->s_addr, fso->d_addr)) { + log_err("Failed to del edge of graph."); + fso__free_unpacked(fso, NULL); + return -1; + } + } + + fso__free_unpacked(fso, NULL); + + return 0; +} + +static void * rib_listener(void * o) { - char addr[ADDR_SIZE]; + int32_t flag; + char path[RIB_MAX_PATH_LEN + 1]; + char ** children; + ssize_t len; + int i; + + (void) o; + + if (ro_set_add(routing.set, ROUTING_PATH, + RO_MODIFY | RO_CREATE | RO_DELETE)) { + log_err("Failed to add to RO set"); + return (void * ) -1; + } + + len = rib_children(ROUTING_PATH, &children); + if (len < 0) { + log_err("Failed to retrieve children."); + return (void *) -1; + } + + for (i = 0; i < len; i++) { + if (read_fso(children[i], RO_CREATE)) { + log_err("Failed to parse FSO."); + continue; + } + } + + while (rib_event_wait(routing.set, routing.queue, NULL)) { + flag = rqueue_next(routing.queue, path); + if (flag < 0) + continue; + if (read_fso(children[i], flag)) { + log_err("Failed to parse FSO."); + continue; + } + } + + return (void *) 0; +} + +int routing_init(struct nbs * nbs) +{ routing.graph = graph_create(); if (routing.graph == NULL) return -1; @@ -146,32 +249,47 @@ int routing_init(struct nbs * nbs) return -1; } - rib_path_append(routing.fso_path, ROUTING_NAME); - - snprintf(addr, ADDR_SIZE, "%" PRIx64, ipcpi.dt_addr); + routing.nbs = nbs; - if (rib_add(routing.fso_path, addr)) { + routing.nb_notifier.notify_call = routing_neighbor_event; + if (nbs_reg_notifier(routing.nbs, &routing.nb_notifier)) { graph_destroy(routing.graph); rib_del(ROUTING_PATH); return -1; } - rib_path_append(routing.fso_path, addr); - - routing.nbs = nbs; + routing.set = ro_set_create(); + if (routing.set == NULL) { + nbs_unreg_notifier(routing.nbs, &routing.nb_notifier); + graph_destroy(routing.graph); + rib_del(ROUTING_PATH); + return -1; + } - routing.nb_notifier.notify_call = routing_neighbor_event; - if (nbs_reg_notifier(routing.nbs, &routing.nb_notifier)) { + routing.queue = rqueue_create(); + if (routing.queue == NULL) { + ro_set_destroy(routing.set); + nbs_unreg_notifier(routing.nbs, &routing.nb_notifier); graph_destroy(routing.graph); rib_del(ROUTING_PATH); return -1; } + pthread_create(&routing.rib_listener, NULL, rib_listener, NULL); + return 0; } void routing_fini(void) { + pthread_cancel(routing.rib_listener); + + pthread_join(routing.rib_listener, NULL); + + rqueue_destroy(routing.queue); + + ro_set_destroy(routing.set); + graph_destroy(routing.graph); rib_del(ROUTING_PATH); -- cgit v1.2.3